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 #pragma warning( push )
84 #pragma warning( disable : 4244)
85 unsigned int read32(std::istream& f1)
87 unsigned char a, b, c, d;
88 f1.read((
char*)&a, 1);
89 f1.read((
char*)&b, 1);
90 f1.read((
char*)&c, 1);
91 f1.read((
char*)&d, 1);
92 return (
static_cast<unsigned int>(a) << 24) + (
static_cast<unsigned int>(b) << 16) + (
static_cast<unsigned int>(c) << 8) +
static_cast<unsigned int>(d);
95 unsigned short read16(std::istream& f1)
98 f1.read((
char*)&a, 1);
99 f1.read((
char*)&b, 1);
100 return (
static_cast<unsigned short>(a) << 8) +
static_cast<unsigned short>(b);
105 return (
static_cast<unsigned short>(*p) << 8) +
static_cast<unsigned short>(*(p + 1));
108 void read_sub_row(
unsigned char* row,
unsigned int rowbytes,
unsigned int bpp)
112 for (i = bpp; i < rowbytes; i++)
113 row[i] += row[i - bpp];
116 void read_up_row(
unsigned char* row,
unsigned char* prev_row,
unsigned int rowbytes,
unsigned int)
121 for (i = 0; i < rowbytes; i++)
122 row[i] += prev_row[i];
125 void read_average_row(
unsigned char* row,
unsigned char* prev_row,
unsigned int rowbytes,
unsigned int bpp)
131 for (i = 0; i < bpp; i++)
132 row[i] += prev_row[i] >> 1;
133 for (i = bpp; i < rowbytes; i++)
134 row[i] += (prev_row[i] + row[i - bpp]) >> 1;
138 for (i = bpp; i < rowbytes; i++)
139 row[i] += row[i - bpp] >> 1;
143 void read_paeth_row(
unsigned char* row,
unsigned char* prev_row,
unsigned int rowbytes,
unsigned int bpp)
146 int a, b, c, pa, pb, pc, p;
150 for (i = 0; i < bpp; i++)
151 row[i] += prev_row[i];
152 for (i = bpp; i < rowbytes; i++)
156 c = prev_row[i - bpp];
162 row[i] += ((pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c);
167 for (i = bpp; i < rowbytes; i++)
168 row[i] += row[i - bpp];
172 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)
175 unsigned char* row = dst;
176 unsigned char* prev_row =
nullptr;
178 zstream.next_out = dst;
179 zstream.avail_out = dst_size;
180 zstream.next_in = src;
181 zstream.avail_in = src_size;
182 inflate(&zstream, Z_FINISH);
183 inflateReset(&zstream);
185 for (j = 0; j < h; j++)
191 case 2:
read_up_row(row, prev_row, rowbytes, bpp);
break;
200 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)
202 unsigned int i, j, g, a;
207 for (j = 0; j < h; j++)
211 dp2 = (
unsigned int*)dst2;
217 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;
218 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;
219 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;
220 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;
221 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;
228 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;
229 case 8:
for (i = 0; i < w; i++, dp1++, dp2++) {
if (*sp !=
trns1) { *dp1 = *sp; *dp2 = 0xFF000000 + (*sp << 16) + (*sp << 8) + *sp; } sp++; }
break;
230 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;
231 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;
232 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;
242 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)
245 unsigned int r, g, b, a;
250 for (j = 0; j < h; j++)
254 dp2 = (
unsigned int*)dst2;
260 for (i = 0; i < w; i++)
268 *dp1++ = b; *dp1++ = g; *dp1++ = r;
269 *dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
274 for (i = 0; i < w; i++, sp += 6)
282 *dp1++ = b; *dp1++ = g; *dp1++ = r;
283 *dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
291 for (i = 0; i < w; i++, sp += 3, dp1 += 3, dp2++)
294 *dp1 = *sp; *(dp1 + 1) = *(sp + 1); *(dp1 + 2) = *(sp + 2);
295 *dp2 = 0xFF000000 + (*(sp + 2) << 16) + (*(sp + 1) << 8) + *sp;
300 for (i = 0; i < w; i++, sp += 6, dp1 += 3, dp2++)
303 *dp1 = *sp; *(dp1 + 1) = *(sp + 2); *(dp1 + 2) = *(sp + 4);
304 *dp2 = 0xFF000000 + (*(sp + 4) << 16) + (*(sp + 2) << 8) + *sp;
314 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)
317 unsigned int r, g, b, a;
318 unsigned int r2, g2, b2, a2;
320 unsigned char col = 0;
325 for (j = 0; j < h; j++)
329 dp2 = (
unsigned int*)dst2;
331 for (i = 0; i < w; i++)
335 case 8: col = sp[i];
break;
336 case 4: col = (sp[i >> 1] &
mask4[i & 1]) >>
shift4[i & 1];
break;
337 case 2: col = (sp[i >> 2] &
mask2[i & 3]) >>
shift2[i & 3];
break;
338 case 1: col = (sp[i >> 3] &
mask1[i & 7]) >>
shift1[i & 7];
break;
349 *dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
356 *dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
361 if ((a2 = (*dp2) >> 24) != 0)
366 al = 255 * 255 - (255 - a) * (255 - a2);
368 g2 = (((*dp2) >> 8) & 255);
369 r2 = (((*dp2) >> 16) & 255);
370 b = (b * u + b2 * v) / al;
371 g = (g * u + g2 * v) / al;
372 r = (r * u + r2 * v) / al;
376 *dp2++ = (a << 24) + (r << 16) + (g << 8) + b;
391 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)
393 unsigned int i, j, step;
394 unsigned int g, a, g2, a2;
399 step = (depth + 7) / 8;
401 for (j = 0; j < h; j++)
408 for (i = 0; i < w; i++)
418 for (i = 0; i < w; i++)
430 if ((a2 = *(dp + 1)) != 0)
434 al = 255 * 255 - (255 - a) * (255 - a2);
436 g = (g * u + g2 * v) / al;
451 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)
453 unsigned int i, j, step;
454 unsigned int r, g, b, a;
455 unsigned int r2, g2, b2, a2;
460 step = (depth + 7) / 8;
462 for (j = 0; j < h; j++)
465 dp = (
unsigned int*)dst;
469 for (i = 0; i < w; i++)
475 *dp++ = (a << 24) + (r << 16) + (g << 8) + b;
480 for (i = 0; i < w; i++)
487 *dp++ = (a << 24) + (r << 16) + (g << 8) + b;
491 if ((a2 = (*dp) >> 24) != 0)
495 al = 255 * 255 - (255 - a) * (255 - a2);
497 g2 = (((*dp) >> 8) & 255);
498 r2 = (((*dp) >> 16) & 255);
499 b = (b * u + b2 * v) / al;
500 g = (g * u + g2 * v) / al;
501 r = (r * u + r2 * v) / al;
504 *dp++ = (a << 24) + (r << 16) + (g << 8) + b;
518 unsigned int rowbytes;
519 int imagesize, zbuf_size, zsize, trns_idx;
520 unsigned int len, chunk;
521 unsigned int w, h, w0, h0, x0, y0;
522 unsigned int frames, loops, first_frame, cur_frame;
523 unsigned int outrow1, outrow2, outimg1, outimg2;
524 unsigned short d1, d2;
526 unsigned char channels, depth, pixeldepth, bpp;
527 unsigned char coltype, compr, filter, interl;
529 memset(apng, 0,
sizeof(
struct apng_data));
531 for (i = 0; i < 256; i++)
539 zstream.zalloc = Z_NULL;
540 zstream.zfree = Z_NULL;
541 zstream.opaque = Z_NULL;
542 inflateInit(&zstream);
555 unsigned char sig[8];
556 unsigned char* pOut1;
557 unsigned char* pOut2;
558 unsigned char* pTemp;
559 unsigned char* pData;
560 unsigned char* pImg1;
561 unsigned char* pImg2;
562 unsigned char* pDst1;
563 unsigned char* pDst2;
564 unsigned short* frames_delay;
566 file.read((
char*)sig, 8);
567 if (!file.eof() && memcmp(sig,
png_sign, 8) == 0) {
571 if ((len == 13) && (chunk == 0x49484452))
575 file.read((
char*)&depth, 1);
576 file.read((
char*)&coltype, 1);
577 file.read((
char*)&compr, 1);
578 file.read((
char*)&filter, 1);
579 file.read((
char*)&interl, 1);
585 else if (coltype == 4)
587 else if (coltype == 6)
590 pixeldepth = depth * channels;
591 bpp = (pixeldepth + 7) >> 3;
594 imagesize = (rowbytes + 1) * h;
595 zbuf_size = imagesize + ((imagesize + 7) >> 3) + ((imagesize + 63) >> 6) + 11;
605 outrow1 = w * channels;
607 outimg1 = h * outrow1;
608 outimg2 = h * outrow2;
610 pOut1 =
static_cast<unsigned char*
>(malloc(outimg1));
611 pOut2 =
static_cast<unsigned char*
>(malloc(outimg2));
612 pTemp =
static_cast<unsigned char*
>(malloc(imagesize));
613 pData =
static_cast<unsigned char*
>(malloc(zbuf_size));
616 frames_delay =
nullptr;
619 memset(pOut1, 0, outimg1);
620 memset(pOut2, 0, outimg2);
627 if (chunk == 0x504C5445)
630 for (i = 0; i < len; i++)
632 file.read((
char*)&c, 1);
642 else if (chunk == 0x74524E53)
645 for (i = 0; i < len; i++)
647 file.read((
char*)&c, 1);
652 if (c == 0 && coltype == 3 && trns_idx == -1)
679 else if (chunk == 0x6163544C)
684 frames_delay =
static_cast<unsigned short*
>(malloc(frames *
sizeof(
unsigned short)));
691 pOut1 =
static_cast<unsigned char*
>(malloc((frames + 1) * outimg1));
692 pOut2 =
static_cast<unsigned char*
>(malloc((frames + 1) * outimg2));
695 memset(pOut1, 0, outimg1);
696 memset(pOut2, 0, outimg2);
698 else if (chunk == 0x6663544C)
707 memcpy(pImg1 + outimg1, pImg1, outimg1);
709 memcpy(pImg2 + outimg2, pImg2, outimg2);
712 pDst1 = pImg1 + y0 * outrow1 + x0 * channels;
713 pDst2 = pImg2 + y0 * outrow2 + x0 * 4;
714 unpack(zstream, pTemp, imagesize, pData, zsize, h0, rowbytes, bpp);
717 case 0:
compose0(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes + 1, w0, h0, bop, depth);
break;
718 case 2:
compose2(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes + 1, w0, h0, bop, depth);
break;
719 case 3:
compose3(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes + 1, w0, h0, bop, depth);
break;
720 case 4:
compose4(pDst1, outrow1, pTemp, rowbytes + 1, w0, h0, bop, depth);
break;
721 case 6:
compose6(pDst2, outrow2, pTemp, rowbytes + 1, w0, h0, bop, depth);
break;
728 memcpy(pImg1 + outimg1, pImg1, outimg1);
730 memcpy(pImg2 + outimg2, pImg2, outimg2);
737 for (j = 0; j < h0; j++)
742 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]; }
744 case 3: memset(pDst2, 0, w0 * 4);
if (trns_idx >= 0) memset(pDst1, trns_idx, w0);
else keep_original = 0;
break;
745 case 4: memset(pDst1, 0, w0 * 2);
break;
746 case 6: memset(pDst2, 0, w0 * 4);
break;
762 file.read((
char*)&dop, 1);
763 file.read((
char*)&bop, 1);
768 frames_delay[cur_frame] = (d1 * 1000) / d2;
777 if (!(coltype & 4) && !(
hasTRNS))
780 rowbytes =
ROWBYTES(pixeldepth, w0);
785 else if (chunk == 0x49444154)
787 file.read((
char*)(pData + zsize), len);
791 else if (chunk == 0x66644154)
795 file.read((
char*)(pData + zsize), len);
799 else if (chunk == 0x49454E44)
801 pDst1 = pImg1 + y0 * outrow1 + x0 * channels;
802 pDst2 = pImg2 + y0 * outrow2 + x0 * 4;
803 unpack(zstream, pTemp, imagesize, pData, zsize, h0, rowbytes, bpp);
806 case 0:
compose0(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes + 1, w0, h0, bop, depth);
break;
807 case 2:
compose2(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes + 1, w0, h0, bop, depth);
break;
808 case 3:
compose3(pDst1, outrow1, pDst2, outrow2, pTemp, rowbytes + 1, w0, h0, bop, depth);
break;
809 case 4:
compose4(pDst1, outrow1, pTemp, rowbytes + 1, w0, h0, bop, depth);
break;
810 case 6:
compose6(pDst2, outrow2, pTemp, rowbytes + 1, w0, h0, bop, depth);
break;
816 c =
static_cast<unsigned char>(chunk >> 24);
818 c =
static_cast<unsigned char>((chunk >> 16) & 0xFF);
820 c =
static_cast<unsigned char>((chunk >> 8) & 0xFF);
822 c =
static_cast<unsigned char>(chunk & 0xFF);
825 file.seekg(len, std::ios_base::cur);
835 case 4:
trns[1] *= 0x11;
break;
836 case 2:
trns[1] *= 0x55;
break;
837 case 1:
trns[1] *= 0xFF;
break;
841 inflateEnd(&zstream);
843 apng->
bpp = channels;
872 void write_chunk(std::ostream& f,
const char* name,
unsigned char* data,
unsigned int length)
874 unsigned int crc = crc32(0, Z_NULL, 0);
875 unsigned int len =
swap32(length);
877 f.write((
char*)&len, 4);
879 crc = crc32(crc, (
const Bytef*)name, 4);
881 if (data !=
nullptr && length > 0) {
882 f.write((
char*)data, length);
883 crc = crc32(crc, data, length);
887 f.write((
char*)&crc, 4);
890 void write_IDATs(std::ostream& f,
unsigned char* data,
unsigned int length,
unsigned int idat_size)
892 unsigned int z_cmf = data[0];
894 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) {
896 unsigned int z_cinfo = z_cmf >> 4;
897 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
899 while (idat_size <= half_z_window_size && half_z_window_size >= 256) {
901 half_z_window_size >>= 1;
904 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
906 if (data[0] !=
static_cast<unsigned char>(z_cmf)) {
907 data[0] =
static_cast<unsigned char>(z_cmf);
909 data[1] +=
static_cast<unsigned char>(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
915 unsigned int ds = length;
927 void save_png(std::stringstream& f,
unsigned int width,
unsigned int height,
int channels,
unsigned char* pixels)
929 unsigned int bpp = 4;
930 unsigned char coltype = 0;
934 else if (channels == 2)
936 else if (channels == 4)
941 unsigned int mHeight;
942 unsigned char mDepth;
943 unsigned char mColorType;
944 unsigned char mCompression;
945 unsigned char mFilterMethod;
946 unsigned char mInterlaceMethod;
947 } ihdr = {
swap32(width),
swap32(height), 8, coltype, 0, 0, 0 };
953 unsigned int rowbytes = width * bpp;
954 unsigned int idat_size = (rowbytes + 1) * height;
955 unsigned int zbuf_size = idat_size + ((idat_size + 7) >> 3) + ((idat_size + 63) >> 6) + 11;
957 unsigned char* row_buf =
static_cast<unsigned char*
>(malloc(rowbytes + 1));
958 unsigned char* sub_row =
static_cast<unsigned char*
>(malloc(rowbytes + 1));
959 unsigned char* up_row =
static_cast<unsigned char*
>(malloc(rowbytes + 1));
960 unsigned char* avg_row =
static_cast<unsigned char*
>(malloc(rowbytes + 1));
961 unsigned char* paeth_row =
static_cast<unsigned char*
>(malloc(rowbytes + 1));
962 unsigned char* zbuf1 =
static_cast<unsigned char*
>(malloc(zbuf_size));
963 unsigned char* zbuf2 =
static_cast<unsigned char*
>(malloc(zbuf_size));
965 if (!row_buf || !sub_row || !up_row || !avg_row || !paeth_row || !zbuf1 || !zbuf2) {
983 zstream1.data_type = Z_BINARY;
984 zstream1.zalloc = Z_NULL;
985 zstream1.zfree = Z_NULL;
986 zstream1.opaque = Z_NULL;
987 deflateInit2(&zstream1, Z_BEST_COMPRESSION, 8, 15, 8, Z_DEFAULT_STRATEGY);
989 zstream2.data_type = Z_BINARY;
990 zstream2.zalloc = Z_NULL;
991 zstream2.zfree = Z_NULL;
992 zstream2.opaque = Z_NULL;
993 deflateInit2(&zstream2, Z_BEST_COMPRESSION, 8, 15, 8, Z_FILTERED);
995 int a, b, c, pa, pb, pc, p, v;
1000 write_chunk(f,
"IHDR", (
unsigned char*)(&ihdr), 13);
1008 zstream1.next_out = zbuf1;
1009 zstream1.avail_out = zbuf_size;
1010 zstream2.next_out = zbuf2;
1011 zstream2.avail_out = zbuf_size;
1016 for (j = 0; j < static_cast<unsigned int>(height); j++) {
1018 unsigned int sum = 0;
1019 unsigned char* best_row = row_buf;
1020 unsigned int mins =
static_cast<unsigned int>(-1) >> 1;
1024 for (i = 0; i < rowbytes; i++) {
1025 v = out[i] = row[i];
1026 sum += (v < 128) ? v : 256 - v;
1034 for (i = 0; i < bpp; i++) {
1035 v = out[i] = row[i];
1036 sum += (v < 128) ? v : 256 - v;
1039 for (i = bpp; i < rowbytes; i++) {
1040 v = out[i] = row[i] - row[i - bpp];
1041 sum += (v < 128) ? v : 256 - v;
1043 if (sum > mins)
break;
1055 for (i = 0; i < rowbytes; i++) {
1056 v = out[i] = row[i] - prev[i];
1057 sum += (v < 128) ? v : 256 - v;
1059 if (sum > mins)
break;
1070 for (i = 0; i < bpp; i++) {
1071 v = out[i] = row[i] - prev[i] / 2;
1072 sum += (v < 128) ? v : 256 - v;
1075 for (i = bpp; i < rowbytes; i++) {
1076 v = out[i] = row[i] - (prev[i] + row[i - bpp]) / 2;
1077 sum += (v < 128) ? v : 256 - v;
1079 if (sum > mins)
break;
1088 out = paeth_row + 1;
1090 for (i = 0; i < bpp; i++) {
1091 v = out[i] = row[i] - prev[i];
1092 sum += (v < 128) ? v : 256 - v;
1095 for (i = bpp; i < rowbytes; i++) {
1104 p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
1105 v = out[i] = row[i] - p;
1106 sum += (v < 128) ? v : 256 - v;
1108 if (sum > mins)
break;
1112 best_row = paeth_row;
1116 zstream1.next_in = row_buf;
1117 zstream1.avail_in = rowbytes + 1;
1118 deflate(&zstream1, Z_NO_FLUSH);
1120 zstream2.next_in = best_row;
1121 zstream2.avail_in = rowbytes + 1;
1122 deflate(&zstream2, Z_NO_FLUSH);
1128 deflate(&zstream1, Z_FINISH);
1129 deflate(&zstream2, Z_FINISH);
1131 if (zstream1.total_out <= zstream2.total_out)
1132 write_IDATs(f, zbuf1, zstream1.total_out, idat_size);
1134 write_IDATs(f, zbuf2, zstream2.total_out, idat_size);
1136 deflateReset(&zstream1);
1137 zstream1.data_type = Z_BINARY;
1138 deflateReset(&zstream2);
1139 zstream2.data_type = Z_BINARY;
1143 deflateEnd(&zstream1);
1144 deflateEnd(&zstream2);
1161 #pragma warning( pop )