Do not mutate source Image in Image::save_jpg and use encoder return value

This commit is contained in:
Wilson E. Alvarez 2023-11-06 14:38:42 -05:00
parent 4c96e9676b
commit 62cbd6805c
No known key found for this signature in database
GPG Key ID: A32174A3D2ED3F9E
1 changed files with 9 additions and 4 deletions

View File

@ -158,6 +158,7 @@ static Error _jpgd_save_to_output_stream(jpge::output_stream *p_output_stream, c
ERR_FAIL_COND_V(p_img.is_null() || p_img->is_empty(), ERR_INVALID_PARAMETER);
Ref<Image> image = p_img;
if (image->get_format() != Image::FORMAT_RGB8) {
image = p_img->duplicate();
image->convert(Image::FORMAT_RGB8);
}
@ -169,12 +170,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) {