37 #if defined(_MSC_VER) && _MSC_VER >= 1300
38 #define swap16(data) _byteswap_ushort(data)
39 #define swap32(data) _byteswap_ulong(data)
42 #define swap16(data) bswap_16(data)
43 #define swap32(data) bswap_32(data)
45 #define swap16(data) (((data >> 8) & 255) | ((data & 255) << 8))
46 #define swap32(data) ((swap16(data) << 16) | swap16(data >> 16))
49 #define PNG_ZBUF_SIZE 32768
51 #define PNG_DISPOSE_OP_NONE 0x00
52 #define PNG_DISPOSE_OP_BACKGROUND 0x01
53 #define PNG_DISPOSE_OP_PREVIOUS 0x02
55 #define PNG_BLEND_OP_SOURCE 0x00
56 #define PNG_BLEND_OP_OVER 0x01
58 #define notabc(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
60 #define ROWBYTES(pixel_bits, width) \
61 ((pixel_bits) >= 8 ? \
62 ((width) * (((unsigned int)(pixel_bits)) >> 3)) : \
63 (( ((width) * ((unsigned int)(pixel_bits))) + 7) >> 3) )
65 unsigned char png_sign[8] = {137, 80, 78, 71, 13, 10, 26, 10};
73 int mask1[8]={128,64,32,16,8,4,2,1};
77 unsigned char pal[256][3];
83 unsigned int read32(std::istream& f1)
85 unsigned char a, b, c, d;
86 f1.read((
char*)&a, 1);
87 f1.read((
char*)&b, 1);
88 f1.read((
char*)&c, 1);
89 f1.read((
char*)&d, 1);
90 return ((
unsigned int)a<<24)+((
unsigned int)b<<16)+((
unsigned int)c<<8)+(
unsigned int)d;
93 unsigned short read16(std::istream& f1)
96 f1.read((
char*)&a, 1);
97 f1.read((
char*)&b, 1);
98 return ((
unsigned short)a<<8)+(
unsigned short)b;
103 return ((
unsigned short)(*p)<<8)+(
unsigned short)(*(p+1));
106 void read_sub_row(
unsigned char * row,
unsigned int rowbytes,
unsigned int bpp)
110 for (i=bpp; i<rowbytes; i++)
111 row[i] += row[i-bpp];
114 void read_up_row(
unsigned char * row,
unsigned char * prev_row,
unsigned int rowbytes,
unsigned int bpp)
119 for (i=0; i<rowbytes; i++)
120 row[i] += prev_row[i];
123 void read_average_row(
unsigned char * row,
unsigned char * prev_row,
unsigned int rowbytes,
unsigned int bpp)
129 for (i=0; i<bpp; i++)
130 row[i] += prev_row[i]>>1;
131 for (i=bpp; i<rowbytes; i++)
132 row[i] += (prev_row[i] + row[i-bpp])>>1;
136 for (i=bpp; i<rowbytes; i++)
137 row[i] += row[i-bpp]>>1;
141 void read_paeth_row(
unsigned char * row,
unsigned char * prev_row,
unsigned int rowbytes,
unsigned int bpp)
144 int a, b, c, pa, pb, pc, p;
148 for (i=0; i<bpp; i++)
149 row[i] += prev_row[i];
150 for (i=bpp; i<rowbytes; i++)
160 row[i] += ((pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c);
165 for (i=bpp; i<rowbytes; i++)
166 row[i] += row[i-bpp];
170 void unpack(z_stream& zstream,
unsigned char * dst,
unsigned int dst_size,
unsigned char * src,
unsigned int src_size,
unsigned int h,
unsigned int rowbytes,
unsigned char bpp)
173 unsigned char * row = dst;
174 unsigned char * prev_row =
nullptr;
176 zstream.next_out = dst;
177 zstream.avail_out = dst_size;
178 zstream.next_in = src;
179 zstream.avail_in = src_size;
180 inflate(&zstream, Z_FINISH);
181 inflateReset(&zstream);
189 case 2:
read_up_row(row, prev_row, rowbytes, bpp);
break;
198 void compose0(
unsigned char * dst1,
unsigned int dstbytes1,
unsigned char * dst2,
unsigned int dstbytes2,
unsigned char * src,
unsigned int srcbytes,
unsigned int w,
unsigned int h,
unsigned int bop,
unsigned char depth)
200 unsigned int i, j, g, a;
209 dp2 = (
unsigned int*)dst2;
215 case 16:
for (i=0; i<w; i++) { a = 0xFF;
if (
hasTRNS &&
readshort(sp)==
trns1) a = 0; *dp1++ = *sp; *dp2++ = (a << 24) + (*sp << 16) + (*sp << 8) + *sp; sp+=2; }
break;
216 case 8:
for (i=0; i<w; i++) { a = 0xFF;
if (
hasTRNS && *sp==
trns1) a = 0; *dp1++ = *sp; *dp2++ = (a << 24) + (*sp << 16) + (*sp << 8) + *sp; sp++; }
break;
217 case 4:
for (i=0; i<w; i++) { g = (sp[i>>1] &
mask4[i&1]) >>
shift4[i&1]; a = 0xFF;
if (
hasTRNS && g==
trns1) a = 0; *dp1++ = g*0x11; *dp2++ = (a<<24) + g*0x111111; }
break;
218 case 2:
for (i=0; i<w; i++) { g = (sp[i>>2] &
mask2[i&3]) >>
shift2[i&3]; a = 0xFF;
if (
hasTRNS && g==
trns1) a = 0; *dp1++ = g*0x55; *dp2++ = (a<<24) + g*0x555555; }
break;
219 case 1:
for (i=0; i<w; i++) { g = (sp[i>>3] &
mask1[i&7]) >>
shift1[i&7]; a = 0xFF;
if (
hasTRNS && g==
trns1) a = 0; *dp1++ = g*0xFF; *dp2++ = (a<<24) + g*0xFFFFFF; }
break;
226 case 16:
for (i=0; i<w; i++, dp1++, dp2++) {
if (
readshort(sp) !=
trns1) { *dp1 = *sp; *dp2 = 0xFF000000 + (*sp << 16) + (*sp << 8) + *sp; } sp+=2; }
break;
227 case 8:
for (i=0; i<w; i++, dp1++, dp2++) {
if (*sp !=
trns1) { *dp1 = *sp; *dp2 = 0xFF000000 + (*sp << 16) + (*sp << 8) + *sp; } sp++; }
break;
228 case 4:
for (i=0; i<w; i++, dp1++, dp2++) { g = (sp[i>>1] &
mask4[i&1]) >>
shift4[i&1];
if (g !=
trns1) { *dp1 = g*0x11; *dp2 = 0xFF000000+g*0x111111; } }
break;
229 case 2:
for (i=0; i<w; i++, dp1++, dp2++) { g = (sp[i>>2] &
mask2[i&3]) >>
shift2[i&3];
if (g !=
trns1) { *dp1 = g*0x55; *dp2 = 0xFF000000+g*0x555555; } }
break;
230 case 1:
for (i=0; i<w; i++, dp1++, dp2++) { g = (sp[i>>3] &
mask1[i&7]) >>
shift1[i&7];
if (g !=
trns1) { *dp1 = g*0xFF; *dp2 = 0xFF000000+g*0xFFFFFF; } }
break;
240 void compose2(
unsigned char * dst1,
unsigned int dstbytes1,
unsigned char * dst2,
unsigned int dstbytes2,
unsigned char * src,
unsigned int srcbytes,
unsigned int w,
unsigned int h,
unsigned int bop,
unsigned char depth)
243 unsigned int r, g, b, a;
252 dp2 = (
unsigned int*)dst2;
266 *dp1++ = b; *dp1++ = g; *dp1++ = r;
267 *dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
272 for (i=0; i<w; i++, sp+=6)
280 *dp1++ = b; *dp1++ = g; *dp1++ = r;
281 *dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
289 for (i=0; i<w; i++, sp+=3, dp1+=3, dp2++)
292 *dp1 = *sp; *(dp1+1) = *(sp+1); *(dp1+2) = *(sp+2);
293 *dp2 = 0xFF000000 + (*(sp+2) << 16) + (*(sp+1) << 8) + *sp;
298 for (i=0; i<w; i++, sp+=6, dp1+=3, dp2++)
301 *dp1 = *sp; *(dp1+1) = *(sp+2); *(dp1+2) = *(sp+4);
302 *dp2 = 0xFF000000 + (*(sp+4) << 16) + (*(sp+2) << 8) + *sp;
312 void compose3(
unsigned char * dst1,
unsigned int dstbytes1,
unsigned char * dst2,
unsigned int dstbytes2,
unsigned char * src,
unsigned int srcbytes,
unsigned int w,
unsigned int h,
unsigned int bop,
unsigned char depth)
315 unsigned int r, g, b, a;
316 unsigned int r2, g2, b2, a2;
318 unsigned char col = 0;
327 dp2 = (
unsigned int*)dst2;
333 case 8: col = sp[i];
break;
334 case 4: col = (sp[i>>1] &
mask4[i&1]) >>
shift4[i&1];
break;
335 case 2: col = (sp[i>>2] &
mask2[i&3]) >>
shift2[i&3];
break;
336 case 1: col = (sp[i>>3] &
mask1[i&7]) >>
shift1[i&7];
break;
347 *dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
354 *dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
359 if ((a2 = (*dp2)>>24))
364 al = 255*255-(255-a)*(255-a2);
366 g2 = (((*dp2)>>8)&255);
367 r2 = (((*dp2)>>16)&255);
374 *dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
389 void compose4(
unsigned char * dst,
unsigned int dstbytes,
unsigned char * src,
unsigned int srcbytes,
unsigned int w,
unsigned int h,
unsigned int bop,
unsigned char depth)
391 unsigned int i, j, step;
392 unsigned int g, a, g2, a2;
432 al = 255*255-(255-a)*(255-a2);
449 void compose6(
unsigned char * dst,
unsigned int dstbytes,
unsigned char * src,
unsigned int srcbytes,
unsigned int w,
unsigned int h,
unsigned int bop,
unsigned char depth)
451 unsigned int i, j, step;
452 unsigned int r, g, b, a;
453 unsigned int r2, g2, b2, a2;
463 dp = (
unsigned int*)dst;
473 *dp++ = (a << 24) + (r << 16) + (g << 8) + b;
485 *dp++ = (a << 24) + (r << 16) + (g << 8) + b;
489 if ((a2 = (*dp)>>24))
493 al = 255*255-(255-a)*(255-a2);
495 g2 = (((*dp)>>8)&255);
496 r2 = (((*dp)>>16)&255);
502 *dp++ = (a << 24) + (r << 16) + (g << 8) + b;
516 unsigned int rowbytes;
517 int imagesize, zbuf_size, zsize, trns_idx;
518 unsigned int len, chunk;
519 unsigned int w, h, w0, h0, x0, y0;
520 unsigned int frames, loops, first_frame, cur_frame;
521 unsigned int outrow1, outrow2, outimg1, outimg2;
522 unsigned short d1, d2;
523 unsigned char c, dop, bop;
524 unsigned char channels, depth, pixeldepth, bpp;
525 unsigned char coltype, compr, filter, interl;
527 memset(apng, 0,
sizeof(
struct apng_data));
529 for (i=0; i<256; i++)
537 zstream.zalloc = Z_NULL;
538 zstream.zfree = Z_NULL;
539 zstream.opaque = Z_NULL;
540 inflateInit(&zstream);
553 unsigned char sig[8];
554 unsigned char * pOut1;
555 unsigned char * pOut2;
556 unsigned char * pTemp;
557 unsigned char * pData;
558 unsigned char * pImg1;
559 unsigned char * pImg2;
560 unsigned char * pDst1;
561 unsigned char * pDst2;
562 unsigned short* frames_delay;
564 file.read((
char*)sig, 8);
565 if(!file.eof() && memcmp(sig,
png_sign, 8) == 0) {
569 if ((len == 13) && (chunk == 0x49484452))
573 file.read((
char*)&depth, 1);
574 file.read((
char*)&coltype, 1);
575 file.read((
char*)&compr, 1);
576 file.read((
char*)&filter, 1);
577 file.read((
char*)&interl, 1);
583 else if (coltype == 4)
585 else if (coltype == 6)
588 pixeldepth = depth*channels;
589 bpp = (pixeldepth + 7) >> 3;
592 imagesize = (rowbytes + 1) * h;
593 zbuf_size = imagesize + ((imagesize + 7) >> 3) + ((imagesize + 63) >> 6) + 11;
603 outrow1 = w*channels;
608 pOut1=(
unsigned char *)malloc(outimg1);
609 pOut2=(
unsigned char *)malloc(outimg2);
610 pTemp=(
unsigned char *)malloc(imagesize);
611 pData=(
unsigned char *)malloc(zbuf_size);
614 frames_delay =
nullptr;
617 memset(pOut1, 0, outimg1);
618 memset(pOut2, 0, outimg2);
625 if (chunk == 0x504C5445)
628 for (i=0; i<len; i++)
630 file.read((
char*)&c, 1);
640 else if (chunk == 0x74524E53)
643 for (i=0; i<len; i++)
645 file.read((
char*)&c, 1);
650 if (c == 0 && coltype == 3 && trns_idx == -1)
677 else if (chunk == 0x6163544C)
682 frames_delay = (
unsigned short*)malloc(frames*
sizeof(
unsigned short));
689 pOut1 = (
unsigned char *)malloc((frames+1)*outimg1);
690 pOut2 = (
unsigned char *)malloc((frames+1)*outimg2);
693 memset(pOut1, 0, outimg1);
694 memset(pOut2, 0, outimg2);
696 else if (chunk == 0x6663544C)
705 memcpy(pImg1 + outimg1, pImg1, outimg1);
707 memcpy(pImg2 + outimg2, pImg2, outimg2);
710 pDst1 = pImg1 + y0*outrow1 + x0*channels;
711 pDst2 = pImg2 + y0*outrow2 + x0*4;
712 unpack(zstream, pTemp, imagesize, pData, zsize, h0, rowbytes, bpp);
715 case 0:
compose0(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth);
break;
716 case 2:
compose2(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth);
break;
717 case 3:
compose3(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth);
break;
718 case 4:
compose4(pDst1, outrow1, pTemp, rowbytes+1, w0, h0, bop, depth);
break;
719 case 6:
compose6( pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth);
break;
726 memcpy(pImg1 + outimg1, pImg1, outimg1);
728 memcpy(pImg2 + outimg2, pImg2, outimg2);
740 case 2: memset(pDst2, 0, w0*4);
if (
hasTRNS)
for (i=0; i<w0; i++) { pDst1[i*3] =
trns[1]; pDst1[i*3+1] =
trns[3]; pDst1[i*3+2] =
trns[5]; }
else keep_original = 0;
break;
741 case 3: memset(pDst2, 0, w0*4);
if (trns_idx >= 0) memset(pDst1, trns_idx, w0);
else keep_original = 0;
break;
742 case 4: memset(pDst1, 0, w0*2);
break;
743 case 6: memset(pDst2, 0, w0*4);
break;
759 file.read((
char*)&dop, 1);
760 file.read((
char*)&bop, 1);
765 frames_delay[cur_frame] = (d1 * 1000)/d2;
774 if (!(coltype & 4) && !(
hasTRNS))
777 rowbytes =
ROWBYTES(pixeldepth, w0);
782 else if (chunk == 0x49444154)
784 file.read((
char*)(pData + zsize), len);
788 else if (chunk == 0x66644154)
792 file.read((
char*)(pData + zsize), len);
796 else if (chunk == 0x49454E44)
798 pDst1 = pImg1 + y0*outrow1 + x0*channels;
799 pDst2 = pImg2 + y0*outrow2 + x0*4;
800 unpack(zstream, pTemp, imagesize, pData, zsize, h0, rowbytes, bpp);
803 case 0:
compose0(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth);
break;
804 case 2:
compose2(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth);
break;
805 case 3:
compose3(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth);
break;
806 case 4:
compose4(pDst1, outrow1, pTemp, rowbytes+1, w0, h0, bop, depth);
break;
807 case 6:
compose6( pDst2, outrow2, pTemp, rowbytes+1, w0, h0, bop, depth);
break;
813 c = (
unsigned char)(chunk>>24);
815 c = (
unsigned char)((chunk>>16) & 0xFF);
817 c = (
unsigned char)((chunk>>8) & 0xFF);
819 c = (
unsigned char)(chunk & 0xFF);
822 file.seekg(len, std::ios_base::cur);
832 case 4:
trns[1] *= 0x11;
break;
833 case 2:
trns[1] *= 0x55;
break;
834 case 1:
trns[1] *= 0xFF;
break;
838 inflateEnd(&zstream);
840 apng->
bpp = channels;
867 void write_chunk(std::ostream& f,
const char* name,
unsigned char* data,
unsigned int length)
869 unsigned int crc = crc32(0, Z_NULL, 0);
870 unsigned int len =
swap32(length);
872 f.write((
char*)&len, 4);
874 crc = crc32(crc, (
const Bytef*)name, 4);
876 if(data !=
nullptr && length > 0) {
877 f.write((
char*)data, length);
878 crc = crc32(crc, data, length);
882 f.write((
char*)&crc, 4);
885 void write_IDATs(std::ostream& f,
unsigned char* data,
unsigned int length,
unsigned int idat_size)
887 unsigned int z_cmf = data[0];
889 if((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) {
891 unsigned int z_cinfo = z_cmf >> 4;
892 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
894 while(idat_size <= half_z_window_size && half_z_window_size >= 256) {
896 half_z_window_size >>= 1;
899 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
901 if(data[0] != (
unsigned char)z_cmf) {
902 data[0] = (
unsigned char)z_cmf;
904 data[1] += (
unsigned char)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
910 unsigned int ds = length;
922 void save_png(std::stringstream& f,
unsigned int width,
unsigned int height,
int channels,
unsigned char *pixels)
924 unsigned int bpp = 4;
925 unsigned char coltype = 0;
929 else if (channels == 2)
931 else if (channels == 4)
936 unsigned int mHeight;
937 unsigned char mDepth;
938 unsigned char mColorType;
939 unsigned char mCompression;
940 unsigned char mFilterMethod;
941 unsigned char mInterlaceMethod;
942 } ihdr = {
swap32(width),
swap32(height), 8, coltype, 0, 0, 0 };
948 unsigned int rowbytes = width * bpp;
949 unsigned int idat_size = (rowbytes + 1) * height;
950 unsigned int zbuf_size = idat_size + ((idat_size + 7) >> 3) + ((idat_size + 63) >> 6) + 11;
952 unsigned char* row_buf = (
unsigned char*)malloc(rowbytes + 1);
953 unsigned char* sub_row = (
unsigned char*)malloc(rowbytes + 1);
954 unsigned char* up_row = (
unsigned char*)malloc(rowbytes + 1);
955 unsigned char* avg_row = (
unsigned char*)malloc(rowbytes + 1);
956 unsigned char* paeth_row = (
unsigned char*)malloc(rowbytes + 1);
957 unsigned char* zbuf1 = (
unsigned char*)malloc(zbuf_size);
958 unsigned char* zbuf2 = (
unsigned char*)malloc(zbuf_size);
960 if(!row_buf || !sub_row || !up_row || !avg_row || !paeth_row || !zbuf1 || !zbuf2) {
978 zstream1.data_type = Z_BINARY;
979 zstream1.zalloc = Z_NULL;
980 zstream1.zfree = Z_NULL;
981 zstream1.opaque = Z_NULL;
982 deflateInit2(&zstream1, Z_BEST_COMPRESSION, 8, 15, 8, Z_DEFAULT_STRATEGY);
984 zstream2.data_type = Z_BINARY;
985 zstream2.zalloc = Z_NULL;
986 zstream2.zfree = Z_NULL;
987 zstream2.opaque = Z_NULL;
988 deflateInit2(&zstream2, Z_BEST_COMPRESSION, 8, 15, 8, Z_FILTERED);
990 int a, b, c, pa, pb, pc, p, v;
995 write_chunk(f,
"IHDR", (
unsigned char*)(&ihdr), 13);
1003 zstream1.next_out = zbuf1;
1004 zstream1.avail_out = zbuf_size;
1005 zstream2.next_out = zbuf2;
1006 zstream2.avail_out = zbuf_size;
1011 for(j = 0; j < (
unsigned int)height; j++) {
1013 unsigned int sum = 0;
1014 unsigned char* best_row = row_buf;
1015 unsigned int mins = ((
unsigned int)(-1)) >> 1;
1019 for(i = 0; i < rowbytes; i++) {
1020 v = out[i] = row[i];
1021 sum += (v < 128) ? v : 256 - v;
1029 for(i = 0; i < bpp; i++) {
1030 v = out[i] = row[i];
1031 sum += (v < 128) ? v : 256 - v;
1034 for(i = bpp; i < rowbytes; i++) {
1035 v = out[i] = row[i] - row[i - bpp];
1036 sum += (v < 128) ? v : 256 - v;
1038 if(sum > mins)
break;
1050 for(i = 0; i < rowbytes; i++) {
1051 v = out[i] = row[i] - prev[i];
1052 sum += (v < 128) ? v : 256 - v;
1054 if(sum > mins)
break;
1065 for(i = 0; i < bpp; i++) {
1066 v = out[i] = row[i] - prev[i] / 2;
1067 sum += (v < 128) ? v : 256 - v;
1070 for(i = bpp; i < rowbytes; i++) {
1071 v = out[i] = row[i] - (prev[i] + row[i - bpp]) / 2;
1072 sum += (v < 128) ? v : 256 - v;
1074 if(sum > mins)
break;
1083 out = paeth_row + 1;
1085 for(i = 0; i < bpp; i++) {
1086 v = out[i] = row[i] - prev[i];
1087 sum += (v < 128) ? v : 256 - v;
1090 for(i = bpp; i < rowbytes; i++) {
1099 p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
1100 v = out[i] = row[i] - p;
1101 sum += (v < 128) ? v : 256 - v;
1103 if(sum > mins)
break;
1107 best_row = paeth_row;
1111 zstream1.next_in = row_buf;
1112 zstream1.avail_in = rowbytes + 1;
1113 deflate(&zstream1, Z_NO_FLUSH);
1115 zstream2.next_in = best_row;
1116 zstream2.avail_in = rowbytes + 1;
1117 deflate(&zstream2, Z_NO_FLUSH);
1123 deflate(&zstream1, Z_FINISH);
1124 deflate(&zstream2, Z_FINISH);
1126 if(zstream1.total_out <= zstream2.total_out)
1127 write_IDATs(f, zbuf1, zstream1.total_out, idat_size);
1129 write_IDATs(f, zbuf2, zstream2.total_out, idat_size);
1131 deflateReset(&zstream1);
1132 zstream1.data_type = Z_BINARY;
1133 deflateReset(&zstream2);
1134 zstream2.data_type = Z_BINARY;
1138 deflateEnd(&zstream1);
1139 deflateEnd(&zstream2);