aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSimon Steiner <ssteiner@apache.org>2015-06-05 12:19:21 +0000
committerSimon Steiner <ssteiner@apache.org>2015-06-05 12:19:21 +0000
commit8470af9fd9972bedce303a5110724230af163520 (patch)
treed0ca7b9dbd67d5cf19bb160effa437471c924410 /src
parent0fb8462107767bb0b39f6cd872542722ac683b8e (diff)
downloadxmlgraphics-fop-8470af9fd9972bedce303a5110724230af163520.tar.gz
xmlgraphics-fop-8470af9fd9972bedce303a5110724230af163520.zip
XGC-97: Add PreloaderRawPNG to handle images ImageIO cant
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1683737 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r--src/java/org/apache/fop/render/pdf/ImageRawPNGAdapter.java104
1 files changed, 45 insertions, 59 deletions
diff --git a/src/java/org/apache/fop/render/pdf/ImageRawPNGAdapter.java b/src/java/org/apache/fop/render/pdf/ImageRawPNGAdapter.java
index f40ca6d94..9742036b7 100644
--- a/src/java/org/apache/fop/render/pdf/ImageRawPNGAdapter.java
+++ b/src/java/org/apache/fop/render/pdf/ImageRawPNGAdapter.java
@@ -116,62 +116,52 @@ public class ImageRawPNGAdapter extends AbstractImageAdapter {
// TODO: Implement code to combine image with background color if transparency is not allowed
// here we need to inflate the PNG pixel data, which includes alpha, separate the alpha channel
// and then deflate it back again
- ByteArrayOutputStream baos = null;
- DeflaterOutputStream dos = null;
- InputStream in = null;
- InflaterInputStream infStream = null;
- DataInputStream dataStream = null;
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater());
+ InputStream in = ((ImageRawStream) image).createInputStream();
try {
- baos = new ByteArrayOutputStream();
- dos = new DeflaterOutputStream(baos, new Deflater());
- in = ((ImageRawStream) image).createInputStream();
- try {
- infStream = new InflaterInputStream(in, new Inflater());
- dataStream = new DataInputStream(infStream);
- // offset is the byte offset of the alpha component
- int offset = numberOfInterleavedComponents - 1; // 1 for GA, 3 for RGBA
- int numColumns = image.getSize().getWidthPx();
- int bytesPerRow = numberOfInterleavedComponents * numColumns;
- int filter;
- // read line by line; the first byte holds the filter
- while ((filter = dataStream.read()) != -1) {
- byte[] bytes = new byte[bytesPerRow];
- dataStream.readFully(bytes, 0, bytesPerRow);
- dos.write((byte) filter);
- for (int j = 0; j < numColumns; j++) {
- dos.write(bytes, offset, 1);
- offset += numberOfInterleavedComponents;
- }
- offset = numberOfInterleavedComponents - 1;
+ InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());
+ DataInputStream dataStream = new DataInputStream(infStream);
+ // offset is the byte offset of the alpha component
+ int offset = numberOfInterleavedComponents - 1; // 1 for GA, 3 for RGBA
+ int numColumns = image.getSize().getWidthPx();
+ int bytesPerRow = numberOfInterleavedComponents * numColumns;
+ int filter;
+ // read line by line; the first byte holds the filter
+ while ((filter = dataStream.read()) != -1) {
+ byte[] bytes = new byte[bytesPerRow];
+ dataStream.readFully(bytes, 0, bytesPerRow);
+ dos.write((byte) filter);
+ for (int j = 0; j < numColumns; j++) {
+ dos.write(bytes, offset, 1);
+ offset += numberOfInterleavedComponents;
}
- } catch (IOException e) {
- throw new RuntimeException("Error processing transparency channel:", e);
- }
- // set up alpha channel compression
- FlateFilter transFlate;
- try {
- transFlate = new FlateFilter();
- transFlate.setApplied(true);
- transFlate.setPredictor(FlateFilter.PREDICTION_PNG_OPT);
- transFlate.setColors(1);
- transFlate.setColumns(image.getSize().getWidthPx());
- transFlate.setBitsPerComponent(this.getBitsPerComponent());
- } catch (PDFFilterException e) {
- throw new RuntimeException("FlateFilter configuration error", e);
+ offset = numberOfInterleavedComponents - 1;
}
- BitmapImage alphaMask = new BitmapImage("Mask:" + this.getKey(), image.getSize().getWidthPx(),
- image.getSize().getHeightPx(), baos.toByteArray(), null);
- alphaMask.setPDFFilter(transFlate);
- alphaMask.disallowMultipleFilters();
- alphaMask.setColorSpace(new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_GRAY));
- softMask = doc.addImage(null, alphaMask).makeReference();
+ dos.close();
+ } catch (IOException e) {
+ throw new RuntimeException("Error processing transparency channel:", e);
} finally {
- IOUtils.closeQuietly(infStream);
- IOUtils.closeQuietly(dataStream);
IOUtils.closeQuietly(in);
- IOUtils.closeQuietly(dos);
- IOUtils.closeQuietly(baos);
}
+ // set up alpha channel compression
+ FlateFilter transFlate;
+ try {
+ transFlate = new FlateFilter();
+ transFlate.setApplied(true);
+ transFlate.setPredictor(FlateFilter.PREDICTION_PNG_OPT);
+ transFlate.setColors(1);
+ transFlate.setColumns(image.getSize().getWidthPx());
+ transFlate.setBitsPerComponent(this.getBitsPerComponent());
+ } catch (PDFFilterException e) {
+ throw new RuntimeException("FlateFilter configuration error", e);
+ }
+ BitmapImage alphaMask = new BitmapImage("Mask:" + this.getKey(), image.getSize().getWidthPx(),
+ image.getSize().getHeightPx(), baos.toByteArray(), null);
+ alphaMask.setPDFFilter(transFlate);
+ alphaMask.disallowMultipleFilters();
+ alphaMask.setColorSpace(new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_GRAY));
+ softMask = doc.addImage(null, alphaMask).makeReference();
}
}
@@ -219,9 +209,7 @@ public class ImageRawPNGAdapter extends AbstractImageAdapter {
/** {@inheritDoc} */
public void outputContents(OutputStream out) throws IOException {
InputStream in = ((ImageRawStream) image).createInputStream();
- InflaterInputStream infStream = null;
- DataInputStream dataStream = null;
- DeflaterOutputStream dos = null;
+
try {
if (numberOfInterleavedComponents == 1 || numberOfInterleavedComponents == 3) {
// means we have Gray, RGB, or Palette
@@ -231,14 +219,14 @@ public class ImageRawPNGAdapter extends AbstractImageAdapter {
// TODO: since we have alpha here do this when the alpha channel is extracted
int numBytes = numberOfInterleavedComponents - 1; // 1 for Gray, 3 for RGB
int numColumns = image.getSize().getWidthPx();
- infStream = new InflaterInputStream(in, new Inflater());
- dataStream = new DataInputStream(infStream);
+ InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());
+ DataInputStream dataStream = new DataInputStream(infStream);
int offset = 0;
int bytesPerRow = numberOfInterleavedComponents * numColumns;
int filter;
// here we need to inflate the PNG pixel data, which includes alpha, separate the alpha
// channel and then deflate the RGB channels back again
- dos = new DeflaterOutputStream(out, new Deflater());
+ DeflaterOutputStream dos = new DeflaterOutputStream(out, new Deflater());
while ((filter = dataStream.read()) != -1) {
byte[] bytes = new byte[bytesPerRow];
dataStream.readFully(bytes, 0, bytesPerRow);
@@ -249,11 +237,9 @@ public class ImageRawPNGAdapter extends AbstractImageAdapter {
}
offset = 0;
}
+ dos.close();
}
} finally {
- IOUtils.closeQuietly(dos);
- IOUtils.closeQuietly(dataStream);
- IOUtils.closeQuietly(infStream);
IOUtils.closeQuietly(in);
}
}