-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
348 lines (294 loc) · 10.5 KB
/
main.c
File metadata and controls
348 lines (294 loc) · 10.5 KB
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
/*
* vim: tabstop=8 shiftwidth=8
* trying out kernel coding style
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <time.h>
#include <png.h>
extern int optind;
extern char *optarg;
#define STACK_PNG_VERSION "1.6.37"
#define PNG_SIG_LENGTH 8
#define stack_image_row_size(image) (sizeof(uint8_t) * 3 * (image)->width)
#define stack_png_reader_row_size(reader) (sizeof(uint8_t) * 3 * (reader)->width)
#define max(A, B) ((A) > (B) ? (A) : (B))
#define DEBUG_BOOL(b) printf("%s\n", (b) ? "true" : "false")
#define TIME(stmt) do { \
clock_t t = clock(); \
stmt \
t = clock() - t; \
printf("%f\n", t / ((double) CLOCKS_PER_SEC)); \
} while(0);
#define true 1
#define false 0
struct stack_png_reader {
png_struct *png;
png_info *info;
size_t width;
size_t height;
};
struct stack_png_writer {
png_struct *png;
png_info *info;
FILE *file;
};
/*
* 8-bit RGB image data
*/
struct stack_image {
size_t width;
size_t height;
uint8_t *data;
};
struct stack_pixel {
uint8_t r;
uint8_t g;
uint8_t b;
};
/*
* Reads the first 8 bytes and checks them against the magic png header
* Should probably be followed with a call to "png_set_sig_bytes"
*/
int stack_check_if_png(FILE *file) {
uint8_t bytes[PNG_SIG_LENGTH];
fread(bytes, sizeof(uint8_t), PNG_SIG_LENGTH, file);
return png_sig_cmp(bytes, 0, PNG_SIG_LENGTH) == 0;
}
/*
* struct stack_png reading functions
*/
void stack_png_reader_init(struct stack_png_reader *reader, char *file_name) {
png_struct *png;
png_info *info;
FILE *file;
png = png_create_read_struct(STACK_PNG_VERSION, NULL, NULL, NULL);
if (png == NULL) {
fprintf(stderr, "Unable to create read struct\n");
exit(EXIT_FAILURE);
}
info = png_create_info_struct(png);
if (info == NULL) {
fprintf(stderr, "Unable to create info struct\n");
exit(EXIT_FAILURE);
}
file = fopen(file_name, "rb");
if (file == NULL) {
fprintf(stderr, "Unable to open file %s for reading\n", file_name);
exit(EXIT_FAILURE);
} else if (!stack_check_if_png(file)) {
fprintf(stderr, "File %s is not a png file\n", file_name);
exit(EXIT_FAILURE);
}
png_init_io(png, file);
png_set_sig_bytes(png, PNG_SIG_LENGTH);
png_read_png(
png, info,
PNG_TRANSFORM_STRIP_16 |
PNG_TRANSFORM_PACKING |
PNG_TRANSFORM_GRAY_TO_RGB |
PNG_TRANSFORM_STRIP_ALPHA,
NULL);
reader->png = png;
reader->info = info;
reader->width = png_get_image_width(png, info);
reader->height = png_get_image_height(png, info);
fclose(file);
}
void stack_png_reader_destroy(struct stack_png_reader *reader) {
png_destroy_read_struct(&reader->png, &reader->info, NULL);
}
void stack_png_reader_load_image(struct stack_png_reader *reader, struct stack_image *image) {
size_t i, row_size;
uint8_t **image_rows;
image->width = reader->width;
image->height = reader->height;
row_size = stack_image_row_size(image);
image->data = malloc(row_size * image->height);
image_rows = png_get_rows(reader->png, reader->info);
for(i = 0; i < image->height; i++)
memcpy(image->data + i * row_size, image_rows[i], row_size);
}
/*
* struct stack_png writing functions
*/
void stack_png_writer_init(struct stack_png_writer *writer, char *file_name) {
png_struct *png;
png_info *info;
FILE *file;
png = png_create_write_struct(STACK_PNG_VERSION, NULL, NULL, NULL);
if (png == NULL) {
fprintf(stderr, "Unable to create write struct\n");
exit(EXIT_FAILURE);
}
info = png_create_info_struct(png);
if (info == NULL) {
fprintf(stderr, "Unable to create info struct\n");
exit(EXIT_FAILURE);
}
file = fopen(file_name, "wb");
if (file == NULL) {
fprintf(stderr, "Unable to open file %s for writing\n", file_name);
exit(EXIT_FAILURE);
}
png_init_io(png, file);
png_set_compression_level(png, 2);
writer->png = png;
writer->info = info;
writer->file = file;
}
void stack_png_writer_destroy(struct stack_png_writer *writer) {
png_destroy_write_struct(&writer->png, &writer->info);
fclose(writer->file);
}
void stack_png_writer_save_image(struct stack_png_writer *writer, struct stack_image *image) {
uint8_t **data;
size_t row_size, i;
row_size = stack_image_row_size(image);
data = malloc(sizeof(uint8_t *) * image->height);
for (i = 0; i < image->height; i++)
data[i] = image->data + i * row_size;
png_set_IHDR(
writer->png,
writer->info,
image->width,
image->height,
8, /* bit depth of 8 */
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_set_rows(writer->png, writer->info, data);
png_write_png(
writer->png,
writer->info,
PNG_TRANSFORM_IDENTITY,
NULL);
free(data);
}
/*
* struct stack_image functions
*/
void stack_image_destroy(struct stack_image *image) {
free(image->data);
}
struct stack_pixel stack_image_get_pixel(struct stack_image *image, size_t x, size_t y) {
struct stack_pixel pixel;
size_t row_size;
row_size = stack_image_row_size(image);
pixel.r = image->data[row_size * y + 3 * x];
pixel.g = image->data[row_size * y + 3 * x + 1];
pixel.b = image->data[row_size * y + 3 * x + 2];
return pixel;
}
void stack_image_set_pixel(struct stack_image *image,
struct stack_pixel pixel,
size_t x, size_t y) {
size_t row_size = stack_image_row_size(image);
image->data[row_size * y + 3 * x] = pixel.r;
image->data[row_size * y + 3 * x + 1] = pixel.g;
image->data[row_size * y + 3 * x + 2] = pixel.b;
}
void stack_image_init_blank(struct stack_image *image, size_t width, size_t height) {
size_t row_size;
image->width = width;
image->height = height;
row_size = stack_image_row_size(image);
image->data = calloc(row_size * height, sizeof(uint8_t));
}
/*
* Does no safety checks
*/
void stack_image_paste_png_reader(struct stack_image *dest,
struct stack_png_reader *src,
size_t x, size_t y) {
size_t i, dest_row_size, src_row_size;
uint8_t **src_rows, *dest_location;
dest_row_size = stack_image_row_size(dest);
src_row_size = stack_png_reader_row_size(src);
src_rows = png_get_rows(src->png, src->info);
dest_location = dest->data + y * dest_row_size + 3 * x;
for(i = 0; i < src->height; i++) {
memcpy(dest_location + i * dest_row_size,
src_rows[i],
src_row_size);
}
}
int stack_parse_int(char *s) {
int n = 0, c;
while (('0' ^ (c = *(s++))) < 10)
n = n * 10 + c - '0';
return n;
}
int main(int argc, char **argv) {
size_t width, height, y;
int i, option, num_images, gap = 0, long_index;
struct stack_image result;
struct stack_png_writer writer;
struct stack_png_reader *readers;
char *out_file = "out.png";
struct option long_options[] = {
{ "help", false, NULL, 'h' },
{ "output", true, NULL, 'o' },
{ "gap-width", true, NULL, 'g' },
{ 0, 0, 0, 0 }
};
/* Read cmd line options */
while ((option = getopt_long(argc, argv, "ho:g:", long_options, &long_index)) != -1) {
if (option == 0)
option = long_options[long_index].val;
switch (option) {
case 'o':
out_file = optarg;
break;
case 'g':
gap = stack_parse_int(optarg);
break;
case 'h':
printf("Usage: stack-png [OPTION]... [FILE]...\n");
printf(" Vertically stack a list of png files\n");
printf("\n");
printf("OPTIONS\n");
printf("-h --help Print this message and exit\n");
printf("-o --output=FILE Write the generated png to FILE\n");
printf(" Default: out.png\n");
printf("-g --gap-width=n Place an n pixel gap between each file\n");
printf(" Default: 0\n");
return EXIT_SUCCESS;
default:
return EXIT_FAILURE;
}
}
/* load up images; calculate size of output */
num_images = argc - optind;
if (num_images <= 0) {
fprintf(stderr, "Please give at least 1 file\n");
return EXIT_FAILURE;
}
readers = malloc(sizeof(struct stack_png_reader) * num_images);
width = 0;
height = gap * (num_images - 1);
for (i = 0; i < num_images; i++) {
stack_png_reader_init(&readers[i], argv[i+optind]);
width = max(width, readers[i].width);
height += readers[i].height;
}
/* paste all the images together */
y = 0;
stack_image_init_blank(&result, width, height);
for (i = 0; i < num_images; i++) {
size_t x = (width - readers[i].width) / 2;
stack_image_paste_png_reader(&result, &readers[i], x, y);
stack_png_reader_destroy(&readers[i]);
y += readers[i].height + gap;
}
stack_png_writer_init(&writer, out_file);
stack_png_writer_save_image(&writer, &result);
stack_png_writer_destroy(&writer);
stack_image_destroy(&result);
free(readers);
return EXIT_SUCCESS;
}