// TODO: Implement code to combine image with background color if transparency is not allowed\r
// here we need to inflate the PNG pixel data, which includes alpha, separate the alpha channel\r
// and then deflate it back again\r
- ByteArrayOutputStream baos = null;\r
- DeflaterOutputStream dos = null;\r
- InputStream in = null;\r
- InflaterInputStream infStream = null;\r
- DataInputStream dataStream = null;\r
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
+ DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater());\r
+ InputStream in = ((ImageRawStream) image).createInputStream();\r
try {\r
- baos = new ByteArrayOutputStream();\r
- dos = new DeflaterOutputStream(baos, new Deflater());\r
- in = ((ImageRawStream) image).createInputStream();\r
- try {\r
- infStream = new InflaterInputStream(in, new Inflater());\r
- dataStream = new DataInputStream(infStream);\r
- // offset is the byte offset of the alpha component\r
- int offset = numberOfInterleavedComponents - 1; // 1 for GA, 3 for RGBA\r
- int numColumns = image.getSize().getWidthPx();\r
- int bytesPerRow = numberOfInterleavedComponents * numColumns;\r
- int filter;\r
- // read line by line; the first byte holds the filter\r
- while ((filter = dataStream.read()) != -1) {\r
- byte[] bytes = new byte[bytesPerRow];\r
- dataStream.readFully(bytes, 0, bytesPerRow);\r
- dos.write((byte) filter);\r
- for (int j = 0; j < numColumns; j++) {\r
- dos.write(bytes, offset, 1);\r
- offset += numberOfInterleavedComponents;\r
- }\r
- offset = numberOfInterleavedComponents - 1;\r
+ InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());\r
+ DataInputStream dataStream = new DataInputStream(infStream);\r
+ // offset is the byte offset of the alpha component\r
+ int offset = numberOfInterleavedComponents - 1; // 1 for GA, 3 for RGBA\r
+ int numColumns = image.getSize().getWidthPx();\r
+ int bytesPerRow = numberOfInterleavedComponents * numColumns;\r
+ int filter;\r
+ // read line by line; the first byte holds the filter\r
+ while ((filter = dataStream.read()) != -1) {\r
+ byte[] bytes = new byte[bytesPerRow];\r
+ dataStream.readFully(bytes, 0, bytesPerRow);\r
+ dos.write((byte) filter);\r
+ for (int j = 0; j < numColumns; j++) {\r
+ dos.write(bytes, offset, 1);\r
+ offset += numberOfInterleavedComponents;\r
}\r
- } catch (IOException e) {\r
- throw new RuntimeException("Error processing transparency channel:", e);\r
- }\r
- // set up alpha channel compression\r
- FlateFilter transFlate;\r
- try {\r
- transFlate = new FlateFilter();\r
- transFlate.setApplied(true);\r
- transFlate.setPredictor(FlateFilter.PREDICTION_PNG_OPT);\r
- transFlate.setColors(1);\r
- transFlate.setColumns(image.getSize().getWidthPx());\r
- transFlate.setBitsPerComponent(this.getBitsPerComponent());\r
- } catch (PDFFilterException e) {\r
- throw new RuntimeException("FlateFilter configuration error", e);\r
+ offset = numberOfInterleavedComponents - 1;\r
}\r
- BitmapImage alphaMask = new BitmapImage("Mask:" + this.getKey(), image.getSize().getWidthPx(),\r
- image.getSize().getHeightPx(), baos.toByteArray(), null);\r
- alphaMask.setPDFFilter(transFlate);\r
- alphaMask.disallowMultipleFilters();\r
- alphaMask.setColorSpace(new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_GRAY));\r
- softMask = doc.addImage(null, alphaMask).makeReference();\r
+ dos.close();\r
+ } catch (IOException e) {\r
+ throw new RuntimeException("Error processing transparency channel:", e);\r
} finally {\r
- IOUtils.closeQuietly(infStream);\r
- IOUtils.closeQuietly(dataStream);\r
IOUtils.closeQuietly(in);\r
- IOUtils.closeQuietly(dos);\r
- IOUtils.closeQuietly(baos);\r
}\r
+ // set up alpha channel compression\r
+ FlateFilter transFlate;\r
+ try {\r
+ transFlate = new FlateFilter();\r
+ transFlate.setApplied(true);\r
+ transFlate.setPredictor(FlateFilter.PREDICTION_PNG_OPT);\r
+ transFlate.setColors(1);\r
+ transFlate.setColumns(image.getSize().getWidthPx());\r
+ transFlate.setBitsPerComponent(this.getBitsPerComponent());\r
+ } catch (PDFFilterException e) {\r
+ throw new RuntimeException("FlateFilter configuration error", e);\r
+ }\r
+ BitmapImage alphaMask = new BitmapImage("Mask:" + this.getKey(), image.getSize().getWidthPx(),\r
+ image.getSize().getHeightPx(), baos.toByteArray(), null);\r
+ alphaMask.setPDFFilter(transFlate);\r
+ alphaMask.disallowMultipleFilters();\r
+ alphaMask.setColorSpace(new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_GRAY));\r
+ softMask = doc.addImage(null, alphaMask).makeReference();\r
}\r
}\r
\r
/** {@inheritDoc} */\r
public void outputContents(OutputStream out) throws IOException {\r
InputStream in = ((ImageRawStream) image).createInputStream();\r
- InflaterInputStream infStream = null;\r
- DataInputStream dataStream = null;\r
- DeflaterOutputStream dos = null;\r
+\r
try {\r
if (numberOfInterleavedComponents == 1 || numberOfInterleavedComponents == 3) {\r
// means we have Gray, RGB, or Palette\r
// TODO: since we have alpha here do this when the alpha channel is extracted\r
int numBytes = numberOfInterleavedComponents - 1; // 1 for Gray, 3 for RGB\r
int numColumns = image.getSize().getWidthPx();\r
- infStream = new InflaterInputStream(in, new Inflater());\r
- dataStream = new DataInputStream(infStream);\r
+ InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());\r
+ DataInputStream dataStream = new DataInputStream(infStream);\r
int offset = 0;\r
int bytesPerRow = numberOfInterleavedComponents * numColumns;\r
int filter;\r
// here we need to inflate the PNG pixel data, which includes alpha, separate the alpha\r
// channel and then deflate the RGB channels back again\r
- dos = new DeflaterOutputStream(out, new Deflater());\r
+ DeflaterOutputStream dos = new DeflaterOutputStream(out, new Deflater());\r
while ((filter = dataStream.read()) != -1) {\r
byte[] bytes = new byte[bytesPerRow];\r
dataStream.readFully(bytes, 0, bytesPerRow);\r
}\r
offset = 0;\r
}\r
+ dos.close();\r
}\r
} finally {\r
- IOUtils.closeQuietly(dos);\r
- IOUtils.closeQuietly(dataStream);\r
- IOUtils.closeQuietly(infStream);\r
IOUtils.closeQuietly(in);\r
}\r
}\r