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 | #include "gate/gatemain.h"
#include "gate/console.hpp"
#include "gate/applications.hpp"
#include "gate/files.hpp"
#include "gate/environments.hpp"
#include "gate/utilities.hpp"
#include "gate/numbers.hpp"
#include "gate/graphics/bitmapimages.hpp"
#include "gate/graphics/pixmapimages.hpp"
#include "gate/graphics/jpegimages.hpp"
#include "gate/graphics/pngimages.hpp"
#include "gate/graphics/gifimages.hpp"
#include "gate/graphics/imageformats.hpp"
#include "gate/graphics/fonts.hpp"
#include <stdio.h>
#if defined(GATE_COMPILER_MSVC) && defined(GATE_LEAK_DETECTION) && defined(GATE_SYS_WIN) && !defined(GATE_SYS_WINCE) && (GATE_ARCH != GATE_ARCH_ARM32) && (GATE_ARCH != GATE_ARCH_ARM64)
# include <vld.h>
#endif
namespace gate
{
namespace apps
{
using namespace gate::graph;
class GATE_API_LOCAL PixApp : public App
{
private:
ImageFormats::FormatEnum parseFormatId(String const& formatId)
{
if (formatId == "bmp") return ImageFormats::Format_Bitmap;
else if (formatId == "xpm") return ImageFormats::Format_Pixmap;
else if (formatId == "jpg") return ImageFormats::Format_Jpeg;
else if (formatId == "png") return ImageFormats::Format_Png;
else if (formatId == "gif") return ImageFormats::Format_Gif;
return ImageFormats::Format_Unknown;
}
RasterImage loadImage(String const& filePath, String const& formatId)
{
ImageFormats::FormatEnum format = ImageFormats::Format_Unknown;
<--- Variable 'format' is reassigned a value before the old one has been used.
if (formatId.empty())
{
format = ImageFormats::parseFileExtension(filePath);
}<--- Variable 'format' is reassigned a value before the old one has been used.
else
{
format = parseFormatId(formatId);
}
if (format == ImageFormats::Format_Unknown)
{
GATEXX_RAISE_EXCEPTION(results::NotSupported, "Unknown/unsupported image format", 0);
}
ControlStream stream = File::open(filePath, Stream::Open_Read);
enumint_t flags = 0;
#if defined(GATE_SYS_DOS)
flags |= gate::graph::ImageFormats::Flag_Depth_8bit_Pal;
#endif
return ImageFormats::load(format, stream, flags);
}
void saveImage(String const& filePath, String const& formatId, RasterImage const& image)
{
ImageFormats::FormatEnum format = ImageFormats::Format_Unknown;
<--- Variable 'format' is reassigned a value before the old one has been used.
if (formatId.empty())
{
format = ImageFormats::parseFileExtension(filePath);
}<--- Variable 'format' is reassigned a value before the old one has been used.
else
{
format = parseFormatId(formatId);
}
if (format == ImageFormats::Format_Unknown)
{
GATEXX_RAISE_EXCEPTION(results::NotSupported, "Unknown/unsupported image format", 0);
}
ControlStream stream = File::open(filePath, Stream::Open_Write);
ImageFormats::save(image, format, stream, 0);
}
String createSliceFilename(String const& sourcePath, uint32_t x, uint32_t y)
{
String parent, filename;
File::splitPath(sourcePath, parent, filename);
size_t pos = filename.positionOfLast('.');
StringBuilder newFilename;
if (pos == String::npos)
{
newFilename << filename << "_" << y << "-" << x;
}
else
{
newFilename << filename.substr(0, pos) << "_" << y << "-" << x << filename.substr(pos);
}
return File::buildPath(parent, newFilename.toString());
}
void createImageSlices(RasterImage const& srcImage, unsigned sliceWidth, unsigned sliceHeight, String const& outputPath, String const& formatId)
{
if (sliceWidth == 0) sliceWidth = srcImage.getWidth();
if (sliceHeight == 0) sliceHeight = srcImage.getHeight();
unsigned xCount = (srcImage.getWidth() + sliceWidth - 1) / sliceWidth;
unsigned yCount = (srcImage.getHeight() + sliceHeight - 1) / sliceHeight;
RasterImage newImage(sliceWidth, sliceHeight, srcImage.getPixelFormat());
for (unsigned y = 0; y < yCount; ++y)
{
for (unsigned x = 0; x < xCount; ++x)
{
newImage.clear(Color::White);
newImage.paste(srcImage, 0, 0, x * sliceWidth, y * sliceHeight, sliceWidth, sliceHeight, false);
String sliceFilepath = createSliceFilename(outputPath, x, y);
saveImage(sliceFilepath, formatId, newImage);
}
}
}
void append_frame_to_gif(String const& inputFile, RasterImage const& inputImage, Array<String> const& gif_append, int32_t gif_time, String const& outputFile)
{
GifAnimationWriter animation;
const ImageFormats::FormatEnum id = ImageFormats::parseFileExtension(inputFile);
FileStream outStream(outputFile, File::Open_Write);
animation.start(outStream, inputImage.getWidth(), inputImage.getHeight());
const time::Milliseconds frameDisplayTime(gif_time);
if (id == ImageFormats::Format_Gif)
{
FileStream inStream(inputFile, File::Open_Read);
animation.import(inStream);
}
else
{
animation.addFrame(inputImage, 0, 0, frameDisplayTime);
}
for (Enumerator<const String> e = gif_append.enumerate(); e.valid(); e.next())
{
RasterImage frame = loadImage(*e, String());
animation.addFrame(frame, 0, 0, frameDisplayTime);
}
animation.finish(GifAnimationWriter::Flag_Loop);
}
public:
virtual void run() override
{
static AppOptionDef help = AppOptionDef::createSwitch("help", "?", "show_help", "Shows help");
static AppOptionDef rot_left = AppOptionDef::createSwitch("rotate-left", "RL", "rot_left", "Rotates image to left side");
static AppOptionDef rot_right = AppOptionDef::createSwitch("rotate-right", "RR", "rot_right", "Rotates image to right side");
static AppOptionDef flip_x = AppOptionDef::createSwitch("flip-x", "FX", "flip_x", "Flips image horizontally");
static AppOptionDef flip_y = AppOptionDef::createSwitch("flip-y", "FY", "flip_y", "Flips image vertically");
static AppOptionDef roll = AppOptionDef::createSwitch("roll-over", "RO", "roll_over", "Rolls over image");
static AppOptionDef gray = AppOptionDef::createSwitch("gray", "G", "gray_scale", "Converts color to grayscale");
static AppOptionDef bw = AppOptionDef::createSwitch("monochrome", "MC", "monochrome", "Converts color to monochrome");
static AppOptionDef resize = AppOptionDef::createSwitch("resize", "S", "resize", "Resize the image to new dimensions (width, height)");
static AppOptionDef slice = AppOptionDef::createSwitch("slice", "SL", "slice", "Creates slices of an image (width, height)");
static AppOptionDef width = AppOptionDef::createInt32("width", "W", 0, "width", "Width of resized output image");
static AppOptionDef height = AppOptionDef::createInt32("height", "H", 0, "height", "Height of resized output image");
static AppOptionDef print = AppOptionDef::createString("print", "P", "", "text", "Creates an image and prints text on it");
static AppOptionDef gif_append = AppOptionDef::createString("gif-append", "GA", "", "new_gif_frame_file", "Frame image to be added to GIF animation");
static AppOptionDef gif_time = AppOptionDef::createInt32("gif-time", "GT", 1000, "gif_frame_time", "Milliseconds the appended frame is displayed");
static AppOptionDef in_format = AppOptionDef::createString("input-format", "IFMT", "", "in_format", "Input file format (bmp,jpg,png,gif,xpm)");
static AppOptionDef out_format = AppOptionDef::createString("output-format", "OFMT", "", "out_format", "Output file format (bmp,jpg,png,gif,xpm)");
static AppOptionDef in_file = AppOptionDef::createString("", "", "", "in_file", "Path to input image file");
static AppOptionDef out_file = AppOptionDef::createString("", "", "", "out_file", "Path to output image file");
static AppOptionDef* options[] =
{
&help,
&rot_left, &rot_right,
&flip_x, &flip_y,
&roll, &gray, &bw,
&resize, &width, &height, &print,
&gif_append, &gif_time,
&in_format, &out_format,
&in_file, &out_file
};
static size_t const optionsCount = sizeof(options) / sizeof(options[0]);
App::parseAppOptions(this->getArgs(), options, optionsCount);
if (help)
{
App::printAppOptions(options, optionsCount, Console);
return;
}
String inputFile = in_file.getString();
if (!inputFile)
{
ConError << "Missing input file path" << strings::NewLine;
GATEXX_RAISE_EXCEPTION(results::InvalidInput, NULL, 0);
}
String outputFile = out_file.getString();
if (outputFile.empty())
{
outputFile = inputFile;
}
RasterImage image;
if (!File::exists(inputFile))
{
if (width && height)
{
image = RasterImage((unsigned)width.getInt32(), (unsigned)height.getInt32());
image.clear(Color::White);
}
else
{
ConError << "Input file does not exist and not parameters found to create a new image" << strings::NewLine;
GATEXX_RAISE_EXCEPTION(results::InvalidInput, NULL, 0);
}
}
else
{
try
{
image = loadImage(inputFile, in_format.getString());
}
catch (gate::Throwable const& xcpt)
{
ConError << "Failed to load image: " << util::printException(xcpt, true, true) << strings::NewLine;
GATEXX_RETHROW;
}
}
if (resize)
{
int new_width = (int)width.getInt32();
int new_height = (int)height.getInt32();
if (new_width < 0)
{
new_width += (int)image.getWidth();
}
if (new_height < 0)
{
new_height += (int)image.getHeight();
}
if ((new_width < 0) || (new_height < 0))
{
GATEXX_RAISE_EXCEPTION(results::InvalidArg, "Invalid size arguments", 0);
}
if ((new_width == 0) && (new_height != 0))
{
const float aspect_ratio = (float)image.getWidth() / (float)image.getHeight();
new_width = (int)((float)new_height * aspect_ratio);
}
else if ((new_height == 0) && (new_width != 0))
{
const float aspect_ratio = (float)image.getWidth() / (float)image.getHeight();
new_height = (int)((float)new_width / aspect_ratio);
}
if ((new_width != 0) && (new_height != 0))
{
RasterImage newImage((unsigned)new_width, (unsigned)new_height);
newImage.resampleFrom(image);
newImage.swap(image);
}
}
if (rot_left)
{
image = image.rotateLeft();
}
if (rot_right)
{
image = image.rotateRight();
}
if (flip_x)
{
image = image.flipX();
}
if (flip_y)
{
image = image.flipY();
}
if (roll)
{
image = image.rollOver();
}
if (gray)
{
image.makeGrayscale();
}
if (bw)
{
image.makeMonochrome();
}
if (slice)
{
createImageSlices(image, width.getInt32(), height.getInt32(), outputFile, out_format.getString());
return;
}
if (print)
{
DefaultFont16::printText(image, print.getString());
}
if (gif_append)
{
int32_t gif_time_ms = gif_time.getInt32();
ArrayList<String> images;
images.add(gif_append.getString());
append_frame_to_gif(inputFile, image, images.toArray(), gif_time_ms, outputFile);
return;
}
saveImage(outputFile, out_format.getString(), image);
}
};
} // end of namespace apps
} // end of namespace gate
#include <stdio.h>
int gate_main(char const* program, char const* const* arguments, gate_size_t argcount, gate_uintptr_t apphandle)
{
gate::apps::PixApp app;
return gate::App::runApp(app, program, arguments, argcount, apphandle);
}
|