| 1 |
// Copyright (c) the JPEG XL Project Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
package org.jpeg.jpegxl.wrapper; |
| 7 |
|
| 8 |
import java.nio.Buffer; |
| 9 |
|
| 10 |
/** |
| 11 |
* Low level JNI wrapper. |
| 12 |
* |
| 13 |
* This class is package-private, should be only be used by high level wrapper. |
| 14 |
*/ |
| 15 |
class DecoderJni { |
| 16 |
private static native void nativeGetBasicInfo(int[] context, Buffer data); |
| 17 |
private static native void nativeGetPixels(int[] context, Buffer data, Buffer pixels, Buffer icc); |
| 18 |
|
| 19 |
static Status makeStatus(int statusCode) { |
| 20 |
switch (statusCode) { |
| 21 |
case 0: |
| 22 |
return Status.OK; |
| 23 |
case -1: |
| 24 |
return Status.INVALID_STREAM; |
| 25 |
case 1: |
| 26 |
return Status.NOT_ENOUGH_INPUT; |
| 27 |
default: |
| 28 |
throw new IllegalStateException("Unknown status code"); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
static StreamInfo makeStreamInfo(int[] context) { |
| 33 |
StreamInfo result = new StreamInfo(); |
| 34 |
result.status = makeStatus(context[0]); |
| 35 |
result.width = context[1]; |
| 36 |
result.height = context[2]; |
| 37 |
result.pixelsSize = context[3]; |
| 38 |
result.iccSize = context[4]; |
| 39 |
result.alphaBits = context[5]; |
| 40 |
return result; |
| 41 |
} |
| 42 |
|
| 43 |
/** Decode stream information. */ |
| 44 |
static StreamInfo getBasicInfo(Buffer data, PixelFormat pixelFormat) { |
| 45 |
if (!data.isDirect()) { |
| 46 |
throw new IllegalArgumentException("data must be direct buffer"); |
| 47 |
} |
| 48 |
int[] context = new int[6]; |
| 49 |
context[0] = (pixelFormat == null) ? -1 : pixelFormat.ordinal(); |
| 50 |
nativeGetBasicInfo(context, data); |
| 51 |
return makeStreamInfo(context); |
| 52 |
} |
| 53 |
|
| 54 |
/** One-shot decoding. */ |
| 55 |
static Status getPixels(Buffer data, Buffer pixels, Buffer icc, PixelFormat pixelFormat) { |
| 56 |
if (!data.isDirect()) { |
| 57 |
throw new IllegalArgumentException("data must be direct buffer"); |
| 58 |
} |
| 59 |
if (!pixels.isDirect()) { |
| 60 |
throw new IllegalArgumentException("pixels must be direct buffer"); |
| 61 |
} |
| 62 |
if (!icc.isDirect()) { |
| 63 |
throw new IllegalArgumentException("icc must be direct buffer"); |
| 64 |
} |
| 65 |
int[] context = new int[1]; |
| 66 |
context[0] = pixelFormat.ordinal(); |
| 67 |
nativeGetPixels(context, data, pixels, icc); |
| 68 |
return makeStatus(context[0]); |
| 69 |
} |
| 70 |
|
| 71 |
/** Utility library, disable object construction. */ |
| 72 |
private DecoderJni() {} |
| 73 |
} |