KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfImage


1 /*
2  * $Id: PdfImage.java 2752 2007-05-15 14:58:33Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.pdf;
52
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.InputStream JavaDoc;
56 import java.io.OutputStream JavaDoc;
57
58 import com.lowagie.text.Image;
59
60 /**
61  * <CODE>PdfImage</CODE> is a <CODE>PdfStream</CODE> containing an image-<CODE>Dictionary</CODE> and -stream.
62  */

63
64 public class PdfImage extends PdfStream {
65     
66     static final int TRANSFERSIZE = 4096;
67     // membervariables
68

69     /** This is the <CODE>PdfName</CODE> of the image. */
70     protected PdfName name = null;
71     
72     // constructor
73

74     /**
75      * Constructs a <CODE>PdfImage</CODE>-object.
76      *
77      * @param image the <CODE>Image</CODE>-object
78      * @param name the <CODE>PdfName</CODE> for this image
79      * @throws BadPdfFormatException on error
80      */

81     
82     public PdfImage(Image image, String JavaDoc name, PdfIndirectReference maskRef) throws BadPdfFormatException {
83         super();
84         this.name = new PdfName(name);
85         put(PdfName.TYPE, PdfName.XOBJECT);
86         put(PdfName.SUBTYPE, PdfName.IMAGE);
87         put(PdfName.WIDTH, new PdfNumber(image.getWidth()));
88         put(PdfName.HEIGHT, new PdfNumber(image.getHeight()));
89         if (image.getLayer() != null)
90             put(PdfName.OC, image.getLayer().getRef());
91         if (image.isMask() && (image.getBpc() == 1 || image.getBpc() > 0xff))
92             put(PdfName.IMAGEMASK, PdfBoolean.PDFTRUE);
93         if (maskRef != null) {
94             if (image.isSmask())
95                 put(PdfName.SMASK, maskRef);
96             else
97                 put(PdfName.MASK, maskRef);
98         }
99         if (image.isMask() && image.isInverted())
100             put(PdfName.DECODE, new PdfLiteral("[1 0]"));
101         if (image.isInterpolation())
102             put(PdfName.INTERPOLATE, PdfBoolean.PDFTRUE);
103         InputStream JavaDoc is = null;
104         try {
105             
106             // Raw Image data
107
if (image.isImgRaw()) {
108                 // will also have the CCITT parameters
109
int colorspace = image.getColorspace();
110                 int transparency[] = image.getTransparency();
111                 if (transparency != null && !image.isMask() && maskRef == null) {
112                     String JavaDoc s = "[";
113                     for (int k = 0; k < transparency.length; ++k)
114                         s += transparency[k] + " ";
115                     s += "]";
116                     put(PdfName.MASK, new PdfLiteral(s));
117                 }
118                 bytes = image.getRawData();
119                 put(PdfName.LENGTH, new PdfNumber(bytes.length));
120                 int bpc = image.getBpc();
121                 if (bpc > 0xff) {
122                     if (!image.isMask())
123                         put(PdfName.COLORSPACE, PdfName.DEVICEGRAY);
124                     put(PdfName.BITSPERCOMPONENT, new PdfNumber(1));
125                     put(PdfName.FILTER, PdfName.CCITTFAXDECODE);
126                     int k = bpc - Image.CCITTG3_1D;
127                     PdfDictionary decodeparms = new PdfDictionary();
128                     if (k != 0)
129                         decodeparms.put(PdfName.K, new PdfNumber(k));
130                     if ((colorspace & Image.CCITT_BLACKIS1) != 0)
131                         decodeparms.put(PdfName.BLACKIS1, PdfBoolean.PDFTRUE);
132                     if ((colorspace & Image.CCITT_ENCODEDBYTEALIGN) != 0)
133                         decodeparms.put(PdfName.ENCODEDBYTEALIGN, PdfBoolean.PDFTRUE);
134                     if ((colorspace & Image.CCITT_ENDOFLINE) != 0)
135                         decodeparms.put(PdfName.ENDOFLINE, PdfBoolean.PDFTRUE);
136                     if ((colorspace & Image.CCITT_ENDOFBLOCK) != 0)
137                         decodeparms.put(PdfName.ENDOFBLOCK, PdfBoolean.PDFFALSE);
138                     decodeparms.put(PdfName.COLUMNS, new PdfNumber(image.getWidth()));
139                     decodeparms.put(PdfName.ROWS, new PdfNumber(image.getHeight()));
140                     put(PdfName.DECODEPARMS, decodeparms);
141                 }
142                 else {
143                     switch(colorspace) {
144                         case 1:
145                             put(PdfName.COLORSPACE, PdfName.DEVICEGRAY);
146                             if (image.isInverted())
147                                 put(PdfName.DECODE, new PdfLiteral("[1 0]"));
148                             break;
149                         case 3:
150                             put(PdfName.COLORSPACE, PdfName.DEVICERGB);
151                             if (image.isInverted())
152                                 put(PdfName.DECODE, new PdfLiteral("[1 0 1 0 1 0]"));
153                             break;
154                         case 4:
155                         default:
156                             put(PdfName.COLORSPACE, PdfName.DEVICECMYK);
157                             if (image.isInverted())
158                                 put(PdfName.DECODE, new PdfLiteral("[1 0 1 0 1 0 1 0]"));
159                     }
160                     PdfDictionary additional = image.getAdditional();
161                     if (additional != null)
162                         putAll(additional);
163                     if (image.isMask() && (image.getBpc() == 1 || image.getBpc() > 8))
164                         remove(PdfName.COLORSPACE);
165                     put(PdfName.BITSPERCOMPONENT, new PdfNumber(image.getBpc()));
166                     if (image.isDeflated())
167                         put(PdfName.FILTER, PdfName.FLATEDECODE);
168                     else {
169                         flateCompress();
170                     }
171                 }
172                 return;
173             }
174             
175             // GIF, JPEG or PNG
176
String JavaDoc errorID;
177             if (image.getRawData() == null){
178                 is = image.getUrl().openStream();
179                 errorID = image.getUrl().toString();
180             }
181             else{
182                 is = new java.io.ByteArrayInputStream JavaDoc(image.getRawData());
183                 errorID = "Byte array";
184             }
185             switch(image.type()) {
186                 case Image.JPEG:
187                     put(PdfName.FILTER, PdfName.DCTDECODE);
188                     switch(image.getColorspace()) {
189                         case 1:
190                             put(PdfName.COLORSPACE, PdfName.DEVICEGRAY);
191                             break;
192                         case 3:
193                             put(PdfName.COLORSPACE, PdfName.DEVICERGB);
194                             break;
195                         default:
196                             put(PdfName.COLORSPACE, PdfName.DEVICECMYK);
197                             if (image.isInverted()) {
198                                 put(PdfName.DECODE, new PdfLiteral("[1 0 1 0 1 0 1 0]"));
199                             }
200                     }
201                     put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));
202                     if (image.getRawData() != null){
203                         bytes = image.getRawData();
204                         put(PdfName.LENGTH, new PdfNumber(bytes.length));
205                         return;
206                     }
207                     streamBytes = new ByteArrayOutputStream JavaDoc();
208                     transferBytes(is, streamBytes, -1);
209                     break;
210                 default:
211                     throw new BadPdfFormatException(errorID + " is an unknown Image format.");
212             }
213             put(PdfName.LENGTH, new PdfNumber(streamBytes.size()));
214         }
215         catch(IOException JavaDoc ioe) {
216             throw new BadPdfFormatException(ioe.getMessage());
217         }
218         finally {
219             if (is != null) {
220                 try{
221                     is.close();
222                 }
223                 catch (Exception JavaDoc ee) {
224                     // empty on purpose
225
}
226             }
227         }
228     }
229     
230     /**
231      * Returns the <CODE>PdfName</CODE> of the image.
232      *
233      * @return the name
234      */

235     
236     public PdfName name() {
237         return name;
238     }
239     
240     static void transferBytes(InputStream JavaDoc in, OutputStream JavaDoc out, int len) throws IOException JavaDoc {
241         byte buffer[] = new byte[TRANSFERSIZE];
242         if (len < 0)
243             len = 0x7ffffff;
244         int size;
245         while (len != 0) {
246             size = in.read(buffer, 0, Math.min(len, TRANSFERSIZE));
247             if (size < 0)
248                 return;
249             out.write(buffer, 0, size);
250             len -= size;
251         }
252     }
253     
254     protected void importAll(PdfImage dup) {
255         name = dup.name;
256         compressed = dup.compressed;
257         streamBytes = dup.streamBytes;
258         bytes = dup.bytes;
259         hashMap = dup.hashMap;
260     }
261 }
262
Popular Tags