1 /* Copyright  (C) 2010-2016 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 <file/archive_file.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       cnt += abs((int8_t)data[i]);
154    return cnt;
155 }
156 
filter_up(uint8_t * target,const uint8_t * line,const uint8_t * prev,unsigned width,unsigned bpp)157 static unsigned filter_up(uint8_t *target, const uint8_t *line,
158       const uint8_t *prev, unsigned width, unsigned bpp)
159 {
160    unsigned i;
161    width *= bpp;
162    for (i = 0; i < width; i++)
163       target[i] = line[i] - prev[i];
164 
165    return count_sad(target, width);
166 }
167 
filter_sub(uint8_t * target,const uint8_t * line,unsigned width,unsigned bpp)168 static unsigned filter_sub(uint8_t *target, const uint8_t *line,
169       unsigned width, unsigned bpp)
170 {
171    unsigned i;
172    width *= bpp;
173    for (i = 0; i < bpp; i++)
174       target[i] = line[i];
175    for (i = bpp; i < width; i++)
176       target[i] = line[i] - line[i - bpp];
177 
178    return count_sad(target, width);
179 }
180 
filter_avg(uint8_t * target,const uint8_t * line,const uint8_t * prev,unsigned width,unsigned bpp)181 static unsigned filter_avg(uint8_t *target, const uint8_t *line,
182       const uint8_t *prev, unsigned width, unsigned bpp)
183 {
184    unsigned i;
185    width *= bpp;
186    for (i = 0; i < bpp; i++)
187       target[i] = line[i] - (prev[i] >> 1);
188    for (i = bpp; i < width; i++)
189       target[i] = line[i] - ((line[i - bpp] + prev[i]) >> 1);
190 
191    return count_sad(target, width);
192 }
193 
filter_paeth(uint8_t * target,const uint8_t * line,const uint8_t * prev,unsigned width,unsigned bpp)194 static unsigned filter_paeth(uint8_t *target,
195       const uint8_t *line, const uint8_t *prev,
196       unsigned width, unsigned bpp)
197 {
198    unsigned i;
199    width *= bpp;
200    for (i = 0; i < bpp; i++)
201       target[i] = line[i] - paeth(0, prev[i], 0);
202    for (i = bpp; i < width; i++)
203       target[i] = line[i] - paeth(line[i - bpp], prev[i], prev[i - bpp]);
204 
205    return count_sad(target, width);
206 }
207 
rpng_save_image(const char * path,const uint8_t * data,unsigned width,unsigned height,unsigned pitch,unsigned bpp)208 static bool rpng_save_image(const char *path,
209       const uint8_t *data,
210       unsigned width, unsigned height, unsigned pitch, unsigned bpp)
211 {
212    unsigned h;
213    bool ret = true;
214    struct png_ihdr ihdr = {0};
215 
216    const struct file_archive_file_backend *stream_backend = NULL;
217    size_t encode_buf_size  = 0;
218    uint8_t *encode_buf     = NULL;
219    uint8_t *deflate_buf    = NULL;
220    uint8_t *rgba_line      = NULL;
221    uint8_t *up_filtered    = NULL;
222    uint8_t *sub_filtered   = NULL;
223    uint8_t *avg_filtered   = NULL;
224    uint8_t *paeth_filtered = NULL;
225    uint8_t *prev_encoded   = NULL;
226    uint8_t *encode_target  = NULL;
227    void *stream            = NULL;
228    RFILE *file             = filestream_open(path, RFILE_MODE_WRITE, -1);
229    if (!file)
230       GOTO_END_ERROR();
231 
232    stream_backend = file_archive_get_zlib_file_backend();
233 
234    if (filestream_write(file, png_magic, sizeof(png_magic)) != sizeof(png_magic))
235       GOTO_END_ERROR();
236 
237    ihdr.width = width;
238    ihdr.height = height;
239    ihdr.depth = 8;
240    ihdr.color_type = bpp == sizeof(uint32_t) ? 6 : 2; /* RGBA or RGB */
241    if (!png_write_ihdr(file, &ihdr))
242       GOTO_END_ERROR();
243 
244    encode_buf_size = (width * bpp + 1) * height;
245    encode_buf = (uint8_t*)malloc(encode_buf_size);
246    if (!encode_buf)
247       GOTO_END_ERROR();
248 
249    prev_encoded = (uint8_t*)calloc(1, width * bpp);
250    if (!prev_encoded)
251       GOTO_END_ERROR();
252 
253    rgba_line      = (uint8_t*)malloc(width * bpp);
254    up_filtered    = (uint8_t*)malloc(width * bpp);
255    sub_filtered   = (uint8_t*)malloc(width * bpp);
256    avg_filtered   = (uint8_t*)malloc(width * bpp);
257    paeth_filtered = (uint8_t*)malloc(width * bpp);
258    if (!rgba_line || !up_filtered || !sub_filtered || !avg_filtered || !paeth_filtered)
259       GOTO_END_ERROR();
260 
261    encode_target = encode_buf;
262    for (h = 0; h < height;
263          h++, encode_target += width * bpp, data += pitch)
264    {
265       if (bpp == sizeof(uint32_t))
266          copy_argb_line(rgba_line, (const uint32_t*)data, width);
267       else
268          copy_bgr24_line(rgba_line, data, width);
269 
270       /* Try every filtering method, and choose the method
271        * which has most entries as zero.
272        *
273        * This is probably not very optimal, but it's very
274        * simple to implement.
275        */
276       {
277          unsigned none_score  = count_sad(rgba_line, width * bpp);
278          unsigned up_score    = filter_up(up_filtered, rgba_line, prev_encoded, width, bpp);
279          unsigned sub_score   = filter_sub(sub_filtered, rgba_line, width, bpp);
280          unsigned avg_score   = filter_avg(avg_filtered, rgba_line, prev_encoded, width, bpp);
281          unsigned paeth_score = filter_paeth(paeth_filtered, rgba_line, prev_encoded, width, bpp);
282 
283          uint8_t filter       = 0;
284          unsigned min_sad     = none_score;
285          const uint8_t *chosen_filtered = rgba_line;
286 
287          if (sub_score < min_sad)
288          {
289             filter = 1;
290             chosen_filtered = sub_filtered;
291             min_sad = sub_score;
292          }
293 
294          if (up_score < min_sad)
295          {
296             filter = 2;
297             chosen_filtered = up_filtered;
298             min_sad = up_score;
299          }
300 
301          if (avg_score < min_sad)
302          {
303             filter = 3;
304             chosen_filtered = avg_filtered;
305             min_sad = avg_score;
306          }
307 
308          if (paeth_score < min_sad)
309          {
310             filter = 4;
311             chosen_filtered = paeth_filtered;
312             min_sad = paeth_score;
313          }
314 
315          *encode_target++ = filter;
316          memcpy(encode_target, chosen_filtered, width * bpp);
317 
318          memcpy(prev_encoded, rgba_line, width * bpp);
319       }
320    }
321 
322    deflate_buf = (uint8_t*)malloc(encode_buf_size * 2); /* Just to be sure. */
323    if (!deflate_buf)
324       GOTO_END_ERROR();
325 
326    stream = stream_backend->stream_new();
327 
328    if (!stream)
329       GOTO_END_ERROR();
330 
331    stream_backend->stream_set(
332          stream,
333          encode_buf_size,
334          encode_buf_size * 2,
335          encode_buf,
336          deflate_buf + 8);
337 
338    stream_backend->stream_compress_init(stream, 9);
339 
340    if (stream_backend->stream_compress_data_to_file(stream) != 1)
341    {
342       stream_backend->stream_compress_free(stream);
343       GOTO_END_ERROR();
344    }
345 
346    stream_backend->stream_compress_free(stream);
347 
348    memcpy(deflate_buf + 4, "IDAT", 4);
349    dword_write_be(deflate_buf + 0,        ((uint32_t)stream_backend->stream_get_total_out(stream)));
350    if (!png_write_idat(file, deflate_buf, ((size_t)stream_backend->stream_get_total_out(stream) + 8)))
351       GOTO_END_ERROR();
352 
353    if (!png_write_iend(file))
354       GOTO_END_ERROR();
355 
356 end:
357    filestream_close(file);
358    free(encode_buf);
359    free(deflate_buf);
360    free(rgba_line);
361    free(prev_encoded);
362    free(up_filtered);
363    free(sub_filtered);
364    free(avg_filtered);
365    free(paeth_filtered);
366 
367    if (stream_backend)
368    {
369       if (stream)
370       {
371          if (stream_backend->stream_free)
372             stream_backend->stream_free(stream);
373       }
374    }
375    return ret;
376 }
377 
rpng_save_image_argb(const char * path,const uint32_t * data,unsigned width,unsigned height,unsigned pitch)378 bool rpng_save_image_argb(const char *path, const uint32_t *data,
379       unsigned width, unsigned height, unsigned pitch)
380 {
381    return rpng_save_image(path, (const uint8_t*)data,
382          width, height, pitch, sizeof(uint32_t));
383 }
384 
rpng_save_image_bgr24(const char * path,const uint8_t * data,unsigned width,unsigned height,unsigned pitch)385 bool rpng_save_image_bgr24(const char *path, const uint8_t *data,
386       unsigned width, unsigned height, unsigned pitch)
387 {
388    return rpng_save_image(path, (const uint8_t*)data,
389          width, height, pitch, 3);
390 }
391