Bladeren bron

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

Wilson E. Alvarez 1 jaar geleden
bovenliggende
commit
62cbd6805c
1 gewijzigde bestanden met toevoegingen van 9 en 4 verwijderingen
  1. 9 4
      modules/jpg/image_loader_jpegd.cpp

+ 9 - 4
modules/jpg/image_loader_jpegd.cpp

@@ -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) {