Skip to content

Commit

Permalink
Merge branch 'main' into fix_960
Browse files Browse the repository at this point in the history
  • Loading branch information
wasimabbas-arm authored Jan 7, 2025
2 parents 0ead350 + 7a8cd47 commit 01ab513
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 85 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.

14 changes: 13 additions & 1 deletion tools/imageio/exr.imageio/exrinput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class ExrInput final : public ImageInput {
}
~ExrInput() {
// FreeEXRVersion(&version); // No need to call, no such function
FreeEXRImage(&image);
if (image.width != 0 && image.height != 0) {
FreeEXRImage(&image);
}
FreeEXRHeader(&header);
FreeEXRErrorMessage(err);
}
Expand Down Expand Up @@ -223,6 +225,16 @@ void ExrInput::readImage(void* outputBuffer, size_t bufferByteCount,
"Requested format conversion from the input type is not supported."));
}

// Load image version
EXRVersion exr_version;
ec = ParseEXRVersionFromMemory(&exr_version, exrBuffer.data(), exrBuffer.size());
if (ec != TINYEXR_SUCCESS)
throw std::runtime_error(
fmt::format("EXR load error: {} - {}.", ec, "Failed to parse EXR version"));
if (exr_version.multipart || exr_version.non_image)
throw std::runtime_error(
fmt::format("EXR load error: {}.", "Unsupported EXR version (2.0)"));

// Load image data
ec = LoadEXRImageFromMemory(&image, &header, exrBuffer.data(), exrBuffer.size(), &err);
if (ec != TINYEXR_SUCCESS)
Expand Down

0 comments on commit 01ab513

Please sign in to comment.