put a pixel, given its RGBA color, into image of any color type*/
| 3064 | |
| 3065 | /*put a pixel, given its RGBA color, into image of any color type*/ |
| 3066 | static unsigned rgba8ToPixel(unsigned char* out, size_t i, |
| 3067 | const LodePNGColorMode* mode, ColorTree* tree /*for palette*/, |
| 3068 | unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 3069 | { |
| 3070 | if(mode->colortype == LCT_GREY) |
| 3071 | { |
| 3072 | unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/; |
| 3073 | if(mode->bitdepth == 8) out[i] = grey; |
| 3074 | else if(mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = grey; |
| 3075 | else |
| 3076 | { |
| 3077 | /*take the most significant bits of grey*/ |
| 3078 | grey = (grey >> (8 - mode->bitdepth)) & ((1 << mode->bitdepth) - 1); |
| 3079 | addColorBits(out, i, mode->bitdepth, grey); |
| 3080 | } |
| 3081 | } |
| 3082 | else if(mode->colortype == LCT_RGB) |
| 3083 | { |
| 3084 | if(mode->bitdepth == 8) |
| 3085 | { |
| 3086 | out[i * 3 + 0] = r; |
| 3087 | out[i * 3 + 1] = g; |
| 3088 | out[i * 3 + 2] = b; |
| 3089 | } |
| 3090 | else |
| 3091 | { |
| 3092 | out[i * 6 + 0] = out[i * 6 + 1] = r; |
| 3093 | out[i * 6 + 2] = out[i * 6 + 3] = g; |
| 3094 | out[i * 6 + 4] = out[i * 6 + 5] = b; |
| 3095 | } |
| 3096 | } |
| 3097 | else if(mode->colortype == LCT_PALETTE) |
| 3098 | { |
| 3099 | int index = color_tree_get(tree, r, g, b, a); |
| 3100 | if(index < 0) return 82; /*color not in palette*/ |
| 3101 | if(mode->bitdepth == 8) out[i] = index; |
| 3102 | else addColorBits(out, i, mode->bitdepth, (unsigned)index); |
| 3103 | } |
| 3104 | else if(mode->colortype == LCT_GREY_ALPHA) |
| 3105 | { |
| 3106 | unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/; |
| 3107 | if(mode->bitdepth == 8) |
| 3108 | { |
| 3109 | out[i * 2 + 0] = grey; |
| 3110 | out[i * 2 + 1] = a; |
| 3111 | } |
| 3112 | else if(mode->bitdepth == 16) |
| 3113 | { |
| 3114 | out[i * 4 + 0] = out[i * 4 + 1] = grey; |
| 3115 | out[i * 4 + 2] = out[i * 4 + 3] = a; |
| 3116 | } |
| 3117 | } |
| 3118 | else if(mode->colortype == LCT_RGBA) |
| 3119 | { |
| 3120 | if(mode->bitdepth == 8) |
| 3121 | { |
| 3122 | out[i * 4 + 0] = r; |
| 3123 | out[i * 4 + 1] = g; |
no test coverage detected