Skip to content

Commit 3038f7c

Browse files
authored
Java swizzle and constant fixes (#972)
Fixes #969 (see the issue for details). The fix for the `inputSwizzle` (for both the `KtxAstcParams` and `KtxBasisParams`) is implemented by checking whether the input equals the default, which is a `char[] { 0, 0, 0, 0 }` array. If this is the case, then this value will be accepted, passed to the native layer, and handled there accordingly. Only when the input is _not_ that default, the elements will be checked to be in `rgba01`.
1 parent b619004 commit 3038f7c

File tree

6 files changed

+137
-84
lines changed

6 files changed

+137
-84
lines changed

interface/java_binding/src/main/java/org/khronos/ktx/KtxAstcParams.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
package org.khronos.ktx;
88

9+
import java.util.Arrays;
10+
911
/**
1012
* Structure for passing extended parameters to
1113
* {@link KtxTexture2#compressAstcEx(KtxAstcParams)}.<br>
@@ -235,28 +237,16 @@ public char[] getInputSwizzle() {
235237
/**
236238
* Set the swizzle that should be applied to the input.<br>
237239
* <br>
238-
* This swizzle must match the regular expression /^[rgba01]{4}$/.<br>
240+
* When the given swizzle is <code>null</code> or all its elements are
241+
* <code>0</code>, then no swizzling will be applied to the input.<br>
242+
* <br>
243+
* Otherwise, this swizzle must match the regular expression
244+
* <code>/^[rgba01]{4}$/</code>.<br>
239245
* <br>
240-
* When the given swizzle is <code>null</code>, then no swizzling will be
241-
* applied to the input.
242246
*
243247
* @param inputSwizzle The swizzle
244248
*/
245249
public void setInputSwizzle(char[] inputSwizzle) {
246-
if (inputSwizzle == null) {
247-
this.inputSwizzle = new char[4];
248-
return;
249-
}
250-
if (inputSwizzle.length != 4) {
251-
throw new IllegalArgumentException("The inputSwizzle must contain 4 characters");
252-
}
253-
String valid = "rgba01";
254-
for (int i = 0; i < inputSwizzle.length; i++) {
255-
char c = inputSwizzle[i];
256-
if (valid.indexOf(c) == -1) {
257-
throw new IllegalArgumentException("The inputSwizzle may only consist of 'rgba01', but contains " + c);
258-
}
259-
}
260-
this.inputSwizzle = inputSwizzle;
250+
this.inputSwizzle = KtxUtilities.validateSwizzle(inputSwizzle);
261251
}
262252
}

interface/java_binding/src/main/java/org/khronos/ktx/KtxBasisParams.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
package org.khronos.ktx;
88

9+
import java.util.Arrays;
10+
911
/**
1012
* Structure for passing parameters to {@link KtxTexture2#compressBasisEx(KtxBasisParams)}.<br>
1113
* <br>
@@ -410,29 +412,17 @@ public char[] getInputSwizzle() {
410412
/**
411413
* Set the swizzle that should be applied to the input.<br>
412414
* <br>
413-
* This swizzle must match the regular expression /^[rgba01]{4}$/.<br>
415+
* When the given swizzle is <code>null</code> or all its elements are
416+
* <code>0</code>, then no swizzling will be applied to the input.<br>
417+
* <br>
418+
* Otherwise, this swizzle must match the regular expression
419+
* <code>/^[rgba01]{4}$/</code>.<br>
414420
* <br>
415-
* When the given swizzle is <code>null</code>, then no swizzling will be
416-
* applied to the input.
417421
*
418422
* @param inputSwizzle The swizzle
419423
*/
420424
public void setInputSwizzle(char[] inputSwizzle) {
421-
if (inputSwizzle == null) {
422-
this.inputSwizzle = new char[4];
423-
return;
424-
}
425-
if (inputSwizzle.length != 4) {
426-
throw new IllegalArgumentException("The inputSwizzle must contain 4 characters");
427-
}
428-
String valid = "rgba01";
429-
for (int i = 0; i < inputSwizzle.length; i++) {
430-
char c = inputSwizzle[i];
431-
if (valid.indexOf(c) == -1) {
432-
throw new IllegalArgumentException("The inputSwizzle may only consist of 'rgba01', but contains " + c);
433-
}
434-
}
435-
this.inputSwizzle = inputSwizzle;
425+
this.inputSwizzle = KtxUtilities.validateSwizzle(inputSwizzle);
436426
}
437427

438428
/**

interface/java_binding/src/main/java/org/khronos/ktx/KtxPackAstcBlockDimension.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class KtxPackAstcBlockDimension {
2929
/**
3030
* 4.27 bpp
3131
*/
32-
public static final int D6x4 = 3;
32+
public static final int D6x5 = 3;
3333

