converts from any color type to 24-bit or 32-bit (later maybe more supported). return value = LodePNG error code the out buffer must have (w * h * bpp + 7) / 8 bytes, where bpp is the bits per pixel of the output color type (lodepng_get_bpp) for < 8 bpp images, there may _not_ be padding bits at the end of scanlines. */
| 3391 | (lodepng_get_bpp) for < 8 bpp images, there may _not_ be padding bits at the end of scanlines. |
| 3392 | */ |
| 3393 | unsigned lodepng_convert(unsigned char* out, const unsigned char* in, |
| 3394 | LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in, |
| 3395 | unsigned w, unsigned h, unsigned fix_png) |
| 3396 | { |
| 3397 | unsigned error = 0; |
| 3398 | size_t i; |
| 3399 | ColorTree tree; |
| 3400 | size_t numpixels = w * h; |
| 3401 | |
| 3402 | if(lodepng_color_mode_equal(mode_out, mode_in)) |
| 3403 | { |
| 3404 | size_t numbytes = lodepng_get_raw_size(w, h, mode_in); |
| 3405 | for(i = 0; i < numbytes; i++) out[i] = in[i]; |
| 3406 | return error; |
| 3407 | } |
| 3408 | |
| 3409 | if(mode_out->colortype == LCT_PALETTE) |
| 3410 | { |
| 3411 | size_t palsize = 1 << mode_out->bitdepth; |
| 3412 | if(mode_out->palettesize < palsize) palsize = mode_out->palettesize; |
| 3413 | color_tree_init(&tree); |
| 3414 | for(i = 0; i < palsize; i++) |
| 3415 | { |
| 3416 | unsigned char* p = &mode_out->palette[i * 4]; |
| 3417 | color_tree_add(&tree, p[0], p[1], p[2], p[3], i); |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | if(mode_in->bitdepth == 16 && mode_out->bitdepth == 16) |
| 3422 | { |
| 3423 | for(i = 0; i < numpixels; i++) |
| 3424 | { |
| 3425 | unsigned short r = 0, g = 0, b = 0, a = 0; |
| 3426 | error = getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in); |
| 3427 | if(error) break; |
| 3428 | error = rgba16ToPixel(out, i, mode_out, r, g, b, a); |
| 3429 | if(error) break; |
| 3430 | } |
| 3431 | } |
| 3432 | else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGBA) |
| 3433 | { |
| 3434 | error = getPixelColorsRGBA8(out, numpixels, 1, in, mode_in, fix_png); |
| 3435 | } |
| 3436 | else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGB) |
| 3437 | { |
| 3438 | error = getPixelColorsRGBA8(out, numpixels, 0, in, mode_in, fix_png); |
| 3439 | } |
| 3440 | else |
| 3441 | { |
| 3442 | unsigned char r = 0, g = 0, b = 0, a = 0; |
| 3443 | for(i = 0; i < numpixels; i++) |
| 3444 | { |
| 3445 | error = getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in, fix_png); |
| 3446 | if(error) break; |
| 3447 | error = rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a); |
| 3448 | if(error) break; |
| 3449 | } |
| 3450 | } |
no test coverage detected