Skip to content

Commit

Permalink
improving JPEG writing in ImageWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
Simone Giannecchini committed Mar 11, 2013
1 parent dfe0151 commit 9606019
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
Expand Up @@ -114,6 +114,7 @@
import org.opengis.referencing.operation.MathTransform2D;
import org.opengis.referencing.operation.MathTransformFactory;

import com.sun.imageio.plugins.jpeg.JPEGImageWriterSpi;
import com.sun.imageio.plugins.png.PNGImageWriter;
import com.sun.media.imageioimpl.common.BogusColorSpace;
import com.sun.media.imageioimpl.common.PackageUtil;
Expand Down Expand Up @@ -142,6 +143,15 @@
*/
public class ImageWorker {

/** CODEC_LIB_AVAILABLE */
private static final boolean CODEC_LIB_AVAILABLE = PackageUtil.isCodecLibAvailable();

/** JDK_JPEG_IMAGE_WRITER_SPI */
private static final JPEGImageWriterSpi JDK_JPEG_IMAGE_WRITER_SPI = new JPEGImageWriterSpi();

/** IMAGEIO_JPEG_IMAGE_WRITER_SPI */
private static final CLibJPEGImageWriterSpi IMAGEIO_JPEG_IMAGE_WRITER_SPI = new CLibJPEGImageWriterSpi();

/** CS_PYCC */
private static final ColorSpace CS_PYCC = ColorSpace.getInstance(ColorSpace.CS_PYCC);

Expand Down Expand Up @@ -2711,8 +2721,9 @@ public final void writeJPEG(final Object destination, final String compression,
throws IOException
{
// Reformatting this image for jpeg.
if(LOGGER.isLoggable(Level.FINER))
LOGGER.finer("Encoding input image to write out as JPEG.");
if(LOGGER.isLoggable(Level.FINE)){
LOGGER.fine("Encoding input image to write out as JPEG.");
}

// go to component color model if needed
ColorModel cm = image.getColorModel();
Expand All @@ -2732,30 +2743,40 @@ public final void writeJPEG(final Object destination, final String compression,


// Getting a writer.
if(LOGGER.isLoggable(Level.FINER))
LOGGER.finer("Getting a JPEG writer and configuring it.");
final Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("JPEG");
if (!it.hasNext()) {
throw new IllegalStateException(Errors.format(ErrorKeys.NO_IMAGE_WRITER));
if(LOGGER.isLoggable(Level.FINE)){
LOGGER.fine("Getting a JPEG writer and configuring it.");
}
ImageWriter writer = it.next();
if (!nativeAcc && writer.getClass().getName().equals(
"com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriter"))
{
writer = it.next();
}
if((!PackageUtil.isCodecLibAvailable()||!(writer.getOriginatingProvider() instanceof CLibJPEGImageWriterSpi))
ImageWriter writer=null;
if (nativeAcc&&CODEC_LIB_AVAILABLE){
try{
writer = IMAGEIO_JPEG_IMAGE_WRITER_SPI.createWriterInstance();
}catch (Exception e) {
if(LOGGER.isLoggable(Level.INFO)){
LOGGER.log(Level.INFO,"Unable to instantiate CLIB JPEG ImageWriter",e);
}
writer=null;
}
}
// in case we want the JDK one or in case the native one is not at hand we use the JDK one
if(writer==null){
writer = JDK_JPEG_IMAGE_WRITER_SPI.createWriterInstance();
}

// only imageio IO does JPEG-LS compression
if((!CODEC_LIB_AVAILABLE||!(writer.getOriginatingProvider() instanceof CLibJPEGImageWriterSpi))
&&
compression.equals("JPEG-LS")
)
throw new IllegalArgumentException(Errors.format(ErrorKeys.ILLEGAL_ARGUMENT_$2,"compression","JPEG-LS"));
){
throw new IOException(Errors.format(ErrorKeys.ILLEGAL_ARGUMENT_$2,"compression","JPEG-LS"));
}


// Compression is available on both lib
final ImageWriteParam iwp = writer.getDefaultWriteParam();
final ImageOutputStream outStream = ImageIOExt.createImageOutputStream(image, destination);
if(outStream==null)
throw new IIOException(Errors.format(ErrorKeys.NULL_ARGUMENT_$1,"stream"));
if(outStream==null){
throw new IIOException(Errors.format(ErrorKeys.NULL_ARGUMENT_$1,"stream"));
}

iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionType(compression); // Lossy compression.
Expand All @@ -2766,13 +2787,13 @@ public final void writeJPEG(final Object destination, final String compression,
try {
param.setProgressiveMode(JPEGImageWriteParam.MODE_DEFAULT);
} catch (UnsupportedOperationException e) {
throw (IOException) new IOException().initCause(e);
// TODO: inline cause when we will be allowed to target Java 6.
throw new IOException(e);
}
}

if(LOGGER.isLoggable(Level.FINER))
LOGGER.finer("Writing out...");
if(LOGGER.isLoggable(Level.FINE)){
LOGGER.fine("Writing out...");
}

try{

Expand Down Expand Up @@ -2803,7 +2824,9 @@ public final void writeJPEG(final Object destination, final String compression,
if(LOGGER.isLoggable(Level.FINEST))
LOGGER.log(Level.FINEST,e.getLocalizedMessage(),e);
}

if(LOGGER.isLoggable(Level.FINE)){
LOGGER.fine("Writing out... Done!");
}

}

Expand Down
Expand Up @@ -352,6 +352,13 @@ public void testJPEGWrite() throws IOException {
worker.writeJPEG(outFile, "JPEG-LS", 0.75f, true);
readWorker = new ImageWorker(ImageIO.read(outFile));
show(readWorker, "Native JPEG LS");
} else {
try{
worker.writeJPEG(outFile, "JPEG-LS", 0.75f, true);
assertFalse(true);
} catch (Exception e) {
// TODO: handle exception
}
}

// /////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 9606019

Please sign in to comment.