📄 pngdecode.c
字号:
if((++i) < data->width) { TRANS_PACK_GS(dst, src2, shift2, inc2, data->trans); } if((++i) < data->width) { TRANS_PACK_GS(dst, src1, shift1, inc1, data->trans); } if((++i) < data->width) { TRANS_PACK_GS(dst, src3, shift3, inc3, data->trans); } if((++i) < data->width) { TRANS_PACK_GS(dst, src2, shift2, inc2, data->trans); } if((++i) < data->width) { TRANS_PACK_GS(dst, src1, shift1, inc1, data->trans); } } } } } else if (data->depth < 8) { INIT_UNPACK(data->depth, shift0, mask0); INIT_UNPACK(data->depth, shift1, mask1); INIT_UNPACK(data->depth, shift2, mask2); INIT_UNPACK(data->depth, shift3, mask3); x0 = shift0; m0 = mask0; for (i = 0; i < data->width; ++i) { UNPACK(dst, src0, shift0, mask0, data->depth, x0, m0); if ((++i) < data->width) { UNPACK(dst, src1, shift1, mask1, data->depth, x0, m0); } if ((++i) < data->width) { UNPACK(dst, src2, shift2, mask2, data->depth, x0, m0); } if ((++i) < data->width) { UNPACK(dst, src1, shift1, mask1, data->depth, x0, m0); } if ((++i) < data->width) { UNPACK(dst, src3, shift3, mask3, data->depth, x0, m0); } if ((++i) < data->width) { UNPACK(dst, src1, shift1, mask1, data->depth, x0, m0); } if ((++i) < data->width) { UNPACK(dst, src2, shift2, mask2, data->depth, x0, m0); } if ((++i) < data->width) { UNPACK(dst, src1, shift1, mask1, data->depth, x0, m0); } } } else if ((data->colorType & 3) == 2) { INIT_PACK3(data->depth, data->colorType & CT_ALPHA, shift0, inc0); INIT_PACK3(data->depth, data->colorType & CT_ALPHA, shift1, inc1); INIT_PACK3(data->depth, data->colorType & CT_ALPHA, shift2, inc2); INIT_PACK3(data->depth, data->colorType & CT_ALPHA, shift3, inc3); for (i = 0; i < data->width; ++i) { PACK3(dst, src0, shift0, inc0, data->colorType & CT_ALPHA); if ((++i) < data->width) { PACK3(dst, src1, shift1, inc1, data->colorType & CT_ALPHA); } if ((++i) < data->width) { PACK3(dst, src2, shift2, inc2, data->colorType & CT_ALPHA); } if ((++i) < data->width) { PACK3(dst, src1, shift1, inc1, data->colorType & CT_ALPHA); } if ((++i) < data->width) { PACK3(dst, src3, shift3, inc3, data->colorType & CT_ALPHA); } if ((++i) < data->width) { PACK3(dst, src1, shift1, inc1, data->colorType & CT_ALPHA); } if ((++i) < data->width) { PACK3(dst, src2, shift2, inc2, data->colorType & CT_ALPHA); } if ((++i) < data->width) { PACK3(dst, src1, shift1, inc1, data->colorType & CT_ALPHA); } } } else { INIT_PACK1(data->depth, data->colorType & CT_ALPHA, shift0, inc0); INIT_PACK1(data->depth, data->colorType & CT_ALPHA, shift1, inc1); INIT_PACK1(data->depth, data->colorType & CT_ALPHA, shift2, inc2); INIT_PACK1(data->depth, data->colorType & CT_ALPHA, shift3, inc3); for (i = 0; i < data->width; ++i) { PACK1(dst, src0, shift0, inc0, data->colorType & CT_ALPHA); if ((++i) < data->width) { PACK1(dst, src1, shift1, inc1, data->colorType & CT_ALPHA); } if ((++i) < data->width) { PACK1(dst, src2, shift2, inc2, data->colorType & CT_ALPHA); } if ((++i) < data->width) { PACK1(dst, src1, shift1, inc1, data->colorType & CT_ALPHA); } if ((++i) < data->width) { PACK1(dst, src3, shift3, inc3, data->colorType & CT_ALPHA); } if ((++i) < data->width) { PACK1(dst, src1, shift1, inc1, data->colorType & CT_ALPHA); } if ((++i) < data->width) { PACK1(dst, src2, shift2, inc2, data->colorType & CT_ALPHA); } if ((++i) < data->width) { PACK1(dst, src1, shift1, inc1, data->colorType & CT_ALPHA); } } }}static jbooleanhandleImageData(unsigned char *pixels, int pixelsLength, imageDstPtr dst, pngData *data){ int pixelSize = ((data->colorType & (CT_PALETTE | CT_COLOR)) ? 3 : 1) + (((data->colorType & CT_ALPHA) || (data->trans != NULL)) ? 1 : 0 ); int rgba = data->colorType; int sendDirect = KNI_FALSE; unsigned char *scanline = NULL; unsigned char *passes[7]; int y; filterAllRows(pixels, data); if (data->interlace) { scanline = (unsigned char *) midpMalloc(data->width * pixelSize); if (scanline == NULL) { return KNI_FALSE; } passes[0] = pixels; for (y = 1; y < 7; ++y) { passes[y] = passes[y - 1] + data->passSize[y - 1]; } } else { passes[6] = pixels; } if ( (data->depth == 8) && ( (data->colorType & CT_PALETTE) || (data->trans == NULL) ) ) { /* if data is in the desired format i.e. 8 bit Palette or 8 bit RGB/gs without transparency*/ sendDirect = KNI_TRUE; } else if (scanline == NULL) { scanline = (unsigned char *) midpMalloc(data->width * pixelSize); if (scanline == NULL) { return KNI_FALSE; } } for (y = 0; y < data->height; ++y) { if ((y & 1) || !data->interlace) { if (sendDirect) { dst->sendPixels(dst, y, passes[6] + 1, rgba); } else { unpack1(scanline, passes[6] + 1, data); dst->sendPixels(dst, y, scanline, rgba); } passes[6] += data->lineBytes[6]; } else { switch (y & 6) { case 2: case 6: unpack2(scanline, passes[4] + 1, passes[5] + 1, data); passes[4] += data->lineBytes[4]; passes[5] += data->lineBytes[5]; break; case 4: unpack3(scanline, passes[2] + 1, passes[5] + 1, passes[3] + 1, data); passes[2] += data->lineBytes[2]; passes[5] += data->lineBytes[5]; passes[3] += data->lineBytes[3]; break; case 0: unpack4(scanline, passes[0] + 1, passes[5] + 1, passes[3] + 1, passes[1] + 1, data); passes[0] += data->lineBytes[0]; passes[5] += data->lineBytes[5]; passes[3] += data->lineBytes[3]; passes[1] += data->lineBytes[1]; break; } dst->sendPixels(dst, y, scanline, rgba); } } if (scanline != NULL) { midpFree(scanline); } return KNI_TRUE;}static unsigned longgetInt(imageSrcPtr src){ unsigned long n; n = ((unsigned long)src->getByte(src) << 24); n |= ((unsigned long)src->getByte(src) << 16); n |= ((unsigned long)src->getByte(src) << 8); n |= ((unsigned long)src->getByte(src) << 0); return n;}static jbooleangetChunk(imageSrcPtr src, unsigned long *chunkType, long *chunkLength){ *chunkLength = getInt(src); *chunkType = getInt(src); return (*chunkType != IEND_CHUNK) ? KNI_TRUE : KNI_FALSE;}static unsigned longskip(imageSrcPtr src, int nBytes, unsigned long CRC){ static unsigned char buf[64]; while (nBytes > 0) { int n = (nBytes > sizeof(buf)) ? sizeof(buf) : nBytes; src->getBytes(src, buf, n); CRC = update_crc(CRC, buf, n); nBytes -= n; } return CRC;}static jbooleansignatureOK(imageSrcPtr src){ unsigned char buf[8]; static unsigned char png_header[] = { 137, 80, 78, 71, 13, 10, 26, 10 }; src->getBytes(src, buf, sizeof(buf)); return (memcmp(png_header, buf, sizeof(png_header)) == 0) ? KNI_TRUE : KNI_FALSE;}static jbooleanheaderOK(pngData *data){ /* * simple sanity checks. Most are obvious. * (data->depth & (data->depth-1)) is checking to see that * depth is a simple power of two, i.e. only one bit is set */ if ( ((data->width <= 0) || (data->height <= 0)) || (data->compress != 0) || (data->filter != 0) || ((data->interlace != 0) && (data->interlace != 1)) || ((data->depth > 16) || (data->depth & (data->depth-1))) ) { return KNI_FALSE; } /* based on colorType, only certain depths are allowed. */ /* depth > 16 was already guarded against above. */ switch (data->colorType) { case 0: return KNI_TRUE; case 2: case 4: case 6: return (data->depth >= 8) ? KNI_TRUE : KNI_FALSE; case 3: return (data->depth <= 8) ? KNI_TRUE : KNI_FALSE; default: return KNI_FALSE; }}static unsigned longinit_CRC(unsigned long chunkType){ unsigned char buf[4]; buf[0] = (chunkType >> 24) & 0xff; buf[1] = (chunkType >> 16) & 0xff; buf[2] = (chunkType >> 8) & 0xff; buf[3] = (chunkType >> 0) & 0xff; return update_crc(0xffffffffL, buf, 4);}static jbooleancheck_CRC(imageSrcPtr src, unsigned long CRC){ unsigned long transmitted_CRC = getInt(src); CRC = (~CRC) & 0xffffffffl;#if 0 if (transmitted_CRC != CRC) { fprintf(stderr, "CRC mismatch: xmitted 0x%08lx\n", transmitted_CRC); fprintf(stderr, " vs calculated 0x%08lx\n", CRC); }#endif return (transmitted_CRC == CRC) ? KNI_TRUE : KNI_FALSE;}/***** CRC support, copied from PNG spec *****//* Table of CRCs of all 8-bit messages. */static unsigned long crc_table[256];/* Flag: has the table been computed? Initially false. */static int crc_table_computed = 0;/* Make the table for a fast CRC. */static voidmake_crc_table(void){ unsigned long c; int n, k; for (n = 0; n < 256; n++) { c = (unsigned long) n; for (k = 0; k < 8; k++) { if (c & 1) { c = 0xedb88320L ^ (c >> 1); } else { c = c >> 1; } } crc_table[n] = c; } crc_table_computed = 1;}/** Update a running CRC with the bytes buf[0..len-1]*/static unsigned longupdate_crc(unsigned long crc, unsigned char *buf, int len){ unsigned long c = crc; int n; if (!crc_table_computed) { make_crc_table(); } for (n = 0; n < len; n++) { c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8); } return c;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -