您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ImageRawPNGAdapter.java 12KB

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