| 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 |
import java.nio.ByteBuffer; |
| 10 |
|
| 11 |
/** JPEG XL JNI decoder wrapper. */ |
| 12 |
public class Decoder { |
| 13 |
static { |
| 14 |
JniHelper.ensureInitialized(); |
| 15 |
} |
| 16 |
|
| 17 |
/** Utility library, disable object construction. */ |
| 18 |
private Decoder() {} |
| 19 |
|
| 20 |
/** One-shot decoding. */ |
| 21 |
public static ImageData decode(Buffer data, PixelFormat pixelFormat) { |
| 22 |
StreamInfo basicInfo = DecoderJni.getBasicInfo(data, pixelFormat); |
| 23 |
if (basicInfo.status != Status.OK) { |
| 24 |
throw new IllegalStateException("Decoding failed"); |
| 25 |
} |
| 26 |
if (basicInfo.width < 0 || basicInfo.height < 0 || basicInfo.pixelsSize < 0 |
| 27 |
|| basicInfo.iccSize < 0) { |
| 28 |
throw new IllegalStateException("JNI has returned negative size"); |
| 29 |
} |
| 30 |
Buffer pixels = ByteBuffer.allocateDirect(basicInfo.pixelsSize); |
| 31 |
Buffer icc = ByteBuffer.allocateDirect(basicInfo.iccSize); |
| 32 |
Status status = DecoderJni.getPixels(data, pixels, icc, pixelFormat); |
| 33 |
if (status != Status.OK) { |
| 34 |
throw new IllegalStateException("Decoding failed"); |
| 35 |
} |
| 36 |
return new ImageData(basicInfo.width, basicInfo.height, pixels, icc, pixelFormat); |
| 37 |
} |
| 38 |
|
| 39 |
public static StreamInfo decodeInfo(byte[] data) { |
| 40 |
return decodeInfo(ByteBuffer.wrap(data)); |
| 41 |
} |
| 42 |
|
| 43 |
public static StreamInfo decodeInfo(byte[] data, int offset, int length) { |
| 44 |
return decodeInfo(ByteBuffer.wrap(data, offset, length)); |
| 45 |
} |
| 46 |
|
| 47 |
public static StreamInfo decodeInfo(Buffer data) { |
| 48 |
return DecoderJni.getBasicInfo(data, null); |
| 49 |
} |
| 50 |
} |