| 5509 | #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/ |
| 5510 | |
| 5511 | unsigned lodepng_encode(unsigned char** out, size_t* outsize, |
| 5512 | const unsigned char* image, unsigned w, unsigned h, |
| 5513 | LodePNGState* state) |
| 5514 | { |
| 5515 | LodePNGInfo info; |
| 5516 | ucvector outv; |
| 5517 | unsigned char* data = 0; /*uncompressed version of the IDAT chunk data*/ |
| 5518 | size_t datasize = 0; |
| 5519 | |
| 5520 | /*provide some proper output values if error will happen*/ |
| 5521 | *out = 0; |
| 5522 | *outsize = 0; |
| 5523 | state->error = 0; |
| 5524 | |
| 5525 | lodepng_info_init(&info); |
| 5526 | lodepng_info_copy(&info, &state->info_png); |
| 5527 | |
| 5528 | if((info.color.colortype == LCT_PALETTE || state->encoder.force_palette) |
| 5529 | && (info.color.palettesize == 0 || info.color.palettesize > 256)) |
| 5530 | { |
| 5531 | state->error = 68; /*invalid palette size, it is only allowed to be 1-256*/ |
| 5532 | return state->error; |
| 5533 | } |
| 5534 | |
| 5535 | if(state->encoder.auto_convert) |
| 5536 | { |
| 5537 | state->error = lodepng_auto_choose_color(&info.color, image, w, h, &state->info_raw); |
| 5538 | } |
| 5539 | if(state->error) return state->error; |
| 5540 | |
| 5541 | if(state->encoder.zlibsettings.btype > 2) |
| 5542 | { |
| 5543 | CERROR_RETURN_ERROR(state->error, 61); /*error: unexisting btype*/ |
| 5544 | } |
| 5545 | if(state->info_png.interlace_method > 1) |
| 5546 | { |
| 5547 | CERROR_RETURN_ERROR(state->error, 71); /*error: unexisting interlace mode*/ |
| 5548 | } |
| 5549 | |
| 5550 | state->error = checkColorValidity(info.color.colortype, info.color.bitdepth); |
| 5551 | if(state->error) return state->error; /*error: unexisting color type given*/ |
| 5552 | state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth); |
| 5553 | if(state->error) return state->error; /*error: unexisting color type given*/ |
| 5554 | |
| 5555 | if(!lodepng_color_mode_equal(&state->info_raw, &info.color)) |
| 5556 | { |
| 5557 | unsigned char* converted; |
| 5558 | size_t size = (w * h * lodepng_get_bpp(&info.color) + 7) / 8; |
| 5559 | |
| 5560 | converted = (unsigned char*)lodepng_malloc(size); |
| 5561 | if(!converted && size) state->error = 83; /*alloc fail*/ |
| 5562 | if(!state->error) |
| 5563 | { |
| 5564 | state->error = lodepng_convert(converted, image, &info.color, &state->info_raw, w, h); |
| 5565 | } |
| 5566 | if(!state->error) preProcessScanlines(&data, &datasize, converted, w, h, &info, &state->encoder); |
| 5567 | lodepng_free(converted); |
| 5568 | } |
no test coverage detected