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.

PDFXObject.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.pdf;
  18. // Java
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. /* modified by JKT to integrate with 0.12.0 */
  22. /* modified by Eric SCHAEFFER to integrate with 0.13.0 */
  23. /**
  24. * PDF XObject
  25. *
  26. * A derivative of the PDF Object, is a PDF Stream that has not only a
  27. * dictionary but a stream of image data.
  28. * The dictionary just provides information like the stream length.
  29. * This outputs the image dictionary and the image data.
  30. * This is used as a reference for inserting the same image in the
  31. * document in another place.
  32. */
  33. public class PDFXObject extends AbstractPDFStream {
  34. private PDFImage pdfimage;
  35. private int xnum;
  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 PDFXObject(int xnumber, PDFImage img) {
  44. super();
  45. this.xnum = xnumber;
  46. pdfimage = img;
  47. }
  48. /**
  49. * Get the xnumber for this pdf object.
  50. *
  51. * @return the PDF XObject number
  52. */
  53. public int getXNumber() {
  54. return this.xnum;
  55. }
  56. /**
  57. * Output the image as PDF.
  58. * This sets up the image dictionary and adds the image data stream.
  59. *
  60. * @param stream the output stream to write the data
  61. * @throws IOException if there is an error writing the data
  62. * @return the length of the data written
  63. */
  64. protected int output(OutputStream stream) throws IOException {
  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. /**
  73. * @see org.apache.fop.pdf.AbstractPDFStream#buildStreamDict(String)
  74. */
  75. protected String buildStreamDict(String lengthEntry) {
  76. String dictEntries = getFilterList().buildFilterDictEntries();
  77. if (pdfimage.isPS()) {
  78. return buildDictionaryFromPS(lengthEntry, dictEntries);
  79. } else {
  80. return buildDictionaryFromImage(lengthEntry, dictEntries);
  81. }
  82. }
  83. private String buildDictionaryFromPS(String lengthEntry,
  84. String dictEntries) {
  85. StringBuffer sb = new StringBuffer(128);
  86. sb.append(getObjectID());
  87. sb.append("<</Type /XObject\n");
  88. sb.append("/Subtype /PS\n");
  89. sb.append("/Length " + lengthEntry);
  90. sb.append(dictEntries);
  91. sb.append("\n>>\n");
  92. return sb.toString();
  93. }
  94. private String buildDictionaryFromImage(String lengthEntry,
  95. String dictEntries) {
  96. StringBuffer sb = new StringBuffer(128);
  97. sb.append(getObjectID());
  98. sb.append("<</Type /XObject\n");
  99. sb.append("/Subtype /Image\n");
  100. sb.append("/Name /Im" + xnum + "\n");
  101. sb.append("/Length " + lengthEntry + "\n");
  102. sb.append("/Width " + pdfimage.getWidth() + "\n");
  103. sb.append("/Height " + pdfimage.getHeight() + "\n");
  104. sb.append("/BitsPerComponent " + pdfimage.getBitsPerPixel() + "\n");
  105. PDFICCStream pdfICCStream = pdfimage.getICCStream();
  106. if (pdfICCStream != null) {
  107. sb.append("/ColorSpace [/ICCBased "
  108. + pdfICCStream.referencePDF() + "]\n");
  109. } else {
  110. PDFColorSpace cs = pdfimage.getColorSpace();
  111. sb.append("/ColorSpace /" + cs.getColorSpacePDFString()
  112. + "\n");
  113. }
  114. /* PhotoShop generates CMYK values that's inverse,
  115. this will invert the values - too bad if it's not
  116. a PhotoShop image...
  117. */
  118. if (pdfimage.getColorSpace().getColorSpace()
  119. == PDFColorSpace.DEVICE_CMYK) {
  120. sb.append("/Decode [ 1.0 0.0 1.0 0.0 1.0 0.0 1.1 0.0 ]\n");
  121. }
  122. if (pdfimage.isTransparent()) {
  123. PDFColor transp = pdfimage.getTransparentColor();
  124. sb.append("/Mask ["
  125. + transp.red255() + " "
  126. + transp.red255() + " "
  127. + transp.green255() + " "
  128. + transp.green255() + " "
  129. + transp.blue255() + " "
  130. + transp.blue255() + "]\n");
  131. }
  132. String ref = pdfimage.getSoftMask();
  133. if (ref != null) {
  134. sb.append("/SMask " + ref + "\n");
  135. }
  136. sb.append(dictEntries);
  137. sb.append("\n>>\n");
  138. return sb.toString();
  139. }
  140. /**
  141. * @see org.apache.fop.pdf.PDFStream#outputRawStreamData(OutputStream)
  142. */
  143. protected void outputRawStreamData(OutputStream out) throws IOException {
  144. pdfimage.outputContents(out);
  145. }
  146. /**
  147. * @see org.apache.fop.pdf.AbstractPDFStream#getSizeHint()
  148. */
  149. protected int getSizeHint() throws IOException {
  150. return 0;
  151. }
  152. /**
  153. * @see org.apache.fop.pdf.AbstractPDFStream#prepareImplicitFilters()
  154. */
  155. protected void prepareImplicitFilters() {
  156. if (pdfimage.isDCT()) {
  157. getFilterList().ensureDCTFilterInPlace();
  158. }
  159. }
  160. /**
  161. * This sets up the default filters for XObjects. It uses the PDFImage
  162. * instance to determine what default filters to apply.
  163. * @see org.apache.fop.pdf.AbstractPDFStream#setupFilterList()
  164. */
  165. protected void setupFilterList() {
  166. if (!getFilterList().isInitialized()) {
  167. getFilterList().addDefaultFilters(
  168. getDocumentSafely().getFilterMap(),
  169. pdfimage.getFilterHint());
  170. }
  171. super.setupFilterList();
  172. }
  173. }