You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageRawPNGAdapter.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. // Original author: Matthias Reichenbacher
  19. package org.apache.fop.render.pdf;
  20. import java.awt.image.ColorModel;
  21. import java.awt.image.IndexColorModel;
  22. import java.io.DataInputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.OutputStream;
  26. import java.util.zip.Deflater;
  27. import java.util.zip.DeflaterOutputStream;
  28. import java.util.zip.Inflater;
  29. import java.util.zip.InflaterInputStream;
  30. import org.apache.commons.io.IOUtils;
  31. import org.apache.commons.io.output.ByteArrayOutputStream;
  32. import org.apache.commons.logging.Log;
  33. import org.apache.commons.logging.LogFactory;
  34. import org.apache.xmlgraphics.image.loader.impl.ImageRawPNG;
  35. import org.apache.xmlgraphics.image.loader.impl.ImageRawStream;
  36. import org.apache.fop.pdf.BitmapImage;
  37. import org.apache.fop.pdf.FlateFilter;
  38. import org.apache.fop.pdf.PDFColor;
  39. import org.apache.fop.pdf.PDFDeviceColorSpace;
  40. import org.apache.fop.pdf.PDFDictionary;
  41. import org.apache.fop.pdf.PDFDocument;
  42. import org.apache.fop.pdf.PDFFilter;
  43. import org.apache.fop.pdf.PDFFilterException;
  44. import org.apache.fop.pdf.PDFFilterList;
  45. import org.apache.fop.pdf.PDFICCStream;
  46. import org.apache.fop.pdf.PDFReference;
  47. public class ImageRawPNGAdapter extends AbstractImageAdapter {
  48. /** logging instance */
  49. private static Log log = LogFactory.getLog(ImageRawPNGAdapter.class);
  50. private PDFICCStream pdfICCStream;
  51. private PDFFilter pdfFilter;
  52. private String maskRef;
  53. private PDFReference softMask;
  54. private int numberOfInterleavedComponents;
  55. /**
  56. * Creates a new PDFImage from an Image instance.
  57. * @param image the image
  58. * @param key XObject key
  59. */
  60. public ImageRawPNGAdapter(ImageRawPNG image, String key) {
  61. super(image, key);
  62. }
  63. /** {@inheritDoc} */
  64. public void setup(PDFDocument doc) {
  65. super.setup(doc);
  66. ColorModel cm = ((ImageRawPNG) this.image).getColorModel();
  67. if (cm instanceof IndexColorModel) {
  68. numberOfInterleavedComponents = 1;
  69. } else {
  70. // this can be 1 (gray), 2 (gray + alpha), 3 (rgb) or 4 (rgb + alpha)
  71. // numberOfInterleavedComponents = (cm.hasAlpha() ? 1 : 0) + cm.getNumColorComponents();
  72. numberOfInterleavedComponents = cm.getNumComponents();
  73. }
  74. // set up image compression for non-alpha channel
  75. FlateFilter flate;
  76. try {
  77. flate = new FlateFilter();
  78. flate.setApplied(true);
  79. flate.setPredictor(FlateFilter.PREDICTION_PNG_OPT);
  80. if (numberOfInterleavedComponents < 3) {
  81. // means palette (1) or gray (1) or gray + alpha (2)
  82. flate.setColors(1);
  83. } else {
  84. // means rgb (3) or rgb + alpha (4)
  85. flate.setColors(3);
  86. }
  87. flate.setColumns(image.getSize().getWidthPx());
  88. flate.setBitsPerComponent(this.getBitsPerComponent());
  89. } catch (PDFFilterException e) {
  90. throw new RuntimeException("FlateFilter configuration error", e);
  91. }
  92. this.pdfFilter = flate;
  93. // Handle transparency channel if applicable; note that for palette images the transparency is
  94. // not TRANSLUCENT
  95. if (cm.hasAlpha() && cm.getTransparency() == ColorModel.TRANSLUCENT) {
  96. doc.getProfile().verifyTransparencyAllowed(image.getInfo().getOriginalURI());
  97. // TODO: Implement code to combine image with background color if transparency is not allowed
  98. // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha channel
  99. // and then deflate it back again
  100. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  101. DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater());
  102. InputStream in = ((ImageRawStream) image).createInputStream();
  103. try {
  104. InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());
  105. DataInputStream dataStream = new DataInputStream(infStream);
  106. // offset is the byte offset of the alpha component
  107. int offset = numberOfInterleavedComponents - 1; // 1 for GA, 3 for RGBA
  108. int numColumns = image.getSize().getWidthPx();
  109. int bytesPerRow = numberOfInterleavedComponents * numColumns;
  110. int filter;
  111. // read line by line; the first byte holds the filter
  112. while ((filter = dataStream.read()) != -1) {
  113. byte[] bytes = new byte[bytesPerRow];
  114. dataStream.readFully(bytes, 0, bytesPerRow);
  115. dos.write((byte) filter);
  116. for (int j = 0; j < numColumns; j++) {
  117. dos.write(bytes, offset, 1);
  118. offset += numberOfInterleavedComponents;
  119. }
  120. offset = numberOfInterleavedComponents - 1;
  121. }
  122. dos.close();
  123. } catch (IOException e) {
  124. throw new RuntimeException("Error processing transparency channel:", e);
  125. } finally {
  126. IOUtils.closeQuietly(in);
  127. }
  128. // set up alpha channel compression
  129. FlateFilter transFlate;
  130. try {
  131. transFlate = new FlateFilter();
  132. transFlate.setApplied(true);
  133. transFlate.setPredictor(FlateFilter.PREDICTION_PNG_OPT);
  134. transFlate.setColors(1);
  135. transFlate.setColumns(image.getSize().getWidthPx());
  136. transFlate.setBitsPerComponent(this.getBitsPerComponent());
  137. } catch (PDFFilterException e) {
  138. throw new RuntimeException("FlateFilter configuration error", e);
  139. }
  140. BitmapImage alphaMask = new BitmapImage("Mask:" + this.getKey(), image.getSize().getWidthPx(),
  141. image.getSize().getHeightPx(), baos.toByteArray(), null);
  142. alphaMask.setPDFFilter(transFlate);
  143. alphaMask.setColorSpace(new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_GRAY));
  144. softMask = doc.addImage(null, alphaMask).makeReference();
  145. }
  146. }
  147. /** {@inheritDoc} */
  148. public PDFDeviceColorSpace getColorSpace() {
  149. // DeviceGray, DeviceRGB, or DeviceCMYK
  150. return toPDFColorSpace(image.getColorSpace());
  151. }
  152. /** {@inheritDoc} */
  153. public int getBitsPerComponent() {
  154. return ((ImageRawPNG) this.image).getBitDepth();
  155. }
  156. /** {@inheritDoc} */
  157. public boolean isTransparent() {
  158. return ((ImageRawPNG) this.image).isTransparent();
  159. }
  160. /** {@inheritDoc} */
  161. public PDFColor getTransparentColor() {
  162. return new PDFColor(((ImageRawPNG) this.image).getTransparentColor());
  163. }
  164. /** {@inheritDoc} */
  165. public String getMask() {
  166. return maskRef;
  167. }
  168. /** {@inheritDoc} */
  169. public String getSoftMask() {
  170. return softMask.toString();
  171. }
  172. /** {@inheritDoc} */
  173. public PDFReference getSoftMaskReference() {
  174. return softMask;
  175. }
  176. /** {@inheritDoc} */
  177. public PDFFilter getPDFFilter() {
  178. return pdfFilter;
  179. }
  180. /** {@inheritDoc} */
  181. public void outputContents(OutputStream out) throws IOException {
  182. InputStream in = ((ImageRawStream) image).createInputStream();
  183. try {
  184. if (numberOfInterleavedComponents == 1 || numberOfInterleavedComponents == 3) {
  185. // means we have Gray, RGB, or Palette
  186. IOUtils.copy(in, out);
  187. } else {
  188. // means we have Gray + alpha or RGB + alpha
  189. // TODO: since we have alpha here do this when the alpha channel is extracted
  190. int numBytes = numberOfInterleavedComponents - 1; // 1 for Gray, 3 for RGB
  191. int numColumns = image.getSize().getWidthPx();
  192. InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());
  193. DataInputStream dataStream = new DataInputStream(infStream);
  194. int offset = 0;
  195. int bytesPerRow = numberOfInterleavedComponents * numColumns;
  196. int filter;
  197. // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha
  198. // channel and then deflate the RGB channels back again
  199. DeflaterOutputStream dos = new DeflaterOutputStream(out, new Deflater());
  200. while ((filter = dataStream.read()) != -1) {
  201. byte[] bytes = new byte[bytesPerRow];
  202. dataStream.readFully(bytes, 0, bytesPerRow);
  203. dos.write((byte) filter);
  204. for (int j = 0; j < numColumns; j++) {
  205. dos.write(bytes, offset, numBytes);
  206. offset += numberOfInterleavedComponents;
  207. }
  208. offset = 0;
  209. }
  210. dos.close();
  211. }
  212. } finally {
  213. IOUtils.closeQuietly(in);
  214. }
  215. }
  216. /** {@inheritDoc} */
  217. public PDFICCStream getICCStream() {
  218. return pdfICCStream;
  219. }
  220. /** {@inheritDoc} */
  221. public String getFilterHint() {
  222. return PDFFilterList.PRECOMPRESSED_FILTER;
  223. }
  224. public void populateXObjectDictionary(PDFDictionary dict) {
  225. ColorModel cm = ((ImageRawPNG) image).getColorModel();
  226. if (cm instanceof IndexColorModel) {
  227. IndexColorModel icm = (IndexColorModel) cm;
  228. super.populateXObjectDictionaryForIndexColorModel(dict, icm);
  229. }
  230. }
  231. }