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.

AlphaRasterImage.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.pdf;
  19. import java.awt.image.DataBuffer;
  20. import java.awt.image.Raster;
  21. import java.awt.image.RenderedImage;
  22. import java.awt.image.SampleModel;
  23. import java.awt.image.SinglePixelPackedSampleModel;
  24. import java.io.IOException;
  25. import java.io.OutputStream;
  26. import org.apache.xmlgraphics.image.GraphicsUtil;
  27. /**
  28. * PDFImage implementation for alpha channel "images".
  29. */
  30. public class AlphaRasterImage implements PDFImage {
  31. private int bitsPerComponent;
  32. private PDFDeviceColorSpace colorSpace;
  33. private Raster alpha;
  34. private String key;
  35. /**
  36. * Create a alpha channel image.
  37. * Creates a new bitmap image with the given data.
  38. *
  39. * @param k the key to be used to lookup the image
  40. * @param alpha the alpha channel raster
  41. */
  42. public AlphaRasterImage(String k, Raster alpha) {
  43. this.key = k;
  44. //Enable the commented line below if 16-bit alpha channels are desired.
  45. //Otherwise, we compress the alpha channel to 8 bit which should be sufficient.
  46. //this.bitsPerComponent = alpha.getSampleModel().getSampleSize(0);
  47. this.bitsPerComponent = 8;
  48. this.colorSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_GRAY);
  49. if (alpha == null) {
  50. throw new NullPointerException("Parameter alpha must not be null");
  51. }
  52. this.alpha = alpha;
  53. }
  54. /**
  55. * Create a alpha channel image.
  56. * Extracts the alpha channel from the RenderedImage and creates a new bitmap image
  57. * with the given data.
  58. *
  59. * @param k the key to be used to lookup the image
  60. * @param image the image (must have an alpha channel)
  61. */
  62. public AlphaRasterImage(String k, RenderedImage image) {
  63. this(k, GraphicsUtil.getAlphaRaster(image));
  64. }
  65. /** {@inheritDoc} */
  66. public void setup(PDFDocument doc) {
  67. //nop
  68. }
  69. /** {@inheritDoc} */
  70. public String getKey() {
  71. return key;
  72. }
  73. /** {@inheritDoc} */
  74. public int getWidth() {
  75. return alpha.getWidth();
  76. }
  77. /** {@inheritDoc} */
  78. public int getHeight() {
  79. return alpha.getHeight();
  80. }
  81. /** {@inheritDoc} */
  82. public PDFDeviceColorSpace getColorSpace() {
  83. return colorSpace;
  84. }
  85. /** {@inheritDoc} */
  86. public int getBitsPerComponent() {
  87. return bitsPerComponent;
  88. }
  89. /** {@inheritDoc} */
  90. public boolean isTransparent() {
  91. return false;
  92. }
  93. /** {@inheritDoc} */
  94. public PDFColor getTransparentColor() {
  95. return null;
  96. }
  97. /** {@inheritDoc} */
  98. public String getMask() {
  99. return null;
  100. }
  101. /** @return null (unless overridden) */
  102. public String getSoftMask() {
  103. return null;
  104. }
  105. /** {@inheritDoc} */
  106. public PDFReference getSoftMaskReference() {
  107. return null;
  108. }
  109. /** {@inheritDoc} */
  110. public boolean isInverted() {
  111. return false;
  112. }
  113. /** {@inheritDoc} */
  114. public void outputContents(OutputStream out) throws IOException {
  115. int w = getWidth();
  116. int h = getHeight();
  117. //Check Raster
  118. int nbands = alpha.getNumBands();
  119. if (nbands != 1) {
  120. throw new UnsupportedOperationException(
  121. "Expected only one band/component for the alpha channel");
  122. }
  123. //...and write the Raster line by line with a reusable buffer
  124. int dataType = alpha.getDataBuffer().getDataType();
  125. if (dataType == DataBuffer.TYPE_BYTE) {
  126. byte[] line = new byte[nbands * w];
  127. for (int y = 0; y < h; y++) {
  128. alpha.getDataElements(0, y, w, 1, line);
  129. out.write(line);
  130. }
  131. } else if (dataType == DataBuffer.TYPE_USHORT) {
  132. short[] sline = new short[nbands * w];
  133. byte[] line = new byte[nbands * w];
  134. for (int y = 0; y < h; y++) {
  135. alpha.getDataElements(0, y, w, 1, sline);
  136. for (int i = 0; i < w; i++) {
  137. //this compresses a 16-bit alpha channel to 8 bits!
  138. //we probably don't ever need a 16-bit channel
  139. line[i] = (byte)(sline[i] >> 8);
  140. }
  141. out.write(line);
  142. }
  143. } else if (dataType == DataBuffer.TYPE_INT) {
  144. //Is there an better way to get a 8bit raster from a TYPE_INT raster?
  145. int shift = 24;
  146. SampleModel sampleModel = alpha.getSampleModel();
  147. if (sampleModel instanceof SinglePixelPackedSampleModel) {
  148. SinglePixelPackedSampleModel m = (SinglePixelPackedSampleModel)sampleModel;
  149. shift = m.getBitOffsets()[0];
  150. }
  151. int[] iline = new int[nbands * w];
  152. byte[] line = new byte[nbands * w];
  153. for (int y = 0; y < h; y++) {
  154. alpha.getDataElements(0, y, w, 1, iline);
  155. for (int i = 0; i < w; i++) {
  156. line[i] = (byte)(iline[i] >> shift);
  157. }
  158. out.write(line);
  159. }
  160. } else {
  161. throw new UnsupportedOperationException("Unsupported DataBuffer type: "
  162. + alpha.getDataBuffer().getClass().getName());
  163. }
  164. }
  165. /** {@inheritDoc} */
  166. public void populateXObjectDictionary(PDFDictionary dict) {
  167. //nop
  168. }
  169. /** {@inheritDoc} */
  170. public PDFICCStream getICCStream() {
  171. return null;
  172. }
  173. /** {@inheritDoc} */
  174. public boolean isPS() {
  175. return false;
  176. }
  177. /** {@inheritDoc} */
  178. public String getFilterHint() {
  179. return PDFFilterList.IMAGE_FILTER;
  180. }
  181. /** {@inheritDoc} */
  182. public PDFFilter getPDFFilter() {
  183. return null;
  184. }
  185. /** {@inheritDoc} */
  186. public boolean multipleFiltersAllowed() {
  187. return true;
  188. }
  189. }