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.6KB

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