Blender V5.3
ktx_bridge/intern/decoder.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "decoder.h"
6#include "common.h"
7
8int decodeFromFile(const char *filename, ktxTexture2 **newTex)
9{
10 ktx_error_code_e result = ktxTexture2_CreateFromNamedFile(
11 filename, KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, newTex);
12 if (result != KTX_SUCCESS) {
13 return result;
14 }
15
16 /* Because KHR_texture_basisu requires ETC1S or UASTC encoding,
17 * a transcode is always necessary for glTF usage. */
18
19 if (ktxTexture2_NeedsTranscoding(*newTex)) {
20 result = ktxTexture2_TranscodeBasis(*newTex, KTX_TTF_RGBA32, 0);
21 if (result != KTX_SUCCESS) {
22 ktxTexture2_Destroy(*newTex);
23 *newTex = nullptr;
24 return result;
25 }
26 }
27
28 /* When transcoded, the texture is in RGBA8
29 * In glTF KHR_texture_basisu context, images are always transcoded
30 * Adding a sanity check here, in case the glTF extension specification is not followed
31 * correctly */
32 if ((*newTex)->vkFormat != VK_FORMAT_R8G8B8A8_UNORM &&
33 (*newTex)->vkFormat != VK_FORMAT_R8G8B8A8_SRGB)
34 {
35 ktxTexture2_Destroy(*newTex);
36 *newTex = nullptr;
37 return KTX_UNSUPPORTED_FEATURE;
38 }
39
40 return KTX_SUCCESS;
41}
42
43int getImageInfo(ktxTexture2 *texture, unsigned int *width, unsigned int *height, size_t *dataSize)
44{
45 if (texture == nullptr) {
46 return KTX_INVALID_VALUE;
47 }
48
49 *width = texture->baseWidth;
50 *height = texture->baseHeight;
51 *dataSize = ktxTexture_GetImageSize(ktxTexture(texture), 0);
52
53 return KTX_SUCCESS;
54}
55
56int getImageDataFloat(ktxTexture2 *texture, float *destination, size_t destinationSize)
57{
58 if (texture == nullptr) {
59 return KTX_INVALID_VALUE;
60 }
61
62 /* In glTF KHR_texture_basisu context, images are always transcoded
63 * So we should have a RGBA8 image here
64 * Adding a sanity check here, in case the glTF extension specification is not followed
65 * correctly */
66 if (texture->vkFormat != VK_FORMAT_R8G8B8A8_UNORM &&
68 {
69 return KTX_UNSUPPORTED_FEATURE;
70 }
71
72 const ktx_size_t dataSize = ktxTexture_GetImageSize(ktxTexture(texture), 0);
73 if (destinationSize < dataSize * sizeof(float)) {
74 return KTX_INVALID_VALUE;
75 }
76
77 /* Retrieve the offset of the highest resolution mip level, first array layer, and first face */
78 ktx_size_t offset;
79 if (ktxTexture_GetImageOffset(ktxTexture(texture), 0, 0, 0, &offset) != KTX_SUCCESS) {
80 return KTX_INVALID_OPERATION;
81 }
82 const ktx_uint8_t *src = ktxTexture_GetData(ktxTexture(texture)) + offset;
83 const ktx_uint32_t rowPitch = ktxTexture_GetRowPitch(ktxTexture(texture), 0);
84 const ktx_uint32_t height = texture->baseHeight;
85
86 /* KTX stores rows top-to-bottom; Blender's `Image.pixels` expects bottom-to-top. */
87 for (ktx_uint32_t y = 0; y < height; y++) {
88 const ktx_uint8_t *srcRow = src + (ktx_size_t)y * rowPitch;
89 float *dstRow = destination + (ktx_size_t)(height - 1 - y) * rowPitch;
90 for (ktx_uint32_t x = 0; x < rowPitch; x++) {
91 dstRow[x] = srcRow[x] / 255.0f;
92 }
93 }
94
95 return KTX_SUCCESS;
96}
97
98void destroyTexture(ktxTexture2 *texture)
99{
100 if (texture != nullptr) {
101 ktxTexture2_Destroy(texture);
102 }
103}
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
#define VK_FORMAT_R8G8B8A8_UNORM
#define VK_FORMAT_R8G8B8A8_SRGB
int getImageDataFloat(ktxTexture2 *texture, float *destination, size_t destinationSize)
void destroyTexture(ktxTexture2 *texture)
int decodeFromFile(const char *filename, ktxTexture2 **newTex)
int getImageInfo(ktxTexture2 *texture, unsigned int *width, unsigned int *height, size_t *dataSize)