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

Function getPixelColorRGBA8

depends/lodepng/lodepng.cpp:3254–3322  ·  view source on GitHub ↗

Get RGBA8 color of pixel with index i (y * width + x) from the raw image with given color type.*/

Source from the content-addressed store, hash-verified

3252
3253/*Get RGBA8 color of pixel with index i (y * width + x) from the raw image with given color type.*/
3254static void getPixelColorRGBA8(unsigned char* r, unsigned char* g,
3255 unsigned char* b, unsigned char* a,
3256 const unsigned char* in, size_t i,
3257 const LodePNGColorMode* mode) {
3258 if(mode->colortype == LCT_GREY) {
3259 if(mode->bitdepth == 8) {
3260 *r = *g = *b = in[i];
3261 if(mode->key_defined && *r == mode->key_r) *a = 0;
3262 else *a = 255;
3263 } else if(mode->bitdepth == 16) {
3264 *r = *g = *b = in[i * 2 + 0];
3265 if(mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0;
3266 else *a = 255;
3267 } else {
3268 unsigned highest = ((1U << mode->bitdepth) - 1U); /*highest possible value for this bit depth*/
3269 size_t j = i * mode->bitdepth;
3270 unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth);
3271 *r = *g = *b = (value * 255) / highest;
3272 if(mode->key_defined && value == mode->key_r) *a = 0;
3273 else *a = 255;
3274 }
3275 } else if(mode->colortype == LCT_RGB) {
3276 if(mode->bitdepth == 8) {
3277 *r = in[i * 3 + 0]; *g = in[i * 3 + 1]; *b = in[i * 3 + 2];
3278 if(mode->key_defined && *r == mode->key_r && *g == mode->key_g && *b == mode->key_b) *a = 0;
3279 else *a = 255;
3280 } else {
3281 *r = in[i * 6 + 0];
3282 *g = in[i * 6 + 2];
3283 *b = in[i * 6 + 4];
3284 if(mode->key_defined && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
3285 && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g
3286 && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0;
3287 else *a = 255;
3288 }
3289 } else if(mode->colortype == LCT_PALETTE) {
3290 unsigned index;
3291 if(mode->bitdepth == 8) index = in[i];
3292 else {
3293 size_t j = i * mode->bitdepth;
3294 index = readBitsFromReversedStream(&j, in, mode->bitdepth);
3295 }
3296 /*out of bounds of palette not checked: see lodepng_color_mode_alloc_palette.*/
3297 *r = mode->palette[index * 4 + 0];
3298 *g = mode->palette[index * 4 + 1];
3299 *b = mode->palette[index * 4 + 2];
3300 *a = mode->palette[index * 4 + 3];
3301 } else if(mode->colortype == LCT_GREY_ALPHA) {
3302 if(mode->bitdepth == 8) {
3303 *r = *g = *b = in[i * 2 + 0];
3304 *a = in[i * 2 + 1];
3305 } else {
3306 *r = *g = *b = in[i * 4 + 0];
3307 *a = in[i * 4 + 2];
3308 }
3309 } else if(mode->colortype == LCT_RGBA) {
3310 if(mode->bitdepth == 8) {
3311 *r = in[i * 4 + 0];

Callers 2

lodepng_convertFunction · 0.85

Calls 1

Tested by

no test coverage detected