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.

ImageRenderedAdapter.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. package org.apache.fop.render.pdf;
  19. import java.awt.color.ColorSpace;
  20. import java.awt.image.ColorModel;
  21. import java.awt.image.IndexColorModel;
  22. import java.awt.image.RenderedImage;
  23. import java.io.IOException;
  24. import java.io.OutputStream;
  25. import org.apache.commons.io.output.ByteArrayOutputStream;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
  29. import org.apache.xmlgraphics.ps.ImageEncodingHelper;
  30. import org.apache.fop.pdf.AlphaRasterImage;
  31. import org.apache.fop.pdf.PDFArray;
  32. import org.apache.fop.pdf.PDFColor;
  33. import org.apache.fop.pdf.PDFDeviceColorSpace;
  34. import org.apache.fop.pdf.PDFDictionary;
  35. import org.apache.fop.pdf.PDFDocument;
  36. import org.apache.fop.pdf.PDFFilter;
  37. import org.apache.fop.pdf.PDFFilterList;
  38. import org.apache.fop.pdf.PDFName;
  39. import org.apache.fop.pdf.PDFReference;
  40. /**
  41. * PDFImage implementation for the PDF renderer which handles RenderedImages.
  42. */
  43. public class ImageRenderedAdapter extends AbstractImageAdapter {
  44. /** logging instance */
  45. private static Log log = LogFactory.getLog(ImageRenderedAdapter.class);
  46. private ImageEncodingHelper encodingHelper;
  47. private PDFFilter pdfFilter = null;
  48. private String maskRef;
  49. private PDFReference softMask;
  50. /**
  51. * Creates a new PDFImage from an Image instance.
  52. * @param image the image
  53. * @param key XObject key
  54. */
  55. public ImageRenderedAdapter(ImageRendered image, String key) {
  56. super(image, key);
  57. this.encodingHelper = new ImageEncodingHelper(image.getRenderedImage());
  58. }
  59. /**
  60. * Returns the ImageRendered instance for this adapter.
  61. * @return the ImageRendered instance
  62. */
  63. public ImageRendered getImage() {
  64. return ((ImageRendered)this.image);
  65. }
  66. private ColorModel getEffectiveColorModel() {
  67. return encodingHelper.getEncodedColorModel();
  68. }
  69. /** {@inheritDoc} */
  70. protected ColorSpace getImageColorSpace() {
  71. return getEffectiveColorModel().getColorSpace();
  72. }
  73. /** {@inheritDoc} */
  74. public void setup(PDFDocument doc) {
  75. RenderedImage ri = getImage().getRenderedImage();
  76. ColorModel cm = getEffectiveColorModel();
  77. super.setup(doc);
  78. //Handle transparency mask if applicable
  79. ColorModel orgcm = ri.getColorModel();
  80. if (orgcm.hasAlpha() && orgcm.getTransparency() == ColorModel.TRANSLUCENT) {
  81. doc.getProfile().verifyTransparencyAllowed(image.getInfo().getOriginalURI());
  82. //TODO Implement code to combine image with background color if transparency is not
  83. //allowed (need BufferedImage support for that)
  84. AlphaRasterImage alphaImage = new AlphaRasterImage("Mask:" + getKey(), ri);
  85. this.softMask = doc.addImage(null, alphaImage).makeReference();
  86. }
  87. }
  88. /** {@inheritDoc} */
  89. public PDFDeviceColorSpace getColorSpace() {
  90. // DeviceGray, DeviceRGB, or DeviceCMYK
  91. return toPDFColorSpace(getEffectiveColorModel().getColorSpace());
  92. }
  93. /** {@inheritDoc} */
  94. public int getBitsPerComponent() {
  95. ColorModel cm = getEffectiveColorModel();
  96. if (cm instanceof IndexColorModel) {
  97. IndexColorModel icm = (IndexColorModel)cm;
  98. return icm.getComponentSize(0);
  99. } else {
  100. return cm.getComponentSize(0);
  101. }
  102. }
  103. /** {@inheritDoc} */
  104. public boolean isTransparent() {
  105. ColorModel cm = getEffectiveColorModel();
  106. if (cm instanceof IndexColorModel) {
  107. if (cm.getTransparency() == IndexColorModel.TRANSLUCENT) {
  108. return true;
  109. }
  110. }
  111. return (getImage().getTransparentColor() != null);
  112. }
  113. private static Integer getIndexOfFirstTransparentColorInPalette(RenderedImage image) {
  114. ColorModel cm = image.getColorModel();
  115. if (cm instanceof IndexColorModel) {
  116. IndexColorModel icm = (IndexColorModel)cm;
  117. //Identify the transparent color in the palette
  118. byte[] alphas = new byte[icm.getMapSize()];
  119. byte[] reds = new byte[icm.getMapSize()];
  120. byte[] greens = new byte[icm.getMapSize()];
  121. byte[] blues = new byte[icm.getMapSize()];
  122. icm.getAlphas(alphas);
  123. icm.getReds(reds);
  124. icm.getGreens(greens);
  125. icm.getBlues(blues);
  126. for (int i = 0;
  127. i < ((IndexColorModel) cm).getMapSize();
  128. i++) {
  129. if ((alphas[i] & 0xFF) == 0) {
  130. return new Integer(i);
  131. }
  132. }
  133. }
  134. return null;
  135. }
  136. /** {@inheritDoc} */
  137. public PDFColor getTransparentColor() {
  138. ColorModel cm = getEffectiveColorModel();
  139. if (cm instanceof IndexColorModel) {
  140. IndexColorModel icm = (IndexColorModel)cm;
  141. if (cm.getTransparency() == IndexColorModel.TRANSLUCENT) {
  142. int transPixel = icm.getTransparentPixel();
  143. return new PDFColor(
  144. icm.getRed(transPixel),
  145. icm.getGreen(transPixel),
  146. icm.getBlue(transPixel));
  147. }
  148. }
  149. return new PDFColor(getImage().getTransparentColor());
  150. }
  151. /** {@inheritDoc} */
  152. public String getMask() {
  153. return maskRef;
  154. }
  155. /** {@inheritDoc} */
  156. public PDFReference getSoftMaskReference() {
  157. return softMask;
  158. }
  159. /** {@inheritDoc} */
  160. public PDFFilter getPDFFilter() {
  161. return pdfFilter;
  162. }
  163. /** {@inheritDoc} */
  164. public void outputContents(OutputStream out) throws IOException {
  165. encodingHelper.encode(out);
  166. }
  167. private static final int MAX_HIVAL = 255;
  168. /** {@inheritDoc} */
  169. public void populateXObjectDictionary(PDFDictionary dict) {
  170. ColorModel cm = getEffectiveColorModel();
  171. if (cm instanceof IndexColorModel) {
  172. IndexColorModel icm = (IndexColorModel)cm;
  173. PDFArray indexed = new PDFArray(dict);
  174. indexed.add(new PDFName("Indexed"));
  175. if (icm.getColorSpace().getType() != ColorSpace.TYPE_RGB) {
  176. log.warn("Indexed color space is not using RGB as base color space."
  177. + " The image may not be handled correctly."
  178. + " Base color space: " + icm.getColorSpace()
  179. + " Image: " + image.getInfo());
  180. }
  181. indexed.add(new PDFName(toPDFColorSpace(icm.getColorSpace()).getName()));
  182. int c = icm.getMapSize();
  183. int hival = c - 1;
  184. if (hival > MAX_HIVAL) {
  185. throw new UnsupportedOperationException("hival must not go beyond " + MAX_HIVAL);
  186. }
  187. indexed.add(new Integer(hival));
  188. int[] palette = new int[c];
  189. icm.getRGBs(palette);
  190. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  191. for (int i = 0; i < c; i++) {
  192. //TODO Probably doesn't work for non RGB based color spaces
  193. //See log warning above
  194. int entry = palette[i];
  195. baout.write((entry & 0xFF0000) >> 16);
  196. baout.write((entry & 0xFF00) >> 8);
  197. baout.write(entry & 0xFF);
  198. }
  199. indexed.add(baout.toByteArray());
  200. dict.put("ColorSpace", indexed);
  201. dict.put("BitsPerComponent", icm.getPixelSize());
  202. Integer index = getIndexOfFirstTransparentColorInPalette(getImage().getRenderedImage());
  203. if (index != null) {
  204. PDFArray mask = new PDFArray(dict);
  205. mask.add(index);
  206. mask.add(index);
  207. dict.put("Mask", mask);
  208. }
  209. }
  210. }
  211. /** {@inheritDoc} */
  212. public String getFilterHint() {
  213. return PDFFilterList.IMAGE_FILTER;
  214. }
  215. }