src/util/png-io.c (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "util/png-io.h"
7
8#ifdef USE_PNG
9
10#include "vfs.h"
11
12static void _pngWrite(png_structp png, png_bytep buffer, png_size_t size) {
13 struct VFile* vf = png_get_io_ptr(png);
14 size_t written = vf->write(vf, buffer, size);
15 if (written != size) {
16 png_error(png, "Could not write PNG");
17 }
18}
19
20static void _pngRead(png_structp png, png_bytep buffer, png_size_t size) {
21 struct VFile* vf = png_get_io_ptr(png);
22 size_t read = vf->read(vf, buffer, size);
23 if (read != size) {
24 png_error(png, "Could not read PNG");
25 }
26}
27
28png_structp PNGWriteOpen(struct VFile* source) {
29 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
30 if (!png) {
31 return 0;
32 }
33 if (setjmp(png_jmpbuf(png))) {
34 png_destroy_write_struct(&png, 0);
35 return 0;
36 }
37 png_set_write_fn(png, source, _pngWrite, 0);
38 return png;
39}
40
41png_infop PNGWriteHeader(png_structp png, unsigned width, unsigned height) {
42 png_infop info = png_create_info_struct(png);
43 if (!info) {
44 return 0;
45 }
46 if (setjmp(png_jmpbuf(png))) {
47 return 0;
48 }
49 png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
50 png_write_info(png, info);
51 return info;
52}
53
54bool PNGWritePixels(png_structp png, unsigned width, unsigned height, unsigned stride, const void* pixels) {
55 png_bytep row = malloc(sizeof(png_byte) * width * 3);
56 if (!row) {
57 return false;
58 }
59 const png_byte* pixelData = pixels;
60 if (setjmp(png_jmpbuf(png))) {
61 free(row);
62 return false;
63 }
64 unsigned i;
65 for (i = 0; i < height; ++i) {
66 unsigned x;
67 for (x = 0; x < width; ++x) {
68#ifdef COLOR_16_BIT
69 uint16_t c = ((uint16_t*) pixelData)[stride * i + x];
70#ifdef COLOR_5_6_5
71 row[x * 3] = (c >> 8) & 0xF8;
72 row[x * 3 + 1] = (c >> 3) & 0xFC;
73 row[x * 3 + 2] = (c << 3) & 0xF8;
74#else
75 row[x * ] = (c >> 7) & 0xF8;
76 row[x * + 1] = (c >> 2) & 0xF8;
77 row[x * + 2] = (c << 3) & 0xF8;
78#endif
79#else
80#ifdef __BIG_ENDIAN__
81 row[x * 3] = pixelData[stride * i * 4 + x * 4 + 3];
82 row[x * 3 + 1] = pixelData[stride * i * 4 + x * 4 + 2];
83 row[x * 3 + 2] = pixelData[stride * i * 4 + x * 4 + 1];
84#else
85 row[x * 3] = pixelData[stride * i * 4 + x * 4];
86 row[x * 3 + 1] = pixelData[stride * i * 4 + x * 4 + 1];
87 row[x * 3 + 2] = pixelData[stride * i * 4 + x * 4 + 2];
88#endif
89#endif
90 }
91 png_write_row(png, row);
92 }
93 free(row);
94 return true;
95}
96
97bool PNGWriteCustomChunk(png_structp png, const char* name, size_t size, void* data) {
98 char realName[5];
99 strncpy(realName, name, 4);
100 realName[0] = tolower((int) realName[0]);
101 realName[1] = tolower((int) realName[1]);
102 realName[4] = '\0';
103 if (setjmp(png_jmpbuf(png))) {
104 return false;
105 }
106 png_write_chunk(png, (png_bytep) realName, data, size);
107 return true;
108}
109
110void PNGWriteClose(png_structp png, png_infop info) {
111 if (!setjmp(png_jmpbuf(png))) {
112 png_write_end(png, info);
113 }
114 png_destroy_write_struct(&png, &info);
115}
116
117bool isPNG(struct VFile* source) {
118 png_byte header[PNG_HEADER_BYTES];
119 source->read(source, header, PNG_HEADER_BYTES);
120 return !png_sig_cmp(header, 0, PNG_HEADER_BYTES);
121}
122
123png_structp PNGReadOpen(struct VFile* source, unsigned offset) {
124 png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
125 if (!png) {
126 return 0;
127 }
128 if (setjmp(png_jmpbuf(png))) {
129 png_destroy_read_struct(&png, 0, 0);
130 return 0;
131 }
132 png_set_read_fn(png, source, _pngRead);
133 png_set_sig_bytes(png, offset);
134 return png;
135}
136
137bool PNGInstallChunkHandler(png_structp png, void* context, ChunkHandler handler, const char* chunkName) {
138 if (setjmp(png_jmpbuf(png))) {
139 return false;
140 }
141 png_set_read_user_chunk_fn(png, context, handler);
142 int len = strlen(chunkName);
143 int chunks = 0;
144 char* chunkList = strdup(chunkName);
145 int i;
146 for (i = 4; i <= len; i += 5) {
147 chunkList[i] = '\0';
148 ++chunks;
149 }
150 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_bytep) chunkList, chunks);
151 free(chunkList);
152 return true;
153}
154
155bool PNGReadHeader(png_structp png, png_infop info) {
156 if (setjmp(png_jmpbuf(png))) {
157 return false;
158 }
159 png_read_info(png, info);
160 return true;
161}
162
163bool PNGIgnorePixels(png_structp png, png_infop info) {
164 if (setjmp(png_jmpbuf(png))) {
165 return false;
166 }
167
168 unsigned height = png_get_image_height(png, info);
169 unsigned i;
170 for (i = 0; i < height; ++i) {
171 png_read_row(png, 0, 0);
172 }
173 return true;
174}
175
176bool PNGReadPixels(png_structp png, png_infop info, void* pixels, unsigned width, unsigned height, unsigned stride) {
177 if (setjmp(png_jmpbuf(png))) {
178 return false;
179 }
180
181 uint8_t* pixelData = pixels;
182 unsigned pngHeight = png_get_image_height(png, info);
183 if (height < pngHeight) {
184 pngHeight = height;
185 }
186
187 unsigned pngWidth = png_get_image_width(png, info);
188 if (width < pngWidth) {
189 pngWidth = width;
190 }
191
192 unsigned i;
193 png_bytep row = malloc(png_get_rowbytes(png, info));
194 for (i = 0; i < pngHeight; ++i) {
195 png_read_row(png, row, 0);
196 unsigned x;
197 for (x = 0; x < pngWidth; ++x) {
198#ifdef COLOR_16_BIT
199 uint16_t c = row[x * 3 + 2] >> 3;
200#ifdef COLOR_5_6_5
201 c |= (row[x * 3 + 1] << 3) & 0x7E0;
202 c |= (row[x * 3] << 8) & 0xF800;
203#else
204 c |= (row[x * 3 + 1] << 2) & 0x3E0;
205 c |= (row[x * 3] << 7) & 0x7C00;
206#endif
207 ((uint16_t*) pixelData)[stride * i + x] = c;
208#else
209#if __BIG_ENDIAN__
210 pixelData[stride * i * 4 + x * 4 + 3] = row[x * 3];
211 pixelData[stride * i * 4 + x * 4 + 2] = row[x * 3 + 1];
212 pixelData[stride * i * 4 + x * 4 + 1] = row[x * 3 + 2];
213 pixelData[stride * i * 4 + x * 4] = 0xFF;
214#else
215 pixelData[stride * i * 4 + x * 4] = row[x * 3];
216 pixelData[stride * i * 4 + x * 4 + 1] = row[x * 3 + 1];
217 pixelData[stride * i * 4 + x * 4 + 2] = row[x * 3 + 2];
218 pixelData[stride * i * 4 + x * 4 + 3] = 0xFF;
219#endif
220#endif
221 }
222 }
223 free(row);
224 return true;
225}
226
227bool PNGReadFooter(png_structp png, png_infop end) {
228 if (setjmp(png_jmpbuf(png))) {
229 return false;
230 }
231 png_read_end(png, end);
232 return true;
233}
234
235void PNGReadClose(png_structp png, png_infop info, png_infop end) {
236 png_destroy_read_struct(&png, &info, &end);
237}
238
239#endif