Repository: godotengine/godot (language: cpp)

Issue:
Unexpected image buffer size when creating PortableCompressedTexture2D from Image
### Tested versions

- Reproducible in v4.5.dev [1f56d96cf], 4.4.1.stable [96579d139]

### System information

Windows 11 Vulkan (Forward+)

### Issue description

When trying to create a PortableCompressedTexture2D from an Image, the following error shows when using compressed formats.
```
  ERROR: Expected Image data size of 512x512x3 (RGB8 without mipmaps) = 786432 bytes, got 131072 bytes instead.
  ERROR: Width must be equal or greater than 1 for all textures
```
If `COMPRESSION_MODE_LOSSLESS `is used, there's no error displayed. Most probably, what's happening is that the Image data buffer is compressed here:
https://github.com/godotengine/godot/blob/1f56d96cf2c768c3844c68ccb504dfeee841ae15/scene/resources/portable_compressed_texture.cpp#L182-L204
So the buffer is no longer of the expected size, since it is compressed.

### Steps to reproduce

- Open the MRP.
- Open the script `test.gd`
- Then run the script with File > Run

### Minimal reproduction project (MRP)

[test.zip](https://github.com/user-attachments/files/19578958/test.zip)


Discussion / hints:

Fix patch:
diff --git a/scene/resources/portable_compressed_texture.cpp b/scene/resources/portable_compressed_texture.cpp
index 99317af5a113..32c7a0965e6a 100644
--- a/scene/resources/portable_compressed_texture.cpp
+++ b/scene/resources/portable_compressed_texture.cpp
@@ -171,6 +171,7 @@ void PortableCompressedTexture2D::create_from_image(const Ref<Image> &p_image, C
 			}
 		} break;
 		case COMPRESSION_MODE_BASIS_UNIVERSAL: {
+			ERR_FAIL_COND(p_image->is_compressed());
 			encode_uint16(DATA_FORMAT_BASIS_UNIVERSAL, buffer.ptrw() + 2);
 			Image::UsedChannels uc = p_image->detect_used_channels(p_normal_map ? Image::COMPRESS_SOURCE_NORMAL : Image::COMPRESS_SOURCE_GENERIC);
 			Vector<uint8_t> budata = Image::basis_universal_packer(p_image, uc);
@@ -180,6 +181,7 @@ void PortableCompressedTexture2D::create_from_image(const Ref<Image> &p_image, C
 		case COMPRESSION_MODE_S3TC:
 		case COMPRESSION_MODE_ETC2:
 		case COMPRESSION_MODE_BPTC: {
+			ERR_FAIL_COND(p_image->is_compressed());
 			encode_uint16(DATA_FORMAT_IMAGE, buffer.ptrw() + 2);
 			Ref<Image> copy = p_image->duplicate();
 			switch (p_compression_mode) {
@@ -195,7 +197,7 @@ void PortableCompressedTexture2D::create_from_image(const Ref<Image> &p_image, C
 				default: {
 				};
 			}
-
+			encode_uint32(copy->get_format(), buffer.ptrw() + 4);
 			buffer.append_array(copy->get_data());
 
 		} break;


Test patch: