|
5 | 5 |
|
6 | 6 | package org.khronos.ktx.test;
|
7 | 7 |
|
8 |
| -import org.junit.jupiter.api.Test; |
9 |
| -import org.junit.jupiter.api.extension.ExtendWith; |
10 |
| -import org.khronos.ktx.*; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
11 | 11 |
|
12 | 12 | import java.io.IOException;
|
13 | 13 | import java.nio.file.Files;
|
14 | 14 | import java.nio.file.Path;
|
15 | 15 | import java.nio.file.Paths;
|
16 | 16 |
|
17 |
| -import static org.junit.jupiter.api.Assertions.assertEquals; |
18 |
| -import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 19 | +import org.khronos.ktx.KtxBasisParams; |
| 20 | +import org.khronos.ktx.KtxCreateStorage; |
| 21 | +import org.khronos.ktx.KtxErrorCode; |
| 22 | +import org.khronos.ktx.KtxSupercmpScheme; |
| 23 | +import org.khronos.ktx.KtxTexture2; |
| 24 | +import org.khronos.ktx.KtxTextureCreateFlagBits; |
| 25 | +import org.khronos.ktx.KtxTextureCreateInfo; |
| 26 | +import org.khronos.ktx.KtxTranscodeFormat; |
| 27 | +import org.khronos.ktx.VkFormat; |
19 | 28 |
|
20 | 29 | @ExtendWith({ KtxTestLibraryLoader.class })
|
21 | 30 | public class KtxTexture2Test {
|
@@ -227,4 +236,91 @@ public void testCreate() {
|
227 | 236 |
|
228 | 237 | texture.destroy();
|
229 | 238 | }
|
| 239 | + |
| 240 | + @Test |
| 241 | + public void testInputSwizzleBasisEx() throws IOException { |
| 242 | + |
| 243 | + int sizeX = 32; |
| 244 | + int sizeY = 32; |
| 245 | + int outputFormat = KtxTranscodeFormat.RGBA32; |
| 246 | + int transcodeFlags = 0; |
| 247 | + |
| 248 | + // Create the actual texture data: |
| 249 | + // - create RGBA pixels |
| 250 | + // - create texture |
| 251 | + // - compress with BRGA input swizzling |
| 252 | + // - obtain resulting RGBA values |
| 253 | + |
| 254 | + // Create a RGBA pixels for an image filled with |
| 255 | + // 8 rows of red pixels |
| 256 | + // 8 rows of green pixels |
| 257 | + // 8 rows of blue pixels |
| 258 | + // 8 rows of white pixels |
| 259 | + byte[] input = new byte[sizeX * sizeY * 4]; |
| 260 | + TestUtils.fillRows(input, sizeX, sizeY, 0, 8, 255, 0, 0, 255); // Red |
| 261 | + TestUtils.fillRows(input, sizeX, sizeY, 8, 16, 0, 255, 0, 255); // Green |
| 262 | + TestUtils.fillRows(input, sizeX, sizeY, 16, 24, 0, 0, 255, 255); // Blue |
| 263 | + TestUtils.fillRows(input, sizeX, sizeY, 24, 32, 255, 255, 255, 255); // White |
| 264 | + |
| 265 | + // Create the input texture from the pixels |
| 266 | + KtxTextureCreateInfo inputInfo = new KtxTextureCreateInfo(); |
| 267 | + inputInfo.setBaseWidth(sizeX); |
| 268 | + inputInfo.setBaseHeight(sizeY); |
| 269 | + inputInfo.setVkFormat(VkFormat.VK_FORMAT_R8G8B8A8_SRGB); |
| 270 | + KtxTexture2 inputTexture = KtxTexture2.create(inputInfo, KtxCreateStorage.ALLOC); |
| 271 | + inputTexture.setImageFromMemory(0, 0, 0, input); |
| 272 | + |
| 273 | + // Apply basis compression to the input, with an input swizzle BRGA, |
| 274 | + // so that |
| 275 | + // the former B channel becomes the R channel |
| 276 | + // the former R channel becomes the G channel |
| 277 | + // the former G channel becomes the B channel |
| 278 | + // the former A channel remains the A channel |
| 279 | + KtxBasisParams inputParams = new KtxBasisParams(); |
| 280 | + inputParams.setUastc(false); |
| 281 | + inputParams.setInputSwizzle(new char[] { 'b', 'r', 'g', 'a' }); |
| 282 | + inputTexture.compressBasisEx(inputParams); |
| 283 | + |
| 284 | + // Transcode the input texture to RGBA32 |
| 285 | + inputTexture.transcodeBasis(outputFormat, transcodeFlags); |
| 286 | + byte[] actualRgba = inputTexture.getData(); |
| 287 | + |
| 288 | + // Create the expected reference data: |
| 289 | + // - create RGBA pixels, swizzled with BRGA |
| 290 | + // - create texture |
| 291 | + // - compress without input swizzling |
| 292 | + // - obtain resulting RGBA values |
| 293 | + |
| 294 | + // Create "golden" reference pixels, where a BRGA |
| 295 | + // swizzling was already applied |
| 296 | + byte[] gold = new byte[sizeX * sizeY * 4]; |
| 297 | + TestUtils.fillRows(gold, sizeX, sizeY, 0, 8, 0, 255, 0, 255); // Green |
| 298 | + TestUtils.fillRows(gold, sizeX, sizeY, 8, 16, 0, 0, 255, 255); // Blue |
| 299 | + TestUtils.fillRows(gold, sizeX, sizeY, 16, 24, 255, 0, 0, 255); // Red |
| 300 | + TestUtils.fillRows(gold, sizeX, sizeY, 24, 32, 255, 255, 255, 255); // White |
| 301 | + |
| 302 | + // Create the reference texture from the swizzled pixels |
| 303 | + KtxTextureCreateInfo goldInfo = new KtxTextureCreateInfo(); |
| 304 | + goldInfo.setBaseWidth(sizeX); |
| 305 | + goldInfo.setBaseHeight(sizeY); |
| 306 | + goldInfo.setVkFormat(VkFormat.VK_FORMAT_R8G8B8A8_SRGB); |
| 307 | + KtxTexture2 goldTexture = KtxTexture2.create(goldInfo, KtxCreateStorage.ALLOC); |
| 308 | + goldTexture.setImageFromMemory(0, 0, 0, gold); |
| 309 | + |
| 310 | + // Apply basis compression to the reference, without swizzling |
| 311 | + KtxBasisParams goldParams = new KtxBasisParams(); |
| 312 | + goldParams.setUastc(false); |
| 313 | + goldTexture.compressBasisEx(goldParams); |
| 314 | + |
| 315 | + // Transcode the reference texture to RGBA32 |
| 316 | + goldTexture.transcodeBasis(outputFormat, transcodeFlags); |
| 317 | + byte[] expectedRgba = goldTexture.getData(); |
| 318 | + |
| 319 | + // Compare the resulting data to the expected RGBA values. |
| 320 | + assertArrayEquals(expectedRgba, actualRgba); |
| 321 | + |
| 322 | + inputTexture.destroy(); |
| 323 | + goldTexture.destroy(); |
| 324 | + } |
230 | 325 | }
|
| 326 | + |
0 commit comments