Skip to content

Commit

Permalink
Java swizzle and constant fixes (#972)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
javagl authored Jan 5, 2025
1 parent b619004 commit 3038f7c
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package org.khronos.ktx;

import java.util.Arrays;

/**
* Structure for passing extended parameters to
* {@link KtxTexture2#compressAstcEx(KtxAstcParams)}.<br>
Expand Down Expand Up @@ -235,28 +237,16 @@ public char[] getInputSwizzle() {
/**
* Set the swizzle that should be applied to the input.<br>
* <br>
* This swizzle must match the regular expression /^[rgba01]{4}$/.<br>
* When the given swizzle is <code>null</code> or all its elements are
* <code>0</code>, then no swizzling will be applied to the input.<br>
* <br>
* Otherwise, this swizzle must match the regular expression
* <code>/^[rgba01]{4}$/</code>.<br>
* <br>
* When the given swizzle is <code>null</code>, then no swizzling will be
* applied to the input.
*
* @param inputSwizzle The swizzle
*/
public void setInputSwizzle(char[] inputSwizzle) {
if (inputSwizzle == null) {
this.inputSwizzle = new char[4];
return;
}
if (inputSwizzle.length != 4) {
throw new IllegalArgumentException("The inputSwizzle must contain 4 characters");
}
String valid = "rgba01";
for (int i = 0; i < inputSwizzle.length; i++) {
char c = inputSwizzle[i];
if (valid.indexOf(c) == -1) {
throw new IllegalArgumentException("The inputSwizzle may only consist of 'rgba01', but contains " + c);
}
}
this.inputSwizzle = inputSwizzle;
this.inputSwizzle = KtxUtilities.validateSwizzle(inputSwizzle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package org.khronos.ktx;

import java.util.Arrays;

/**
* Structure for passing parameters to {@link KtxTexture2#compressBasisEx(KtxBasisParams)}.<br>
* <br>
Expand Down Expand Up @@ -410,29 +412,17 @@ public char[] getInputSwizzle() {
/**
* Set the swizzle that should be applied to the input.<br>
* <br>
* This swizzle must match the regular expression /^[rgba01]{4}$/.<br>
* When the given swizzle is <code>null</code> or all its elements are
* <code>0</code>, then no swizzling will be applied to the input.<br>
* <br>
* Otherwise, this swizzle must match the regular expression
* <code>/^[rgba01]{4}$/</code>.<br>
* <br>
* When the given swizzle is <code>null</code>, then no swizzling will be
* applied to the input.
*
* @param inputSwizzle The swizzle
*/
public void setInputSwizzle(char[] inputSwizzle) {
if (inputSwizzle == null) {
this.inputSwizzle = new char[4];
return;
}
if (inputSwizzle.length != 4) {
throw new IllegalArgumentException("The inputSwizzle must contain 4 characters");
}
String valid = "rgba01";
for (int i = 0; i < inputSwizzle.length; i++) {
char c = inputSwizzle[i];
if (valid.indexOf(c) == -1) {
throw new IllegalArgumentException("The inputSwizzle may only consist of 'rgba01', but contains " + c);
}
}
this.inputSwizzle = inputSwizzle;
this.inputSwizzle = KtxUtilities.validateSwizzle(inputSwizzle);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class KtxPackAstcBlockDimension {
/**
* 4.27 bpp
*/
public static final int D6x4 = 3;
public static final int D6x5 = 3;

/**
* 3.56 bpp
Expand Down Expand Up @@ -142,7 +142,7 @@ public static String stringFor(int n) {
case D4x4: return "D4x4";
case D5x4: return "D5x4";
case D5x5: return "D5x5";
case D6x4: return "D6x4";
case D6x5: return "D6x5";
case D6x6: return "D6x6";
case D8x5: return "D8x5";
case D8x6: return "D8x6";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2024, Khronos Group and Contributors
* Copyright (c) 2021, Shukant Pal and Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.khronos.ktx;

import java.util.Arrays;

/**
* Package-private utility methods
*/
class KtxUtilities {

/**
* Validates the given value as an input swizzle to be set as
* {@link KtxBasisParams#setInputSwizzle(char[])} or
* {@link KtxAstcParams#setInputSwizzle(char[])}.<br>
* <br>
* When the given swizzle is <code>null</code>, then a default swizzle is
* returned, which is a 4-element <code>char</code> array with all elements
* being 0. <br>
* <br>
* Otherwise, if the given array does not have a length of 4, then an
* <code>IllegalArgumentException</code> is thrown.<br>
* <br>
* If the given swizzle is equal to the default swizzle, then it is returned
* directly. <br>
* <br>
* Otherwise, this swizzle must match the regular expression
* <code>/^[rgba01]{4}$/</code>.<br>
*
* @param inputSwizzle The input swizzle
* @return The validated input swizzle, or a new default swizzle if the input
* was <code>null</code>
* @throws IllegalArgumentException If the given swizzle is not valid
*/
static char[] validateSwizzle(char inputSwizzle[]) {
char defaultSwizzle[] = new char[4];
if (inputSwizzle == null) {
return defaultSwizzle;
}
if (inputSwizzle.length != 4) {
throw new IllegalArgumentException("The inputSwizzle must contain 4 characters");
}
if (Arrays.equals(inputSwizzle, defaultSwizzle)) {
return inputSwizzle;
}
String valid = "rgba01";
for (int i = 0; i < inputSwizzle.length; i++) {
char c = inputSwizzle[i];
if (valid.indexOf(c) == -1) {
throw new IllegalArgumentException("The inputSwizzle may only consist of 'rgba01', but contains " + c);
}
}
return inputSwizzle;

}

/**
* Private constructor to prevent instantiation
*/
private KtxUtilities() {
// Private constructor to prevent instantiation
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2024, Khronos Group and Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.khronos.ktx;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;

import org.junit.jupiter.api.Test;

public class KtxUtilitiesTest {
@Test
public void testValidSwizzle() {
char swizzle[] = new char[] { 'a', '1', 'r', '0' };
char expected[] = swizzle;
char actual[] = KtxUtilities.validateSwizzle(swizzle);
assertArrayEquals(expected, actual, "Accepts valid swizzle and returns it");
}

@Test
public void testNullSwizzle() {
char expected[] = new char[] { 0, 0, 0, 0 };
char actual[] = KtxUtilities.validateSwizzle(null);
assertArrayEquals(expected, actual, "Accepts null swizzle (to apply no swizzle), and returns a default");
}

@Test
public void testDefaultSwizzle() {
char swizzle[] = new char[] { 0, 0, 0, 0 };
char expected[] = swizzle;
char actual[] = KtxUtilities.validateSwizzle(swizzle);
assertArrayEquals(expected, actual, "Accepts default swizzle (all zeros)");
}

@Test
public void testInvalidSwizzleLength() throws IOException {
char swizzle[] = new char[] { 'a', 'b', 'r', 'g', 'r', 'g' };
assertThrows(IllegalArgumentException.class, () -> KtxUtilities.validateSwizzle(swizzle),
"Swizzle length != 4 expected to throw IllegalArgumentException");
}

@Test
public void testInvalidSwizzleChar() throws IOException {

char swizzle[] = new char[] { 'a', 'b', 'X', 'g' };
assertThrows(IllegalArgumentException.class, () -> KtxUtilities.validateSwizzle(swizzle),
"Invalid swizzle character expected to throw IllegalArgumentException");
}
}

This file was deleted.

0 comments on commit 3038f7c

Please sign in to comment.