3434
/**
3535
* 3.56 bpp
@@ -142,7 +142,7 @@ public static String stringFor(int n) {
142142
case D4x4: return "D4x4";
143143
case D5x4: return "D5x4";
144144
case D5x5: return "D5x5";
145-
case D6x4: return "D6x4";
145+
case D6x5: return "D6x5";
146146
case D6x6: return "D6x6";
147147
case D8x5: return "D8x5";
148148
case D8x6: return "D8x6";
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2024, Khronos Group and Contributors
3+
* Copyright (c) 2021, Shukant Pal and Contributors
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
package org.khronos.ktx;
7+
8+
import java.util.Arrays;
9+
10+
/**
11+
* Package-private utility methods
12+
*/
13+
class KtxUtilities {
14+
15+
/**
16+
* Validates the given value as an input swizzle to be set as
17+
* {@link KtxBasisParams#setInputSwizzle(char[])} or
18+
* {@link KtxAstcParams#setInputSwizzle(char[])}.<br>
19+
* <br>
20+
* When the given swizzle is <code>null</code>, then a default swizzle is
21+
* returned, which is a 4-element <code>char</code> array with all elements
22+
* being 0. <br>
23+
* <br>
24+
* Otherwise, if the given array does not have a length of 4, then an
25+
* <code>IllegalArgumentException</code> is thrown.<br>
26+
* <br>
27+
* If the given swizzle is equal to the default swizzle, then it is returned
28+
* directly. <br>
29+
* <br>
30+
* Otherwise, this swizzle must match the regular expression
31+
* <code>/^[rgba01]{4}$/</code>.<br>
32+
*
33+
* @param inputSwizzle The input swizzle
34+
* @return The validated input swizzle, or a new default swizzle if the input
35+
* was <code>null</code>
36+
* @throws IllegalArgumentException If the given swizzle is not valid
37+
*/
38+
static char[] validateSwizzle(char inputSwizzle[]) {
39+
char defaultSwizzle[] = new char[4];
40+
if (inputSwizzle == null) {
41+
return defaultSwizzle;
42+
}
43+
if (inputSwizzle.length != 4) {
44+
throw new IllegalArgumentException("The inputSwizzle must contain 4 characters");
45+
}
46+
if (Arrays.equals(inputSwizzle, defaultSwizzle)) {
47+
return inputSwizzle;
48+
}
49+
String valid = "rgba01";
50+
for (int i = 0; i < inputSwizzle.length; i++) {
51+
char c = inputSwizzle[i];
52+
if (valid.indexOf(c) == -1) {
53+
throw new IllegalArgumentException("The inputSwizzle may only consist of 'rgba01', but contains " + c);
54+
}
55+
}
56+
return inputSwizzle;
57+
58+
}
59+
60+
/**
61+
* Private constructor to prevent instantiation
62+
*/
63+
private KtxUtilities() {
64+
// Private constructor to prevent instantiation
65+
}
66+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2024, Khronos Group and Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.khronos.ktx;
7+
8+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
10+
11+
import java.io.IOException;
12+
13+
import org.junit.jupiter.api.Test;
14+
15+
public class KtxUtilitiesTest {
16+
@Test
17+
public void testValidSwizzle() {
18+
char swizzle[] = new char[] { 'a', '1', 'r', '0' };
19+
char expected[] = swizzle;
20+
char actual[] = KtxUtilities.validateSwizzle(swizzle);
21+
assertArrayEquals(expected, actual, "Accepts valid swizzle and returns it");
22+
}
23+
24+
@Test
25+
public void testNullSwizzle() {
26+
char expected[] = new char[] { 0, 0, 0, 0 };
27+
char actual[] = KtxUtilities.validateSwizzle(null);
28+
assertArrayEquals(expected, actual, "Accepts null swizzle (to apply no swizzle), and returns a default");
29+
}
30+
31+
@Test
32+
public void testDefaultSwizzle() {
33+
char swizzle[] = new char[] { 0, 0, 0, 0 };
34+
char expected[] = swizzle;
35+
char actual[] = KtxUtilities.validateSwizzle(swizzle);
36+
assertArrayEquals(expected, actual, "Accepts default swizzle (all zeros)");
37+
}
38+
39+
@Test
40+
public void testInvalidSwizzleLength() throws IOException {
41+
char swizzle[] = new char[] { 'a', 'b', 'r', 'g', 'r', 'g' };
42+
assertThrows(IllegalArgumentException.class, () -> KtxUtilities.validateSwizzle(swizzle),
43+
"Swizzle length != 4 expected to throw IllegalArgumentException");
44+
}
45+
46+
@Test
47+
public void testInvalidSwizzleChar() throws IOException {
48+
49+
char swizzle[] = new char[] { 'a', 'b', 'X', 'g' };
50+
assertThrows(IllegalArgumentException.class, () -> KtxUtilities.validateSwizzle(swizzle),
51+
"Invalid swizzle character expected to throw IllegalArgumentException");
52+
}
53+
}

interface/java_binding/src/test/java/org/khronos/ktx/test/KtxAstcParamsTest.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)