| 6566 | #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ |
| 6567 | |
| 6568 | unsigned lodepng_encode(unsigned char** out, size_t* outsize, |
| 6569 | const unsigned char* image, unsigned w, unsigned h, |
| 6570 | LodePNGState* state) { |
| 6571 | unsigned char* data = 0; /*uncompressed version of the IDAT chunk data*/ |
| 6572 | size_t datasize = 0; |
| 6573 | ucvector outv = ucvector_init(NULL, 0); |
| 6574 | LodePNGInfo info; |
| 6575 | const LodePNGInfo* info_png = &state->info_png; |
| 6576 | LodePNGColorMode auto_color; |
| 6577 | |
| 6578 | lodepng_info_init(&info); |
| 6579 | lodepng_color_mode_init(&auto_color); |
| 6580 | |
| 6581 | /*provide some proper output values if error will happen*/ |
| 6582 | *out = 0; |
| 6583 | *outsize = 0; |
| 6584 | state->error = 0; |
| 6585 | |
| 6586 | /*check input values validity*/ |
| 6587 | if((info_png->color.colortype == LCT_PALETTE || state->encoder.force_palette) |
| 6588 | && (info_png->color.palettesize == 0 || info_png->color.palettesize > 256)) { |
| 6589 | /*this error is returned even if auto_convert is enabled and thus encoder could |
| 6590 | generate the palette by itself: while allowing this could be possible in theory, |
| 6591 | it may complicate the code or edge cases, and always requiring to give a palette |
| 6592 | when setting this color type is a simpler contract*/ |
| 6593 | state->error = 68; /*invalid palette size, it is only allowed to be 1-256*/ |
| 6594 | goto cleanup; |
| 6595 | } |
| 6596 | if(state->encoder.zlibsettings.btype > 2) { |
| 6597 | state->error = 61; /*error: invalid btype*/ |
| 6598 | goto cleanup; |
| 6599 | } |
| 6600 | if(info_png->interlace_method > 1) { |
| 6601 | state->error = 71; /*error: invalid interlace mode*/ |
| 6602 | goto cleanup; |
| 6603 | } |
| 6604 | state->error = checkColorValidity(info_png->color.colortype, info_png->color.bitdepth); |
| 6605 | if(state->error) goto cleanup; /*error: invalid color type given*/ |
| 6606 | state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth); |
| 6607 | if(state->error) goto cleanup; /*error: invalid color type given*/ |
| 6608 | |
| 6609 | /* color convert and compute scanline filter types */ |
| 6610 | lodepng_info_copy(&info, &state->info_png); |
| 6611 | if(state->encoder.auto_convert) { |
| 6612 | LodePNGColorStats stats; |
| 6613 | unsigned allow_convert = 1; |
| 6614 | lodepng_color_stats_init(&stats); |
| 6615 | #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS |
| 6616 | if(info_png->iccp_defined && |
| 6617 | isGrayICCProfile(info_png->iccp_profile, info_png->iccp_profile_size)) { |
| 6618 | /*the PNG specification does not allow to use palette with a GRAY ICC profile, even |
| 6619 | if the palette has only gray colors, so disallow it.*/ |
| 6620 | stats.allow_palette = 0; |
| 6621 | } |
| 6622 | if(info_png->iccp_defined && |
| 6623 | isRGBICCProfile(info_png->iccp_profile, info_png->iccp_profile_size)) { |
| 6624 | /*the PNG specification does not allow to use grayscale color with RGB ICC profile, so disallow gray.*/ |
| 6625 | stats.allow_greyscale = 0; |
no test coverage detected