From 49e4badda7983d71f04d3e2d8e1bf6935f02bfd0 Mon Sep 17 00:00:00 2001 From: jeb228 Date: Mon, 11 Oct 2010 18:17:00 +0000 Subject: [PATCH] NIHVIVO-1208 When creating a thumbnail from a PNG image, suppress the alpha channel so the colors of the thumbnail won't be screwed up. --- .../freemarker/ImageUploadHelper.java | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadHelper.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadHelper.java index 12f273f3b..f3efa992e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadHelper.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadHelper.java @@ -33,6 +33,7 @@ import org.apache.commons.logging.LogFactory; import com.sun.media.jai.codec.JPEGEncodeParam; import com.sun.media.jai.codec.MemoryCacheSeekableStream; +import com.sun.media.jai.codec.PNGDecodeParam; import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; @@ -284,7 +285,7 @@ public class ImageUploadHelper { mainStream = fileStorage.getInputStream(mainBytestreamUri, mainFilename); - thumbStream = scaleImageForThumbnail(mainStream, crop); + thumbStream = scaleImageForThumbnail(mainStream, crop, newImage); String mimeType = RECOGNIZED_FILE_TYPES.get(".jpg"); String filename = createThumbnailFilename(mainFilename); @@ -392,12 +393,17 @@ public class ImageUploadHelper { * width, height). */ private InputStream scaleImageForThumbnail(InputStream source, - CropRectangle crop) throws IOException { + CropRectangle crop, FileInfo newImage) throws IOException { try { // Read the main image. MemoryCacheSeekableStream stream = new MemoryCacheSeekableStream( source); - RenderedOp mainImage = JAI.create("stream", stream); + + ParameterBlock streamParams = new ParameterBlock(); + streamParams.add(stream); + addDecodeParametersAsNeeded(newImage, streamParams); + RenderedOp mainImage = JAI.create("stream", streamParams); + int imageWidth = mainImage.getWidth(); int imageHeight = mainImage.getHeight(); @@ -445,6 +451,22 @@ public class ImageUploadHelper { } } + /** + * The JAI 1.1.3 package has a known bug writing JPEG images from sources + * that have transparency (alpha channel) enabled. + * + * For PNG images, we can add a parameter that will disable the alpha + * channel. + */ + private void addDecodeParametersAsNeeded(FileInfo newImage, + ParameterBlock streamParams) { + if ("image/png".equals(newImage.getMimeType())) { + PNGDecodeParam pdp = new PNGDecodeParam(); + pdp.setSuppressAlpha(true); + streamParams.add(pdp); + } + } + /** * The bounds of the cropping rectangle should be limited to the bounds of * the image.