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