MCPcopy Create free account
hub / github.com/DFHack/dfhack / rgba8ToPixel

Function rgba8ToPixel

depends/lodepng/lodepng.cpp:3167–3218  ·  view source on GitHub ↗

put a pixel, given its RGBA color, into image of any color type*/

Source from the content-addressed store, hash-verified

3165
3166/*put a pixel, given its RGBA color, into image of any color type*/
3167static unsigned rgba8ToPixel(unsigned char* out, size_t i,
3168 const LodePNGColorMode* mode, ColorTree* tree /*for palette*/,
3169 unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
3170 if(mode->colortype == LCT_GREY) {
3171 unsigned char gray = r; /*((unsigned short)r + g + b) / 3u;*/
3172 if(mode->bitdepth == 8) out[i] = gray;
3173 else if(mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = gray;
3174 else {
3175 /*take the most significant bits of gray*/
3176 gray = ((unsigned)gray >> (8u - mode->bitdepth)) & ((1u << mode->bitdepth) - 1u);
3177 addColorBits(out, i, mode->bitdepth, gray);
3178 }
3179 } else if(mode->colortype == LCT_RGB) {
3180 if(mode->bitdepth == 8) {
3181 out[i * 3 + 0] = r;
3182 out[i * 3 + 1] = g;
3183 out[i * 3 + 2] = b;
3184 } else {
3185 out[i * 6 + 0] = out[i * 6 + 1] = r;
3186 out[i * 6 + 2] = out[i * 6 + 3] = g;
3187 out[i * 6 + 4] = out[i * 6 + 5] = b;
3188 }
3189 } else if(mode->colortype == LCT_PALETTE) {
3190 int index = color_tree_get(tree, r, g, b, a);
3191 if(index < 0) return 82; /*color not in palette*/
3192 if(mode->bitdepth == 8) out[i] = index;
3193 else addColorBits(out, i, mode->bitdepth, (unsigned)index);
3194 } else if(mode->colortype == LCT_GREY_ALPHA) {
3195 unsigned char gray = r; /*((unsigned short)r + g + b) / 3u;*/
3196 if(mode->bitdepth == 8) {
3197 out[i * 2 + 0] = gray;
3198 out[i * 2 + 1] = a;
3199 } else if(mode->bitdepth == 16) {
3200 out[i * 4 + 0] = out[i * 4 + 1] = gray;
3201 out[i * 4 + 2] = out[i * 4 + 3] = a;
3202 }
3203 } else if(mode->colortype == LCT_RGBA) {
3204 if(mode->bitdepth == 8) {
3205 out[i * 4 + 0] = r;
3206 out[i * 4 + 1] = g;
3207 out[i * 4 + 2] = b;
3208 out[i * 4 + 3] = a;
3209 } else {
3210 out[i * 8 + 0] = out[i * 8 + 1] = r;
3211 out[i * 8 + 2] = out[i * 8 + 3] = g;
3212 out[i * 8 + 4] = out[i * 8 + 5] = b;
3213 out[i * 8 + 6] = out[i * 8 + 7] = a;
3214 }
3215 }
3216
3217 return 0; /*no error*/
3218}
3219
3220/*put a pixel, given its RGBA16 color, into image of any color 16-bitdepth type*/
3221static void rgba16ToPixel(unsigned char* out, size_t i,

Callers 1

lodepng_convertFunction · 0.85

Calls 2

addColorBitsFunction · 0.85
color_tree_getFunction · 0.85

Tested by

no test coverage detected