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_bytep) * 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 row[x * 3] = pixelData[stride * i * 4 + x * 4];
69 row[x * 3 + 1] = pixelData[stride * i * 4 + x * 4 + 1];
70 row[x * 3 + 2] = pixelData[stride * i * 4 + x * 4 + 2];
71 }
72 png_write_row(png, row);
73 }
74 free(row);
75 return true;
76}
77
78bool PNGWriteCustomChunk(png_structp png, const char* name, size_t size, void* data) {
79 char realName[5];
80 strncpy(realName, name, 4);
81 realName[0] = tolower(realName[0]);
82 realName[1] = tolower(realName[1]);
83 realName[4] = '\0';
84 if (setjmp(png_jmpbuf(png))) {
85 return false;
86 }
87 png_write_chunk(png, (png_bytep) realName, data, size);
88 return true;
89}
90
91void PNGWriteClose(png_structp png, png_infop info) {
92 if (!setjmp(png_jmpbuf(png))) {
93 png_write_end(png, info);
94 }
95 png_destroy_write_struct(&png, &info);
96}
97
98bool isPNG(struct VFile* source) {
99 png_byte header[PNG_HEADER_BYTES];
100 source->read(source, header, PNG_HEADER_BYTES);
101 return !png_sig_cmp(header, 0, PNG_HEADER_BYTES);
102}
103
104png_structp PNGReadOpen(struct VFile* source, unsigned offset) {
105 png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
106 if (!png) {
107 return 0;
108 }
109 if (setjmp(png_jmpbuf(png))) {
110 png_destroy_read_struct(&png, 0, 0);
111 return 0;
112 }
113 png_set_read_fn(png, source, _pngRead);
114 png_set_sig_bytes(png, offset);
115 return png;
116}
117
118bool PNGInstallChunkHandler(png_structp png, void* context, ChunkHandler handler, const char* chunkName) {
119 if (setjmp(png_jmpbuf(png))) {
120 return false;
121 }
122 png_set_read_user_chunk_fn(png, context, handler);
123 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_bytep) chunkName, 1);
124 return true;
125}
126
127bool PNGReadHeader(png_structp png, png_infop info) {
128 if (setjmp(png_jmpbuf(png))) {
129 return false;
130 }
131 png_read_info(png, info);
132 return true;
133}
134
135bool PNGIgnorePixels(png_structp png, png_infop info) {
136 if (setjmp(png_jmpbuf(png))) {
137 return false;
138 }
139
140 unsigned height = png_get_image_height(png, info);
141 unsigned i;
142 for (i = 0; i < height; ++i) {
143 png_read_row(png, 0, 0);
144 }
145 return true;
146}
147
148bool PNGReadPixels(png_structp png, png_infop info, void* pixels, unsigned width, unsigned height, unsigned stride) {
149 if (setjmp(png_jmpbuf(png))) {
150 return false;
151 }
152
153 uint8_t* pixelData = pixels;
154 unsigned pngHeight = png_get_image_height(png, info);
155 if (height < pngHeight) {
156 pngHeight = height;
157 }
158
159 unsigned pngWidth = png_get_image_width(png, info);
160 if (width < pngWidth) {
161 pngWidth = width;
162 }
163
164 unsigned i;
165 png_bytep row = malloc(png_get_rowbytes(png, info));
166 for (i = 0; i < pngHeight; ++i) {
167 png_read_row(png, row, 0);
168 unsigned x;
169 for (x = 0; x < pngWidth; ++x) {
170 pixelData[stride * i * 4 + x * 4] = row[x * 3];
171 pixelData[stride * i * 4 + x * 4 + 1] = row[x * 3 + 1];
172 pixelData[stride * i * 4 + x * 4 + 2] = row[x * 3 + 2];
173 }
174 }
175 free(row);
176 return true;
177}
178
179bool PNGReadFooter(png_structp png, png_infop end) {
180 if (setjmp(png_jmpbuf(png))) {
181 return false;
182 }
183 png_read_end(png, end);
184 return true;
185}
186
187void PNGReadClose(png_structp png, png_infop info, png_infop end) {
188 png_destroy_read_struct(&png, &info, &end);
189}
190
191#endif