Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PDFImageXObject.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // Java
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.util.Set;
  24. import java.util.UUID;
  25. /* modified by JKT to integrate with 0.12.0 */
  26. /* modified by Eric SCHAEFFER to integrate with 0.13.0 */
  27. /**
  28. * PDF XObject
  29. *
  30. * A derivative of the PDF Object, is a PDF Stream that has not only a
  31. * dictionary but a stream of image data.
  32. * The dictionary just provides information like the stream length.
  33. * This outputs the image dictionary and the image data.
  34. * This is used as a reference for inserting the same image in the
  35. * document in another place.
  36. */
  37. public class PDFImageXObject extends PDFXObject {
  38. private PDFImage pdfimage;
  39. /**
  40. * create an XObject with the given number and name and load the
  41. * image in the object
  42. *
  43. * @param xnumber the pdf object X number
  44. * @param img the pdf image that contains the image data
  45. */
  46. public PDFImageXObject(int xnumber, PDFImage img) {
  47. super();
  48. put("Name", new PDFName("Im" + xnumber));
  49. pdfimage = img;
  50. }
  51. /**
  52. * Output the image as PDF.
  53. * This sets up the image dictionary and adds the image data stream.
  54. *
  55. * @param stream the output stream to write the data
  56. * @throws IOException if there is an error writing the data
  57. * @return the length of the data written
  58. */
  59. public int output(OutputStream stream) throws IOException {
  60. if (getDocument().getProfile().isPDFVTActive()) {
  61. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  62. pdfimage.outputContents(baos);
  63. put("GTS_XID", "uuid:" + UUID.nameUUIDFromBytes(baos.toByteArray()));
  64. }
  65. int length = super.output(stream);
  66. // let it gc
  67. // this object is retained as a reference to inserting
  68. // the same image but the image data is no longer needed
  69. pdfimage = null;
  70. return length;
  71. }
  72. /** {@inheritDoc} */
  73. protected void populateStreamDict(Object lengthEntry) {
  74. super.populateStreamDict(lengthEntry);
  75. if (pdfimage.isPS()) {
  76. populateDictionaryFromPS();
  77. } else {
  78. populateDictionaryFromImage();
  79. }
  80. }
  81. private void populateDictionaryFromPS() {
  82. getDocumentSafely().getProfile().verifyPSXObjectsAllowed();
  83. put("Subtype", new PDFName("PS"));
  84. }
  85. private void populateDictionaryFromImage() {
  86. put("Subtype", new PDFName("Image"));
  87. put("Width", pdfimage.getWidth());
  88. put("Height", pdfimage.getHeight());
  89. put("BitsPerComponent", pdfimage.getBitsPerComponent());
  90. PDFICCStream pdfICCStream = pdfimage.getICCStream();
  91. if (pdfICCStream != null) {
  92. put("ColorSpace", new PDFArray(this,
  93. new Object[] {new PDFName("ICCBased"), pdfICCStream}));
  94. } else {
  95. PDFDeviceColorSpace cs = pdfimage.getColorSpace();
  96. put("ColorSpace", new PDFName(cs.getName()));
  97. }
  98. if (pdfimage.isInverted()) {
  99. /* PhotoShop generates CMYK values that's inverse,
  100. * this will invert the values - too bad if it's not
  101. * a PhotoShop image...
  102. */
  103. final Float zero = 0.0f;
  104. final Float one = 1.0f;
  105. PDFArray decode = new PDFArray(this);
  106. for (int i = 0, c = pdfimage.getColorSpace().getNumComponents(); i < c; i++) {
  107. decode.add(one);
  108. decode.add(zero);
  109. }
  110. put("Decode", decode);
  111. }
  112. if (pdfimage.isTransparent()) {
  113. PDFColor transp = pdfimage.getTransparentColor();
  114. PDFArray mask = new PDFArray(this);
  115. if (pdfimage.getColorSpace().isGrayColorSpace()) {
  116. mask.add(Integer.valueOf(transp.red255()));
  117. mask.add(Integer.valueOf(transp.red255()));
  118. } else {
  119. mask.add(Integer.valueOf(transp.red255()));
  120. mask.add(Integer.valueOf(transp.red255()));
  121. mask.add(Integer.valueOf(transp.green255()));
  122. mask.add(Integer.valueOf(transp.green255()));
  123. mask.add(Integer.valueOf(transp.blue255()));
  124. mask.add(Integer.valueOf(transp.blue255()));
  125. }
  126. put("Mask", mask);
  127. }
  128. PDFReference ref = pdfimage.getSoftMaskReference();
  129. if (ref != null) {
  130. put("SMask", ref);
  131. }
  132. //Important: do this at the end so previous values can be overwritten.
  133. pdfimage.populateXObjectDictionary(getDictionary());
  134. }
  135. /** {@inheritDoc} */
  136. protected void outputRawStreamData(OutputStream out) throws IOException {
  137. pdfimage.outputContents(out);
  138. }
  139. /** {@inheritDoc} */
  140. protected int getSizeHint() throws IOException {
  141. return 0;
  142. }
  143. /** {@inheritDoc} */
  144. protected void prepareImplicitFilters() {
  145. PDFFilter pdfFilter = pdfimage.getPDFFilter();
  146. if (pdfFilter != null) {
  147. getFilterList().ensureFilterInPlace(pdfFilter);
  148. }
  149. }
  150. /**
  151. * {@inheritDoc}
  152. * This class uses the PDFImage instance to determine the default filter.
  153. */
  154. protected String getDefaultFilterName() {
  155. return pdfimage.getFilterHint();
  156. }
  157. /** {@inheritDoc} */
  158. protected boolean multipleFiltersAllowed() {
  159. return pdfimage.multipleFiltersAllowed();
  160. }
  161. @Override
  162. public void getChildren(Set<PDFObject> children) {
  163. super.getChildren(children);
  164. PDFICCStream pdfICCStream = pdfimage.getICCStream();
  165. if (pdfICCStream != null) {
  166. children.add(pdfICCStream);
  167. pdfICCStream.getChildren(children);
  168. }
  169. }
  170. }