| 5632 | #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ |
| 5633 | |
| 5634 | unsigned lodepng_encode(unsigned char** out, size_t* outsize, |
| 5635 | const unsigned char* image, unsigned w, unsigned h, |
| 5636 | LodePNGState* state) |
| 5637 | { |
| 5638 | LodePNGInfo info; |
| 5639 | ucvector outv; |
| 5640 | unsigned char* data = 0; /*uncompressed version of the IDAT chunk data*/ |
| 5641 | size_t datasize = 0; |
| 5642 | |
| 5643 | /*provide some proper output values if error will happen*/ |
| 5644 | *out = 0; |
| 5645 | *outsize = 0; |
| 5646 | state->error = 0; |
| 5647 | |
| 5648 | lodepng_info_init(&info); |
| 5649 | lodepng_info_copy(&info, &state->info_png); |
| 5650 | |
| 5651 | if((info.color.colortype == LCT_PALETTE || state->encoder.force_palette) |
| 5652 | && (info.color.palettesize == 0 || info.color.palettesize > 256)) |
| 5653 | { |
| 5654 | state->error = 68; /*invalid palette size, it is only allowed to be 1-256*/ |
| 5655 | return state->error; |
| 5656 | } |
| 5657 | |
| 5658 | if(state->encoder.auto_convert) |
| 5659 | { |
| 5660 | state->error = lodepng_auto_choose_color(&info.color, image, w, h, &state->info_raw); |
| 5661 | } |
| 5662 | if(state->error) return state->error; |
| 5663 | |
| 5664 | if(state->encoder.zlibsettings.btype > 2) |
| 5665 | { |
| 5666 | CERROR_RETURN_ERROR(state->error, 61); /*error: unexisting btype*/ |
| 5667 | } |
| 5668 | if(state->info_png.interlace_method > 1) |
| 5669 | { |
| 5670 | CERROR_RETURN_ERROR(state->error, 71); /*error: unexisting interlace mode*/ |
| 5671 | } |
| 5672 | |
| 5673 | state->error = checkColorValidity(info.color.colortype, info.color.bitdepth); |
| 5674 | if(state->error) return state->error; /*error: unexisting color type given*/ |
| 5675 | state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth); |
| 5676 | if(state->error) return state->error; /*error: unexisting color type given*/ |
| 5677 | |
| 5678 | if(!lodepng_color_mode_equal(&state->info_raw, &info.color)) |
| 5679 | { |
| 5680 | unsigned char* converted; |
| 5681 | size_t size = (w * h * (size_t)lodepng_get_bpp(&info.color) + 7) / 8; |
| 5682 | |
| 5683 | converted = (unsigned char*)lodepng_malloc(size); |
| 5684 | if(!converted && size) state->error = 83; /*alloc fail*/ |
| 5685 | if(!state->error) |
| 5686 | { |
| 5687 | state->error = lodepng_convert(converted, image, &info.color, &state->info_raw, w, h); |
| 5688 | } |
| 5689 | if(!state->error) preProcessScanlines(&data, &datasize, converted, w, h, &info, &state->encoder); |
| 5690 | lodepng_free(converted); |
| 5691 | } |
no test coverage detected