put a pixel, given its RGBA color, into image of any color type*/
| 3015 | |
| 3016 | /*put a pixel, given its RGBA color, into image of any color type*/ |
| 3017 | static unsigned rgba8ToPixel(unsigned char* out, size_t i, |
| 3018 | const LodePNGColorMode* mode, ColorTree* tree /*for palette*/, |
| 3019 | unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 3020 | { |
| 3021 | if(mode->colortype == LCT_GREY) |
| 3022 | { |
| 3023 | unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/; |
| 3024 | if(mode->bitdepth == 8) out[i] = grey; |
| 3025 | else if(mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = grey; |
| 3026 | else |
| 3027 | { |
| 3028 | /*take the most significant bits of grey*/ |
| 3029 | grey = (grey >> (8 - mode->bitdepth)) & ((1 << mode->bitdepth) - 1); |
| 3030 | addColorBits(out, i, mode->bitdepth, grey); |
| 3031 | } |
| 3032 | } |
| 3033 | else if(mode->colortype == LCT_RGB) |
| 3034 | { |
| 3035 | if(mode->bitdepth == 8) |
| 3036 | { |
| 3037 | out[i * 3 + 0] = r; |
| 3038 | out[i * 3 + 1] = g; |
| 3039 | out[i * 3 + 2] = b; |
| 3040 | } |
| 3041 | else |
| 3042 | { |
| 3043 | out[i * 6 + 0] = out[i * 6 + 1] = r; |
| 3044 | out[i * 6 + 2] = out[i * 6 + 3] = g; |
| 3045 | out[i * 6 + 4] = out[i * 6 + 5] = b; |
| 3046 | } |
| 3047 | } |
| 3048 | else if(mode->colortype == LCT_PALETTE) |
| 3049 | { |
| 3050 | int index = color_tree_get(tree, r, g, b, a); |
| 3051 | if(index < 0) return 82; /*color not in palette*/ |
| 3052 | if(mode->bitdepth == 8) out[i] = index; |
| 3053 | else addColorBits(out, i, mode->bitdepth, (unsigned)index); |
| 3054 | } |
| 3055 | else if(mode->colortype == LCT_GREY_ALPHA) |
| 3056 | { |
| 3057 | unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/; |
| 3058 | if(mode->bitdepth == 8) |
| 3059 | { |
| 3060 | out[i * 2 + 0] = grey; |
| 3061 | out[i * 2 + 1] = a; |
| 3062 | } |
| 3063 | else if(mode->bitdepth == 16) |
| 3064 | { |
| 3065 | out[i * 4 + 0] = out[i * 4 + 1] = grey; |
| 3066 | out[i * 4 + 2] = out[i * 4 + 3] = a; |
| 3067 | } |
| 3068 | } |
| 3069 | else if(mode->colortype == LCT_RGBA) |
| 3070 | { |
| 3071 | if(mode->bitdepth == 8) |
| 3072 | { |
| 3073 | out[i * 4 + 0] = r; |
| 3074 | out[i * 4 + 1] = g; |
no test coverage detected