-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_io.c
More file actions
275 lines (236 loc) · 5.91 KB
/
Copy pathimage_io.c
File metadata and controls
275 lines (236 loc) · 5.91 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
#include <errno.h>
#include <png.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "s2.h"
int
image_load(const struct app_config *cfg, struct image *img)
{
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width;
png_uint_32 height;
png_byte color_type;
png_byte bit_depth;
png_bytep *rows;
size_t rowbytes;
size_t packed_rowbytes;
size_t i;
if (!cfg || !img) {
fprintf(stderr, "s2: internal error: invalid load args\n");
return -1;
}
memset(img, 0, sizeof(*img));
fp = NULL;
if (cfg->input_kind == INPUT_STDIN) {
fp = stdin;
} else if (cfg->input_kind == INPUT_FILE && cfg->input_path) {
fp = fopen(cfg->input_path, "rb");
if (!fp) {
fprintf(stderr, "s2: cannot open input '%s': %s\n", cfg->input_path, strerror(errno));
return -1;
}
}
if (!fp) {
fprintf(stderr, "s2: no input image\n");
return -1;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fprintf(stderr, "s2: failed to init png reader\n");
if (fp != stdin) {
fclose(fp);
}
return -1;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
fprintf(stderr, "s2: failed to init png info\n");
png_destroy_read_struct(&png_ptr, NULL, NULL);
if (fp != stdin) {
fclose(fp);
}
return -1;
}
rows = NULL;
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stderr, "s2: invalid or unsupported png input\n");
if (rows) {
free(rows);
}
image_free(img);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if (fp != stdin) {
fclose(fp);
}
return -1;
}
png_init_io(png_ptr, fp);
png_read_info(png_ptr, info_ptr);
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
if (bit_depth == 16) {
png_set_strip_16(png_ptr);
}
if (color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png_ptr);
}
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
png_set_expand_gray_1_2_4_to_8(png_ptr);
}
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
png_set_tRNS_to_alpha(png_ptr);
}
if (color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
}
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
png_set_gray_to_rgb(png_ptr);
}
png_read_update_info(png_ptr, info_ptr);
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
if (width == 0 || height == 0 || rowbytes == 0) {
fprintf(stderr, "s2: invalid png dimensions\n");
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if (fp != stdin) {
fclose(fp);
}
return -1;
}
packed_rowbytes = (size_t)width * 4;
img->len = packed_rowbytes * (size_t)height;
img->pixels = malloc(img->len);
if (!img->pixels) {
fprintf(stderr, "s2: out of memory while loading png\n");
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if (fp != stdin) {
fclose(fp);
}
return -1;
}
rows = malloc(sizeof(*rows) * (size_t)height);
if (!rows) {
fprintf(stderr, "s2: out of memory while preparing png rows\n");
image_free(img);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if (fp != stdin) {
fclose(fp);
}
return -1;
}
for (i = 0; i < (size_t)height; i++) {
rows[i] = img->pixels + (i * packed_rowbytes);
}
png_read_image(png_ptr, rows);
png_read_end(png_ptr, NULL);
img->width = (int)width;
img->height = (int)height;
free(rows);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if (fp != stdin) {
fclose(fp);
}
return 0;
}
int
image_write_png_stream(const struct image *img, void *stream)
{
png_structp png_ptr;
png_infop info_ptr;
png_bytep *rows;
size_t i;
size_t rowbytes;
FILE *fp;
if (!img || !img->pixels || img->width <= 0 || img->height <= 0 || !stream) {
fprintf(stderr, "s2: invalid image for png write\n");
return -1;
}
fp = stream;
rowbytes = (size_t)img->width * 4;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fprintf(stderr, "s2: failed to init png writer\n");
return -1;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
fprintf(stderr, "s2: failed to init png info\n");
png_destroy_write_struct(&png_ptr, NULL);
return -1;
}
rows = NULL;
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stderr, "s2: failed to write png\n");
if (rows) {
free(rows);
}
png_destroy_write_struct(&png_ptr, &info_ptr);
return -1;
}
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr,
info_ptr,
(png_uint_32)img->width,
(png_uint_32)img->height,
8,
PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
rows = malloc(sizeof(*rows) * (size_t)img->height);
if (!rows) {
fprintf(stderr, "s2: out of memory while writing png\n");
png_destroy_write_struct(&png_ptr, &info_ptr);
return -1;
}
for (i = 0; i < (size_t)img->height; i++) {
rows[i] = (png_bytep)(img->pixels + (i * rowbytes));
}
png_write_image(png_ptr, rows);
png_write_end(png_ptr, NULL);
free(rows);
png_destroy_write_struct(&png_ptr, &info_ptr);
return 0;
}
int
image_save_png(const struct image *img, const char *path)
{
FILE *fp;
int rc;
if (!path || !*path) {
fprintf(stderr, "s2: invalid output path\n");
return -1;
}
fp = fopen(path, "wb");
if (!fp) {
fprintf(stderr, "s2: cannot open output '%s': %s\n", path, strerror(errno));
return -1;
}
rc = image_write_png_stream(img, fp);
if (fclose(fp) != 0) {
fprintf(stderr, "s2: failed to close output '%s'\n", path);
return -1;
}
return rc;
}
void
image_free(struct image *img)
{
if (!img) {
return;
}
if (img->pixels) {
free(img->pixels);
}
img->pixels = NULL;
img->len = 0;
img->width = 0;
img->height = 0;
}