-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
421 lines (372 loc) · 12.5 KB
/
main.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include <QColor>
#include <QDataStream>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QImage>
#include <QImageWriter>
#if SVG_ENABLED
#include <QPainter>
#include <QSvgGenerator>
#endif
#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <string>
#include <vector>
static constexpr auto PaletteSize = 256;
static constexpr auto PaletteComponents = 3;
#if SVG_ENABLED
static constexpr auto svgFormat = "svg";
#endif
struct Dc6Header
{
uint32_t alwaysSix;
uint32_t alwaysOne;
uint32_t alwaysZero;
uint32_t terminator;
uint32_t directions;
uint32_t framesPerDirection;
};
struct Dc6FrameHeader
{
uint32_t isFlipped;
uint32_t width;
uint32_t height;
uint32_t offsetX;
uint32_t offsetY;
uint32_t alwaysZero;
uint32_t nextFrameIndex;
uint32_t length;
};
using Options = std::vector<std::string>;
QDebug operator<<(QDebug d, const Options& opts)
{
Q_ASSERT(!opts.empty());
d.nospace() << " " << opts.front().c_str();
std::for_each(std::next(opts.cbegin()), opts.cend(), [&](const Options::value_type& opt) {
d << ", " << opt.c_str();
});
return d.nospace();
}
bool containsOption(const Options& opts, const char* s)
{
return std::find(opts.cbegin(), opts.cend(), s) != opts.cend();
}
void printSupportedFormats()
{
qDebug() << "Supported image output formats:\n" << QImageWriter::supportedImageFormats()
#if SVG_ENABLED
<< "and" << svgFormat
#endif
;
}
QString convertedPath(const char* path)
{
return QString::fromLocal8Bit(path);
}
#if SVG_ENABLED
void saveSvg(const QString& fileName, const QImage& image)
{
const auto size = image.size();
QSvgGenerator svg;
svg.setFileName(fileName);
svg.setSize(size);
svg.setViewBox(QRect{{}, size});
svg.setTitle(QFileInfo{fileName}.completeBaseName());
QPainter painter;
if (!painter.begin(&svg)) {
qCritical() << "can't save svg to" << fileName;
return;
}
const auto pixels = reinterpret_cast<const QRgb*>(image.bits());
for (int x = 0; x < size.width(); ++x) {
for (int y = 0; y < size.height(); ++y) {
painter.setPen(QColor::fromRgba(pixels[y * size.width() + x]));
painter.drawPoint(x, y);
}
}
}
#endif
int main(int argc, char* argv[])
{
const Options paletteOpts{"-p", "--palette"};
const Options formatOpts{"-f", "--format"};
const Options qualityOpts{"-q", "--quality"};
const Options transparentColorOpts{"-t", "--transparent-color"};
const Options outDirOpts{"-o", "--out-dir"};
const Options separateDirOpts{"-d", "--separate-dir"};
const Options firstFrameOnlyOpts{"--first-frame-only"};
const Options verboseOpts{"-v", "--verbose"};
const Options treatArgsAsPositionalsOpt{"--"};
const Options supportedFormatsOpts{"-l", "--list-supported-formats"};
const Options helpOpts{"-h", "--help"};
const auto defaultFormat = "png";
const auto minQuality = 0;
const auto maxQuality = 100;
const auto defaultTransparentColor = qRgba(0, 0, 0, 0);
auto printHelp = [&] {
const auto treatArgsAsPositionalsOptHelp = QString{"[%1]"}.arg(treatArgsAsPositionalsOpt.front().c_str()).toLatin1();
const auto defaultTransparentColorStr = QColor{defaultTransparentColor}.name().toLatin1();
qDebug() << "Usage:" << argv[0] << "[options]" << treatArgsAsPositionalsOptHelp.constData() << "[directory or dc6 path...]\n\nOptions:";
qDebug() << paletteOpts << " <file>\t\tPalette file to use, defaults to the embedded one";
qDebug() << formatOpts << " <format>\t\tOutput image format, defaults to " << defaultFormat;
qDebug() << qualityOpts << " <integer>\tOutput image quality in range " << minQuality << '-' << maxQuality << " inclusive"
#if SVG_ENABLED
<< ", doesn't apply to " << svgFormat
#endif
;
qDebug() << transparentColorOpts << " <str>\tColor to use as transparent, defaults to " << defaultTransparentColorStr.constData() << ", see QColor::setNamedColor() for full list of supported formats";
qDebug() << outDirOpts << " <directory>\tWhere to save output files, defaults to input file's directory";
qDebug() << separateDirOpts << "\t\tSave multiframe images in a directory named after the input file";
qDebug() << firstFrameOnlyOpts << "\t\tSave only first frame of multiframe images, ignores" << separateDirOpts;
qDebug() << verboseOpts << "\t\t\tVerbose output";
qDebug();
qDebug() << supportedFormatsOpts << "\tPrint supported image formats";
qDebug() << helpOpts << "\t\t\tPrint this message\n";
printSupportedFormats();
};
QStringList dc6Paths;
QString palettePath;
auto imageFormat = defaultFormat;
int imageQuality = -1;
auto transparentColor = defaultTransparentColor;
QString outDirPath;
auto useSeparateDir = false;
auto saveOnlyFirstFrame = false;
auto treatArgsAsPositionals = false;
auto verboseOutput = false;
auto showSupportedFormats = false;
auto showHelp = false;
using Argument = const char*;
for (int i = 1; i < argc; ++i) {
if (treatArgsAsPositionals) {
dc6Paths << convertedPath(argv[i]);
continue;
}
auto processNextArg = [&](const std::function<void(Argument)>& processor) {
if (i + 1 < argc)
processor(argv[++i]);
};
if (containsOption(helpOpts, argv[i]))
showHelp = true;
else if (containsOption(supportedFormatsOpts, argv[i]))
showSupportedFormats = true;
else if (containsOption(paletteOpts, argv[i]))
processNextArg([&](Argument arg) {
palettePath = convertedPath(arg);
});
else if (containsOption(formatOpts, argv[i]))
processNextArg([&](Argument arg) {
imageFormat = arg;
});
else if (containsOption(qualityOpts, argv[i]))
processNextArg([&](Argument arg) {
try {
imageQuality = std::stoi(arg);
if (imageQuality < minQuality || imageQuality > maxQuality) {
imageQuality = -1;
qWarning() << "image quality exceeds valid range, default setting will be used";
}
}
catch (const std::exception& e) {
qWarning() << "couldn't convert image quality to number, default setting will be used:" << e.what();
}
});
else if (containsOption(transparentColorOpts, argv[i]))
processNextArg([&](Argument arg) {
const QColor color{arg};
if (color.isValid())
transparentColor = color.rgba();
});
else if (containsOption(outDirOpts, argv[i]))
processNextArg([&](Argument arg) {
outDirPath = convertedPath(arg);
});
else if (containsOption(separateDirOpts, argv[i]))
useSeparateDir = true;
else if (containsOption(firstFrameOnlyOpts, argv[i]))
saveOnlyFirstFrame = true;
else if (containsOption(verboseOpts, argv[i]))
verboseOutput = true;
else if (containsOption(treatArgsAsPositionalsOpt, argv[i]))
treatArgsAsPositionals = true;
else
dc6Paths << convertedPath(argv[i]);
}
if (showHelp || argc == 1) {
printHelp();
return 0;
}
if (showSupportedFormats) {
printSupportedFormats();
return 0;
}
if (dc6Paths.isEmpty()) {
qWarning() << "no input files specified";
return 1;
}
if (!outDirPath.isEmpty() && !QDir{outDirPath}.mkpath(".")) {
qCritical() << "unable to create output directory at" << outDirPath;
return 1;
}
for (int i = 0; i < dc6Paths.size(); ++i) {
const QDir dir{dc6Paths[i]};
if (!dir.exists())
continue;
dc6Paths.removeAt(i--);
const auto dc6InDir = dir.entryList(QStringList{QLatin1String{"*.dc6"}}, QDir::Files | QDir::Readable);
if (verboseOutput)
qDebug() << "files in dir" << dir << ':' << dc6InDir;
for (const auto& dc6Filename : dc6InDir)
dc6Paths += dir.absoluteFilePath(dc6Filename);
}
if (palettePath.isEmpty()) {
palettePath = QLatin1String{":/units_pal.dat"};
if (verboseOutput)
qDebug() << "using embedded palette";
}
QFile paletteFile{palettePath};
if (!paletteFile.open(QFile::ReadOnly)) {
qCritical() << "error opening palette file:" << paletteFile.errorString();
return 1;
}
const auto paletteFileSize = QFileInfo{paletteFile}.size();
if (paletteFileSize != PaletteSize * PaletteComponents) {
qCritical() << "palette file has wrong size:" << paletteFileSize;
return 1;
}
std::vector<QRgb> colorPalette;
colorPalette.reserve(PaletteSize);
QDataStream ds{&paletteFile};
while (!ds.atEnd()) {
std::array<uint8_t, PaletteComponents> bgr;
ds >> bgr[0] >> bgr[1] >> bgr[2];
colorPalette.push_back(qRgb(bgr[2], bgr[1], bgr[0]));
}
paletteFile.close();
for (const auto& dc6Path : dc6Paths) {
if (verboseOutput)
qDebug() << "processing file" << dc6Path;
QFile f{dc6Path};
if (!f.open(QFile::ReadOnly)) {
qCritical() << "error opening dc6 file:" << dc6Path << '\n' << f.errorString();
continue;
}
ds.setDevice(&f);
ds.setByteOrder(QDataStream::LittleEndian);
Dc6Header header;
ds >> header.alwaysSix;
ds >> header.alwaysOne;
ds >> header.alwaysZero;
if (header.alwaysSix != 6 || header.alwaysOne != 1 || header.alwaysZero != 0) {
qCritical() << "invalid header in file" << dc6Path;
continue;
}
ds.skipRawData(sizeof header.terminator);
ds >> header.directions;
ds >> header.framesPerDirection;
auto framesTotal = header.directions * header.framesPerDirection;
if (verboseOutput)
qDebug() << header.directions << "direction(s) with" << header.framesPerDirection << "frame(s) =" << framesTotal << "frames total";
if (saveOnlyFirstFrame) {
if (framesTotal > 1 && verboseOutput)
qDebug() << "saving only first frame from a multiframe image";
framesTotal = 1;
}
std::vector<uint32_t> frameIndexes;
frameIndexes.resize(framesTotal);
for (std::size_t i = 0; i < framesTotal; ++i)
ds >> frameIndexes[i];
const QFileInfo dc6FileInfo{f};
auto outImageBasePath = QString{QLatin1String{"%1/%2"}}.arg(outDirPath.isEmpty() ? dc6FileInfo.path() : outDirPath, dc6FileInfo.completeBaseName());
if (framesTotal > 1) {
if (useSeparateDir) {
QDir{outImageBasePath}.mkpath(".");
outImageBasePath += '/';
}
else
outImageBasePath += '_';
}
std::size_t j = 0;
for (auto index : frameIndexes) {
f.seek(index);
if (verboseOutput)
qDebug() << "frame index" << j << ", offset" << index;
Dc6FrameHeader frameHeader;
ds >> frameHeader.isFlipped;
ds >> frameHeader.width;
ds >> frameHeader.height;
ds.skipRawData(sizeof frameHeader.offsetX);
ds.skipRawData(sizeof frameHeader.offsetY);
ds.skipRawData(sizeof frameHeader.alwaysZero);
ds.skipRawData(sizeof frameHeader.nextFrameIndex);
ds >> frameHeader.length;
if (verboseOutput)
qDebug() << "width =" << frameHeader.width << ", height =" << frameHeader.height << ", length =" << frameHeader.length << ", isFlipped =" << frameHeader.isFlipped;
std::vector<QRgb> pixels(frameHeader.width * frameHeader.height, transparentColor);
std::size_t pixI = 0;
for (std::size_t i = 0; i < frameHeader.length; ++i) {
uint8_t pixel;
ds >> pixel;
const auto writeTransparent = (pixel & 0b10000000) != 0;
const auto pixelsToAdd = pixel & 0b01111111;
if (writeTransparent) {
if (pixelsToAdd > 0)
pixI += pixelsToAdd;
else
pixI = (pixI / frameHeader.width + 1) * frameHeader.width;
}
else {
i += pixelsToAdd;
for (std::size_t k = 0; k < pixelsToAdd && pixI < pixels.size(); ++k, ++pixI) {
ds >> pixel;
pixels[pixI] = colorPalette[pixel];
}
// if filled to the end of scan line, move one pixel back to stay on the current line because next instruction is "advance to next line"
if (pixI % frameHeader.width == 0)
--pixI;
}
}
QImage image(reinterpret_cast<const uchar*>(pixels.data()), frameHeader.width, frameHeader.height, QImage::Format_ARGB32_Premultiplied);
if (!frameHeader.isFlipped)
image = image.mirrored();
auto outImageBaseName = outImageBasePath;
if (framesTotal > 1)
outImageBaseName += QString::number(j);
outImageBaseName += '.';
const auto outFileName = outImageBaseName + imageFormat;
auto verbosePrintOutFileName = [&](const QString& fileName) {
if (verboseOutput)
qDebug() << "save image to" << fileName;
};
#if SVG_ENABLED
if (outFileName.endsWith(QLatin1String{svgFormat}, Qt::CaseInsensitive)) {
verbosePrintOutFileName(outFileName);
saveSvg(outFileName, image);
}
else
#endif
{
QImageWriter imageWriter{outFileName};
if (!imageWriter.canWrite()) {
imageWriter.setFileName(outImageBaseName + defaultFormat);
qWarning() << "can't save using the specified format, falling back to" << defaultFormat;
}
verbosePrintOutFileName(imageWriter.fileName());
if (imageQuality > -1)
imageWriter.setQuality(imageQuality);
if (!imageWriter.write(image))
qCritical() << "error saving output image:" << imageWriter.errorString();
}
++j;
if (verboseOutput)
qDebug() << "-----";
}
}
if (verboseOutput)
qDebug() << "all images processed";
return 0;
}