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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. this.disallowMultipleFilters();
  94. // Handle transparency channel if applicable; note that for palette images the transparency is
  95. // not TRANSLUCENT
  96. if (cm.hasAlpha() && cm.getTransparency() == ColorModel.TRANSLUCENT) {
  97. doc.getProfile().verifyTransparencyAllowed(image.getInfo().getOriginalURI());
  98. // TODO: Implement code to combine image with background color if transparency is not allowed
  99. // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha channel
  100. // and then deflate it back again
  101. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  102. DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater());
  103. InputStream in = ((ImageRawStream) image).createInputStream();
  104. try {
  105. InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());
  106. DataInputStream dataStream = new DataInputStream(infStream);
  107. // offset is the byte offset of the alpha component
  108. int offset = numberOfInterleavedComponents - 1; // 1 for GA, 3 for RGBA
  109. int numColumns = image.getSize().getWidthPx();
  110. int bytesPerRow = numberOfInterleavedComponents * numColumns;
  111. int filter;
  112. // read line by line; the first byte holds the filter
  113. while ((filter = dataStream.read()) != -1) {
  114. byte[] bytes = new byte[bytesPerRow];
  115. dataStream.readFully(bytes, 0, bytesPerRow);
  116. dos.write((byte) filter);
  117. for (int j = 0; j < numColumns; j++) {
  118. dos.write(bytes, offset, 1);
  119. offset += numberOfInterleavedComponents;
  120. }
  121. offset = numberOfInterleavedComponents - 1;
  122. }
  123. dos.close();
  124. } catch (IOException e) {
  125. throw new RuntimeException("Error processing transparency channel:", e);
  126. } finally {
  127. IOUtils.closeQuietly(in);
  128. }
  129. // set up alpha channel compression
  130. FlateFilter transFlate;
  131. try {
  132. transFlate = new FlateFilter();
  133. transFlate.setApplied(true);
  134. transFlate.setPredictor(FlateFilter.PREDICTION_PNG_OPT);
  135. transFlate.setColors(1);
  136. transFlate.setColumns(image.getSize().getWidthPx());
  137. transFlate.setBitsPerComponent(this.getBitsPerComponent());
  138. } catch (PDFFilterException e) {
  139. throw new RuntimeException("FlateFilter configuration error", e);
  140. }
  141. BitmapImage alphaMask = new BitmapImage("Mask:" + this.getKey(), image.getSize().getWidthPx(),
  142. image.getSize().getHeightPx(), baos.toByteArray(), null);
  143. alphaMask.setPDFFilter(transFlate);
  144. alphaMask.disallowMultipleFilters();
  145. alphaMask.setColorSpace(new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_GRAY));
  146. softMask = doc.addImage(null, alphaMask).makeReference();
  147. }
  148. }
  149. /** {@inheritDoc} */
  150. public PDFDeviceColorSpace getColorSpace() {
  151. // DeviceGray, DeviceRGB, or DeviceCMYK
  152. return toPDFColorSpace(image.getColorSpace());
  153. }
  154. /** {@inheritDoc} */
  155. public int getBitsPerComponent() {
  156. return ((ImageRawPNG) this.image).getBitDepth();
  157. }
  158. /** {@inheritDoc} */
  159. public boolean isTransparent() {
  160. return ((ImageRawPNG) this.image).isTransparent();
  161. }
  162. /** {@inheritDoc} */
  163. public PDFColor getTransparentColor() {
  164. return new PDFColor(((ImageRawPNG) this.image).getTransparentColor());
  165. }
  166. /** {@inheritDoc} */
  167. public String getMask() {
  168. return maskRef;
  169. }
  170. /** {@inheritDoc} */
  171. public String getSoftMask() {
  172. return softMask.toString();
  173. }
  174. /** {@inheritDoc} */
  175. public PDFReference getSoftMaskReference() {
  176. return softMask;
  177. }
  178. /** {@inheritDoc} */
  179. public PDFFilter getPDFFilter() {
  180. return pdfFilter;
  181. }
  182. /** {@inheritDoc} */
  183. public void outputContents(OutputStream out) throws IOException {
  184. InputStream in = ((ImageRawStream) image).createInputStream();
  185. try {
  186. if (numberOfInterleavedComponents == 1 || numberOfInterleavedComponents == 3) {
  187. // means we have Gray, RGB, or Palette
  188. IOUtils.copy(in, out);
  189. } else {
  190. // means we have Gray + alpha or RGB + alpha
  191. // TODO: since we have alpha here do this when the alpha channel is extracted
  192. int numBytes = numberOfInterleavedComponents - 1; // 1 for Gray, 3 for RGB
  193. int numColumns = image.getSize().getWidthPx();
  194. InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());
  195. DataInputStream dataStream = new DataInputStream(infStream);
  196. int offset = 0;
  197. int bytesPerRow = numberOfInterleavedComponents * numColumns;
  198. int filter;
  199. // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha
  200. // channel and then deflate the RGB channels back again
  201. DeflaterOutputStream dos = new DeflaterOutputStream(out, new Deflater());
  202. while ((filter = dataStream.read()) != -1) {
  203. byte[] bytes = new byte[bytesPerRow];
  204. dataStream.readFully(bytes, 0, bytesPerRow);
  205. dos.write((byte) filter);
  206. for (int j = 0; j < numColumns; j++) {
  207. dos.write(bytes, offset, numBytes);
  208. offset += numberOfInterleavedComponents;
  209. }
  210. offset = 0;
  211. }
  212. dos.close();
  213. }
  214. } finally {
  215. IOUtils.closeQuietly(in);
  216. }
  217. }
  218. /** {@inheritDoc} */
  219. public PDFICCStream getICCStream() {
  220. return pdfICCStream;
  221. }
  222. /** {@inheritDoc} */
  223. public String getFilterHint() {
  224. return PDFFilterList.PRECOMPRESSED_FILTER;
  225. }
  226. public void populateXObjectDictionary(PDFDictionary dict) {
  227. ColorModel cm = ((ImageRawPNG) image).getColorModel();
  228. if (cm instanceof IndexColorModel) {
  229. IndexColorModel icm = (IndexColorModel) cm;
  230. super.populateXObjectDictionaryForIndexColorModel(dict, icm);
  231. }
  232. }
  233. }