Merge pull request #84546 from Rubonnek/fix-jpg-save-bugs

Do not mutate source Image in `Image::save_jpg` and use encoder return value
This commit is contained in:
Rémi Verschelde 2024-01-04 14:26:04 +01:00
commit bfbe145457
No known key found for this signature in database
GPG key ID: C3336907360768E1

View file

@ -162,6 +162,7 @@ static Error _jpgd_save_to_output_stream(jpge::output_stream *p_output_stream, c
ERR_FAIL_COND_V_MSG(error != OK, error, "Couldn't decompress image.");
}
if (image->get_format() != Image::FORMAT_RGB8) {
image = p_img->duplicate();
image->convert(Image::FORMAT_RGB8);
}
@ -173,12 +174,16 @@ static Error _jpgd_save_to_output_stream(jpge::output_stream *p_output_stream, c
const uint8_t *src_data = image->get_data().ptr();
for (int i = 0; i < image->get_height(); i++) {
enc.process_scanline(&src_data[i * image->get_width() * 3]);
if (!enc.process_scanline(&src_data[i * image->get_width() * 3])) {
return FAILED;
}
}
enc.process_scanline(nullptr);
return OK;
if (enc.process_scanline(nullptr)) {
return OK;
} else {
return FAILED;
}
}
static Vector<uint8_t> _jpgd_buffer_save_func(const Ref<Image> &p_img, float p_quality) {