1 /* Copyright (C) 2010-2018 The RetroArch team
2 *
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (rpng_encode.c).
5 * ---------------------------------------------------------------------------------------
6 *
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <encodings/crc32.h>
28 #include <streams/file_stream.h>
29 #include <streams/trans_stream.h>
30
31 #include "rpng_internal.h"
32
33 #undef GOTO_END_ERROR
34 #define GOTO_END_ERROR() do { \
35 fprintf(stderr, "[RPNG]: Error in line %d.\n", __LINE__); \
36 ret = false; \
37 goto end; \
38 } while(0)
39
dword_write_be(uint8_t * buf,uint32_t val)40 static void dword_write_be(uint8_t *buf, uint32_t val)
41 {
42 *buf++ = (uint8_t)(val >> 24);
43 *buf++ = (uint8_t)(val >> 16);
44 *buf++ = (uint8_t)(val >> 8);
45 *buf++ = (uint8_t)(val >> 0);
46 }
47
png_write_crc(RFILE * file,const uint8_t * data,size_t size)48 static bool png_write_crc(RFILE *file, const uint8_t *data, size_t size)
49 {
50 uint8_t crc_raw[4] = {0};
51 uint32_t crc = encoding_crc32(0, data, size);
52
53 dword_write_be(crc_raw, crc);
54 return filestream_write(file, crc_raw, sizeof(crc_raw)) == sizeof(crc_raw);
55 }
56
png_write_ihdr(RFILE * file,const struct png_ihdr * ihdr)57 static bool png_write_ihdr(RFILE *file, const struct png_ihdr *ihdr)
58 {
59 uint8_t ihdr_raw[21];
60
61 ihdr_raw[0] = '0'; /* Size */
62 ihdr_raw[1] = '0';
63 ihdr_raw[2] = '0';
64 ihdr_raw[3] = '0';
65 ihdr_raw[4] = 'I';
66 ihdr_raw[5] = 'H';
67 ihdr_raw[6] = 'D';
68 ihdr_raw[7] = 'R';
69 ihdr_raw[8] = 0; /* Width */
70 ihdr_raw[9] = 0;
71 ihdr_raw[10] = 0;
72 ihdr_raw[11] = 0;
73 ihdr_raw[12] = 0; /* Height */
74 ihdr_raw[13] = 0;
75 ihdr_raw[14] = 0;
76 ihdr_raw[15] = 0;
77 ihdr_raw[16] = ihdr->depth; /* Depth */
78 ihdr_raw[17] = ihdr->color_type;
79 ihdr_raw[18] = ihdr->compression;
80 ihdr_raw[19] = ihdr->filter;
81 ihdr_raw[20] = ihdr->interlace;
82
83 dword_write_be(ihdr_raw + 0, sizeof(ihdr_raw) - 8);
84 dword_write_be(ihdr_raw + 8, ihdr->width);
85 dword_write_be(ihdr_raw + 12, ihdr->height);
86 if (filestream_write(file, ihdr_raw, sizeof(ihdr_raw)) != sizeof(ihdr_raw))
87 return false;
88
89 if (!png_write_crc(file, ihdr_raw + sizeof(uint32_t),
90 sizeof(ihdr_raw) - sizeof(uint32_t)))
91 return false;
92
93 return true;
94 }
95
png_write_idat(RFILE * file,const uint8_t * data,size_t size)96 static bool png_write_idat(RFILE *file, const uint8_t *data, size_t size)
97 {
98 if (filestream_write(file, data, size) != (ssize_t)size)
99 return false;
100
101 if (!png_write_crc(file, data + sizeof(uint32_t), size - sizeof(uint32_t)))
102 return false;
103
104 return true;
105 }
106
png_write_iend(RFILE * file)107 static bool png_write_iend(RFILE *file)
108 {
109 const uint8_t data[] = {
110 0, 0, 0, 0,
111 'I', 'E', 'N', 'D',
112 };
113
114 if (filestream_write(file, data, sizeof(data)) != sizeof(data))
115 return false;
116
117 if (!png_write_crc(file, data + sizeof(uint32_t),
118 sizeof(data) - sizeof(uint32_t)))
119 return false;
120
121 return true;
122 }
123
copy_argb_line(uint8_t * dst,const uint32_t * src,unsigned width)124 static void copy_argb_line(uint8_t *dst, const uint32_t *src, unsigned width)
125 {
126 unsigned i;
127 for (i = 0; i < width; i++)
128 {
129 uint32_t col = src[i];
130 *dst++ = (uint8_t)(col >> 16);
131 *dst++ = (uint8_t)(col >> 8);
132 *dst++ = (uint8_t)(col >> 0);
133 *dst++ = (uint8_t)(col >> 24);
134 }
135 }
136
copy_bgr24_line(uint8_t * dst,const uint8_t * src,unsigned width)137 static void copy_bgr24_line(uint8_t *dst, const uint8_t *src, unsigned width)
138 {
139 unsigned i;
140 for (i = 0; i < width; i++, dst += 3, src += 3)
141 {
142 dst[2] = src[0];
143 dst[1] = src[1];
144 dst[0] = src[2];
145 }
146 }
147
count_sad(const uint8_t * data,size_t size)148 static unsigned count_sad(const uint8_t *data, size_t size)
149 {
150 size_t i;
151 unsigned cnt = 0;
152 for (i = 0; i < size; i++)
153 {
154 if (data[i])
155 cnt += abs((int8_t)data[i]);
156 }
157 return cnt;
158 }
159
filter_up(uint8_t * target,const uint8_t * line,const uint8_t * prev,unsigned width,unsigned bpp)160 static unsigned filter_up(uint8_t *target, const uint8_t *line,
161 const uint8_t *prev, unsigned width, unsigned bpp)
162 {
163 unsigned i;
164 width *= bpp;
165 for (i = 0; i < width; i++)
166 target[i] = line[i] - prev[i];
167
168 return count_sad(target, width);
169 }
170
filter_sub(uint8_t * target,const uint8_t * line,unsigned width,unsigned bpp)171 static unsigned filter_sub(uint8_t *target, const uint8_t *line,
172 unsigned width, unsigned bpp)
173 {
174 unsigned i;
175 width *= bpp;
176 for (i = 0; i < bpp; i++)
177 target[i] = line[i];
178 for (i = bpp; i < width; i++)
179 target[i] = line[i] - line[i - bpp];
180
181 return count_sad(target, width);
182 }
183
filter_avg(uint8_t * target,const uint8_t * line,const uint8_t * prev,unsigned width,unsigned bpp)184 static unsigned filter_avg(uint8_t *target, const uint8_t *line,
185 const uint8_t *prev, unsigned width, unsigned bpp)
186 {
187 unsigned i;
188 width *= bpp;
189 for (i = 0; i < bpp; i++)
190 target[i] = line[i] - (prev[i] >> 1);
191 for (i = bpp; i < width; i++)
192 target[i] = line[i] - ((line[i - bpp] + prev[i]) >> 1);
193
194 return count_sad(target, width);
195 }
196
filter_paeth(uint8_t * target,const uint8_t * line,const uint8_t * prev,unsigned width,unsigned bpp)197 static unsigned filter_paeth(uint8_t *target,
198 const uint8_t *line, const uint8_t *prev,
199 unsigned width, unsigned bpp)
200 {
201 unsigned i;
202 width *= bpp;
203 for (i = 0; i < bpp; i++)
204 target[i] = line[i] - paeth(0, prev[i], 0);
205 for (i = bpp; i < width; i++)
206 target[i] = line[i] - paeth(line[i - bpp], prev[i], prev[i - bpp]);
207
208 return count_sad(target, width);
209 }
210
rpng_save_image(const char * path,const uint8_t * data,unsigned width,unsigned height,unsigned pitch,unsigned bpp)211 static bool rpng_save_image(const char *path,
212 const uint8_t *data,
213 unsigned width, unsigned height, unsigned pitch, unsigned bpp)
214 {
215 unsigned h;
216 bool ret = true;
217 struct png_ihdr ihdr = {0};
218
219 const struct trans_stream_backend *stream_backend = NULL;
220 size_t encode_buf_size = 0;
221 uint8_t *encode_buf = NULL;
222 uint8_t *deflate_buf = NULL;
223 uint8_t *rgba_line = NULL;
224 uint8_t *up_filtered = NULL;
225 uint8_t *sub_filtered = NULL;
226 uint8_t *avg_filtered = NULL;
227 uint8_t *paeth_filtered = NULL;
228 uint8_t *prev_encoded = NULL;
229 uint8_t *encode_target = NULL;
230 void *stream = NULL;
231 uint32_t total_in = 0;
232 uint32_t total_out = 0;
233 RFILE *file = filestream_open(path,
234 RETRO_VFS_FILE_ACCESS_WRITE,
235 RETRO_VFS_FILE_ACCESS_HINT_NONE);
236 if (!file)
237 GOTO_END_ERROR();
238
239 stream_backend = trans_stream_get_zlib_deflate_backend();
240
241 if (filestream_write(file, png_magic, sizeof(png_magic)) != sizeof(png_magic))
242 GOTO_END_ERROR();
243
244 ihdr.width = width;
245 ihdr.height = height;
246 ihdr.depth = 8;
247 ihdr.color_type = bpp == sizeof(uint32_t) ? 6 : 2; /* RGBA or RGB */
248 if (!png_write_ihdr(file, &ihdr))
249 GOTO_END_ERROR();
250
251 encode_buf_size = (width * bpp + 1) * height;
252 encode_buf = (uint8_t*)malloc(encode_buf_size);
253 if (!encode_buf)
254 GOTO_END_ERROR();
255
256 prev_encoded = (uint8_t*)calloc(1, width * bpp);
257 if (!prev_encoded)
258 GOTO_END_ERROR();
259
260 rgba_line = (uint8_t*)malloc(width * bpp);
261 up_filtered = (uint8_t*)malloc(width * bpp);
262 sub_filtered = (uint8_t*)malloc(width * bpp);
263 avg_filtered = (uint8_t*)malloc(width * bpp);
264 paeth_filtered = (uint8_t*)malloc(width * bpp);
265 if (!rgba_line || !up_filtered || !sub_filtered || !avg_filtered || !paeth_filtered)
266 GOTO_END_ERROR();
267
268 encode_target = encode_buf;
269 for (h = 0; h < height;
270 h++, encode_target += width * bpp, data += pitch)
271 {
272 if (bpp == sizeof(uint32_t))
273 copy_argb_line(rgba_line, (const uint32_t*)data, width);
274 else
275 copy_bgr24_line(rgba_line, data, width);
276
277 /* Try every filtering method, and choose the method
278 * which has most entries as zero.
279 *
280 * This is probably not very optimal, but it's very
281 * simple to implement.
282 */
283 {
284 unsigned none_score = count_sad(rgba_line, width * bpp);
285 unsigned up_score = filter_up(up_filtered, rgba_line, prev_encoded, width, bpp);
286 unsigned sub_score = filter_sub(sub_filtered, rgba_line, width, bpp);
287 unsigned avg_score = filter_avg(avg_filtered, rgba_line, prev_encoded, width, bpp);
288 unsigned paeth_score = filter_paeth(paeth_filtered, rgba_line, prev_encoded, width, bpp);
289
290 uint8_t filter = 0;
291 unsigned min_sad = none_score;
292 const uint8_t *chosen_filtered = rgba_line;
293
294 if (sub_score < min_sad)
295 {
296 filter = 1;
297 chosen_filtered = sub_filtered;
298 min_sad = sub_score;
299 }
300
301 if (up_score < min_sad)
302 {
303 filter = 2;
304 chosen_filtered = up_filtered;
305 min_sad = up_score;
306 }
307
308 if (avg_score < min_sad)
309 {
310 filter = 3;
311 chosen_filtered = avg_filtered;
312 min_sad = avg_score;
313 }
314
315 if (paeth_score < min_sad)
316 {
317 filter = 4;
318 chosen_filtered = paeth_filtered;
319 }
320
321 *encode_target++ = filter;
322 memcpy(encode_target, chosen_filtered, width * bpp);
323
324 memcpy(prev_encoded, rgba_line, width * bpp);
325 }
326 }
327
328 deflate_buf = (uint8_t*)malloc(encode_buf_size * 2); /* Just to be sure. */
329 if (!deflate_buf)
330 GOTO_END_ERROR();
331
332 stream = stream_backend->stream_new();
333
334 if (!stream)
335 GOTO_END_ERROR();
336
337 stream_backend->set_in(
338 stream,
339 encode_buf,
340 (unsigned)encode_buf_size);
341 stream_backend->set_out(
342 stream,
343 deflate_buf + 8,
344 (unsigned)(encode_buf_size * 2));
345
346 if (!stream_backend->trans(stream, true, &total_in, &total_out, NULL))
347 {
348 GOTO_END_ERROR();
349 }
350
351 memcpy(deflate_buf + 4, "IDAT", 4);
352 dword_write_be(deflate_buf + 0, ((uint32_t)total_out));
353 if (!png_write_idat(file, deflate_buf, ((size_t)total_out + 8)))
354 GOTO_END_ERROR();
355
356 if (!png_write_iend(file))
357 GOTO_END_ERROR();
358
359 end:
360 if (file)
361 filestream_close(file);
362 free(encode_buf);
363 free(deflate_buf);
364 free(rgba_line);
365 free(prev_encoded);
366 free(up_filtered);
367 free(sub_filtered);
368 free(avg_filtered);
369 free(paeth_filtered);
370
371 if (stream_backend)
372 {
373 if (stream)
374 {
375 if (stream_backend->stream_free)
376 stream_backend->stream_free(stream);
377 }
378 }
379 return ret;
380 }
381
rpng_save_image_argb(const char * path,const uint32_t * data,unsigned width,unsigned height,unsigned pitch)382 bool rpng_save_image_argb(const char *path, const uint32_t *data,
383 unsigned width, unsigned height, unsigned pitch)
384 {
385 return rpng_save_image(path, (const uint8_t*)data,
386 width, height, pitch, sizeof(uint32_t));
387 }
388
rpng_save_image_bgr24(const char * path,const uint8_t * data,unsigned width,unsigned height,unsigned pitch)389 bool rpng_save_image_bgr24(const char *path, const uint8_t *data,
390 unsigned width, unsigned height, unsigned pitch)
391 {
392 return rpng_save_image(path, (const uint8_t*)data,
393 width, height, pitch, 3);
394 }
395