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

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