Browse Source

Validate image formats, check if resize_to_po2 failed

Paweł Fertyk 4 years ago
parent
commit
3dae9993ac
3 changed files with 12 additions and 1 deletions
  1. 5 0
      core/io/image.cpp
  2. 3 1
      core/io/image.h
  3. 4 0
      modules/pvr/image_compress_pvrtc.cpp

+ 5 - 0
core/io/image.cpp

@@ -2380,6 +2380,8 @@ Error Image::decompress() {
 }
 }
 
 
 Error Image::compress(CompressMode p_mode, CompressSource p_source, float p_lossy_quality) {
 Error Image::compress(CompressMode p_mode, CompressSource p_source, float p_lossy_quality) {
+	ERR_FAIL_INDEX_V_MSG(p_mode, COMPRESS_MAX, ERR_INVALID_PARAMETER, "Invalid compress mode.");
+	ERR_FAIL_INDEX_V_MSG(p_source, COMPRESS_SOURCE_MAX, ERR_INVALID_PARAMETER, "Invalid compress source.");
 	return compress_from_channels(p_mode, detect_used_channels(p_source), p_lossy_quality);
 	return compress_from_channels(p_mode, detect_used_channels(p_source), p_lossy_quality);
 }
 }
 
 
@@ -2405,6 +2407,9 @@ Error Image::compress_from_channels(CompressMode p_mode, UsedChannels p_channels
 			ERR_FAIL_COND_V(!_image_compress_bptc_func, ERR_UNAVAILABLE);
 			ERR_FAIL_COND_V(!_image_compress_bptc_func, ERR_UNAVAILABLE);
 			_image_compress_bptc_func(this, p_lossy_quality, p_channels);
 			_image_compress_bptc_func(this, p_lossy_quality, p_channels);
 		} break;
 		} break;
+		case COMPRESS_MAX: {
+			ERR_FAIL_V(ERR_INVALID_PARAMETER);
+		} break;
 	}
 	}
 
 
 	return OK;
 	return OK;

+ 3 - 1
core/io/image.h

@@ -336,11 +336,13 @@ public:
 		COMPRESS_ETC,
 		COMPRESS_ETC,
 		COMPRESS_ETC2,
 		COMPRESS_ETC2,
 		COMPRESS_BPTC,
 		COMPRESS_BPTC,
+		COMPRESS_MAX,
 	};
 	};
 	enum CompressSource {
 	enum CompressSource {
 		COMPRESS_SOURCE_GENERIC,
 		COMPRESS_SOURCE_GENERIC,
 		COMPRESS_SOURCE_SRGB,
 		COMPRESS_SOURCE_SRGB,
-		COMPRESS_SOURCE_NORMAL
+		COMPRESS_SOURCE_NORMAL,
+		COMPRESS_SOURCE_MAX,
 	};
 	};
 
 
 	Error compress(CompressMode p_mode, CompressSource p_source = COMPRESS_SOURCE_GENERIC, float p_lossy_quality = 0.7);
 	Error compress(CompressMode p_mode, CompressSource p_source = COMPRESS_SOURCE_GENERIC, float p_lossy_quality = 0.7);

+ 4 - 0
modules/pvr/image_compress_pvrtc.cpp

@@ -43,6 +43,10 @@ static void _compress_pvrtc1_4bpp(Image *p_img) {
 	if (!img->is_size_po2() || img->get_width() != img->get_height()) {
 	if (!img->is_size_po2() || img->get_width() != img->get_height()) {
 		make_mipmaps = img->has_mipmaps();
 		make_mipmaps = img->has_mipmaps();
 		img->resize_to_po2(true);
 		img->resize_to_po2(true);
+		// Resizing can fail for some formats
+		if (!img->is_size_po2() || img->get_width() != img->get_height()) {
+			ERR_FAIL_MSG("Failed to resize the image for compression.");
+		}
 	}
 	}
 	img->convert(Image::FORMAT_RGBA8);
 	img->convert(Image::FORMAT_RGBA8);
 	if (!img->has_mipmaps() && make_mipmaps) {
 	if (!img->has_mipmaps() && make_mipmaps) {