Repository: godot
Issue: PortableCompressedTexture2D.create_from_image fails in various ways.
Labels: bug,topic:core

### Tested versions

- Reproducible in: 4.2.stable, 4.1.stable

### System information

Godot v4.2.stable.mono - Windows 10.0.19044 - GLES3 (Compatibility) - NVIDIA GeForce GTX 1060 6GB (NVIDIA; 31.0.15.3742) - Intel(R) Core(TM) i5-4670K CPU @ 3.40GHz (4 Threads)

### Issue description

PortableCompressedTexture2D.create_from_image generates errors in basically every configuration.

- LOSSLESS, source image has no mip maps: ⚠ Console shows errors
- LOSSLESS, source image has mip maps: ❌ Texture is unusable
- LOSSY, source image has no mip maps: ⚠ Console shows errors
- LOSSY, source image has mip maps: ❌ Texture is unusable
- BASIS_UNIVERSAL: ⚠ Console shows errors

---------------

## create_from_image LOSSLESS without mip-maps.

⚠ Resulting PortableCompressedTexture2D is valid but multiple errors are generated.

```swift
var img = Image.load_from_file(sheet_path)
var sheet = PortableCompressedTexture2D.new()
sheet.create_from_image(img, PortableCompressedTexture2D.COMPRESSION_MODE_LOSSLESS)
```

> WARNING: Not a PNG file
>      at: check_error (drivers/png/png_driver_common.cpp:56)
> ERROR: Condition "!success" is true. Returning: ERR_FILE_CORRUPT
>    at: png_to_image (drivers/png/png_driver_common.cpp:69)
> ERROR: Condition "err" is true. Returning: Ref<Image>()
>    at: load_mem_png (drivers/png/image_loader_png.cpp:64)
> ERROR: It's not a reference to a valid Image object.
>    at: (./core/io/image.h:433)
> ERROR: Condition "err" is true. Returning: Ref<Image>()
>    at: _jpegd_mem_loader_func (modules/jpg/image_loader_jpegd.cpp:132)
> ERROR: It's not a reference to a valid Image object.
>    at: (./core/io/image.h:433)
> ERROR: Condition "p_width <= 0 || p_width > 16384" is true.
>    at: texture_set_size_override (drivers/gles3/storage/texture_storage.cpp:1117)

## create_from_image LOSSLESS with mip-maps.

❌ Resulting PortableCompressedTexture2D is invalid. 

```swift
var img = Image.load_from_file(sheet_path)
img.generate_mipmaps()
var sheet = PortableCompressedTexture2D.new()
sheet.create_from_image(img, PortableCompressedTexture2D.COMPRESSION_MODE_LOSSLESS)
```

> ERROR: Condition "mipsize < data_size" is true.
>    at: _set_data (scene/resources/portable_compressed_texture.cpp:66)

## create_from_image LOSSY without mip-maps.

⚠ Resulting PortableCompressedTexture2D is valid but multiple errors are generated.

```swift
var img = Image.load_from_file(sheet_path)
var sheet = PortableCompressedTexture2D.new()
sheet.create_from_image(img, PortableCompressedTexture2D.COMPRESSION_MODE_LOSSY, false, 0.98)
```

> WARNING: Not a PNG file
>      at: check_error (drivers/png/png_driver_common.cpp:56)
> ERROR: Condition "!success" is true. Returning: ERR_FILE_CORRUPT
>    at: png_to_image (drivers/png/png_driver_common.cpp:69)
> ERROR: Condition "err" is true. Returning: Ref<Image>()
>    at: load_mem_png (drivers/png/image_loader_png.cpp:64)
> ERROR: It's not a reference to a valid Image object.
>    at: (./core/io/image.h:433)
> ERROR: Condition "err" is true. Returning: Ref<Image>()
>    at: _jpegd_mem_loader_func (modules/jpg/image_loader_jpegd.cpp:132)
> ERROR: It's not a reference to a valid Image object.
>    at: (./core/io/image.h:433)
> ERROR: Condition "p_width <= 0 || p_width > 16384" is true.
>    at: texture_set_size_override (drivers/gles3/storage/texture_storage.cpp:1117)

## create_from_image LOSSY with mip-maps.

❌ Resulting PortableCompressedTexture2D is invalid.

```swift
var img = Image.load_from_file(sheet_path)
img.generate_mipmaps()
var sheet = PortableCompressedTexture2D.new()
sheet.create_from_image(img, PortableCompressedTexture2D.COMPRESSION_MODE_LOSSY, false, 0.98)
```

> ERROR: Condition "mipsize < data_size" is true.
>   at: _set_data (scene/resources/portable_compressed_texture.cpp:66)

## create_from_image BASIS_UNIVERSAL without mip-maps.

⚠ Resulting PortableCompressedTexture2D is usable, but errors are generated.

```swift
var img = Image.load_from_file(sheet_path)
var sheet = PortableCompressedTexture2D.new()
sheet.create_from_image(img, PortableCompressedTexture2D.COMPRESSION_MODE_BASIS_UNIVERSAL, false, 1.0)
```
> Total basis file slices: 1
> Slice: 0, alpha: 1, orig width/height: 91x78, width/height: 92x80, first_block: 0, image_index: 0, mip_level: 0, iframe: 0
> Basis universal cannot unpack level 1.
> ERROR: Condition "p_width <= 0 || p_width > 16384" is true.
>   at: texture_set_size_override (drivers/gles3/storage/texture_storage.cpp:1117)

## create_from_image BASIS_UNIVERSAL with mip-maps.

⚠ Resulting PortableCompressedTexture2D is usable, but errors are generated.

```swift
var img = Image.load_from_file(sheet_path)
img.generate_mipmaps()
var sheet = PortableCompressedTexture2D.new()
sheet.create_from_image(img, PortableCompressedTexture2D.COMPRESSION_MODE_BASIS_UNIVERSAL, false, 1.0)
```

> Total basis file slices: 7
> Slice: 0, alpha: 1, orig width/height: 91x78, width/height: 92x80, first_block: 0, image_index: 0, mip_level: 0, iframe: 0
> Slice: 1, alpha: 1, orig width/height: 45x39, width/height: 48x40, first_block: 460, image_index: 0, mip_level: 1, iframe: 0
> Slice: 2, alpha: 1, orig width/height: 22x19, width/height: 24x20, first_block: 580, image_index: 0, mip_level: 2, iframe: 0
> Slice: 3, alpha: 1, orig width/height: 11x9, width/height: 12x12, first_block: 610, image_index: 0, mip_level: 3, iframe: 0
> Slice: 4, alpha: 1, orig width/height: 5x4, width/height: 8x4, first_block: 619, image_index: 0, mip_level: 4, iframe: 0
> Slice: 5, alpha: 1, orig width/height: 2x2, width/height: 4x4, first_block: 621, image_index: 0, mip_level: 5, iframe: 0
> Slice: 6, alpha: 1, orig width/height: 1x1, width/height: 4x4, first_block: 622, image_index: 0, mip_level: 6, iframe: 0
> ERROR: Condition "p_width <= 0 || p_width > 16384" is true.
>    at: texture_set_size_override (drivers/gles3/storage/texture_storage.cpp:1117)

### Minimal reproduction project (MRP)

[PortableCompressedTexture2D.zip](https://github.com/godotengine/godot/files/13830864/PortableCompressedTexture2D.zip)
^ Just open in Godot, the script is automatically run as toolscript.