| 1 | #ifndef YSPNG_IS_INCLUDED |
|---|
| 2 | #define YSPNG_IS_INCLUDED |
|---|
| 3 | /* { */ |
|---|
| 4 | |
|---|
| 5 | #ifndef YSRESULT_IS_DEFINED |
|---|
| 6 | #define YSERR 0 |
|---|
| 7 | #define YSOK 1 |
|---|
| 8 | #endif |
|---|
| 9 | |
|---|
| 10 | #ifndef YSBOOL_IS_DEFINED |
|---|
| 11 | #define YSBOOL_IS_DEFINED |
|---|
| 12 | #define YSFALSE 0 |
|---|
| 13 | #define YSTRUE 1 |
|---|
| 14 | #endif |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | class YsPngHuffmanTree |
|---|
| 19 | { |
|---|
| 20 | public: |
|---|
| 21 | YsPngHuffmanTree(); |
|---|
| 22 | ~YsPngHuffmanTree(); |
|---|
| 23 | YsPngHuffmanTree *zero,*one; |
|---|
| 24 | unsigned int dat; |
|---|
| 25 | unsigned int weight,depth; |
|---|
| 26 | static int leakTracker; |
|---|
| 27 | |
|---|
| 28 | static void DeleteHuffmanTree(YsPngHuffmanTree *node); |
|---|
| 29 | }; |
|---|
| 30 | |
|---|
| 31 | class YsPngUncompressor |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | class YsGenericPngDecoder *output; |
|---|
| 35 | |
|---|
| 36 | inline unsigned int GetNextBit(const unsigned char dat[],unsigned &bytePtr,unsigned &bitPtr) |
|---|
| 37 | { |
|---|
| 38 | unsigned a; |
|---|
| 39 | a=dat[bytePtr]&bitPtr; |
|---|
| 40 | bitPtr<<=1; |
|---|
| 41 | if(bitPtr>=256) |
|---|
| 42 | { |
|---|
| 43 | bitPtr=1; |
|---|
| 44 | bytePtr++; |
|---|
| 45 | } |
|---|
| 46 | return (a!=0 ? 1 : 0); |
|---|
| 47 | } |
|---|
| 48 | inline unsigned int GetNextMultiBit(const unsigned char dat[],unsigned &bytePtr,unsigned &bitPtr,unsigned n) |
|---|
| 49 | { |
|---|
| 50 | unsigned value,mask,i; |
|---|
| 51 | value=0; |
|---|
| 52 | mask=1; |
|---|
| 53 | for(i=0; i<n; i++) |
|---|
| 54 | { |
|---|
| 55 | if(GetNextBit(dat,bytePtr,bitPtr)) |
|---|
| 56 | { |
|---|
| 57 | value|=mask; |
|---|
| 58 | } |
|---|
| 59 | mask<<=1; |
|---|
| 60 | } |
|---|
| 61 | return value; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | void MakeFixedHuffmanCode(unsigned hLength[288],unsigned hCode[288]); |
|---|
| 65 | static void MakeDynamicHuffmanCode(unsigned hLength[288],unsigned hCode[288],unsigned nLng,unsigned lng[]); |
|---|
| 66 | int DecodeDynamicHuffmanCode |
|---|
| 67 | (unsigned int &hLit,unsigned int &hDist,unsigned int &hCLen, |
|---|
| 68 | unsigned int *&hLengthLiteral,unsigned int *&hCodeLiteral, |
|---|
| 69 | unsigned int *&hLengthDist,unsigned int *&hCodeDist, |
|---|
| 70 | unsigned int hLengthBuf[322],unsigned int hCodeBuf[322], |
|---|
| 71 | const unsigned char dat[],unsigned int &bytePtr,unsigned int &bitPtr); |
|---|
| 72 | |
|---|
| 73 | YsPngHuffmanTree *MakeHuffmanTree(unsigned n,unsigned hLength[],unsigned hCode[]); |
|---|
| 74 | void DeleteHuffmanTree(YsPngHuffmanTree *node); |
|---|
| 75 | |
|---|
| 76 | unsigned GetCopyLength(unsigned value,unsigned char dat[],unsigned &bytePtr,unsigned &bitPtr); |
|---|
| 77 | unsigned GetBackwardDistance(unsigned distCode,unsigned char dat[],unsigned &bytePtr,unsigned &bitPtr); |
|---|
| 78 | |
|---|
| 79 | int Uncompress(unsigned length,unsigned char dat[]); |
|---|
| 80 | }; |
|---|
| 81 | |
|---|
| 82 | //////////////////////////////////////////////////////////// |
|---|
| 83 | |
|---|
| 84 | class YsPngHeader |
|---|
| 85 | { |
|---|
| 86 | public: |
|---|
| 87 | unsigned int width,height; |
|---|
| 88 | unsigned int bitDepth,colorType; |
|---|
| 89 | unsigned int compressionMethod,filterMethod,interlaceMethod; |
|---|
| 90 | |
|---|
| 91 | void Decode(unsigned char dat[]); |
|---|
| 92 | }; |
|---|
| 93 | |
|---|
| 94 | class YsPngPalette |
|---|
| 95 | { |
|---|
| 96 | public: |
|---|
| 97 | unsigned int nEntry; |
|---|
| 98 | unsigned char *entry; |
|---|
| 99 | |
|---|
| 100 | YsPngPalette(); |
|---|
| 101 | ~YsPngPalette(); |
|---|
| 102 | int Decode(unsigned length,unsigned char dat[]); |
|---|
| 103 | }; |
|---|
| 104 | |
|---|
| 105 | class YsPngTransparency |
|---|
| 106 | { |
|---|
| 107 | public: |
|---|
| 108 | unsigned int col[3]; |
|---|
| 109 | |
|---|
| 110 | // For color type 3, up to three transparent colors is supported. |
|---|
| 111 | int Decode(unsigned length,unsigned char dat[],unsigned int colorType); |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | class YsGenericPngDecoder |
|---|
| 115 | { |
|---|
| 116 | public: |
|---|
| 117 | enum |
|---|
| 118 | { |
|---|
| 119 | gamma_default=100000 |
|---|
| 120 | }; |
|---|
| 121 | |
|---|
| 122 | YsPngHeader hdr; |
|---|
| 123 | YsPngPalette plt; |
|---|
| 124 | YsPngTransparency trns; |
|---|
| 125 | unsigned int gamma; |
|---|
| 126 | |
|---|
| 127 | static unsigned int verboseMode; |
|---|
| 128 | |
|---|
| 129 | YsGenericPngDecoder(); |
|---|
| 130 | void Initialize(void); |
|---|
| 131 | int CheckSignature(FILE *fp); |
|---|
| 132 | int ReadChunk(unsigned &length,unsigned char *&buf,unsigned &chunkType,unsigned &crc,FILE *fp); |
|---|
| 133 | int Decode(const char fn[]); |
|---|
| 134 | |
|---|
| 135 | virtual int PrepareOutput(void); |
|---|
| 136 | virtual int Output(unsigned char dat); |
|---|
| 137 | virtual int EndOutput(void); |
|---|
| 138 | }; |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | //////////////////////////////////////////////////////////// |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | class YsRawPngDecoder : public YsGenericPngDecoder |
|---|
| 146 | { |
|---|
| 147 | public: |
|---|
| 148 | YsRawPngDecoder(); |
|---|
| 149 | ~YsRawPngDecoder(); |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | int wid,hei; |
|---|
| 153 | unsigned char *rgba; // Raw data of R,G,B,A |
|---|
| 154 | int autoDeleteRgbaBuffer; |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | int filter,x,y,firstByte; |
|---|
| 158 | int inLineCount; |
|---|
| 159 | int inPixelCount; |
|---|
| 160 | unsigned int r,g,b,msb; // msb for reading 16 bit depth |
|---|
| 161 | unsigned int index; |
|---|
| 162 | |
|---|
| 163 | unsigned int interlacePass; |
|---|
| 164 | |
|---|
| 165 | // For filtering |
|---|
| 166 | unsigned char *twoLineBuf8,*curLine8,*prvLine8; |
|---|
| 167 | |
|---|
| 168 | void ShiftTwoLineBuf(void); |
|---|
| 169 | |
|---|
| 170 | virtual int PrepareOutput(void); |
|---|
| 171 | virtual int Output(unsigned char dat); |
|---|
| 172 | virtual int EndOutput(void); |
|---|
| 173 | |
|---|
| 174 | void Flip(void); // For drawing in OpenGL |
|---|
| 175 | }; |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | /* } */ |
|---|
| 180 | #endif |
|---|