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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*-- $Id$ --
  2. ============================================================================
  3. The Apache Software License, Version 1.1
  4. ============================================================================
  5. Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modifica-
  7. tion, are permitted provided that the following conditions are met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. 3. The end-user documentation included with the redistribution, if any, must
  14. include the following acknowledgment: "This product includes software
  15. developed by the Apache Software Foundation (http://www.apache.org/)."
  16. Alternately, this acknowledgment may appear in the software itself, if
  17. and wherever such third-party acknowledgments normally appear.
  18. 4. The names "Fop" and "Apache Software Foundation" must not be used to
  19. endorse or promote products derived from this software without prior
  20. written permission. For written permission, please contact
  21. apache@apache.org.
  22. 5. Products derived from this software may not be called "Apache", nor may
  23. "Apache" appear in their name, without prior written permission of the
  24. Apache Software Foundation.
  25. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  26. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  30. DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. This software consists of voluntary contributions made by many individuals
  36. on behalf of the Apache Software Foundation and was originally created by
  37. James Tauber <jtauber@jtauber.com>. For more information on the Apache
  38. Software Foundation, please see <http://www.apache.org/>.
  39. */
  40. /* modified by JKT to integrate with 0.12.0 */
  41. /* modified by Eric SCHAEFFER to integrate with 0.13.0 */
  42. package org.apache.fop.pdf;
  43. // Java
  44. import java.io.IOException;
  45. import org.apache.fop.messaging.MessageHandler;
  46. import java.io.PrintWriter;
  47. // FOP
  48. import org.apache.fop.datatypes.ColorSpace;
  49. import org.apache.fop.image.FopImage;
  50. import org.apache.fop.image.FopImageException;
  51. /**
  52. * PDF XObject
  53. *
  54. * A derivative of the PDF Object, is a PDF Stream that has not only a
  55. * dictionary but a stream of image data.
  56. * the dictionary just provides information like the stream length
  57. */
  58. public class PDFXObject extends PDFObject {
  59. FopImage fopimage;
  60. int Xnum;
  61. /**
  62. * create an Xobject with the given number and name and load the
  63. * image in the object
  64. */
  65. public PDFXObject(int number,int Xnumber,FopImage img) {
  66. super(number);
  67. this.Xnum=Xnumber;
  68. if (img == null)
  69. MessageHandler.errorln("FISH");
  70. fopimage=img;
  71. }
  72. /**
  73. * @return the PDF XObject number
  74. */
  75. public int getXNumber() {
  76. return this.Xnum;
  77. }
  78. /**
  79. * represent as PDF
  80. */
  81. protected int output(PrintWriter writer) throws IOException {
  82. int length=0;
  83. int i=0;
  84. int x,y;
  85. try {
  86. PDFBinaryStream imgStream = new PDFBinaryStream();
  87. imgStream.setData(fopimage.getBitmaps());
  88. imgStream.encode(new PDFFilter(PDFFilter.FLATE_DECODE));
  89. imgStream.encode(new PDFFilter(PDFFilter.ASCII_HEX_DECODE));
  90. String p = this.number + " " + this.generation + " obj\n";
  91. p = p + "<</Type /XObject\n";
  92. p = p + "/Subtype /Image\n";
  93. p = p + "/Name /Im" + Xnum + "\n";
  94. p = p + "/Width " + fopimage.getWidth() + "\n";
  95. p = p + "/Height " + fopimage.getHeight() + "\n";
  96. p = p + "/BitsPerComponent " + fopimage.getBitsPerPixel() + "\n";
  97. ColorSpace cs = fopimage.getColorSpace();
  98. p = p + "/ColorSpace /" + cs.getColorSpacePDFString() + "\n";
  99. if (fopimage.isTransparent()) {
  100. PDFColor transp = fopimage.getTransparentColor();
  101. p = p + "/Mask [" + transp.red255() + " " + transp.red255() + " " + transp.green255() + " " + transp.green255() + " " + transp.blue255() + " " + transp.blue255() + "]\n";
  102. }
  103. p = p + imgStream.getPDFDictionary();
  104. p = p + ">>\n";
  105. // don't know if it's the good place (other objects can have references to it)
  106. fopimage.close();
  107. // push the pdf dictionary on the writer
  108. writer.write(p);
  109. length += p.length();
  110. // push all the image data on the writer and takes care of length for trailer
  111. length += imgStream.outputPDFStream(writer);
  112. p = "endobj\n";
  113. writer.write(p);
  114. length += p.length();
  115. } catch (FopImageException imgex) {
  116. MessageHandler.errorln("Error in XObject : " + imgex.getMessage());
  117. } catch (PDFFilterException filterex) {
  118. MessageHandler.errorln("Error in XObject : " + filterex.getMessage());
  119. }
  120. return length;
  121. }
  122. String toPDF() {
  123. /* Not used any more
  124. String p = this.number + " " + this.generation + " obj\n";
  125. p = p + "<</Type /XObject\n";
  126. p = p + "/Subtype /Image\n";
  127. p = p + "/Name /Im"+Xnum+"\n";
  128. p = p + "/Width "+fopimage.getpixelwidth()+"\n";
  129. p = p + "/Height "+fopimage.getpixelheight()+"\n";
  130. p = p + "/BitsPerComponent 8\n";
  131. if (fopimage.getcolor())
  132. p = p + "/ColorSpace /DeviceRGB\n";
  133. else
  134. p = p + "/ColorSpace /DeviceGray\n";
  135. p = p + "/Filter /ASCIIHexDecode\n";
  136. p = p + "/Length ";
  137. return p;
  138. */
  139. return null;
  140. }
  141. }