ome.codecs.JPEG2000Codec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ome-codecs Show documentation
Show all versions of ome-codecs Show documentation
Image encoding and decoding routines.
The newest version!
/*
* #%L
* Image encoding and decoding routines.
* %%
* Copyright (C) 2005 - 2017 Open Microscopy Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package ome.codecs;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferUShort;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import loci.common.ByteArrayHandle;
import loci.common.DataTools;
import loci.common.RandomAccessInputStream;
import loci.common.RandomAccessOutputStream;
import loci.common.services.DependencyException;
import loci.common.services.ServiceException;
import loci.common.services.ServiceFactory;
import ome.codecs.CodecException;
import ome.codecs.MissingLibraryException;
import ome.codecs.gui.AWTImageTools;
import ome.codecs.gui.UnsignedIntBuffer;
import ome.codecs.services.JAIIIOService;
import ome.codecs.services.JAIIIOServiceImpl;
/**
* This class implements JPEG 2000 compression and decompression.
*
*
*
*/
public class JPEG2000Codec extends BaseCodec {
// -- Fields --
private JAIIIOService service;
// -- Codec API methods --
/**
* The CodecOptions parameter should have the following fields set:
* {@link CodecOptions#width width}
* {@link CodecOptions#height height}
* {@link CodecOptions#bitsPerSample bitsPerSample}
* {@link CodecOptions#channels channels}
* {@link CodecOptions#interleaved interleaved}
* {@link CodecOptions#littleEndian littleEndian}
* {@link CodecOptions#lossless lossless}
*
* @see Codec#compress(byte[], CodecOptions)
*/
@Override
public byte[] compress(byte[] data, CodecOptions options)
throws CodecException
{
if (data == null || data.length == 0) return data;
initialize();
JPEG2000CodecOptions j2kOptions;
if (options instanceof JPEG2000CodecOptions) {
j2kOptions = (JPEG2000CodecOptions) options;
}
else {
j2kOptions = (JPEG2000CodecOptions)
JPEG2000CodecOptions.getDefaultOptions(options);
}
ByteArrayHandle handle = new ByteArrayHandle();
RandomAccessOutputStream out = new RandomAccessOutputStream(handle);
BufferedImage img = null;
int next = 0;
// NB: Construct BufferedImages manually, rather than using
// AWTImageTools.makeImage. The AWTImageTools.makeImage methods construct
// images that are not properly handled by the JPEG2000 writer.
// Specifically, 8-bit multi-channel images are constructed with type
// DataBuffer.TYPE_INT (so a single int is used to store all of the
// channels for a specific pixel).
int plane = j2kOptions.width * j2kOptions.height;
if (j2kOptions.bitsPerSample == 8) {
byte[][] b = new byte[j2kOptions.channels][plane];
if (j2kOptions.interleaved) {
for (int q=0; q