فهرست منبع

Added error to catch conversion on invalid image

(cherry picked from commit dec2269bca8c616dac73ec987f7e670cf0451c77)
rsburke4 1 سال پیش
والد
کامیت
c9924b11a4
2فایلهای تغییر یافته به همراه25 افزوده شده و 0 حذف شده
  1. 1 0
      core/io/image.cpp
  2. 24 0
      tests/core/io/test_image.h

+ 1 - 0
core/io/image.cpp

@@ -509,6 +509,7 @@ static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p
 }
 }
 
 
 void Image::convert(Format p_new_format) {
 void Image::convert(Format p_new_format) {
+	ERR_FAIL_INDEX_MSG(p_new_format, FORMAT_MAX, "The Image format specified (" + itos(p_new_format) + ") is out of range. See Image's Format enum.");
 	if (data.size() == 0) {
 	if (data.size() == 0) {
 		return;
 		return;
 	}
 	}

+ 24 - 0
tests/core/io/test_image.h

@@ -323,6 +323,30 @@ TEST_CASE("[Image] Modifying pixels of an image") {
 		CHECK_MESSAGE(gray_image->get_pixel(2, 2).is_equal_approx(Color(0.266666681, 0.266666681, 0.266666681, 1)), "convert() RGBA to L8 should be around 0.266666681 (68).");
 		CHECK_MESSAGE(gray_image->get_pixel(2, 2).is_equal_approx(Color(0.266666681, 0.266666681, 0.266666681, 1)), "convert() RGBA to L8 should be around 0.266666681 (68).");
 	}
 	}
 }
 }
+
+TEST_CASE("[Image] Convert image") {
+	for (int format = Image::FORMAT_RF; format < Image::FORMAT_RGBE9995; format++) {
+		for (int new_format = Image::FORMAT_RF; new_format < Image::FORMAT_RGBE9995; new_format++) {
+			Ref<Image> image = memnew(Image(4, 4, false, (Image::Format)format));
+			image->convert((Image::Format)new_format);
+			String format_string = Image::format_names[(Image::Format)format];
+			String new_format_string = Image::format_names[(Image::Format)new_format];
+			format_string = "Error converting from " + format_string + " to " + new_format_string + ".";
+			CHECK_MESSAGE(image->get_format() == new_format, format_string);
+		}
+	}
+
+	Ref<Image> image = memnew(Image(4, 4, false, Image::FORMAT_RGBA8));
+	PackedByteArray image_data = image->get_data();
+	image->convert((Image::Format)-1);
+	CHECK_MESSAGE(image->get_data() == image_data, "Image conversion to invalid type (-1) should not alter image.");
+
+	Ref<Image> image2 = memnew(Image(4, 4, false, Image::FORMAT_RGBA8));
+	image_data = image2->get_data();
+	image2->convert((Image::Format)(Image::FORMAT_MAX + 1));
+	CHECK_MESSAGE(image2->get_data() == image_data, "Image conversion to invalid type (Image::FORMAT_MAX + 1) should not alter image.");
+}
+
 } // namespace TestImage
 } // namespace TestImage
 
 
 #endif // TEST_IMAGE_H
 #endif // TEST_IMAGE_H