| 5812 | #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ |
| 5813 | |
| 5814 | unsigned lodepng_encode(unsigned char** out, size_t* outsize, |
| 5815 | const unsigned char* image, unsigned w, unsigned h, |
| 5816 | LodePNGState* state) { |
| 5817 | unsigned char* data = 0; /*uncompressed version of the IDAT chunk data*/ |
| 5818 | size_t datasize = 0; |
| 5819 | ucvector outv = ucvector_init(NULL, 0); |
| 5820 | LodePNGInfo info; |
| 5821 | const LodePNGInfo* info_png = &state->info_png; |
| 5822 | |
| 5823 | lodepng_info_init(&info); |
| 5824 | |
| 5825 | /*provide some proper output values if error will happen*/ |
| 5826 | *out = 0; |
| 5827 | *outsize = 0; |
| 5828 | state->error = 0; |
| 5829 | |
| 5830 | /*check input values validity*/ |
| 5831 | if((info_png->color.colortype == LCT_PALETTE || state->encoder.force_palette) |
| 5832 | && (info_png->color.palettesize == 0 || info_png->color.palettesize > 256)) { |
| 5833 | state->error = 68; /*invalid palette size, it is only allowed to be 1-256*/ |
| 5834 | goto cleanup; |
| 5835 | } |
| 5836 | if(state->encoder.zlibsettings.btype > 2) { |
| 5837 | state->error = 61; /*error: invalid btype*/ |
| 5838 | goto cleanup; |
| 5839 | } |
| 5840 | if(info_png->interlace_method > 1) { |
| 5841 | state->error = 71; /*error: invalid interlace mode*/ |
| 5842 | goto cleanup; |
| 5843 | } |
| 5844 | state->error = checkColorValidity(info_png->color.colortype, info_png->color.bitdepth); |
| 5845 | if(state->error) goto cleanup; /*error: invalid color type given*/ |
| 5846 | state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth); |
| 5847 | if(state->error) goto cleanup; /*error: invalid color type given*/ |
| 5848 | |
| 5849 | /* color convert and compute scanline filter types */ |
| 5850 | lodepng_info_copy(&info, &state->info_png); |
| 5851 | if(state->encoder.auto_convert) { |
| 5852 | LodePNGColorStats stats; |
| 5853 | lodepng_color_stats_init(&stats); |
| 5854 | #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS |
| 5855 | if(info_png->iccp_defined && |
| 5856 | isGrayICCProfile(info_png->iccp_profile, info_png->iccp_profile_size)) { |
| 5857 | /*the PNG specification does not allow to use palette with a GRAY ICC profile, even |
| 5858 | if the palette has only gray colors, so disallow it.*/ |
| 5859 | stats.allow_palette = 0; |
| 5860 | } |
| 5861 | if(info_png->iccp_defined && |
| 5862 | isRGBICCProfile(info_png->iccp_profile, info_png->iccp_profile_size)) { |
| 5863 | /*the PNG specification does not allow to use grayscale color with RGB ICC profile, so disallow gray.*/ |
| 5864 | stats.allow_greyscale = 0; |
| 5865 | } |
| 5866 | #endif /* LODEPNG_COMPILE_ANCILLARY_CHUNKS */ |
| 5867 | state->error = lodepng_compute_color_stats(&stats, image, w, h, &state->info_raw); |
| 5868 | if(state->error) goto cleanup; |
| 5869 | #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS |
| 5870 | if(info_png->background_defined) { |
| 5871 | /*the background chunk's color must be taken into account as well*/ |
no test coverage detected