forked from octalmage/robotjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpng_io.c
338 lines (281 loc) · 8.78 KB
/
png_io.c
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
#include "png_io.h"
#include "os.h"
#include <png.h>
#include <stdio.h> /* fopen() */
#include <stdlib.h> /* malloc/realloc */
#include <assert.h>
#if defined(_MSC_VER)
#include "ms_stdint.h"
#include "ms_stdbool.h"
#else
#include <stdint.h>
#include <stdbool.h>
#endif
const char *MMPNGReadErrorString(MMIOError error)
{
switch (error) {
case kPNGAccessError:
return "Could not open file";
case kPNGReadError:
return "Could not read file";
case kPNGInvalidHeaderError:
return "Not a PNG file";
default:
return NULL;
}
}
MMBitmapRef newMMBitmapFromPNG(const char *path, MMPNGReadError *err)
{
FILE *fp;
uint8_t header[8];
png_struct *png_ptr = NULL;
png_info *info_ptr = NULL;
png_byte bit_depth, color_type;
uint8_t *row, *bitmapData;
uint8_t bytesPerPixel;
png_uint_32 width, height, y;
uint32_t bytewidth;
if ((fp = fopen(path, "rb")) == NULL) {
if (err != NULL) *err = kPNGAccessError;
return NULL;
}
/* Initialize error code to generic value. */
if (err != NULL) *err = kPNGGenericError;
/* Validate the PNG. */
if (fread(header, 1, sizeof header, fp) == 0) {
if (err != NULL) *err = kPNGReadError;
goto bail;
} else if (png_sig_cmp(header, 0, sizeof(header)) != 0) {
if (err != NULL) *err = kPNGInvalidHeaderError;
goto bail;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) goto bail;
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) goto bail;
/* Set up error handling. */
if (setjmp(png_jmpbuf(png_ptr))) {
goto bail;
}
png_init_io(png_ptr, fp);
/* Skip past the header. */
png_set_sig_bytes(png_ptr, sizeof header);
png_read_info(png_ptr, info_ptr);
/* Convert different image types to common type to be read. */
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
/* Convert color palettes to RGB. */
if (color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png_ptr);
}
/* Convert PNG to bit depth of 8. */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
png_set_expand_gray_1_2_4_to_8(png_ptr);
} else if (bit_depth == 16) {
png_set_strip_16(png_ptr);
}
/* Convert transparency chunk to alpha channel. */
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
png_set_tRNS_to_alpha(png_ptr);
}
/* Convert gray images to RGB. */
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
png_set_gray_to_rgb(png_ptr);
}
/* Ignore alpha for now. */
if (color_type & PNG_COLOR_MASK_ALPHA) {
png_set_strip_alpha(png_ptr);
}
/* Get image attributes. */
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
bytesPerPixel = 3; /* All images decompress to this size. */
bytewidth = ADD_PADDING(width * bytesPerPixel); /* Align width. */
/* Decompress the PNG row by row. */
bitmapData = calloc(1, bytewidth * height);
row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
if (bitmapData == NULL || row == NULL) goto bail;
for (y = 0; y < height; ++y) {
png_uint_32 x;
const uint32_t rowOffset = y * bytewidth;
uint8_t *rowptr = row;
png_read_row(png_ptr, (png_byte *)row, NULL);
for (x = 0; x < width; ++x) {
const uint32_t colOffset = x * bytesPerPixel;
MMRGBColor *color = (MMRGBColor *)(bitmapData + rowOffset + colOffset);
color->red = *rowptr++;
color->green = *rowptr++;
color->blue = *rowptr++;
}
}
free(row);
/* Finish reading. */
png_read_end(png_ptr, NULL);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return createMMBitmap(bitmapData, width, height,
bytewidth, bytesPerPixel * 8, bytesPerPixel);
bail:
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return NULL;
}
struct _PNGWriteInfo {
png_struct *png_ptr;
png_info *info_ptr;
png_byte **row_pointers;
size_t row_count;
bool free_row_pointers;
};
typedef struct _PNGWriteInfo PNGWriteInfo;
typedef PNGWriteInfo *PNGWriteInfoRef;
/* Returns pointer to PNGWriteInfo struct containing data ready to be used with
* functions such as png_write_png().
*
* It is the caller's responsibility to destroy() the returned structure with
* destroyPNGWriteInfo(). */
static PNGWriteInfoRef createPNGWriteInfo(MMBitmapRef bitmap)
{
PNGWriteInfoRef info = malloc(sizeof(PNGWriteInfo));
png_uint_32 y;
if (info == NULL) return NULL;
info->png_ptr = NULL;
info->info_ptr = NULL;
info->row_pointers = NULL;
assert(bitmap != NULL);
/* Initialize the write struct. */
info->png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (info->png_ptr == NULL) goto bail;
/* Set up error handling. */
if (setjmp(png_jmpbuf(info->png_ptr))) {
png_destroy_write_struct(&(info->png_ptr), &(info->info_ptr));
goto bail;
}
/* Initialize the info struct. */
info->info_ptr = png_create_info_struct(info->png_ptr);
if (info->info_ptr == NULL) {
png_destroy_write_struct(&(info->png_ptr), NULL);
goto bail;
}
/* Set image attributes. */
png_set_IHDR(info->png_ptr,
info->info_ptr,
(png_uint_32)bitmap->width,
(png_uint_32)bitmap->height,
8,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
info->row_count = bitmap->height;
info->row_pointers = png_malloc(info->png_ptr,
sizeof(png_byte *) * info->row_count);
if (bitmap->bytesPerPixel == 3) {
/* No alpha channel; image data can be copied directly. */
for (y = 0; y < info->row_count; ++y) {
info->row_pointers[y] = bitmap->imageBuffer + (bitmap->bytewidth * y);
}
info->free_row_pointers = false;
/* Convert BGR to RGB if necessary. */
if (MMRGB_IS_BGR) {
png_set_bgr(info->png_ptr);
}
} else {
/* Ignore alpha channel; copy image data row by row. */
const size_t bytesPerPixel = 3;
const size_t bytewidth = ADD_PADDING(bitmap->width * bytesPerPixel);
for (y = 0; y < info->row_count; ++y) {
png_uint_32 x;
png_byte *row_ptr = png_malloc(info->png_ptr, bytewidth);
info->row_pointers[y] = row_ptr;
for (x = 0; x < bitmap->width; ++x) {
MMRGBColor *color = MMRGBColorRefAtPoint(bitmap, x, y);
row_ptr[0] = color->red;
row_ptr[1] = color->green;
row_ptr[2] = color->blue;
row_ptr += bytesPerPixel;
}
}
info->free_row_pointers = true;
}
png_set_rows(info->png_ptr, info->info_ptr, info->row_pointers);
return info;
bail:
if (info != NULL) free(info);
return NULL;
}
/* Free memory in use by |info|. */
static void destroyPNGWriteInfo(PNGWriteInfoRef info)
{
assert(info != NULL);
if (info->row_pointers != NULL) {
if (info->free_row_pointers) {
size_t y;
for (y = 0; y < info->row_count; ++y) {
free(info->row_pointers[y]);
}
}
png_free(info->png_ptr, info->row_pointers);
}
png_destroy_write_struct(&(info->png_ptr), &(info->info_ptr));
free(info);
}
int saveMMBitmapAsPNG(MMBitmapRef bitmap, const char *path)
{
FILE *fp = fopen(path, "wb");
PNGWriteInfoRef info;
if (fp == NULL) return -1;
if ((info = createPNGWriteInfo(bitmap)) == NULL) {
fclose(fp);
return -1;
}
png_init_io(info->png_ptr, fp);
png_write_png(info->png_ptr, info->info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
fclose(fp);
destroyPNGWriteInfo(info);
return 0;
}
/* Structure to store PNG image bytes. */
struct io_data
{
uint8_t *buffer; /* Pointer to raw file data. */
size_t size; /* Number of bytes actually written to buffer. */
size_t allocedSize; /* Number of bytes allocated for buffer. */
};
/* Called each time libpng attempts to write data in createPNGData(). */
void png_append_data(png_struct *png_ptr,
png_byte *new_data,
png_size_t length)
{
struct io_data *data = png_get_io_ptr(png_ptr);
data->size += length;
/* Allocate or grow buffer. */
if (data->buffer == NULL) {
data->allocedSize = data->size;
data->buffer = png_malloc(png_ptr, data->allocedSize);
assert(data->buffer != NULL);
} else if (data->allocedSize < data->size) {
do {
/* Double size each time to avoid calls to realloc. */
data->allocedSize <<= 1;
} while (data->allocedSize < data->size);
data->buffer = realloc(data->buffer, data->allocedSize);
}
/* Copy new bytes to end of buffer. */
memcpy(data->buffer + data->size - length, new_data, length);
}
uint8_t *createPNGData(MMBitmapRef bitmap, size_t *len)
{
PNGWriteInfoRef info = NULL;
struct io_data data = {NULL, 0, 0};
assert(bitmap != NULL);
assert(len != NULL);
if ((info = createPNGWriteInfo(bitmap)) == NULL) return NULL;
png_set_write_fn(info->png_ptr, &data, &png_append_data, NULL);
png_write_png(info->png_ptr, info->info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
destroyPNGWriteInfo(info);
*len = data.size;
return data.buffer;
}