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

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

62
63 public class PdfImage extends PdfStream {
64     
65     static final int TRANSFERSIZE = 4096;
66     // membervariables
67     
68     /** This is the <CODE>PdfName</CODE> of the image. */
69     protected PdfName name = null;
70     
71     // constructor
72     
73     /**
74      * Constructs a <CODE>PdfImage</CODE>-object.
75      *
76      * @param image the <CODE>Image</CODE>-object
77      * @param name the <CODE>PdfName</CODE> for this image
78      * @throws BadPdfFormatException on error
79      */

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

267     
268     public PdfName name() {
269         return name;
270     }
271     
272     static void transferBytes(InputStream in, OutputStream out, int len) throws IOException {
273         byte buffer[] = new byte[TRANSFERSIZE];
274         if (len < 0)
275             len = 0x7ffffff;
276         int size;
277         while (len != 0) {
278             size = in.read(buffer, 0, Math.min(len, TRANSFERSIZE));
279             if (size < 0)
280                 return;
281             out.write(buffer, 0, size);
282             len -= size;
283         }
284     }
285     
286     protected void importAll(PdfImage dup) {
287         name = dup.name;
288         compressed = dup.compressed;
289         compressionLevel = dup.compressionLevel;
290         streamBytes = dup.streamBytes;
291         bytes = dup.bytes;
292         hashMap = dup.hashMap;
293     }
294 }
295