First import of JAVAOpenJPEG, a Java wrapper of OpenJPEG, developed by Patrick Piscaglia of Telemis (www.telemis.com). Thank you Patrick for this new module !
This commit is contained in:
230
JavaOpenJPEG/java sources/org/openJpeg/OpenJPEGJavaDecoder.java
Normal file
230
JavaOpenJPEG/java sources/org/openJpeg/OpenJPEGJavaDecoder.java
Normal file
@@ -0,0 +1,230 @@
|
||||
/*
|
||||
* $Id: $
|
||||
*
|
||||
* Copyright (c) 1999-2007 Telemis SA. All Rights Reserved
|
||||
|
||||
* Author: Patrick Piscaglia, Telemis s.a.
|
||||
*/
|
||||
package org.openJpeg;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
/** This class decodes one J2K codestream into an image (width + height + depth + pixels[],
|
||||
* using the OpenJPEG.org library.
|
||||
* To be able to log messages, the called must register a IJavaJ2KDecoderLogger object.
|
||||
*/
|
||||
public class OpenJPEGJavaDecoder {
|
||||
|
||||
public interface IJavaJ2KDecoderLogger {
|
||||
public void logDecoderMessage(String message);
|
||||
public void logDecoderError(String message);
|
||||
}
|
||||
|
||||
private static boolean isInitialized = false;
|
||||
|
||||
// ===== decompression parameters =============>
|
||||
// These value may be changed for each image
|
||||
private String[] decoder_arguments = null;
|
||||
/** number of resolutions decompositions */
|
||||
private int nbResolutions = -1;
|
||||
/** the quality layers */
|
||||
private int[] layers = null;
|
||||
|
||||
/** Contains the 8 bpp version of the image. May NOT be filled together with image16 or image24.<P>
|
||||
* We store in Java the 8 or 16 bpp version of the image while the decoder uses a 32 bpp version, because <UL>
|
||||
* <LI> the storage capacity required is smaller
|
||||
* <LI> the transfer Java <-- C will be faster
|
||||
* <LI> the conversion byte/short ==> int will be done faster by the C
|
||||
* </UL>*/
|
||||
private byte[] image8 = null;
|
||||
/** Contains the 16 bpp version of the image. May NOT be filled together with image8 or image24*/
|
||||
private short[] image16 = null;
|
||||
/** Contains the 24 bpp version of the image. May NOT be filled together with image8 or image16 */
|
||||
private int[] image24 = null;
|
||||
/** Holds the J2K compressed bytecode to decode */
|
||||
private byte compressedStream[] = null;
|
||||
/** Holds the compressed version of the index file, to be used by the decoder */
|
||||
private byte compressedIndex[] = null;
|
||||
/** Width and Height of the image */
|
||||
private int width = -1;
|
||||
private int height = -1;
|
||||
private int depth = -1;
|
||||
/** This parameter is never used in Java but is read by the C library to know the number of resolutions to skip when decoding,
|
||||
* i.e. if there are 5 resolutions and skipped=1 ==> decode until resolution 4. */
|
||||
private int skippedResolutions = 0;
|
||||
|
||||
private Vector<IJavaJ2KDecoderLogger> loggers = new Vector();
|
||||
|
||||
|
||||
public OpenJPEGJavaDecoder(String openJPEGlibraryFullPathAndName, IJavaJ2KDecoderLogger messagesAndErrorsLogger) throws ExceptionInInitializerError
|
||||
{
|
||||
this(openJPEGlibraryFullPathAndName);
|
||||
loggers.addElement(messagesAndErrorsLogger);
|
||||
}
|
||||
|
||||
public OpenJPEGJavaDecoder(String openJPEGlibraryFullPathAndName) throws ExceptionInInitializerError
|
||||
{
|
||||
if (!isInitialized) {
|
||||
try {
|
||||
System.load(openJPEGlibraryFullPathAndName);
|
||||
isInitialized = true;
|
||||
} catch (Throwable t) {
|
||||
throw new ExceptionInInitializerError("OpenJPEG Java Decoder: probably impossible to find the C library");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addLogger(IJavaJ2KDecoderLogger messagesAndErrorsLogger) {
|
||||
loggers.addElement(messagesAndErrorsLogger);
|
||||
}
|
||||
|
||||
public void removeLogger(IJavaJ2KDecoderLogger messagesAndErrorsLogger) {
|
||||
loggers.removeElement(messagesAndErrorsLogger);
|
||||
}
|
||||
|
||||
public int decodeJ2KtoImage() {
|
||||
if ((image16 == null || (image16 != null && image16.length != width*height)) && (depth==-1 || depth==16)) {
|
||||
image16 = new short[width*height];
|
||||
logMessage("OpenJPEGJavaDecoder.decompressImage: image16 length = " + image16.length + " (" + width + " x " + height + ") ");
|
||||
}
|
||||
if ((image8 == null || (image8 != null && image8.length != width*height)) && (depth==-1 || depth==8)) {
|
||||
image8 = new byte[width*height];
|
||||
logMessage("OpenJPEGJavaDecoder.decompressImage: image8 length = " + image8.length + " (" + width + " x " + height + ") ");
|
||||
}
|
||||
if ((image24 == null || (image24 != null && image24.length != width*height)) && (depth==-1 || depth==24)) {
|
||||
image24 = new int[width*height];
|
||||
logMessage("OpenJPEGJavaDecoder.decompressImage: image24 length = " + image24.length + " (" + width + " x " + height + ") ");
|
||||
}
|
||||
|
||||
String[] arguments = new String[0 + (decoder_arguments != null ? decoder_arguments.length : 0)];
|
||||
int offset = 0;
|
||||
if (decoder_arguments != null) {
|
||||
for (int i=0; i<decoder_arguments.length; i++) {
|
||||
arguments[i+offset] = decoder_arguments[i];
|
||||
}
|
||||
}
|
||||
|
||||
return internalDecodeJ2KtoImage(arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode the j2k stream given in the codestream byte[] and fills the image8, image16 or image24 array, according to the bit depth.
|
||||
*/
|
||||
private native int internalDecodeJ2KtoImage(String[] parameters);
|
||||
|
||||
/** Image depth in bpp */
|
||||
public int getDepth() {
|
||||
return depth;
|
||||
}
|
||||
|
||||
/** Image depth in bpp */
|
||||
public void setDepth(int depth) {
|
||||
this.depth = depth;
|
||||
}
|
||||
|
||||
/** Image height in pixels */
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
/** Image height in pixels */
|
||||
public void setHeight(int height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
/** Number of resolutions contained in the image */
|
||||
public int getNbResolutions() {
|
||||
return nbResolutions;
|
||||
}
|
||||
|
||||
/** Number of resolutions contained in the image */
|
||||
public void setNbResolutions(int nbResolutions) {
|
||||
this.nbResolutions = nbResolutions;
|
||||
}
|
||||
|
||||
/** Width of the image in pixels */
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
/** Width of the image in pixels */
|
||||
public void setWidth(int width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
/** Contains the decompressed version of the image, if the depth in is [9,16] bpp.
|
||||
* Returns NULL otherwise.
|
||||
*/
|
||||
public short[] getImage16() {
|
||||
return image16;
|
||||
}
|
||||
|
||||
/** Contains the decompressed version of the image, if the depth in is [17,24] bpp and the image is in color.
|
||||
* Returns NULL otherwise.
|
||||
*/
|
||||
public int[] getImage24() {
|
||||
return image24;
|
||||
}
|
||||
|
||||
/** Contains the decompressed version of the image, if the depth in is [1,8] bpp.
|
||||
* Returns NULL otherwise.
|
||||
*/
|
||||
public byte[] getImage8() {
|
||||
return image8;
|
||||
}
|
||||
|
||||
/** Sets the compressed version of the index file for this image.
|
||||
* This index file is used by the decompressor
|
||||
*/
|
||||
public void setCompressedIndex(byte[] compressedIndex) {
|
||||
this.compressedIndex = compressedIndex;
|
||||
}
|
||||
|
||||
/** Sets the codestream to be decoded */
|
||||
public void setCompressedStream(byte[] compressedStream) {
|
||||
this.compressedStream = compressedStream;
|
||||
}
|
||||
|
||||
/** @return the compressed code stream length, or -1 if not defined */
|
||||
public long getCodestreamLength() {
|
||||
if (compressedStream == null)
|
||||
return -1;
|
||||
else return compressedStream.length;
|
||||
}
|
||||
|
||||
/** This method is called either directly or by the C methods */
|
||||
public void logMessage(String message) {
|
||||
for (IJavaJ2KDecoderLogger logger:loggers)
|
||||
logger.logDecoderMessage(message);
|
||||
}
|
||||
|
||||
/** This method is called either directly or by the C methods */
|
||||
public void logError(String error) {
|
||||
for (IJavaJ2KDecoderLogger logger:loggers)
|
||||
logger.logDecoderError(error);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
nbResolutions = -1;
|
||||
layers = null;
|
||||
image8 = null;
|
||||
image16 = null;
|
||||
image24 = null;
|
||||
compressedStream = null;
|
||||
compressedIndex = null;
|
||||
width = -1;
|
||||
height = -1;
|
||||
depth = -1;
|
||||
}
|
||||
|
||||
public void setSkippedResolutions(int numberOfSkippedResolutions) {
|
||||
skippedResolutions = numberOfSkippedResolutions;
|
||||
}
|
||||
|
||||
/** Contains all the decoding arguments other than the input/output file */
|
||||
public void setDecoderArguments(String[] argumentsForTheDecoder) {
|
||||
decoder_arguments = argumentsForTheDecoder;
|
||||
}
|
||||
|
||||
|
||||
}
|
318
JavaOpenJPEG/java sources/org/openJpeg/OpenJPEGJavaEncoder.java
Normal file
318
JavaOpenJPEG/java sources/org/openJpeg/OpenJPEGJavaEncoder.java
Normal file
@@ -0,0 +1,318 @@
|
||||
/*
|
||||
* $Id: $
|
||||
*
|
||||
* Copyright (c) 1999-2007 Telemis SA. All Rights Reserved
|
||||
*
|
||||
* Author: Patrick Piscaglia, Telemis s.a.
|
||||
*/
|
||||
package org.openJpeg;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Vector;
|
||||
|
||||
/** This class encodes one image into the J2K format,
|
||||
* using the OpenJPEG.org library.
|
||||
* To be able to log messages, the called must register a IJavaJ2KEncoderLogger object.
|
||||
*/
|
||||
public class OpenJPEGJavaEncoder {
|
||||
|
||||
public interface IJavaJ2KEncoderLogger {
|
||||
public void logEncoderMessage(String message);
|
||||
public void logEncoderError(String message);
|
||||
}
|
||||
|
||||
private static boolean isInitialized = false;
|
||||
|
||||
// ===== Compression parameters =============>
|
||||
// These value may be changed for each image
|
||||
private String[] encoder_arguments = null;
|
||||
/** number of resolutions decompositions */
|
||||
private int nbResolutions = -1;
|
||||
/** the quality layers, expressed as compression rate */
|
||||
private float[] ratioLayers = null;
|
||||
/** the quality layers, expressed as PSNR values. This variable, if defined, has priority over the ratioLayers variable */
|
||||
private float[] psnrLayers = null;
|
||||
|
||||
/** Contains the 8 bpp version of the image. May NOT be filled together with image16 or image24.<P>
|
||||
* We store the 8 or 16 bpp version of the original image while the encoder uses a 32 bpp version, because <UL>
|
||||
* <LI> the storage capacity required is smaller
|
||||
* <LI> the transfer Java --> C will be faster
|
||||
* <LI> the conversion byte/short ==> int will be done faster by the C
|
||||
* </UL>*/
|
||||
private byte[] image8 = null;
|
||||
/** Contains the 16 bpp version of the image. May NOT be filled together with image8 or image24*/
|
||||
private short[] image16 = null;
|
||||
/** Contains the 24 bpp version of the image. May NOT be filled together with image8 or image16 */
|
||||
private int[] image24 = null;
|
||||
/** Holds the result of the compression, i.e. the J2K compressed bytecode */
|
||||
private byte compressedStream[] = null;
|
||||
/** Holds the compressed stream length, which may be smaller than compressedStream.length if this byte[] is pre-allocated */
|
||||
private long compressedStreamLength = -1;
|
||||
/** Holds the compressed version of the index file, returned by the encoder */
|
||||
private byte compressedIndex[] = null;
|
||||
/** Width and Height of the image */
|
||||
private int width = -1;
|
||||
private int height = -1;
|
||||
private int depth = -1;
|
||||
/** Tile size. We suppose the same size for the horizontal and vertical tiles.
|
||||
* If size == -1 ==> no tiling */
|
||||
private int tileSize = -1;
|
||||
// <===== Compression parameters =============
|
||||
|
||||
private Vector<IJavaJ2KEncoderLogger> loggers = new Vector();
|
||||
|
||||
public OpenJPEGJavaEncoder(String openJPEGlibraryFullPathAndName, IJavaJ2KEncoderLogger messagesAndErrorsLogger) throws ExceptionInInitializerError
|
||||
{
|
||||
this(openJPEGlibraryFullPathAndName);
|
||||
loggers.addElement(messagesAndErrorsLogger);
|
||||
}
|
||||
|
||||
public OpenJPEGJavaEncoder(String openJPEGlibraryFullPathAndName) throws ExceptionInInitializerError
|
||||
{
|
||||
if (!isInitialized) {
|
||||
try {
|
||||
String absolutePath = (new File(openJPEGlibraryFullPathAndName)).getCanonicalPath();
|
||||
System.load(absolutePath);
|
||||
isInitialized = true;
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
throw new ExceptionInInitializerError("OpenJPEG Java Encoder: probably impossible to find the C library");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addLogger(IJavaJ2KEncoderLogger messagesAndErrorsLogger) {
|
||||
loggers.addElement(messagesAndErrorsLogger);
|
||||
}
|
||||
|
||||
public void removeLogger(IJavaJ2KEncoderLogger messagesAndErrorsLogger) {
|
||||
loggers.removeElement(messagesAndErrorsLogger);
|
||||
}
|
||||
|
||||
/** This method compresses the given image.<P>
|
||||
* It returns the compressed J2K codestream into the compressedStream byte[].<P>
|
||||
* It also returns the compression index as a compressed form, into the compressedIndex byte[].<P>
|
||||
* One of the image8, image16 or image24 arrays must be correctly initialized and filled.<P>
|
||||
* The width, height and depth variables must be correctly filled.<P>
|
||||
* The nbResolutions, nbLayers and if needed the float[] psnrLayers or ratioLayers must also be filled before calling this method.
|
||||
*/
|
||||
public void encodeImageToJ2K() {
|
||||
// Need to allocate / reallocate the compressed stream buffer ? (size = max possible size = original image size)
|
||||
if (compressedStream== null || (compressedStream.length != width*height*depth/8)) {
|
||||
logMessage("OpenJPEGJavaEncoder.encodeImageToJ2K: (re-)allocating " + (width*height*depth/8) + " bytes for the compressedStream");
|
||||
compressedStream = new byte[width*height*depth/8];
|
||||
}
|
||||
// Arguments =
|
||||
// - number of resolutions "-n 5" : 2
|
||||
// - size of tile "-t 512,512" : 2
|
||||
//
|
||||
// Image width, height, depth and pixels are directly fetched by C from the Java class
|
||||
int nbArgs = 2 + (tileSize == -1 ? 0 : 2) + (encoder_arguments != null ? encoder_arguments.length : 0);
|
||||
if (psnrLayers != null && psnrLayers.length>0 && psnrLayers[0] != 0)
|
||||
// If psnrLayers is defined and doesn't just express "lossless"
|
||||
nbArgs += 2;
|
||||
else if (ratioLayers != null && ratioLayers.length>0 && ratioLayers[0]!=0.0)
|
||||
nbArgs += 2;
|
||||
String[] arguments = new String[nbArgs];
|
||||
int offset = 0;
|
||||
arguments[offset] = "-n"; arguments[offset+1] = "" + nbResolutions; offset += 2;
|
||||
if (tileSize!= -1) {
|
||||
arguments[offset++] = "-t";
|
||||
arguments[offset++] = "" + tileSize + "," + tileSize;
|
||||
}
|
||||
// If PSNR layers are defined, use them to encode the images
|
||||
if (psnrLayers != null && psnrLayers.length>0 && psnrLayers[0]!=-1) {
|
||||
arguments[offset++] = "-q";
|
||||
String s = "";
|
||||
for (int i=0; i<psnrLayers.length; i++)
|
||||
s += psnrLayers[i] + ",";
|
||||
arguments[offset++] = s.substring(0, s.length()-1);
|
||||
} else if (ratioLayers != null && ratioLayers.length>0 && ratioLayers[0]!=0.0) {
|
||||
// Specify quality ratioLayers, as compression ratios
|
||||
arguments[offset++] = "-r";
|
||||
String s = "";
|
||||
for (int i=0; i<ratioLayers.length; i++)
|
||||
s += ratioLayers[i] + ",";
|
||||
arguments[offset++] = s.substring(0, s.length()-1);
|
||||
}
|
||||
if (encoder_arguments != null) {
|
||||
for (int i=0; i<encoder_arguments.length; i++) {
|
||||
arguments[i+offset] = encoder_arguments[i];
|
||||
}
|
||||
}
|
||||
logMessage("Encoder additional arguments = " + arrayToString(arguments));
|
||||
long startTime = (new java.util.Date()).getTime();
|
||||
compressedStreamLength = internalEncodeImageToJ2K(arguments);
|
||||
logMessage("compression time = " + ((new java.util.Date()).getTime() - startTime) + " msec");
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills the compressedStream byte[] and the compressedIndex byte[]
|
||||
* @return the codestream length.
|
||||
*/
|
||||
private native long internalEncodeImageToJ2K(String[] parameters);
|
||||
|
||||
/** Image depth in bpp */
|
||||
public int getDepth() {
|
||||
return depth;
|
||||
}
|
||||
|
||||
/** Image depth in bpp */
|
||||
public void setDepth(int depth) {
|
||||
this.depth = depth;
|
||||
}
|
||||
|
||||
/** Image height in pixels */
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
/** Image height in pixels */
|
||||
public void setHeight(int height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
/** This method must be called in depth in [9,16].
|
||||
* @param an array of shorts, containing width*height values
|
||||
*/
|
||||
public void setImage16(short[] image16) {
|
||||
this.image16 = image16;
|
||||
}
|
||||
|
||||
/** This method must be called in depth in [17,24] for RGB images.
|
||||
* @param an array of int, containing width*height values
|
||||
*/
|
||||
public void setImage24(int[] image24) {
|
||||
this.image24 = image24;
|
||||
}
|
||||
|
||||
/** This method must be called in depth in [1,8].
|
||||
* @param an array of bytes, containing width*height values
|
||||
*/
|
||||
public void setImage8(byte[] image8) {
|
||||
this.image8 = image8;
|
||||
}
|
||||
|
||||
/** Return the ratioLayers, i.e. the compression ratio for each quality layer.
|
||||
* If the last value is 0.0, last layer is lossless compressed.
|
||||
*/
|
||||
public float[] getRatioLayers() {
|
||||
return ratioLayers;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the quality layers.
|
||||
* At least one level.
|
||||
* Each level is expressed as a compression ratio (float).
|
||||
* If the last value is 0.0, the last layer will be losslessly compressed
|
||||
*/
|
||||
public void setRatioLayers(float[] layers) {
|
||||
this.ratioLayers = layers;
|
||||
}
|
||||
|
||||
/** Return the PSNR Layers, i.e. the target PSNR for each quality layer.
|
||||
* If the last value is -1, last layer is lossless compressed.
|
||||
*/
|
||||
public float[] getPsnrLayers() {
|
||||
return psnrLayers;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the quality layers.
|
||||
* At least one level.
|
||||
* Each level is expressed as a target PSNR (float).
|
||||
* If the last value is -1, the last layer will be losslessly compressed
|
||||
*/
|
||||
public void setPsnrLayers(float[] layers) {
|
||||
this.psnrLayers = layers;
|
||||
}
|
||||
|
||||
/** Set the number of resolutions that must be created */
|
||||
public void setNbResolutions(int nbResolutions) {
|
||||
this.nbResolutions = nbResolutions;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
/** Width of the image, in pixels */
|
||||
public void setWidth(int width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
/** Return the compressed index file.
|
||||
* Syntax: TODO PP:
|
||||
*/
|
||||
public byte[] getCompressedIndex() {
|
||||
return compressedIndex;
|
||||
}
|
||||
|
||||
public void setCompressedIndex(byte[] index) {
|
||||
compressedIndex = index;
|
||||
}
|
||||
|
||||
public byte[] getCompressedStream() {
|
||||
return compressedStream;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
nbResolutions = -1;
|
||||
ratioLayers = null;
|
||||
psnrLayers = null;
|
||||
image8 = null;
|
||||
image16 = null;
|
||||
image24 = null;
|
||||
compressedStream = null;
|
||||
compressedIndex = null;
|
||||
width = -1;
|
||||
height = -1;
|
||||
depth = -1;
|
||||
}
|
||||
|
||||
public short[] getImage16() {
|
||||
return image16;
|
||||
}
|
||||
|
||||
public int[] getImage24() {
|
||||
return image24;
|
||||
}
|
||||
|
||||
public byte[] getImage8() {
|
||||
return image8;
|
||||
}
|
||||
|
||||
/** Sets the size of the tiles. We assume square tiles */
|
||||
public void setTileSize(int tileSize) {
|
||||
this.tileSize = tileSize;
|
||||
}
|
||||
|
||||
/** Contains all the encoding arguments other than the input/output file, compression ratio, tile size */
|
||||
public void setEncoderArguments(String[] argumentsForTheEncoder) {
|
||||
encoder_arguments = argumentsForTheEncoder;
|
||||
}
|
||||
|
||||
public void logMessage(String message) {
|
||||
for (IJavaJ2KEncoderLogger logger:loggers)
|
||||
logger.logEncoderMessage(message);
|
||||
}
|
||||
|
||||
public void logError(String error) {
|
||||
for (IJavaJ2KEncoderLogger logger:loggers)
|
||||
logger.logEncoderError(error);
|
||||
}
|
||||
|
||||
public long getCompressedStreamLength() {
|
||||
return compressedStreamLength;
|
||||
}
|
||||
|
||||
private String arrayToString(String[] array) {
|
||||
if (array == null)
|
||||
return "NULL";
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i=0; i<array.length; i++)
|
||||
sb.append(array[i]).append(" ");
|
||||
sb.delete(sb.length()-1, sb.length());
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user