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.

BitmapImage.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.io.IOException;
  20. import java.io.OutputStream;
  21. /**
  22. * Bitmap image.
  23. * This is used to create a bitmap image that will be inserted
  24. * into pdf.
  25. */
  26. public class BitmapImage implements PDFImage {
  27. private int height;
  28. private int width;
  29. private int bitsPerComponent;
  30. private PDFDeviceColorSpace colorSpace;
  31. private byte[] bitmaps;
  32. private PDFReference maskRef;
  33. private PDFColor transparent = null;
  34. private String key;
  35. private PDFDocument pdfDoc;
  36. private PDFFilter pdfFilter;
  37. /**
  38. * Create a bitmap image.
  39. * Creates a new bitmap image with the given data.
  40. *
  41. * @param k the key to be used to lookup the image
  42. * @param width the width of the image
  43. * @param height the height of the image
  44. * @param data the bitmap data
  45. * @param mask the transparency mask reference if any
  46. */
  47. public BitmapImage(String k, int width, int height, byte[] data,
  48. String mask) {
  49. this.key = k;
  50. this.height = height;
  51. this.width = width;
  52. this.bitsPerComponent = 8;
  53. this.colorSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
  54. this.bitmaps = data;
  55. if (mask != null) {
  56. maskRef = new PDFReference(mask);
  57. }
  58. }
  59. /**
  60. * Setup this image with the pdf document.
  61. *
  62. * @param doc the pdf document this will be inserted into
  63. */
  64. public void setup(PDFDocument doc) {
  65. this.pdfDoc = doc;
  66. }
  67. /**
  68. * Get the key for this image.
  69. * This key is used by the pdf document so that it will only
  70. * insert an image once. All other references to the same image
  71. * will use the same XObject reference.
  72. *
  73. * @return the unique key to identify this image
  74. */
  75. public String getKey() {
  76. return key;
  77. }
  78. /**
  79. * Get the width of this image.
  80. *
  81. * @return the width of the image
  82. */
  83. public int getWidth() {
  84. return width;
  85. }
  86. /**
  87. * Get the height of this image.
  88. *
  89. * @return the height of the image
  90. */
  91. public int getHeight() {
  92. return height;
  93. }
  94. /**
  95. * Set the color space for this image.
  96. *
  97. * @param cs the pdf color space
  98. */
  99. public void setColorSpace(PDFDeviceColorSpace cs) {
  100. colorSpace = cs;
  101. }
  102. /**
  103. * Get the color space for the image data.
  104. * Possible options are: DeviceGray, DeviceRGB, or DeviceCMYK
  105. *
  106. * @return the pdf doclor space
  107. */
  108. public PDFDeviceColorSpace getColorSpace() {
  109. return colorSpace;
  110. }
  111. /** {@inheritDoc} */
  112. public int getBitsPerComponent() {
  113. return bitsPerComponent;
  114. }
  115. /**
  116. * Set the transparent color for this iamge.
  117. *
  118. * @param t the transparent color
  119. */
  120. public void setTransparent(PDFColor t) {
  121. transparent = t;
  122. }
  123. /**
  124. * Check if this image has a transparent color.
  125. *
  126. * @return true if it has a transparent color
  127. */
  128. public boolean isTransparent() {
  129. return transparent != null;
  130. }
  131. /**
  132. * Get the transparent color for this image.
  133. *
  134. * @return the transparent color if any
  135. */
  136. public PDFColor getTransparentColor() {
  137. return transparent;
  138. }
  139. /**
  140. * Get the bitmap mask reference for this image.
  141. * Current not supported.
  142. *
  143. * @return the bitmap mask reference
  144. */
  145. public String getMask() {
  146. return null;
  147. }
  148. /** {@inheritDoc} */
  149. public PDFReference getSoftMaskReference() {
  150. return maskRef;
  151. }
  152. /** {@inheritDoc} */
  153. public boolean isInverted() {
  154. return false;
  155. }
  156. /** {@inheritDoc} */
  157. public void outputContents(OutputStream out) throws IOException {
  158. out.write(bitmaps);
  159. }
  160. /** {@inheritDoc} */
  161. public void populateXObjectDictionary(PDFDictionary dict) {
  162. //nop
  163. }
  164. /**
  165. * Get the ICC stream.
  166. * @return always returns null since this has no icc color space
  167. */
  168. public PDFICCStream getICCStream() {
  169. return null;
  170. }
  171. /**
  172. * Check if this is a postscript image.
  173. * @return always returns false
  174. */
  175. public boolean isPS() {
  176. return false;
  177. }
  178. /**
  179. * {@inheritDoc}
  180. */
  181. public String getFilterHint() {
  182. return PDFFilterList.IMAGE_FILTER;
  183. }
  184. /**
  185. * {@inheritDoc}
  186. */
  187. public PDFFilter getPDFFilter() {
  188. return pdfFilter;
  189. }
  190. public void setPDFFilter(PDFFilter pdfFilter) {
  191. this.pdfFilter = pdfFilter;
  192. }
  193. }