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.

PDFImageXObject.java 5.6KB

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