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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.BufferedOutputStream;
  23. import java.io.DataInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.OutputStream;
  27. import java.util.zip.Deflater;
  28. import java.util.zip.DeflaterOutputStream;
  29. import java.util.zip.Inflater;
  30. import java.util.zip.InflaterInputStream;
  31. import org.apache.commons.io.IOUtils;
  32. import org.apache.commons.io.output.ByteArrayOutputStream;
  33. import org.apache.commons.logging.Log;
  34. import org.apache.commons.logging.LogFactory;
  35. import org.apache.xmlgraphics.image.loader.impl.ImageRawPNG;
  36. import org.apache.xmlgraphics.image.loader.impl.ImageRawStream;
  37. import org.apache.fop.pdf.BitmapImage;
  38. import org.apache.fop.pdf.FlateFilter;
  39. import org.apache.fop.pdf.PDFColor;
  40. import org.apache.fop.pdf.PDFDeviceColorSpace;
  41. import org.apache.fop.pdf.PDFDictionary;
  42. import org.apache.fop.pdf.PDFDocument;
  43. import org.apache.fop.pdf.PDFFilter;
  44. import org.apache.fop.pdf.PDFFilterException;
  45. import org.apache.fop.pdf.PDFFilterList;
  46. import org.apache.fop.pdf.PDFName;
  47. import org.apache.fop.pdf.PDFReference;
  48. public class ImageRawPNGAdapter extends AbstractImageAdapter {
  49. /** logging instance */
  50. private static Log log = LogFactory.getLog(ImageRawPNGAdapter.class);
  51. private static final PDFName RI_PERCEPTUAL = new PDFName("Perceptual");
  52. private static final PDFName RI_RELATIVE_COLORIMETRIC = new PDFName("RelativeColorimetric");
  53. private static final PDFName RI_SATURATION = new PDFName("Saturation");
  54. private static final PDFName RI_ABSOLUTE_COLORIMETRIC = new PDFName("AbsoluteColorimetric");
  55. private PDFFilter pdfFilter;
  56. private String maskRef;
  57. private PDFReference softMask;
  58. private int numberOfInterleavedComponents;
  59. /**
  60. * Creates a new PDFImage from an Image instance.
  61. * @param image the image
  62. * @param key XObject key
  63. */
  64. public ImageRawPNGAdapter(ImageRawPNG image, String key) {
  65. super(image, key);
  66. }
  67. /** {@inheritDoc} */
  68. public void setup(PDFDocument doc) {
  69. super.setup(doc);
  70. ColorModel cm = ((ImageRawPNG) this.image).getColorModel();
  71. if (cm instanceof IndexColorModel) {
  72. numberOfInterleavedComponents = 1;
  73. } else {
  74. // this can be 1 (gray), 2 (gray + alpha), 3 (rgb) or 4 (rgb + alpha)
  75. // numberOfInterleavedComponents = (cm.hasAlpha() ? 1 : 0) + cm.getNumColorComponents();
  76. numberOfInterleavedComponents = cm.getNumComponents();
  77. }
  78. // set up image compression for non-alpha channel
  79. FlateFilter flate;
  80. try {
  81. flate = new FlateFilter();
  82. flate.setApplied(true);
  83. flate.setPredictor(FlateFilter.PREDICTION_PNG_OPT);
  84. if (numberOfInterleavedComponents < 3) {
  85. // means palette (1) or gray (1) or gray + alpha (2)
  86. flate.setColors(1);
  87. } else {
  88. // means rgb (3) or rgb + alpha (4)
  89. flate.setColors(3);
  90. }
  91. flate.setColumns(image.getSize().getWidthPx());
  92. flate.setBitsPerComponent(this.getBitsPerComponent());
  93. } catch (PDFFilterException e) {
  94. throw new RuntimeException("FlateFilter configuration error", e);
  95. }
  96. this.pdfFilter = flate;
  97. this.disallowMultipleFilters();
  98. // Handle transparency channel if applicable; note that for palette images the transparency is
  99. // not TRANSLUCENT
  100. if (cm.hasAlpha() && cm.getTransparency() == ColorModel.TRANSLUCENT) {
  101. doc.getProfile().verifyTransparencyAllowed(image.getInfo().getOriginalURI());
  102. // TODO: Implement code to combine image with background color if transparency is not allowed
  103. // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha channel
  104. // and then deflate it back again
  105. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  106. BufferedOutputStream dos = new BufferedOutputStream(new DeflaterOutputStream(baos, new Deflater()));
  107. InputStream in = ((ImageRawStream) image).createInputStream();
  108. try {
  109. InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());
  110. DataInputStream dataStream = new DataInputStream(infStream);
  111. // offset is the byte offset of the alpha component
  112. int offset = numberOfInterleavedComponents - 1; // 1 for GA, 3 for RGBA
  113. int numColumns = image.getSize().getWidthPx();
  114. int bytesPerRow = numberOfInterleavedComponents * numColumns;
  115. int filter;
  116. // read line by line; the first byte holds the filter
  117. while ((filter = dataStream.read()) != -1) {
  118. byte[] bytes = new byte[bytesPerRow];
  119. dataStream.readFully(bytes, 0, bytesPerRow);
  120. dos.write((byte) filter);
  121. for (int j = 0; j < numColumns; j++) {
  122. dos.write(bytes, offset, 1);
  123. offset += numberOfInterleavedComponents;
  124. }
  125. offset = numberOfInterleavedComponents - 1;
  126. }
  127. dos.close();
  128. } catch (IOException e) {
  129. throw new RuntimeException("Error processing transparency channel:", e);
  130. } finally {
  131. IOUtils.closeQuietly(in);
  132. }
  133. // set up alpha channel compression
  134. FlateFilter transFlate;
  135. try {
  136. transFlate = new FlateFilter();
  137. transFlate.setApplied(true);
  138. transFlate.setPredictor(FlateFilter.PREDICTION_PNG_OPT);
  139. transFlate.setColors(1);
  140. transFlate.setColumns(image.getSize().getWidthPx());
  141. transFlate.setBitsPerComponent(this.getBitsPerComponent());
  142. } catch (PDFFilterException e) {
  143. throw new RuntimeException("FlateFilter configuration error", e);
  144. }
  145. BitmapImage alphaMask = new BitmapImage("Mask:" + this.getKey(), image.getSize().getWidthPx(),
  146. image.getSize().getHeightPx(), baos.toByteArray(), null);
  147. alphaMask.setPDFFilter(transFlate);
  148. alphaMask.disallowMultipleFilters();
  149. alphaMask.setColorSpace(new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_GRAY));
  150. softMask = doc.addImage(null, alphaMask).makeReference();
  151. }
  152. }
  153. /** {@inheritDoc} */
  154. public PDFDeviceColorSpace getColorSpace() {
  155. // DeviceGray, DeviceRGB, or DeviceCMYK
  156. return toPDFColorSpace(image.getColorSpace());
  157. }
  158. /** {@inheritDoc} */
  159. public int getBitsPerComponent() {
  160. return ((ImageRawPNG) this.image).getBitDepth();
  161. }
  162. /** {@inheritDoc} */
  163. public boolean isTransparent() {
  164. return ((ImageRawPNG) this.image).isTransparent();
  165. }
  166. /** {@inheritDoc} */
  167. public PDFColor getTransparentColor() {
  168. return new PDFColor(((ImageRawPNG) this.image).getTransparentColor());
  169. }
  170. /** {@inheritDoc} */
  171. public String getMask() {
  172. return maskRef;
  173. }
  174. /** {@inheritDoc} */
  175. public String getSoftMask() {
  176. return softMask.toString();
  177. }
  178. /** {@inheritDoc} */
  179. public PDFReference getSoftMaskReference() {
  180. return softMask;
  181. }
  182. /** {@inheritDoc} */
  183. public PDFFilter getPDFFilter() {
  184. return pdfFilter;
  185. }
  186. /** {@inheritDoc} */
  187. public void outputContents(OutputStream out) throws IOException {
  188. InputStream in = ((ImageRawStream) image).createInputStream();
  189. try {
  190. if (numberOfInterleavedComponents == 1 || numberOfInterleavedComponents == 3) {
  191. // means we have Gray, RGB, or Palette
  192. IOUtils.copy(in, out);
  193. } else {
  194. // means we have Gray + alpha or RGB + alpha
  195. // TODO: since we have alpha here do this when the alpha channel is extracted
  196. int numBytes = numberOfInterleavedComponents - 1; // 1 for Gray, 3 for RGB
  197. int numColumns = image.getSize().getWidthPx();
  198. InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());
  199. DataInputStream dataStream = new DataInputStream(infStream);
  200. int offset = 0;
  201. int bytesPerRow = numberOfInterleavedComponents * numColumns;
  202. int filter;
  203. // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha
  204. // channel and then deflate the RGB channels back again
  205. BufferedOutputStream dos = new BufferedOutputStream(new DeflaterOutputStream(out, new Deflater()));
  206. while ((filter = dataStream.read()) != -1) {
  207. byte[] bytes = new byte[bytesPerRow];
  208. dataStream.readFully(bytes, 0, bytesPerRow);
  209. dos.write((byte) filter);
  210. for (int j = 0; j < numColumns; j++) {
  211. dos.write(bytes, offset, numBytes);
  212. offset += numberOfInterleavedComponents;
  213. }
  214. offset = 0;
  215. }
  216. dos.close();
  217. }
  218. } finally {
  219. IOUtils.closeQuietly(in);
  220. }
  221. }
  222. /** {@inheritDoc} */
  223. public String getFilterHint() {
  224. return PDFFilterList.PRECOMPRESSED_FILTER;
  225. }
  226. public void populateXObjectDictionary(PDFDictionary dict) {
  227. int renderingIntent = ((ImageRawPNG) image).getRenderingIntent();
  228. if (renderingIntent != -1) {
  229. switch (renderingIntent) {
  230. case 0:
  231. dict.put("Intent", RI_PERCEPTUAL);
  232. break;
  233. case 1:
  234. dict.put("Intent", RI_RELATIVE_COLORIMETRIC);
  235. break;
  236. case 2:
  237. dict.put("Intent", RI_SATURATION);
  238. break;
  239. case 3:
  240. dict.put("Intent", RI_ABSOLUTE_COLORIMETRIC);
  241. break;
  242. default:
  243. // ignore
  244. }
  245. }
  246. ColorModel cm = ((ImageRawPNG) image).getColorModel();
  247. if (cm instanceof IndexColorModel) {
  248. IndexColorModel icm = (IndexColorModel) cm;
  249. super.populateXObjectDictionaryForIndexColorModel(dict, icm);
  250. }
  251. }
  252. protected boolean issRGB() {
  253. if (((ImageRawPNG) image).getRenderingIntent() != -1) {
  254. return true;
  255. }
  256. return false;
  257. }
  258. }