-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpng2ico.cpp
249 lines (223 loc) · 6.57 KB
/
png2ico.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* Copyright (C) 2002 Matthias S. Benkmann <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License (ONLY THIS VERSION).
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <cstdio>
#include <cstring>
#include <cstdint>
#include <cstdarg>
#include <vector>
#include <string>
#include <fstream>
#include <stdexcept>
#include <memory>
#include <filesystem>
#include <spng.h>
#include "VERSION"
void usage()
{
fprintf(stderr,version"\n");
fprintf(stderr,"USAGE: png2ico icofile [--colors <num>] pngfile1 [pngfile2 ...]\n");
}
struct formatted_error : std::exception {
formatted_error(const char * fmt, ...) /* __attribute__((format(printf, 1, 2))) */
{
va_list args;
va_start(args, fmt);
const auto len = vsnprintf(nullptr, 0, fmt, args);
va_end(args);
m_reason.resize(len + 1);
va_start(args, fmt);
vsnprintf(m_reason.data(), m_reason.size(), fmt, args);
va_end(args);
}
const char * what() const noexcept override {
return m_reason.data();
}
std::vector<char> m_reason;
};
auto parse_args(int argc, char * argv[])
{
const char * outfileName = nullptr;
std::vector<const char *> inputs;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i],"--colors") == 0) {
// eat color parameter for compatibility
++i;
if (i >= argc) {
throw formatted_error("Number missing after --colors");
}
continue;
} else if (!outfileName) {
outfileName = argv[i];
continue;
} else {
inputs.emplace_back(argv[i]);
}
}
return std::make_pair(outfileName, std::move(inputs));
}
#pragma pack(1)
struct ICONDIR {
uint16_t idReserved;
uint16_t idType;
uint16_t idCount;
};
struct ICONDIRECTORY {
uint8_t bWidth;
uint8_t bHeight;
uint8_t bColorCount;
uint8_t bReserved;
uint16_t wPlanes;
uint16_t wBitCount;
uint32_t dwBytesInRes;
uint32_t dwImageOffset;
};
#pragma pack()
struct spng_deleter {
void operator()(spng_ctx * ctx) const {
spng_ctx_free(ctx);
}
};
struct file_deleter {
void operator()(FILE * file) const {
fclose(file);
}
};
using spng_ctx_ptr = std::unique_ptr<spng_ctx, spng_deleter>;
using file_ptr = std::unique_ptr<FILE, file_deleter>;
auto get_dimensions(const char * filename)
{
file_ptr file(fopen(filename, "rb"));
if (!file) {
throw formatted_error("Cannot open %s: %s", filename, strerror(errno));
}
spng_ctx_ptr ctx(spng_ctx_new(0));
if (!ctx) {
throw formatted_error("Cannot create spng context");
} else if (const auto res = spng_set_png_file(ctx.get(), file.get()); res != 0) {
throw formatted_error("Cannot attach file of %s: %s", filename, spng_strerror(res));
}
spng_ihdr header;
if (const auto res = spng_get_ihdr(ctx.get(), &header); res != 0) {
throw formatted_error("Cannot read PNG header of %s: %s", filename, spng_strerror(res));
}
return std::make_pair(
header.width <= 255 ? header.width : 0,
header.height <= 255 ? header.height : 0
);
}
auto make_ifstream(const char * filename)
{
try {
std::ifstream stream;
stream.exceptions(std::ios::badbit);
stream.open(filename, std::ios::binary | std::ios::in);
return stream;
} catch (const std::exception & e) {
throw formatted_error("Cannot open %s: %s", filename, e.what());
}
}
auto make_ofstream(const char * filename)
{
try {
std::ofstream stream;
stream.exceptions(std::ios::badbit);
stream.open(filename, std::ios::binary | std::ios::out | std::ios::trunc);
return stream;
} catch (const std::exception & e) {
throw formatted_error("Cannot open %s: %s", filename, e.what());
}
}
auto write_ICONDIR(const char * filename, std::ostream & stream, uint16_t count)
{
const ICONDIR icondir = { 0, 1, count };
try {
stream.write(reinterpret_cast<const char *>(&icondir), sizeof(ICONDIR));
} catch (const std::exception & e) {
throw formatted_error("Cannot write to %s: %s", filename, e.what());
}
}
auto get_size(const char * filename)
{
try {
return std::filesystem::file_size(filename);
} catch (const std::exception & e) {
throw formatted_error("Cannot get size of %s: %s", filename, e.what());
}
}
auto write_ICONDIRECTORY(const char * filename, std::ostream & stream, uint8_t width, uint8_t height, uint32_t size, uint32_t offset)
{
const ICONDIRECTORY identry = { width, height, 0, 0, 1, 24, size, offset };
try {
stream.write(reinterpret_cast<const char *>(&identry), sizeof(identry));
} catch (const std::exception & e) {
throw formatted_error("Cannot write to %s: %s", filename, e.what());
}
}
auto read(const char * filename, std::istream & stream, std::vector<char> & buffer)
{
try {
stream.read(buffer.data(), buffer.size());
return stream.gcount();
} catch (const std::exception & e) {
throw formatted_error("Cannot read from %s: %s", filename, e.what());
}
}
auto write(const char * filename, std::ostream & stream, const char * buffer, size_t amount)
{
try {
stream.write(buffer, amount);
} catch (const std::exception & e) {
throw formatted_error("Cannot write to %s: %s", filename, e.what());
}
}
void create_icon(const char * outfileName, const std::vector<const char *> & inputs)
{
auto out = make_ofstream(outfileName);
// write directory
write_ICONDIR(outfileName, out, inputs.size());
size_t offset = sizeof(ICONDIR) + (sizeof(ICONDIRECTORY) * inputs.size());
for (const auto infileName : inputs) {
const auto size = get_size(infileName);
const auto [width, height] = get_dimensions(infileName);
write_ICONDIRECTORY(infileName, out, width, height, size, offset);
offset += size;
}
// write icons
for (const auto infileName : inputs) {
auto in = make_ifstream(infileName);
std::vector<char> buffer(8192);
while (!in.eof()) {
const auto len = read(infileName, in, buffer);
write(outfileName, out, buffer.data(), len);
}
}
}
int main(int argc, char* argv[])
{
try {
const auto [ outfileName, inputs ] = parse_args(argc, argv);
if (!outfileName) {
usage();
throw std::runtime_error("No output file specified");
} else if (inputs.empty()) {
usage();
throw std::runtime_error("No input files specified");
}
create_icon(outfileName, inputs);
return 0;
} catch (const std::exception & e) {
fprintf(stderr, "%s\n", e.what());
}
return 1;
}