From 5eea92e9208a028cc1a64b24faaee538166b030d Mon Sep 17 00:00:00 2001 From: Marco Hutter Date: Fri, 22 Mar 2024 18:15:03 +0100 Subject: [PATCH 1/7] Properly convert Java char to C char in inputSwizzle --- .../java_binding/src/main/cpp/libktx-jni.cpp | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/interface/java_binding/src/main/cpp/libktx-jni.cpp b/interface/java_binding/src/main/cpp/libktx-jni.cpp index d88233b989..150be38fca 100644 --- a/interface/java_binding/src/main/cpp/libktx-jni.cpp +++ b/interface/java_binding/src/main/cpp/libktx-jni.cpp @@ -97,12 +97,14 @@ void copy_ktx_astc_params(JNIEnv *env, jobject params, ktxAstcParams &out) out.normalMap = env->GetBooleanField(params, normalMap); out.perceptual = env->GetBooleanField(params, perceptual); - env->GetByteArrayRegion( - static_cast(env->GetObjectField(params, inputSwizzle)), - 0, - 4, - reinterpret_cast(&out.inputSwizzle) - ); + jobject inputSwizzleObject = env->GetObjectField(params, inputSwizzle); + jcharArray inputSwizzleArray = static_cast(inputSwizzleObject); + jchar *inputSwizzleValues = env->GetCharArrayElements( inputSwizzleArray, NULL); + for (int i=0; i<4; i++) + { + out.inputSwizzle[i] = static_cast(inputSwizzleValues[i]); + } + env->ReleaseCharArrayElements(inputSwizzleArray, inputSwizzleValues, JNI_ABORT); } void copy_ktx_basis_params(JNIEnv *env, jobject params, ktxBasisParams &out) @@ -145,12 +147,16 @@ void copy_ktx_basis_params(JNIEnv *env, jobject params, ktxBasisParams &out) out.endpointRDOThreshold = env->GetFloatField(params, endpointRDOThreshold); out.maxSelectors = env->GetIntField(params, maxSelectors); out.selectorRDOThreshold = env->GetFloatField(params, selectorRDOThreshold); - env->GetByteArrayRegion( - static_cast(env->GetObjectField(params, inputSwizzle)), - 0, - 4, - reinterpret_cast(&out.inputSwizzle) - ); + + jobject inputSwizzleObject = env->GetObjectField(params, inputSwizzle); + jcharArray inputSwizzleArray = static_cast(inputSwizzleObject); + jchar *inputSwizzleValues = env->GetCharArrayElements( inputSwizzleArray, NULL); + for (int i=0; i<4; i++) + { + out.inputSwizzle[i] = static_cast(inputSwizzleValues[i]); + } + env->ReleaseCharArrayElements(inputSwizzleArray, inputSwizzleValues, JNI_ABORT); + out.normalMap = env->GetBooleanField(params, normalMap); out.preSwizzle = env->GetBooleanField(params, preSwizzle); out.noEndpointRDO = env->GetBooleanField(params, noEndpointRDO); From 0bedf8d478c2986066a216c1dc231132c7610a0c Mon Sep 17 00:00:00 2001 From: Marco Hutter Date: Sat, 23 Mar 2024 15:19:46 +0100 Subject: [PATCH 2/7] Add basic test for JNI input swizzle --- .../org/khronos/ktx/test/KtxTexture2Test.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java b/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java index 09d958c54a..a6b173b74b 100644 --- a/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java +++ b/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java @@ -227,4 +227,58 @@ public void testCreate() { texture.destroy(); } + + @Test + public void testInputSwizzleBasisEx() throws IOException { + + // Create RGBA pixels for an image with 32x32 pixels, + // filled with + // 8 rows of red pixels + // 8 rows of green pixels + // 8 rows of blue pixels + // 8 rows of white pixels + int sizeX = 32; + int sizeY = 32; + byte[] rgba = new byte[sizeX * sizeY * 4]; + fillRows(rgba, sizeX, sizeY, 0, 8, 255, 0, 0, 255); // Red + fillRows(rgba, sizeX, sizeY, 8, 16, 0, 255, 0, 255); // Green + fillRows(rgba, sizeX, sizeY, 16, 24, 0, 0, 255, 255); // Blue + fillRows(rgba, sizeX, sizeY, 24, 32, 255, 255, 255, 255); // White + + // Create a texture and fill it with the RGBA pixel data + KtxTextureCreateInfo info = new KtxTextureCreateInfo(); + info.setBaseWidth(sizeX); + info.setBaseHeight(sizeY); + info.setVkFormat(VkFormat.VK_FORMAT_R8G8B8A8_SRGB); + KtxTexture2 t = KtxTexture2.create(info, KtxCreateStorage.ALLOC); + t.setImageFromMemory(0, 0, 0, rgba); + + // Apply basis compression with an input swizzle, BRGA, so that + // the former B channel becomes the R channel + // the former R channel becomes the G channel + // the former G channel becomes the B channel + // the former A channel remains the A channel + KtxBasisParams p = new KtxBasisParams(); + p.setUastc(false); + p.setInputSwizzle(new char[] { 'b', 'r', 'g', 'a' }); + t.compressBasisEx(p); + + t.destroy(); + } + + // Fill the specified range of rows of the given RGBA pixels + // array with the given RGBA components + private static void fillRows(byte rgba[], int sizeX, int sizeY, + int minRow, int maxRow, + int r, int g, int b, int a) { + for (int y = minRow; y < maxRow; y++) { + for (int x = 0; x < sizeX; x++) { + int index = (y * sizeX) + x; + rgba[index * 4 + 0] = (byte) r; + rgba[index * 4 + 1] = (byte) g; + rgba[index * 4 + 2] = (byte) b; + rgba[index * 4 + 3] = (byte) a; + } + } + } } From 4638ca0e4c98fe195a667aeb02ac3f4c917a7b43 Mon Sep 17 00:00:00 2001 From: Marco Hutter Date: Sat, 23 Mar 2024 15:19:55 +0100 Subject: [PATCH 3/7] Removed unused imports --- .../test/java/org/khronos/ktx/test/KtxTestLibraryLoader.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTestLibraryLoader.java b/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTestLibraryLoader.java index 2187ef5d35..1964739f5d 100644 --- a/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTestLibraryLoader.java +++ b/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTestLibraryLoader.java @@ -11,9 +11,6 @@ import java.io.File; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; - -import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL; public class KtxTestLibraryLoader implements BeforeAllCallback, ExtensionContext.Store.CloseableResource { From 0ff07883d82f27717185292fab82e0b4525ba61d Mon Sep 17 00:00:00 2001 From: Marco Hutter Date: Sun, 24 Mar 2024 12:10:34 +0100 Subject: [PATCH 4/7] Compare result of swizzle to reference --- .../org/khronos/ktx/test/KtxTexture2Test.java | 15 +++++++++++++++ tests/testimages/swizzle-brga-reference.ktx | 3 +++ 2 files changed, 18 insertions(+) create mode 100644 tests/testimages/swizzle-brga-reference.ktx diff --git a/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java b/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java index a6b173b74b..99a6daf579 100644 --- a/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java +++ b/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java @@ -14,6 +14,7 @@ import java.nio.file.Path; import java.nio.file.Paths; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -263,7 +264,21 @@ public void testInputSwizzleBasisEx() throws IOException { p.setInputSwizzle(new char[] { 'b', 'r', 'g', 'a' }); t.compressBasisEx(p); + // Obtain the texture data, and compare it to the + // expected data of the reference texture + byte[] data = t.getData(); + Path referenceKtxFile = Paths.get("") + .resolve("../../tests/testimages/swizzle-brga-reference.ktx") + .toAbsolutePath() + .normalize(); + KtxTexture2 referenceTexture = KtxTexture2.createFromNamedFile(referenceKtxFile.toString(), + KtxTextureCreateFlagBits.LOAD_IMAGE_DATA_BIT); + byte[] referenceData = referenceTexture.getData(); + + assertArrayEquals(referenceData, data); + t.destroy(); + referenceTexture.destroy(); } // Fill the specified range of rows of the given RGBA pixels diff --git a/tests/testimages/swizzle-brga-reference.ktx b/tests/testimages/swizzle-brga-reference.ktx new file mode 100644 index 0000000000..b5ec396bc9 --- /dev/null +++ b/tests/testimages/swizzle-brga-reference.ktx @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8882e107f875cd29492d4e3e87f01c3ae2e799db2cb38db9fc63cfd22ec9059b +size 359 From 7da5422203c0353d16181fd79d4d3b9f49fdcd02 Mon Sep 17 00:00:00 2001 From: Marco Hutter Date: Tue, 26 Mar 2024 13:09:19 +0100 Subject: [PATCH 5/7] Test swizzle without reference image --- .../org/khronos/ktx/test/KtxTexture2Test.java | 55 +++++++-------- .../java/org/khronos/ktx/test/TestUtils.java | 67 +++++++++++++++++++ tests/testimages/swizzle-brga-reference.ktx | 3 - 3 files changed, 91 insertions(+), 34 deletions(-) create mode 100644 interface/java_binding/src/test/java/org/khronos/ktx/test/TestUtils.java delete mode 100644 tests/testimages/swizzle-brga-reference.ktx diff --git a/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java b/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java index 99a6daf579..7001e1c4a7 100644 --- a/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java +++ b/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java @@ -13,6 +13,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Locale; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -241,10 +243,10 @@ public void testInputSwizzleBasisEx() throws IOException { int sizeX = 32; int sizeY = 32; byte[] rgba = new byte[sizeX * sizeY * 4]; - fillRows(rgba, sizeX, sizeY, 0, 8, 255, 0, 0, 255); // Red - fillRows(rgba, sizeX, sizeY, 8, 16, 0, 255, 0, 255); // Green - fillRows(rgba, sizeX, sizeY, 16, 24, 0, 0, 255, 255); // Blue - fillRows(rgba, sizeX, sizeY, 24, 32, 255, 255, 255, 255); // White + TestUtils.fillRows(rgba, sizeX, sizeY, 0, 8, 255, 0, 0, 255); // Red + TestUtils.fillRows(rgba, sizeX, sizeY, 8, 16, 0, 255, 0, 255); // Green + TestUtils.fillRows(rgba, sizeX, sizeY, 16, 24, 0, 0, 255, 255); // Blue + TestUtils.fillRows(rgba, sizeX, sizeY, 24, 32, 255, 255, 255, 255); // White // Create a texture and fill it with the RGBA pixel data KtxTextureCreateInfo info = new KtxTextureCreateInfo(); @@ -264,36 +266,27 @@ public void testInputSwizzleBasisEx() throws IOException { p.setInputSwizzle(new char[] { 'b', 'r', 'g', 'a' }); t.compressBasisEx(p); - // Obtain the texture data, and compare it to the - // expected data of the reference texture - byte[] data = t.getData(); - Path referenceKtxFile = Paths.get("") - .resolve("../../tests/testimages/swizzle-brga-reference.ktx") - .toAbsolutePath() - .normalize(); - KtxTexture2 referenceTexture = KtxTexture2.createFromNamedFile(referenceKtxFile.toString(), - KtxTextureCreateFlagBits.LOAD_IMAGE_DATA_BIT); - byte[] referenceData = referenceTexture.getData(); + // TODO This constant is currently not defined in KtxTranscodeFormat: + final int KTX_TTF_RGBA32 = 13; - assertArrayEquals(referenceData, data); + // Transcode the resulting texture to RGBA32 + int outputFormat = KTX_TTF_RGBA32; + int transcodeFlags = 0; + t.transcodeBasis(outputFormat, transcodeFlags); + byte[] actualRgba = t.getData(); + + // Define the expected RGBA pixels. These are the swizzled input + // pixels, with slight deviations due to compression artifacts + byte[] expectedRgba = new byte[sizeX * sizeY * 4]; + TestUtils.fillRows(expectedRgba, sizeX, sizeY, 0, 8, 2, 255, 2, 255); + TestUtils.fillRows(expectedRgba, sizeX, sizeY, 8, 16, 0, 0, 253, 255); + TestUtils.fillRows(expectedRgba, sizeX, sizeY, 16, 24, 253, 0, 0, 255); + TestUtils.fillRows(expectedRgba, sizeX, sizeY, 24, 32, 255, 255, 255, 255); + + // Compare the resulting data to the expected RGBA values. + assertArrayEquals(expectedRgba, actualRgba); t.destroy(); - referenceTexture.destroy(); } - // Fill the specified range of rows of the given RGBA pixels - // array with the given RGBA components - private static void fillRows(byte rgba[], int sizeX, int sizeY, - int minRow, int maxRow, - int r, int g, int b, int a) { - for (int y = minRow; y < maxRow; y++) { - for (int x = 0; x < sizeX; x++) { - int index = (y * sizeX) + x; - rgba[index * 4 + 0] = (byte) r; - rgba[index * 4 + 1] = (byte) g; - rgba[index * 4 + 2] = (byte) b; - rgba[index * 4 + 3] = (byte) a; - } - } - } } diff --git a/interface/java_binding/src/test/java/org/khronos/ktx/test/TestUtils.java b/interface/java_binding/src/test/java/org/khronos/ktx/test/TestUtils.java new file mode 100644 index 0000000000..1e1c95e66f --- /dev/null +++ b/interface/java_binding/src/test/java/org/khronos/ktx/test/TestUtils.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024, Khronos Group and Contributors + * SPDX-License-Identifier: Apache-2.0 + */ +package org.khronos.ktx.test; + +import java.util.Locale; + +/** + * Utilities for the test package + */ +class TestUtils { + + /** + * Fill the specified range of rows of the given RGBA pixels array with the + * given RGBA components + * + * @param rgba The RGBA pixels array + * @param sizeX The size of the image in x-direction + * @param sizeY The size of the image in y-direction + * @param minRow The minimum row, inclusive + * @param maxRow The maximum row, exclusive + * @param r The red component (in [0,255]) + * @param g The green component (in [0,255]) + * @param b The blue component (in [0,255]) + * @param a The alpha component (in [0,255]) + */ + static void fillRows(byte rgba[], int sizeX, int sizeY, + int minRow, int maxRow, + int r, int g, int b, int a) { + for (int y = minRow; y < maxRow; y++) { + for (int x = 0; x < sizeX; x++) { + int index = (y * sizeX) + x; + rgba[index * 4 + 0] = (byte) r; + rgba[index * 4 + 1] = (byte) g; + rgba[index * 4 + 2] = (byte) b; + rgba[index * 4 + 3] = (byte) a; + } + } + } + + /** + * Create a string representation of the RGBA components of the specified pixel + * in the given RGBA pixels array. + * + * This is mainly intended for debugging. Some details of the resulting string + * are not specified. + * + * @param rgba The RGBA pixels array + * @param pixelIndex The pixel index + * @return The string + */ + static String createRgbaString(byte rgba[], int pixelIndex) { + byte r = rgba[pixelIndex * 4 + 0]; + byte g = rgba[pixelIndex * 4 + 1]; + byte b = rgba[pixelIndex * 4 + 2]; + byte a = rgba[pixelIndex * 4 + 3]; + int ir = Byte.toUnsignedInt(r); + int ig = Byte.toUnsignedInt(g); + int ib = Byte.toUnsignedInt(b); + int ia = Byte.toUnsignedInt(a); + String s = String.format(Locale.ENGLISH, "%3d, %3d, %3d, %3d", ir, ig, ib, ia); + return s; + + } + +} diff --git a/tests/testimages/swizzle-brga-reference.ktx b/tests/testimages/swizzle-brga-reference.ktx deleted file mode 100644 index b5ec396bc9..0000000000 --- a/tests/testimages/swizzle-brga-reference.ktx +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:8882e107f875cd29492d4e3e87f01c3ae2e799db2cb38db9fc63cfd22ec9059b -size 359 From 89b29cbbe97d8645df03b8d5052862a6bc1ffd60 Mon Sep 17 00:00:00 2001 From: Marco Hutter Date: Tue, 26 Mar 2024 13:30:54 +0100 Subject: [PATCH 6/7] Organize imports --- .../org/khronos/ktx/test/KtxTexture2Test.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java b/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java index 7001e1c4a7..02841ba845 100644 --- a/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java +++ b/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java @@ -5,20 +5,26 @@ package org.khronos.ktx.test; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.khronos.ktx.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Locale; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.khronos.ktx.KtxBasisParams; +import org.khronos.ktx.KtxCreateStorage; +import org.khronos.ktx.KtxErrorCode; +import org.khronos.ktx.KtxSupercmpScheme; +import org.khronos.ktx.KtxTexture2; +import org.khronos.ktx.KtxTextureCreateFlagBits; +import org.khronos.ktx.KtxTextureCreateInfo; +import org.khronos.ktx.KtxTranscodeFormat; +import org.khronos.ktx.VkFormat; @ExtendWith({ KtxTestLibraryLoader.class }) public class KtxTexture2Test { @@ -288,5 +294,5 @@ public void testInputSwizzleBasisEx() throws IOException { t.destroy(); } - } + From 647a5fc659897d889fb107e70b558be33911408e Mon Sep 17 00:00:00 2001 From: Marco Hutter Date: Fri, 12 Apr 2024 12:09:58 +0200 Subject: [PATCH 7/7] Fix test for input swizzling in JNI --- .../org/khronos/ktx/test/KtxTexture2Test.java | 110 +++++++++++------- 1 file changed, 69 insertions(+), 41 deletions(-) diff --git a/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java b/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java index 02841ba845..48db332084 100644 --- a/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java +++ b/interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java @@ -240,59 +240,87 @@ public void testCreate() { @Test public void testInputSwizzleBasisEx() throws IOException { - // Create RGBA pixels for an image with 32x32 pixels, - // filled with + int sizeX = 32; + int sizeY = 32; + int outputFormat = KtxTranscodeFormat.RGBA32; + int transcodeFlags = 0; + + // Create the actual texture data: + // - create RGBA pixels + // - create texture + // - compress with BRGA input swizzling + // - obtain resulting RGBA values + + // Create a RGBA pixels for an image filled with // 8 rows of red pixels // 8 rows of green pixels // 8 rows of blue pixels // 8 rows of white pixels - int sizeX = 32; - int sizeY = 32; - byte[] rgba = new byte[sizeX * sizeY * 4]; - TestUtils.fillRows(rgba, sizeX, sizeY, 0, 8, 255, 0, 0, 255); // Red - TestUtils.fillRows(rgba, sizeX, sizeY, 8, 16, 0, 255, 0, 255); // Green - TestUtils.fillRows(rgba, sizeX, sizeY, 16, 24, 0, 0, 255, 255); // Blue - TestUtils.fillRows(rgba, sizeX, sizeY, 24, 32, 255, 255, 255, 255); // White - - // Create a texture and fill it with the RGBA pixel data - KtxTextureCreateInfo info = new KtxTextureCreateInfo(); - info.setBaseWidth(sizeX); - info.setBaseHeight(sizeY); - info.setVkFormat(VkFormat.VK_FORMAT_R8G8B8A8_SRGB); - KtxTexture2 t = KtxTexture2.create(info, KtxCreateStorage.ALLOC); - t.setImageFromMemory(0, 0, 0, rgba); - - // Apply basis compression with an input swizzle, BRGA, so that + byte[] input = new byte[sizeX * sizeY * 4]; + TestUtils.fillRows(input, sizeX, sizeY, 0, 8, 255, 0, 0, 255); // Red + TestUtils.fillRows(input, sizeX, sizeY, 8, 16, 0, 255, 0, 255); // Green + TestUtils.fillRows(input, sizeX, sizeY, 16, 24, 0, 0, 255, 255); // Blue + TestUtils.fillRows(input, sizeX, sizeY, 24, 32, 255, 255, 255, 255); // White + + // Create the input texture from the pixels + KtxTextureCreateInfo inputInfo = new KtxTextureCreateInfo(); + inputInfo.setBaseWidth(sizeX); + inputInfo.setBaseHeight(sizeY); + inputInfo.setVkFormat(VkFormat.VK_FORMAT_R8G8B8A8_SRGB); + KtxTexture2 inputTexture = KtxTexture2.create(inputInfo, KtxCreateStorage.ALLOC); + inputTexture.setImageFromMemory(0, 0, 0, input); + + // Apply basis compression to the input, with an input swizzle BRGA, + // so that // the former B channel becomes the R channel // the former R channel becomes the G channel // the former G channel becomes the B channel // the former A channel remains the A channel - KtxBasisParams p = new KtxBasisParams(); - p.setUastc(false); - p.setInputSwizzle(new char[] { 'b', 'r', 'g', 'a' }); - t.compressBasisEx(p); - - // TODO This constant is currently not defined in KtxTranscodeFormat: - final int KTX_TTF_RGBA32 = 13; - - // Transcode the resulting texture to RGBA32 - int outputFormat = KTX_TTF_RGBA32; - int transcodeFlags = 0; - t.transcodeBasis(outputFormat, transcodeFlags); - byte[] actualRgba = t.getData(); - - // Define the expected RGBA pixels. These are the swizzled input - // pixels, with slight deviations due to compression artifacts - byte[] expectedRgba = new byte[sizeX * sizeY * 4]; - TestUtils.fillRows(expectedRgba, sizeX, sizeY, 0, 8, 2, 255, 2, 255); - TestUtils.fillRows(expectedRgba, sizeX, sizeY, 8, 16, 0, 0, 253, 255); - TestUtils.fillRows(expectedRgba, sizeX, sizeY, 16, 24, 253, 0, 0, 255); - TestUtils.fillRows(expectedRgba, sizeX, sizeY, 24, 32, 255, 255, 255, 255); + KtxBasisParams inputParams = new KtxBasisParams(); + inputParams.setUastc(false); + inputParams.setInputSwizzle(new char[] { 'b', 'r', 'g', 'a' }); + inputTexture.compressBasisEx(inputParams); + + // Transcode the input texture to RGBA32 + inputTexture.transcodeBasis(outputFormat, transcodeFlags); + byte[] actualRgba = inputTexture.getData(); + + // Create the expected reference data: + // - create RGBA pixels, swizzled with BRGA + // - create texture + // - compress without input swizzling + // - obtain resulting RGBA values + + // Create "golden" reference pixels, where a BRGA + // swizzling was already applied + byte[] gold = new byte[sizeX * sizeY * 4]; + TestUtils.fillRows(gold, sizeX, sizeY, 0, 8, 0, 255, 0, 255); // Green + TestUtils.fillRows(gold, sizeX, sizeY, 8, 16, 0, 0, 255, 255); // Blue + TestUtils.fillRows(gold, sizeX, sizeY, 16, 24, 255, 0, 0, 255); // Red + TestUtils.fillRows(gold, sizeX, sizeY, 24, 32, 255, 255, 255, 255); // White + + // Create the reference texture from the swizzled pixels + KtxTextureCreateInfo goldInfo = new KtxTextureCreateInfo(); + goldInfo.setBaseWidth(sizeX); + goldInfo.setBaseHeight(sizeY); + goldInfo.setVkFormat(VkFormat.VK_FORMAT_R8G8B8A8_SRGB); + KtxTexture2 goldTexture = KtxTexture2.create(goldInfo, KtxCreateStorage.ALLOC); + goldTexture.setImageFromMemory(0, 0, 0, gold); + + // Apply basis compression to the reference, without swizzling + KtxBasisParams goldParams = new KtxBasisParams(); + goldParams.setUastc(false); + goldTexture.compressBasisEx(goldParams); + + // Transcode the reference texture to RGBA32 + goldTexture.transcodeBasis(outputFormat, transcodeFlags); + byte[] expectedRgba = goldTexture.getData(); // Compare the resulting data to the expected RGBA values. assertArrayEquals(expectedRgba, actualRgba); - t.destroy(); + inputTexture.destroy(); + goldTexture.destroy(); } }