| 4130 | } |
| 4131 | |
| 4132 | static unsigned readChunk_tRNS(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength) |
| 4133 | { |
| 4134 | unsigned i; |
| 4135 | if(color->colortype == LCT_PALETTE) |
| 4136 | { |
| 4137 | /*error: more alpha values given than there are palette entries*/ |
| 4138 | if(chunkLength > color->palettesize) return 38; |
| 4139 | |
| 4140 | for(i = 0; i < chunkLength; i++) color->palette[4 * i + 3] = data[i]; |
| 4141 | } |
| 4142 | else if(color->colortype == LCT_GREY) |
| 4143 | { |
| 4144 | /*error: this chunk must be 2 bytes for greyscale image*/ |
| 4145 | if(chunkLength != 2) return 30; |
| 4146 | |
| 4147 | color->key_defined = 1; |
| 4148 | color->key_r = color->key_g = color->key_b = 256u * data[0] + data[1]; |
| 4149 | } |
| 4150 | else if(color->colortype == LCT_RGB) |
| 4151 | { |
| 4152 | /*error: this chunk must be 6 bytes for RGB image*/ |
| 4153 | if(chunkLength != 6) return 41; |
| 4154 | |
| 4155 | color->key_defined = 1; |
| 4156 | color->key_r = 256u * data[0] + data[1]; |
| 4157 | color->key_g = 256u * data[2] + data[3]; |
| 4158 | color->key_b = 256u * data[4] + data[5]; |
| 4159 | } |
| 4160 | else return 42; /*error: tRNS chunk not allowed for other color models*/ |
| 4161 | |
| 4162 | return 0; /* OK */ |
| 4163 | } |
| 4164 | |
| 4165 | |
| 4166 | #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS |