From: <fg...@us...> - 2010-06-07 15:59:37
|
Revision: 2629 http://openutils.svn.sourceforge.net/openutils/?rev=2629&view=rev Author: fgiust Date: 2010-06-07 15:59:31 +0000 (Mon, 07 Jun 2010) Log Message: ----------- MEDIA-72 some images have inverted colors when they are resized Modified Paths: -------------- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/JpegUtils.java trunk/openutils-mgnlmedia/src/test/java/net/sourceforge/openutils/mgnlmedia/media/utils/FileNodeData.java trunk/openutils-mgnlmedia/src/test/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtilsTest.java Added Paths: ----------- trunk/openutils-mgnlmedia/src/test/resources/images/sample_invalid_colors.jpg Modified: trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java =================================================================== --- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java 2010-06-07 15:09:41 UTC (rev 2628) +++ trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java 2010-06-07 15:59:31 UTC (rev 2629) @@ -96,7 +96,7 @@ */ public static String RESOLUTION_PROPERTY = "resolution"; - private static final String[] extensions = new String[]{"jpg", "gif", "png" }; + private static final String[] extensions = new String[]{"jpg", "jpeg", "gif", "png" }; static { @@ -943,22 +943,32 @@ InputStream is = image.getStream(); try { - return ImageIO.read(is); - } - catch (IOException e) - { - // CMYK jpeg can't be read be ImageIO - // Caused by: javax.imageio.IIOException: Unsupported Image Type - BufferedImage result = JpegUtils.processNonStandardImage(image); - if (result == null) + String ext = image.getAttribute("extension"); + + if (StringUtils.equalsIgnoreCase(ext, "jpg") || StringUtils.equalsIgnoreCase(ext, "jpeg")) { - // throw the original exception back - throw new BadImageFormatException("Unable to handle " + image.getHandle(), e); + try + { + return JpegUtils.processNonStandardImage(image); + } + catch (Throwable e) + { + // sun classes may not be available or other errors occurred, try with the standard ImageIO + log + .debug( + "Unable to process image " + image.getHandle() + " using JpegUtils: " + e.getMessage(), + e); + } } - return result; + return ImageIO.read(is); } + catch (IOException e) + { + // throw the original exception back + throw new BadImageFormatException("Unable to handle " + image.getHandle(), e); + } finally { IOUtils.closeQuietly(is); Modified: trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/JpegUtils.java =================================================================== --- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/JpegUtils.java 2010-06-07 15:09:41 UTC (rev 2628) +++ trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/JpegUtils.java 2010-06-07 15:59:31 UTC (rev 2629) @@ -167,7 +167,7 @@ Raster raster = reader.readRaster(0, reader.getDefaultReadParam()); boolean ycckProfile = false; - // yes, we need to read it once again to extract metadatas + // yes, we need to read it once again to extract metadata InputStream is3 = image.getStream(); try { @@ -175,18 +175,21 @@ JpegSegmentReader segmentReader = new JpegSegmentReader(is3); byte[] exifSegment = segmentReader.readSegment(JpegSegmentReader.SEGMENT_APPE); - switch (exifSegment[11]) + if (exifSegment != null) // see MEDIA-72, rgb images have a null exif segment here { - case 2 : - ycckProfile = true; - break; - case 1 : - // "YCbCr" - break; - case 0 : - default : - // Unknown (RGB or CMYK) - break; + switch (exifSegment[11]) + { + case 2 : + ycckProfile = true; + break; + case 1 : + // "YCbCr" + break; + case 0 : + default : + // Unknown (RGB or CMYK) + break; + } } } catch (JpegProcessingException e1) @@ -204,7 +207,24 @@ } reader.dispose(); - return createJPEG4(raster, ycckProfile); + if (ycckProfile) + { + return createJPEG4(raster, ycckProfile); + } + else + { + InputStream is4 = image.getStream(); + try + { + // see MEDIA-72, we need the sun codec to make this work properly + return com.sun.image.codec.jpeg.JPEGCodec.createJPEGDecoder(is4).decodeAsBufferedImage(); + } + finally + { + IOUtils.closeQuietly(is4); + } + } + } throw new BadImageFormatException("No ImageReaders found for " + image.getHandle()); Modified: trunk/openutils-mgnlmedia/src/test/java/net/sourceforge/openutils/mgnlmedia/media/utils/FileNodeData.java =================================================================== --- trunk/openutils-mgnlmedia/src/test/java/net/sourceforge/openutils/mgnlmedia/media/utils/FileNodeData.java 2010-06-07 15:09:41 UTC (rev 2628) +++ trunk/openutils-mgnlmedia/src/test/java/net/sourceforge/openutils/mgnlmedia/media/utils/FileNodeData.java 2010-06-07 15:59:31 UTC (rev 2629) @@ -37,6 +37,7 @@ import javax.jcr.Value; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringUtils; /** @@ -48,6 +49,8 @@ private byte[] content; + private String extension; + /** * @param classpathLocation * @throws IOException @@ -55,12 +58,15 @@ public FileNodeData(String classpathLocation) throws IOException { InputStream is = getClass().getResourceAsStream(classpathLocation); + if (is == null) { throw new IllegalArgumentException("Classpath resource " + classpathLocation + " cannot be found"); } try { + extension = StringUtils.substringAfterLast(classpathLocation, "."); + content = IOUtils.toByteArray(is); } finally @@ -81,8 +87,12 @@ /** * {@inheritDoc} */ - public String getAttribute(String arg0) + public String getAttribute(String key) { + if (StringUtils.equals(key, "extension")) + { + return this.extension; + } // TODO Auto-generated method stub return null; } Modified: trunk/openutils-mgnlmedia/src/test/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtilsTest.java =================================================================== --- trunk/openutils-mgnlmedia/src/test/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtilsTest.java 2010-06-07 15:09:41 UTC (rev 2628) +++ trunk/openutils-mgnlmedia/src/test/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtilsTest.java 2010-06-07 15:59:31 UTC (rev 2629) @@ -89,9 +89,36 @@ } /** + * Test for MEDIA-72 * @throws Exception */ @Test + public void testInvalidJpeg() throws Exception + { + + NodeData cmyk = new FileNodeData("/images/sample_invalid_colors.jpg"); + BufferedImage bufferedImage = ImageUtils.createBufferedImage(cmyk); + Assert.assertNotNull(bufferedImage); + + bufferedImage = ImageUtils.resizeImage(bufferedImage, 100, 100); + + InputStream is = ImageUtils.getStream(bufferedImage, "jpg", 1.0F, false); + + File tempFile = File.createTempFile("image", ".jpg"); + OutputStream os = new BufferedOutputStream(new FileOutputStream(tempFile)); + IOUtils.copy(is, os); + + IOUtils.closeQuietly(is); + IOUtils.closeQuietly(os); + + tempFile.delete(); + + } + + /** + * @throws Exception + */ + @Test public void testBorders() throws Exception { Added: trunk/openutils-mgnlmedia/src/test/resources/images/sample_invalid_colors.jpg =================================================================== (Binary files differ) Property changes on: trunk/openutils-mgnlmedia/src/test/resources/images/sample_invalid_colors.jpg ___________________________________________________________________ Added: svn:mime-type + image/jpeg This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |