-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly convert Java char to C char in inputSwizzle (#876)
Fixes #875 As detailed in the issue: The Java `char[]` array for the input swizzle was casted to a `jbyteArray` in the JNI layer, in order to write it to the C/C++ `char[4]` array. This cast was invalid and caused data corruption. There could be two ways of tackling this: - One could change the type of the `inputSwizzle` in the Java layer to `byte[]` (which has 8 bits, and would make the cast valid). This would be a breaking change, and may require the inconvenient casting at the call site like `p.setInputSwizzle(new byte[]{ (byte)'b', (byte)'r', (byte)'g', (byte)'a' });` - One could fetch the Java `char[]` array as a `jcharArray`, and cast the elements to (C/C++) `char` individually This PR takes the second option (but we could do this either way, depending on the preferences from others).
- Loading branch information
Showing
4 changed files
with
186 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
interface/java_binding/src/test/java/org/khronos/ktx/test/TestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} | ||
|
||
} |