1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (rpng.c).
5 * ---------------------------------------------------------------------------------------
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:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
35 #include <formats/image.h>
36 #include <formats/rpng.h>
37 #include <streams/trans_stream.h>
38 #include <string/stdstring.h>
40 #include "rpng_internal.h"
42 enum png_ihdr_color_type
44 PNG_IHDR_COLOR_GRAY = 0,
45 PNG_IHDR_COLOR_RGB = 2,
46 PNG_IHDR_COLOR_PLT = 3,
47 PNG_IHDR_COLOR_GRAY_ALPHA = 4,
48 PNG_IHDR_COLOR_RGBA = 6
85 enum rpng_process_flags
87 RPNG_PROCESS_FLAG_INFLATE_INITIALIZED = (1 << 0),
88 RPNG_PROCESS_FLAG_ADAM7_PASS_INITIALIZED = (1 << 1),
89 RPNG_PROCESS_FLAG_PASS_INITIALIZED = (1 << 2)
97 const struct trans_stream_backend *stream_backend;
98 uint8_t *prev_scanline;
99 uint8_t *decoded_scanline;
100 uint8_t *inflate_buf;
101 size_t restore_buf_size;
102 size_t adam7_restore_buf_size;
103 size_t data_restore_buf_size;
104 size_t inflate_buf_size;
109 struct png_ihdr ihdr; /* uint32_t alignment */
114 unsigned pass_height;
121 RPNG_FLAG_HAS_IHDR = (1 << 0),
122 RPNG_FLAG_HAS_IDAT = (1 << 1),
123 RPNG_FLAG_HAS_IEND = (1 << 2),
124 RPNG_FLAG_HAS_PLTE = (1 << 3),
125 RPNG_FLAG_HAS_TRNS = (1 << 4)
130 struct rpng_process *process;
133 struct idat_buffer idat_buf; /* ptr alignment */
134 struct png_ihdr ihdr; /* uint32 alignment */
135 uint32_t palette[256];
139 static const struct adam7_pass rpng_passes[] = {
149 static INLINE uint32_t rpng_dword_be(const uint8_t *buf)
151 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | (buf[3] << 0);
154 #if defined(DEBUG) || defined(RPNG_TEST)
155 static bool rpng_process_ihdr(struct png_ihdr *ihdr)
157 uint8_t ihdr_depth = ihdr->depth;
159 switch (ihdr->color_type)
161 case PNG_IHDR_COLOR_RGB:
162 case PNG_IHDR_COLOR_GRAY_ALPHA:
163 case PNG_IHDR_COLOR_RGBA:
164 if (ihdr_depth != 8 && ihdr_depth != 16)
166 fprintf(stderr, "[RPNG]: Error in line %d.\n", __LINE__);
170 case PNG_IHDR_COLOR_GRAY:
171 /* Valid bitdepths are: 1, 2, 4, 8, 16 */
172 if (ihdr_depth > 16 || (0x977F7FFF << ihdr_depth) & 0x80000000)
174 fprintf(stderr, "[RPNG]: Error in line %d.\n", __LINE__);
178 case PNG_IHDR_COLOR_PLT:
179 /* Valid bitdepths are: 1, 2, 4, 8 */
180 if (ihdr_depth > 8 || (0x977F7FFF << ihdr_depth) & 0x80000000)
182 fprintf(stderr, "[RPNG]: Error in line %d.\n", __LINE__);
187 fprintf(stderr, "[RPNG]: Error in line %d.\n", __LINE__);
192 fprintf(stderr, "IHDR: (%u x %u), bpc = %u, palette = %s, color = %s, alpha = %s, adam7 = %s.\n",
193 ihdr->width, ihdr->height,
194 ihdr_depth, (ihdr->color_type == PNG_IHDR_COLOR_PLT) ? "yes" : "no",
195 (ihdr->color_type & PNG_IHDR_COLOR_RGB) ? "yes" : "no",
196 (ihdr->color_type & PNG_IHDR_COLOR_GRAY_ALPHA) ? "yes" : "no",
197 ihdr->interlace == 1 ? "yes" : "no");
203 static bool rpng_process_ihdr(struct png_ihdr *ihdr)
205 uint8_t ihdr_depth = ihdr->depth;
207 switch (ihdr->color_type)
209 case PNG_IHDR_COLOR_RGB:
210 case PNG_IHDR_COLOR_GRAY_ALPHA:
211 case PNG_IHDR_COLOR_RGBA:
212 if (ihdr_depth != 8 && ihdr_depth != 16)
215 case PNG_IHDR_COLOR_GRAY:
216 /* Valid bitdepths are: 1, 2, 4, 8, 16 */
217 if (ihdr_depth > 16 || (0x977F7FFF << ihdr_depth) & 0x80000000)
220 case PNG_IHDR_COLOR_PLT:
221 /* Valid bitdepths are: 1, 2, 4, 8 */
222 if (ihdr_depth > 8 || (0x977F7FFF << ihdr_depth) & 0x80000000)
233 static void rpng_reverse_filter_copy_line_rgb(uint32_t *data,
234 const uint8_t *decoded, unsigned width, unsigned bpp)
240 for (i = 0; i < (int)width; i++)
250 data[i] = (0xffu << 24) | (r << 16) | (g << 8) | (b << 0);
254 static void rpng_reverse_filter_copy_line_rgba(uint32_t *data,
255 const uint8_t *decoded, unsigned width, unsigned bpp)
261 for (i = 0; i < (int)width; i++)
272 data[i] = (a << 24) | (r << 16) | (g << 8) | (b << 0);
276 static void rpng_reverse_filter_copy_line_bw(uint32_t *data,
277 const uint8_t *decoded, unsigned width, unsigned depth)
281 static const unsigned mul_table[] = { 0, 0xff, 0x55, 0, 0x11, 0, 0, 0, 0x01 };
286 for (i = 0; i < (int)width; i++)
288 uint32_t val = decoded[i << 1];
289 data[i] = (val * 0x010101) | (0xffu << 24);
294 mul = mul_table[depth];
295 mask = (1 << depth) - 1;
298 for (i = 0; i < (int)width; i++, bit += depth)
300 unsigned byte = bit >> 3;
301 unsigned val = decoded[byte] >> (8 - depth - (bit & 7));
305 data[i] = (val * 0x010101) | (0xffu << 24);
309 static void rpng_reverse_filter_copy_line_gray_alpha(uint32_t *data,
310 const uint8_t *decoded, unsigned width,
317 for (i = 0; i < (int)width; i++)
319 uint32_t gray, alpha;
326 data[i] = (gray * 0x010101) | (alpha << 24);
330 static void rpng_reverse_filter_copy_line_plt(uint32_t *data,
331 const uint8_t *decoded, unsigned width,
332 unsigned depth, const uint32_t *palette)
339 unsigned w = width / 8;
340 for (i = 0; i < (int)w; i++, decoded++)
342 *data++ = palette[(*decoded >> 7) & 1];
343 *data++ = palette[(*decoded >> 6) & 1];
344 *data++ = palette[(*decoded >> 5) & 1];
345 *data++ = palette[(*decoded >> 4) & 1];
346 *data++ = palette[(*decoded >> 3) & 1];
347 *data++ = palette[(*decoded >> 2) & 1];
348 *data++ = palette[(*decoded >> 1) & 1];
349 *data++ = palette[*decoded & 1];
355 data[6] = palette[(*decoded >> 1) & 1];
357 data[5] = palette[(*decoded >> 2) & 1];
359 data[4] = palette[(*decoded >> 3) & 1];
361 data[3] = palette[(*decoded >> 4) & 1];
363 data[2] = palette[(*decoded >> 5) & 1];
365 data[1] = palette[(*decoded >> 6) & 1];
367 data[0] = palette[(*decoded >> 7) & 1];
376 unsigned w = width / 4;
377 for (i = 0; i < (int)w; i++, decoded++)
379 *data++ = palette[(*decoded >> 6) & 3];
380 *data++ = palette[(*decoded >> 4) & 3];
381 *data++ = palette[(*decoded >> 2) & 3];
382 *data++ = palette[*decoded & 3];
388 data[2] = palette[(*decoded >> 2) & 3];
390 data[1] = palette[(*decoded >> 4) & 3];
392 data[0] = palette[(*decoded >> 6) & 3];
401 unsigned w = width / 2;
402 for (i = 0; i < (int)w; i++, decoded++)
404 *data++ = palette[*decoded >> 4];
405 *data++ = palette[*decoded & 0x0f];
409 *data = palette[*decoded >> 4];
416 for (i = 0; i < (int)width; i++, decoded++, data++)
417 *data = palette[*decoded];
423 static void rpng_pass_geom(const struct png_ihdr *ihdr,
424 unsigned width, unsigned height,
425 unsigned *bpp_out, unsigned *pitch_out, size_t *pass_size)
430 switch (ihdr->color_type)
432 case PNG_IHDR_COLOR_GRAY:
433 bpp = (ihdr->depth + 7) / 8;
434 pitch = (ihdr->width * ihdr->depth + 7) / 8;
436 case PNG_IHDR_COLOR_RGB:
437 bpp = (ihdr->depth * 3 + 7) / 8;
438 pitch = (ihdr->width * ihdr->depth * 3 + 7) / 8;
440 case PNG_IHDR_COLOR_PLT:
441 bpp = (ihdr->depth + 7) / 8;
442 pitch = (ihdr->width * ihdr->depth + 7) / 8;
444 case PNG_IHDR_COLOR_GRAY_ALPHA:
445 bpp = (ihdr->depth * 2 + 7) / 8;
446 pitch = (ihdr->width * ihdr->depth * 2 + 7) / 8;
448 case PNG_IHDR_COLOR_RGBA:
449 bpp = (ihdr->depth * 4 + 7) / 8;
450 pitch = (ihdr->width * ihdr->depth * 4 + 7) / 8;
457 *pass_size = (pitch + 1) * ihdr->height;
464 static void rpng_reverse_filter_adam7_deinterlace_pass(uint32_t *data,
465 const struct png_ihdr *ihdr,
466 const uint32_t *input, unsigned pass_width, unsigned pass_height,
467 const struct adam7_pass *pass)
471 data += pass->y * ihdr->width + pass->x;
473 for (y = 0; y < pass_height;
474 y++, data += ihdr->width * pass->stride_y, input += pass_width)
476 uint32_t *out = data;
478 for (x = 0; x < pass_width; x++, out += pass->stride_x)
483 static void rpng_reverse_filter_deinit(struct rpng_process *pngp)
487 if (pngp->decoded_scanline)
488 free(pngp->decoded_scanline);
489 pngp->decoded_scanline = NULL;
490 if (pngp->prev_scanline)
491 free(pngp->prev_scanline);
492 pngp->prev_scanline = NULL;
494 pngp->flags &= ~RPNG_PROCESS_FLAG_PASS_INITIALIZED;
498 static int rpng_reverse_filter_init(const struct png_ihdr *ihdr,
499 struct rpng_process *pngp)
503 if ( !(pngp->flags & RPNG_PROCESS_FLAG_ADAM7_PASS_INITIALIZED)
506 if ( ihdr->width <= rpng_passes[pngp->pass_pos].x
507 || ihdr->height <= rpng_passes[pngp->pass_pos].y) /* Empty pass */
510 pngp->pass_width = (ihdr->width -
511 rpng_passes[pngp->pass_pos].x + rpng_passes[pngp->pass_pos].stride_x
512 - 1) / rpng_passes[pngp->pass_pos].stride_x;
513 pngp->pass_height = (ihdr->height - rpng_passes[pngp->pass_pos].y +
514 rpng_passes[pngp->pass_pos].stride_y - 1) / rpng_passes[pngp->pass_pos].stride_y;
516 if (!(pngp->data = (uint32_t*)malloc(
517 pngp->pass_width * pngp->pass_height * sizeof(uint32_t))))
521 pngp->ihdr.width = pngp->pass_width;
522 pngp->ihdr.height = pngp->pass_height;
524 rpng_pass_geom(&pngp->ihdr, pngp->pass_width,
525 pngp->pass_height, NULL, NULL, &pngp->pass_size);
527 if (pngp->pass_size > pngp->total_out)
534 pngp->flags |= RPNG_PROCESS_FLAG_ADAM7_PASS_INITIALIZED;
539 if (pngp->flags & RPNG_PROCESS_FLAG_PASS_INITIALIZED)
542 rpng_pass_geom(ihdr, ihdr->width, ihdr->height, &pngp->bpp, &pngp->pitch, &pass_size);
544 if (pngp->total_out < pass_size)
547 pngp->restore_buf_size = 0;
548 pngp->data_restore_buf_size = 0;
549 pngp->prev_scanline = (uint8_t*)calloc(1, pngp->pitch);
550 pngp->decoded_scanline = (uint8_t*)calloc(1, pngp->pitch);
552 if (!pngp->prev_scanline || !pngp->decoded_scanline)
556 pngp->flags |= RPNG_PROCESS_FLAG_PASS_INITIALIZED;
561 rpng_reverse_filter_deinit(pngp);
565 static int rpng_reverse_filter_copy_line(uint32_t *data,
566 const struct png_ihdr *ihdr,
567 struct rpng_process *pngp, unsigned filter)
573 case PNG_FILTER_NONE:
574 memcpy(pngp->decoded_scanline, pngp->inflate_buf, pngp->pitch);
577 memcpy(pngp->decoded_scanline, pngp->inflate_buf, pngp->pitch);
578 for (i = pngp->bpp; i < pngp->pitch; i++)
579 pngp->decoded_scanline[i] += pngp->decoded_scanline[i - pngp->bpp];
582 memcpy(pngp->decoded_scanline, pngp->inflate_buf, pngp->pitch);
583 for (i = 0; i < pngp->pitch; i++)
584 pngp->decoded_scanline[i] += pngp->prev_scanline[i];
586 case PNG_FILTER_AVERAGE:
587 memcpy(pngp->decoded_scanline, pngp->inflate_buf, pngp->pitch);
588 for (i = 0; i < pngp->bpp; i++)
590 uint8_t avg = pngp->prev_scanline[i] >> 1;
591 pngp->decoded_scanline[i] += avg;
593 for (i = pngp->bpp; i < pngp->pitch; i++)
595 uint8_t avg = (pngp->decoded_scanline[i - pngp->bpp] + pngp->prev_scanline[i]) >> 1;
596 pngp->decoded_scanline[i] += avg;
599 case PNG_FILTER_PAETH:
600 memcpy(pngp->decoded_scanline, pngp->inflate_buf, pngp->pitch);
601 for (i = 0; i < pngp->bpp; i++)
602 pngp->decoded_scanline[i] += pngp->prev_scanline[i];
603 for (i = pngp->bpp; i < pngp->pitch; i++)
604 pngp->decoded_scanline[i] += paeth(pngp->decoded_scanline[i - pngp->bpp],
605 pngp->prev_scanline[i], pngp->prev_scanline[i - pngp->bpp]);
608 return IMAGE_PROCESS_ERROR_END;
611 switch (ihdr->color_type)
613 case PNG_IHDR_COLOR_GRAY:
614 rpng_reverse_filter_copy_line_bw(data, pngp->decoded_scanline, ihdr->width, ihdr->depth);
616 case PNG_IHDR_COLOR_RGB:
617 rpng_reverse_filter_copy_line_rgb(data, pngp->decoded_scanline, ihdr->width, ihdr->depth);
619 case PNG_IHDR_COLOR_PLT:
620 rpng_reverse_filter_copy_line_plt(
621 data, pngp->decoded_scanline, ihdr->width,
622 ihdr->depth, pngp->palette);
624 case PNG_IHDR_COLOR_GRAY_ALPHA:
625 rpng_reverse_filter_copy_line_gray_alpha(data, pngp->decoded_scanline, ihdr->width,
628 case PNG_IHDR_COLOR_RGBA:
629 rpng_reverse_filter_copy_line_rgba(data, pngp->decoded_scanline, ihdr->width, ihdr->depth);
633 memcpy(pngp->prev_scanline, pngp->decoded_scanline, pngp->pitch);
635 return IMAGE_PROCESS_NEXT;
638 static int rpng_reverse_filter_regular_iterate(
639 uint32_t **data, const struct png_ihdr *ihdr,
640 struct rpng_process *pngp)
642 int ret = IMAGE_PROCESS_END;
643 if (pngp->h < ihdr->height)
645 unsigned filter = *pngp->inflate_buf++;
646 pngp->restore_buf_size += 1;
647 ret = rpng_reverse_filter_copy_line(*data,
649 if (ret == IMAGE_PROCESS_END || ret == IMAGE_PROCESS_ERROR_END)
656 pngp->inflate_buf += pngp->pitch;
657 pngp->restore_buf_size += pngp->pitch;
659 *data += ihdr->width;
660 pngp->data_restore_buf_size += ihdr->width;
662 return IMAGE_PROCESS_NEXT;
665 rpng_reverse_filter_deinit(pngp);
667 pngp->inflate_buf -= pngp->restore_buf_size;
668 *data -= pngp->data_restore_buf_size;
669 pngp->data_restore_buf_size = 0;
673 static int rpng_reverse_filter_adam7_iterate(uint32_t **data_,
674 const struct png_ihdr *ihdr,
675 struct rpng_process *pngp)
678 bool to_next = pngp->pass_pos < ARRAY_SIZE(rpng_passes);
679 uint32_t *data = *data_;
682 return IMAGE_PROCESS_END;
684 if ((ret = rpng_reverse_filter_init(ihdr, pngp)) == 1)
685 return IMAGE_PROCESS_NEXT;
687 return IMAGE_PROCESS_ERROR_END;
689 if (rpng_reverse_filter_init(&pngp->ihdr, pngp) == -1)
690 return IMAGE_PROCESS_ERROR;
694 ret = rpng_reverse_filter_regular_iterate(&pngp->data,
696 } while (ret == IMAGE_PROCESS_NEXT);
698 if (ret == IMAGE_PROCESS_ERROR || ret == IMAGE_PROCESS_ERROR_END)
699 return IMAGE_PROCESS_ERROR;
701 pngp->inflate_buf += pngp->pass_size;
702 pngp->adam7_restore_buf_size += pngp->pass_size;
704 pngp->total_out -= pngp->pass_size;
706 rpng_reverse_filter_adam7_deinterlace_pass(data,
707 ihdr, pngp->data, pngp->pass_width, pngp->pass_height,
708 &rpng_passes[pngp->pass_pos]);
713 pngp->pass_width = 0;
714 pngp->pass_height = 0;
716 pngp->flags &= ~RPNG_PROCESS_FLAG_ADAM7_PASS_INITIALIZED;
718 return IMAGE_PROCESS_NEXT;
721 static int rpng_reverse_filter_adam7(uint32_t **data_,
722 const struct png_ihdr *ihdr,
723 struct rpng_process *pngp)
725 int ret = rpng_reverse_filter_adam7_iterate(data_,
730 case IMAGE_PROCESS_ERROR_END:
731 case IMAGE_PROCESS_END:
733 case IMAGE_PROCESS_NEXT:
736 case IMAGE_PROCESS_ERROR:
742 pngp->inflate_buf -= pngp->adam7_restore_buf_size;
743 pngp->adam7_restore_buf_size = 0;
747 pngp->inflate_buf -= pngp->adam7_restore_buf_size;
748 pngp->adam7_restore_buf_size = 0;
752 static int rpng_load_image_argb_process_inflate_init(
753 rpng_t *rpng, uint32_t **data)
756 enum trans_stream_error terror;
758 struct rpng_process *process = (struct rpng_process*)rpng->process;
759 bool to_continue = (process->avail_in > 0
760 && process->avail_out > 0);
765 zstatus = process->stream_backend->trans(process->stream, false, &rd, &wn, &terror);
767 if (!zstatus && terror != TRANS_STREAM_ERROR_BUFFER_FULL)
770 process->avail_in -= rd;
771 process->avail_out -= wn;
772 process->total_out += wn;
778 process->stream_backend->stream_free(process->stream);
779 process->stream = NULL;
782 /* we often use these in textures, make sure they're 32-byte aligned */
783 *data = (uint32_t*)memalign(32, rpng->ihdr.width *
784 rpng->ihdr.height * sizeof(uint32_t));
786 *data = (uint32_t*)malloc(rpng->ihdr.width *
787 rpng->ihdr.height * sizeof(uint32_t));
792 process->adam7_restore_buf_size = 0;
793 process->restore_buf_size = 0;
794 process->palette = rpng->palette;
796 if (rpng->ihdr.interlace != 1)
797 if (rpng_reverse_filter_init(&rpng->ihdr, process) == -1)
800 process->flags |= RPNG_PROCESS_FLAG_INFLATE_INITIALIZED;
805 process->flags &= ~RPNG_PROCESS_FLAG_INFLATE_INITIALIZED;
809 static bool rpng_realloc_idat(struct idat_buffer *buf, uint32_t chunk_size)
811 uint8_t *new_buffer = (uint8_t*)realloc(buf->data, buf->size + chunk_size);
816 buf->data = new_buffer;
820 static struct rpng_process *rpng_process_init(rpng_t *rpng)
822 uint8_t *inflate_buf = NULL;
823 struct rpng_process *process = (struct rpng_process*)malloc(sizeof(*process));
829 process->prev_scanline = NULL;
830 process->decoded_scanline = NULL;
831 process->inflate_buf = NULL;
833 process->ihdr.width = 0;
834 process->ihdr.height = 0;
835 process->ihdr.depth = 0;
836 process->ihdr.color_type = 0;
837 process->ihdr.compression = 0;
838 process->ihdr.filter = 0;
839 process->ihdr.interlace = 0;
841 process->restore_buf_size = 0;
842 process->adam7_restore_buf_size = 0;
843 process->data_restore_buf_size = 0;
844 process->inflate_buf_size = 0;
845 process->avail_in = 0;
846 process->avail_out = 0;
847 process->total_out = 0;
848 process->pass_size = 0;
852 process->pass_width = 0;
853 process->pass_height = 0;
854 process->pass_pos = 0;
856 process->palette = 0;
857 process->stream = NULL;
858 process->stream_backend = trans_stream_get_zlib_inflate_backend();
860 rpng_pass_geom(&rpng->ihdr, rpng->ihdr.width,
861 rpng->ihdr.height, NULL, NULL, &process->inflate_buf_size);
862 if (rpng->ihdr.interlace == 1) /* To be sure. */
863 process->inflate_buf_size *= 2;
865 process->stream = process->stream_backend->stream_new();
867 if (!process->stream)
873 inflate_buf = (uint8_t*)malloc(process->inflate_buf_size);
877 process->inflate_buf = inflate_buf;
878 process->avail_in = rpng->idat_buf.size;
879 process->avail_out = process->inflate_buf_size;
881 process->stream_backend->set_in(
884 (uint32_t)rpng->idat_buf.size);
885 process->stream_backend->set_out(
887 process->inflate_buf,
888 (uint32_t)process->inflate_buf_size);
896 process->stream_backend->stream_free(process->stream);
903 * rpng_read_chunk_header:
907 * @return The PNG type of the memory chunk (i.e. IHDR, IDAT, IEND,
910 static enum png_chunk_type rpng_read_chunk_header(
911 uint8_t *buf, uint32_t chunk_size)
916 for (i = 0; i < 4; i++)
918 uint8_t byte = buf[i + 4];
920 /* All four bytes of the chunk type must be
921 * ASCII letters (codes 65-90 and 97-122) */
922 if ((byte < 65) || ((byte > 90) && (byte < 97)) || (byte > 122))
923 return PNG_CHUNK_ERROR;
933 return PNG_CHUNK_IHDR;
941 return PNG_CHUNK_IDAT;
949 return PNG_CHUNK_IEND;
957 return PNG_CHUNK_PLTE;
965 return PNG_CHUNK_tRNS;
967 return PNG_CHUNK_NOOP;
970 bool rpng_iterate_image(rpng_t *rpng)
973 uint8_t *buf = (uint8_t*)rpng->buff_data;
974 uint32_t chunk_size = 0;
976 /* Check whether data buffer pointer is valid */
977 if (buf > rpng->buff_end)
980 /* Check whether reading the header will overflow
982 if (rpng->buff_end - buf < 8)
985 chunk_size = rpng_dword_be(buf);
987 /* Check whether chunk will overflow the data buffer */
988 if (buf + 8 + chunk_size > rpng->buff_end)
991 switch (rpng_read_chunk_header(buf, chunk_size))
997 case PNG_CHUNK_ERROR:
1000 case PNG_CHUNK_IHDR:
1001 if ( (rpng->flags & RPNG_FLAG_HAS_IHDR)
1002 || (rpng->flags & RPNG_FLAG_HAS_IDAT)
1003 || (rpng->flags & RPNG_FLAG_HAS_IEND))
1006 if (chunk_size != 13)
1011 rpng->ihdr.width = rpng_dword_be(buf + 0);
1012 rpng->ihdr.height = rpng_dword_be(buf + 4);
1013 rpng->ihdr.depth = buf[8];
1014 rpng->ihdr.color_type = buf[9];
1015 rpng->ihdr.compression = buf[10];
1016 rpng->ihdr.filter = buf[11];
1017 rpng->ihdr.interlace = buf[12];
1019 if ( rpng->ihdr.width == 0
1020 || rpng->ihdr.height == 0
1021 /* ensure multiplications don't overflow and wrap around, that'd give buffer overflow crashes */
1022 || (uint64_t)rpng->ihdr.width*rpng->ihdr.height*sizeof(uint32_t) >= 0x80000000)
1025 if (!rpng_process_ihdr(&rpng->ihdr))
1028 if (rpng->ihdr.compression != 0)
1030 #if defined(DEBUG) || defined(RPNG_TEST)
1031 fprintf(stderr, "[RPNG]: Error in line %d.\n", __LINE__);
1036 rpng->flags |= RPNG_FLAG_HAS_IHDR;
1039 case PNG_CHUNK_PLTE:
1042 unsigned entries = chunk_size / 3;
1049 if ( !(rpng->flags & RPNG_FLAG_HAS_IHDR)
1050 || (rpng->flags & RPNG_FLAG_HAS_PLTE)
1051 || (rpng->flags & RPNG_FLAG_HAS_IEND)
1052 || (rpng->flags & RPNG_FLAG_HAS_IDAT)
1053 || (rpng->flags & RPNG_FLAG_HAS_TRNS))
1058 for (i = 0; i < (int)entries; i++)
1060 uint32_t r = buf[3 * i + 0];
1061 uint32_t g = buf[3 * i + 1];
1062 uint32_t b = buf[3 * i + 2];
1063 rpng->palette[i] = (r << 16) | (g << 8) | (b << 0) | (0xffu << 24);
1066 rpng->flags |= RPNG_FLAG_HAS_PLTE;
1070 case PNG_CHUNK_tRNS:
1071 if (rpng->flags & RPNG_FLAG_HAS_IDAT)
1074 if (rpng->ihdr.color_type == PNG_IHDR_COLOR_PLT)
1078 /* we should compare with the number of palette entries */
1079 if (chunk_size > 256)
1083 palette = rpng->palette;
1085 for (i = 0; i < (int)chunk_size; i++, buf++, palette++)
1086 *palette = (*palette & 0x00ffffff) | (unsigned)*buf << 24;
1088 /* TODO: support colorkey in grayscale and truecolor images */
1090 rpng->flags |= RPNG_FLAG_HAS_TRNS;
1093 case PNG_CHUNK_IDAT:
1094 if ( !(rpng->flags & RPNG_FLAG_HAS_IHDR)
1095 || (rpng->flags & RPNG_FLAG_HAS_IEND)
1096 || (rpng->ihdr.color_type == PNG_IHDR_COLOR_PLT
1098 !(rpng->flags & RPNG_FLAG_HAS_PLTE)))
1101 if (!rpng_realloc_idat(&rpng->idat_buf, chunk_size))
1106 for (i = 0; i < chunk_size; i++)
1107 rpng->idat_buf.data[i + rpng->idat_buf.size] = buf[i];
1109 rpng->idat_buf.size += chunk_size;
1111 rpng->flags |= RPNG_FLAG_HAS_IDAT;
1114 case PNG_CHUNK_IEND:
1115 if ( !(rpng->flags & RPNG_FLAG_HAS_IHDR)
1116 || !(rpng->flags & RPNG_FLAG_HAS_IDAT))
1119 rpng->flags |= RPNG_FLAG_HAS_IEND;
1123 rpng->buff_data += chunk_size + 12;
1125 /* Check whether data buffer pointer is valid */
1126 if (rpng->buff_data > rpng->buff_end)
1131 int rpng_process_image(rpng_t *rpng,
1132 void **_data, size_t size, unsigned *width, unsigned *height)
1134 uint32_t **data = (uint32_t**)_data;
1138 struct rpng_process *process = rpng_process_init(rpng);
1143 rpng->process = process;
1144 return IMAGE_PROCESS_NEXT;
1147 if (!(rpng->process->flags & RPNG_PROCESS_FLAG_INFLATE_INITIALIZED))
1149 if (rpng_load_image_argb_process_inflate_init(rpng, data) == -1)
1151 return IMAGE_PROCESS_NEXT;
1154 *width = rpng->ihdr.width;
1155 *height = rpng->ihdr.height;
1157 if (rpng->ihdr.interlace && rpng->process)
1158 return rpng_reverse_filter_adam7(data, &rpng->ihdr, rpng->process);
1159 return rpng_reverse_filter_regular_iterate(data, &rpng->ihdr, rpng->process);
1164 if (rpng->process->inflate_buf)
1165 free(rpng->process->inflate_buf);
1166 if (rpng->process->stream)
1167 rpng->process->stream_backend->stream_free(rpng->process->stream);
1168 free(rpng->process);
1169 rpng->process = NULL;
1171 return IMAGE_PROCESS_ERROR;
1174 void rpng_free(rpng_t *rpng)
1179 if (rpng->idat_buf.data)
1180 free(rpng->idat_buf.data);
1183 if (rpng->process->inflate_buf)
1184 free(rpng->process->inflate_buf);
1185 if (rpng->process->stream)
1187 if (rpng->process->stream_backend && rpng->process->stream_backend->stream_free)
1188 rpng->process->stream_backend->stream_free(rpng->process->stream);
1190 free(rpng->process->stream);
1192 free(rpng->process);
1198 bool rpng_start(rpng_t *rpng)
1203 /* Check whether reading the header will overflow
1204 * the data buffer */
1205 if (rpng->buff_end - rpng->buff_data < 8)
1208 if (string_is_not_equal_fast(
1209 rpng->buff_data, png_magic, sizeof(png_magic)))
1212 rpng->buff_data += 8;
1220 * Check if @rpng is a valid PNG image.
1221 * Must contain an IHDR chunk, one or more IDAT
1222 * chunks, and an IEND chunk.
1226 * @return true if it's a valid PNG image, otherwise false.
1228 bool rpng_is_valid(rpng_t *rpng)
1230 return (rpng && ((rpng->flags & (RPNG_FLAG_HAS_IHDR | RPNG_FLAG_HAS_IDAT |
1231 RPNG_FLAG_HAS_IEND)) > 0));
1234 bool rpng_set_buf_ptr(rpng_t *rpng, void *data, size_t len)
1236 if (!rpng || (len < 1))
1239 rpng->buff_data = (uint8_t*)data;
1240 rpng->buff_end = rpng->buff_data + (len - 1);
1245 rpng_t *rpng_alloc(void)
1247 rpng_t *rpng = (rpng_t*)calloc(1, sizeof(*rpng));