You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by ss...@apache.org on 2015/06/05 14:18:55 UTC
svn commit: r1683736 - in /xmlgraphics/commons/trunk:
src/java/org/apache/xmlgraphics/image/codec/png/
src/java/org/apache/xmlgraphics/image/codec/util/
src/java/org/apache/xmlgraphics/image/loader/impl/
src/resources/META-INF/services/ test/java/org/a...
Author: ssteiner
Date: Fri Jun 5 12:18:54 2015
New Revision: 1683736
URL: http://svn.apache.org/r1683736
Log:
XGC-97: Add PreloaderRawPNG to handle images ImageIO cant
Added:
xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PreloaderRawPNG.java (with props)
Modified:
xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/png/PNGImageDecoder.java
xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/util/ImageInputStreamSeekableStreamAdapter.java
xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PNGFile.java
xmlgraphics/commons/trunk/src/resources/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImagePreloader
xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawPNGTestCase.java
Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/png/PNGImageDecoder.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/png/PNGImageDecoder.java?rev=1683736&r1=1683735&r2=1683736&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/png/PNGImageDecoder.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/png/PNGImageDecoder.java Fri Jun 5 12:18:54 2015
@@ -49,9 +49,14 @@ import java.util.TimeZone;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
+import javax.imageio.stream.ImageInputStream;
+
import org.apache.xmlgraphics.image.codec.util.ImageDecoderImpl;
+import org.apache.xmlgraphics.image.codec.util.ImageInputStreamSeekableStreamAdapter;
import org.apache.xmlgraphics.image.codec.util.PropertyUtil;
+import org.apache.xmlgraphics.image.codec.util.SeekableStream;
import org.apache.xmlgraphics.image.codec.util.SimpleRenderedImage;
+import org.apache.xmlgraphics.image.loader.ImageSize;
import org.apache.xmlgraphics.image.loader.impl.PNGConstants;
// CSOFF: ConstantName
@@ -80,6 +85,27 @@ public class PNGImageDecoder extends Ima
}
return new PNGImage(input, (PNGDecodeParam)param);
}
+
+ public static void readPNGHeader(ImageInputStream inputStream, ImageSize size) throws IOException {
+ SeekableStream seekStream = new ImageInputStreamSeekableStreamAdapter(inputStream) {
+ public void close() throws IOException {
+ }
+ };
+ PNGImage pngImage = new PNGImage(seekStream);
+ size.setSizeInPixels(pngImage.getWidth(), pngImage.getHeight());
+ double dpiHorz = size.getDpiHorizontal();
+ double dpiVert = size.getDpiVertical();
+ if (pngImage.unitSpecifier == 1) {
+ if (pngImage.xPixelsPerUnit != 0) {
+ dpiHorz = pngImage.xPixelsPerUnit * 0.0254;
+ }
+ if (pngImage.yPixelsPerUnit != 0) {
+ dpiVert = pngImage.yPixelsPerUnit * 0.0254;
+ }
+ }
+ size.setResolution(dpiHorz, dpiVert);
+ size.calcSizeFromPixels();
+ }
}
/**
@@ -173,6 +199,10 @@ class PNGImage extends SimpleRenderedIma
// Post-processing step implied by above parameters
private int postProcess = POST_NONE;
+ protected int xPixelsPerUnit;
+ protected int yPixelsPerUnit;
+ protected int unitSpecifier;
+
// Possible post-processing steps
// Do nothing
@@ -287,6 +317,29 @@ class PNGImage extends SimpleRenderedIma
}
}
+ public PNGImage(InputStream stream) throws IOException {
+ DataInputStream distream = new DataInputStream(stream);
+ long magic = distream.readLong();
+ if (magic != PNG_SIGNATURE) {
+ throw new IOException("Not a png file");
+ }
+ while (true) {
+ String chunkType = PNGChunk.getChunkType(distream);
+ if (chunkType.equals(PNGChunk.ChunkType.IHDR.name())) {
+ PNGChunk chunk = PNGChunk.readChunk(distream);
+ parse_IHDR_chunk(chunk);
+ } else if (chunkType.equals(PNGChunk.ChunkType.pHYs.name())) {
+ PNGChunk chunk = PNGChunk.readChunk(distream);
+ parse_pHYs_chunk(chunk);
+ return;
+ } else if (chunkType.equals(PNGChunk.ChunkType.IEND.name())) {
+ return;
+ } else {
+ PNGChunk.readChunk(distream);
+ }
+ }
+ }
+
public PNGImage(InputStream stream, PNGDecodeParam decodeParam)
throws IOException {
@@ -1032,9 +1085,9 @@ class PNGImage extends SimpleRenderedIma
}
private void parse_pHYs_chunk(PNGChunk chunk) {
- int xPixelsPerUnit = chunk.getInt4(0);
- int yPixelsPerUnit = chunk.getInt4(4);
- int unitSpecifier = chunk.getInt1(8);
+ xPixelsPerUnit = chunk.getInt4(0);
+ yPixelsPerUnit = chunk.getInt4(4);
+ unitSpecifier = chunk.getInt1(8);
if (encodeParam != null) {
encodeParam.setPhysicalDimension(xPixelsPerUnit,
Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/util/ImageInputStreamSeekableStreamAdapter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/util/ImageInputStreamSeekableStreamAdapter.java?rev=1683736&r1=1683735&r2=1683736&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/util/ImageInputStreamSeekableStreamAdapter.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/util/ImageInputStreamSeekableStreamAdapter.java Fri Jun 5 12:18:54 2015
@@ -27,7 +27,7 @@ import javax.imageio.stream.ImageInputSt
* A subclass of <code>SeekableStream</code> that may be used to wrap
* a regular <code>ImageInputStream</code>.
*/
-public final class ImageInputStreamSeekableStreamAdapter extends SeekableStream {
+public class ImageInputStreamSeekableStreamAdapter extends SeekableStream {
/** The source stream. */
private ImageInputStream stream;
Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PNGFile.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PNGFile.java?rev=1683736&r1=1683735&r2=1683736&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PNGFile.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PNGFile.java Fri Jun 5 12:18:54 2015
@@ -38,8 +38,6 @@ import java.util.List;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
-import org.apache.commons.io.IOUtils;
-
import org.apache.xmlgraphics.image.codec.png.PNGChunk;
import org.apache.xmlgraphics.image.codec.util.PropertyUtil;
import org.apache.xmlgraphics.image.loader.ImageException;
@@ -113,14 +111,14 @@ class PNGFile implements PNGConstants {
PNGChunk.skipChunk(distream);
}
} catch (Exception e) {
- e.printStackTrace();
String msg = PropertyUtil.getString("PNGImageDecoder2");
- throw new RuntimeException(msg);
+ throw new RuntimeException(msg, e);
}
} while (true);
}
public ImageRawPNG getImageRawPNG(ImageInfo info) throws ImageException {
+ InputStream seqStream = new SequenceInputStream(Collections.enumeration(streamVec));
ColorSpace rgbCS = null;
switch (colorType) {
case PNG_COLOR_GRAY:
@@ -171,26 +169,21 @@ class PNGFile implements PNGConstants {
throw new ImageException("Unsupported color type: " + colorType);
}
// the iccProfile is still null for now
- InputStream seqStream = null;
- ImageRawPNG rawImage = null;
- try {
- seqStream = new SequenceInputStream(Collections.enumeration(streamVec));
- rawImage = new ImageRawPNG(info, seqStream, colorModel, bitDepth, iccProfile);
- if (isTransparent) {
- if (colorType == PNG_COLOR_GRAY) {
- rawImage.setGrayTransparentAlpha(grayTransparentAlpha);
- } else if (colorType == PNG_COLOR_RGB) {
- rawImage.setRGBTransparentAlpha(redTransparentAlpha, greenTransparentAlpha,
- blueTransparentAlpha);
- } else if (colorType == PNG_COLOR_PALETTE) {
- rawImage.setTransparent();
- }
- }
- if (sRGBRenderingIntent != -1) {
- rawImage.setRenderingIntent(sRGBRenderingIntent);
+ ImageRawPNG rawImage = new ImageRawPNG(info, seqStream, colorModel, bitDepth, iccProfile);
+ if (isTransparent) {
+ if (colorType == PNG_COLOR_GRAY) {
+ rawImage.setGrayTransparentAlpha(grayTransparentAlpha);
+ } else if (colorType == PNG_COLOR_RGB) {
+ rawImage.setRGBTransparentAlpha(redTransparentAlpha, greenTransparentAlpha,
+ blueTransparentAlpha);
+ } else if (colorType == PNG_COLOR_PALETTE) {
+ rawImage.setTransparent();
+ } else {
+ //
}
- } finally {
- IOUtils.closeQuietly(seqStream);
+ }
+ if (sRGBRenderingIntent != -1) {
+ rawImage.setRenderingIntent(sRGBRenderingIntent);
}
return rawImage;
}
Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PreloaderRawPNG.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PreloaderRawPNG.java?rev=1683736&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PreloaderRawPNG.java (added)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PreloaderRawPNG.java Fri Jun 5 12:18:54 2015
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+package org.apache.xmlgraphics.image.loader.impl;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import javax.imageio.stream.ImageInputStream;
+import javax.xml.transform.Source;
+
+import org.apache.xmlgraphics.image.codec.png.PNGImageDecoder;
+import org.apache.xmlgraphics.image.loader.ImageContext;
+import org.apache.xmlgraphics.image.loader.ImageException;
+import org.apache.xmlgraphics.image.loader.ImageInfo;
+import org.apache.xmlgraphics.image.loader.ImageSize;
+import org.apache.xmlgraphics.image.loader.util.ImageUtil;
+
+public class PreloaderRawPNG extends AbstractImagePreloader {
+ public ImageInfo preloadImage(String uri, Source src, ImageContext context) throws ImageException, IOException {
+ if (!ImageUtil.hasImageInputStream(src)) {
+ return null;
+ }
+ ImageInputStream in = ImageUtil.needImageInputStream(src);
+ long bb = ByteBuffer.wrap(getHeader(in, 8)).getLong();
+ if (bb != PNGConstants.PNG_SIGNATURE) {
+ return null;
+ }
+ in.mark();
+ ImageSize size = new ImageSize();
+ //Resolution (first a default, then try to read the metadata)
+ size.setResolution(context.getSourceResolution());
+ try {
+ PNGImageDecoder.readPNGHeader(in, size);
+ } finally {
+ in.reset();
+ }
+
+ ImageInfo info = new ImageInfo(uri, "image/png");
+ info.setSize(size);
+ return info;
+ }
+
+ public int getPriority() {
+ return DEFAULT_PRIORITY * 2;
+ }
+}
Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PreloaderRawPNG.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: xmlgraphics/commons/trunk/src/resources/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImagePreloader
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/resources/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImagePreloader?rev=1683736&r1=1683735&r2=1683736&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/resources/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImagePreloader (original)
+++ xmlgraphics/commons/trunk/src/resources/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImagePreloader Fri Jun 5 12:18:54 2015
@@ -5,3 +5,4 @@ org.apache.xmlgraphics.image.loader.impl
org.apache.xmlgraphics.image.loader.impl.PreloaderEMF
org.apache.xmlgraphics.image.loader.impl.PreloaderEPS
org.apache.xmlgraphics.image.loader.impl.imageio.PreloaderImageIO
+org.apache.xmlgraphics.image.loader.impl.PreloaderRawPNG
\ No newline at end of file
Modified: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawPNGTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawPNGTestCase.java?rev=1683736&r1=1683735&r2=1683736&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawPNGTestCase.java (original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawPNGTestCase.java Fri Jun 5 12:18:54 2015
@@ -19,8 +19,12 @@
package org.apache.xmlgraphics.image.loader.impl;
+import java.io.FileInputStream;
import java.io.IOException;
+import javax.imageio.ImageIO;
+import javax.imageio.stream.ImageInputStream;
+
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@@ -33,6 +37,7 @@ import org.apache.xmlgraphics.image.load
import org.apache.xmlgraphics.image.loader.ImageFlavor;
import org.apache.xmlgraphics.image.loader.ImageInfo;
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
+import org.apache.xmlgraphics.image.loader.ImageSource;
import org.apache.xmlgraphics.image.loader.MockImageContext;
import org.apache.xmlgraphics.image.loader.MockImageSessionContext;
import org.apache.xmlgraphics.util.MimeConstants;
@@ -73,4 +78,12 @@ public class ImageLoaderRawPNGTestCase {
assertTrue(im instanceof ImageRawPNG);
}
+ @Test
+ public void testPreloaderRawPNG() throws IOException, ImageException {
+ ImageInputStream iis = ImageIO.createImageInputStream(new FileInputStream("test/images/tbbn3p08.png"));
+ ImageContext context = MockImageContext.newSafeInstance();
+ ImageInfo imageInfo = new PreloaderRawPNG().preloadImage(null, new ImageSource(iis, null, true), context);
+ assertEquals(imageInfo.getMimeType(), "image/png");
+ assertEquals(imageInfo.getSize().getWidthPx(), 32);
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org