KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > codec > GifImage


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

47 package com.lowagie.text.pdf.codec;
48
49 import java.io.BufferedInputStream JavaDoc;
50 import java.io.ByteArrayInputStream JavaDoc;
51 import java.io.DataInputStream JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.io.InputStream JavaDoc;
54 import java.net.URL JavaDoc;
55 import java.util.ArrayList JavaDoc;
56
57 import com.lowagie.text.ExceptionConverter;
58 import com.lowagie.text.Image;
59 import com.lowagie.text.ImgRaw;
60 import com.lowagie.text.Utilities;
61 import com.lowagie.text.pdf.PdfArray;
62 import com.lowagie.text.pdf.PdfDictionary;
63 import com.lowagie.text.pdf.PdfName;
64 import com.lowagie.text.pdf.PdfNumber;
65 import com.lowagie.text.pdf.PdfString;
66
67 /** Reads gif images of all types. All the images in a gif are read in the constructors
68  * and can be retrieved with other methods.
69  * @author Paulo Soares (psoares@consiste.pt)
70  */

71 public class GifImage {
72     
73     protected DataInputStream JavaDoc in;
74     protected int width; // full image width
75
protected int height; // full image height
76
protected boolean gctFlag; // global color table used
77

78     protected int bgIndex; // background color index
79
protected int bgColor; // background color
80
protected int pixelAspect; // pixel aspect ratio
81

82     protected boolean lctFlag; // local color table flag
83
protected boolean interlace; // interlace flag
84
protected int lctSize; // local color table size
85

86     protected int ix, iy, iw, ih; // current image rectangle
87

88     protected byte[] block = new byte[256]; // current data block
89
protected int blockSize = 0; // block size
90

91     // last graphic control extension info
92
protected int dispose = 0; // 0=no action; 1=leave in place; 2=restore to bg; 3=restore to prev
93
protected boolean transparency = false; // use transparent color
94
protected int delay = 0; // delay in milliseconds
95
protected int transIndex; // transparent color index
96

97     protected static final int MaxStackSize = 4096; // max decoder pixel stack size
98

99     // LZW decoder working arrays
100
protected short[] prefix;
101     protected byte[] suffix;
102     protected byte[] pixelStack;
103     protected byte[] pixels;
104
105     protected byte m_out[];
106     protected int m_bpc;
107     protected int m_gbpc;
108     protected byte m_global_table[];
109     protected byte m_local_table[];
110     protected byte m_curr_table[];
111     protected int m_line_stride;
112     protected byte fromData[];
113     protected URL JavaDoc fromUrl;
114
115
116     protected ArrayList JavaDoc frames = new ArrayList JavaDoc(); // frames read from current file
117

118     /** Reads gif images from an URL.
119      * @param url the URL
120      * @throws IOException on error
121      */

122     public GifImage(URL JavaDoc url) throws IOException JavaDoc {
123         fromUrl = url;
124         InputStream JavaDoc is = null;
125         try {
126             is = url.openStream();
127             process(is);
128         }
129         finally {
130             if (is != null) {
131                 is.close();
132             }
133         }
134     }
135     
136     /** Reads gif images from a file.
137      * @param file the file
138      * @throws IOException on error
139      */

140     public GifImage(String JavaDoc file) throws IOException JavaDoc {
141         this(Utilities.toURL(file));
142     }
143     
144     /** Reads gif images from a byte array.
145      * @param data the byte array
146      * @throws IOException on error
147      */

148     public GifImage(byte data[]) throws IOException JavaDoc {
149         fromData = data;
150         InputStream JavaDoc is = null;
151         try {
152             is = new ByteArrayInputStream JavaDoc(data);
153             process(is);
154         }
155         finally {
156             if (is != null) {
157                 is.close();
158             }
159         }
160     }
161     
162     /** Reads gif images from a stream. The stream is not closed.
163      * @param is the stream
164      * @throws IOException on error
165      */

166     public GifImage(InputStream JavaDoc is) throws IOException JavaDoc {
167         process(is);
168     }
169     
170     /** Gets the number of frames the gif has.
171      * @return the number of frames the gif has
172      */

173     public int getFrameCount() {
174         return frames.size();
175     }
176     
177     /** Gets the image from a frame. The first frame is 1.
178      * @param frame the frame to get the image from
179      * @return the image
180      */

181     public Image getImage(int frame) {
182         GifFrame gf = (GifFrame)frames.get(frame - 1);
183         return gf.image;
184     }
185     
186     /** Gets the [x,y] position of the frame in reference to the
187      * logical screen.
188      * @param frame the frame
189      * @return the [x,y] position of the frame
190      */

191     public int[] getFramePosition(int frame) {
192         GifFrame gf = (GifFrame)frames.get(frame - 1);
193         return new int[]{gf.ix, gf.iy};
194         
195     }
196     
197     /** Gets the logical screen. The images may be smaller and placed
198      * in some position in this screen to playback some animation.
199      * No image will be be bigger that this.
200      * @return the logical screen dimensions as [x,y]
201      */

202     public int[] getLogicalScreen() {
203         return new int[]{width, height};
204     }
205     
206     void process(InputStream JavaDoc is) throws IOException JavaDoc {
207         in = new DataInputStream JavaDoc(new BufferedInputStream JavaDoc(is));
208         readHeader();
209         readContents();
210         if (frames.isEmpty())
211             throw new IOException JavaDoc("The file does not contain any valid image.");
212     }
213     
214     /**
215      * Reads GIF file header information.
216      */

217     protected void readHeader() throws IOException JavaDoc {
218         String JavaDoc id = "";
219         for (int i = 0; i < 6; i++)
220             id += (char)in.read();
221         if (!id.startsWith("GIF8")) {
222             throw new IOException JavaDoc("Gif signature nor found.");
223         }
224         
225         readLSD();
226         if (gctFlag) {
227             m_global_table = readColorTable(m_gbpc);
228         }
229     }
230
231     /**
232      * Reads Logical Screen Descriptor
233      */

234     protected void readLSD() throws IOException JavaDoc {
235         
236         // logical screen size
237
width = readShort();
238         height = readShort();
239         
240         // packed fields
241
int packed = in.read();
242         gctFlag = (packed & 0x80) != 0; // 1 : global color table flag
243
m_gbpc = (packed & 7) + 1;
244         bgIndex = in.read(); // background color index
245
pixelAspect = in.read(); // pixel aspect ratio
246
}
247
248     /**
249      * Reads next 16-bit value, LSB first
250      */

251     protected int readShort() throws IOException JavaDoc {
252         // read 16-bit value, LSB first
253
return in.read() | (in.read() << 8);
254     }
255
256     /**
257      * Reads next variable length block from input.
258      *
259      * @return number of bytes stored in "buffer"
260      */

261     protected int readBlock() throws IOException JavaDoc {
262         blockSize = in.read();
263         if (blockSize <= 0)
264             return blockSize = 0;
265         for (int k = 0; k < blockSize; ++k) {
266             int v = in.read();
267             if (v < 0) {
268                 return blockSize = k;
269             }
270             block[k] = (byte)v;
271         }
272         return blockSize;
273     }
274
275     protected byte[] readColorTable(int bpc) throws IOException JavaDoc {
276         int ncolors = 1 << bpc;
277         int nbytes = 3*ncolors;
278         bpc = newBpc(bpc);
279         byte table[] = new byte[(1 << bpc) * 3];
280         in.readFully(table, 0, nbytes);
281         return table;
282     }
283  
284     
285     static protected int newBpc(int bpc) {
286         switch (bpc) {
287             case 1:
288             case 2:
289             case 4:
290                 break;
291             case 3:
292                 return 4;
293             default:
294                 return 8;
295         }
296         return bpc;
297     }
298     
299     protected void readContents() throws IOException JavaDoc {
300         // read GIF file content blocks
301
boolean done = false;
302         while (!done) {
303             int code = in.read();
304             switch (code) {
305                 
306                 case 0x2C: // image separator
307
readImage();
308                     break;
309                     
310                 case 0x21: // extension
311
code = in.read();
312                     switch (code) {
313                         
314                         case 0xf9: // graphics control extension
315
readGraphicControlExt();
316                             break;
317                             
318                         case 0xff: // application extension
319
readBlock();
320                             skip(); // don't care
321
break;
322                             
323                         default: // uninteresting extension
324
skip();
325                     }
326                     break;
327                     
328                 default:
329                     done = true;
330                     break;
331             }
332         }
333     }
334
335     /**
336      * Reads next frame image
337      */

338     protected void readImage() throws IOException JavaDoc {
339         ix = readShort(); // (sub)image position & size
340
iy = readShort();
341         iw = readShort();
342         ih = readShort();
343         
344         int packed = in.read();
345         lctFlag = (packed & 0x80) != 0; // 1 - local color table flag
346
interlace = (packed & 0x40) != 0; // 2 - interlace flag
347
// 3 - sort flag
348
// 4-5 - reserved
349
lctSize = 2 << (packed & 7); // 6-8 - local color table size
350
m_bpc = newBpc(m_gbpc);
351         if (lctFlag) {
352             m_curr_table = readColorTable((packed & 7) + 1); // read table
353
m_bpc = newBpc((packed & 7) + 1);
354         }
355         else {
356             m_curr_table = m_global_table;
357         }
358         if (transparency && transIndex >= m_curr_table.length / 3)
359             transparency = false;
360         if (transparency && m_bpc == 1) { // Acrobat 5.05 doesn't like this combination
361
byte tp[] = new byte[12];
362             System.arraycopy(m_curr_table, 0, tp, 0, 6);
363             m_curr_table = tp;
364             m_bpc = 2;
365         }
366         boolean skipZero = decodeImageData(); // decode pixel data
367
if (!skipZero)
368             skip();
369         
370         Image img = null;
371         try {
372             img = new ImgRaw(iw, ih, 1, m_bpc, m_out);
373             PdfArray colorspace = new PdfArray();
374             colorspace.add(PdfName.INDEXED);
375             colorspace.add(PdfName.DEVICERGB);
376             int len = m_curr_table.length;
377             colorspace.add(new PdfNumber(len / 3 - 1));
378             colorspace.add(new PdfString(m_curr_table));
379             PdfDictionary ad = new PdfDictionary();
380             ad.put(PdfName.COLORSPACE, colorspace);
381             img.setAdditional(ad);
382             if (transparency) {
383                 img.setTransparency(new int[]{transIndex, transIndex});
384             }
385         }
386         catch (Exception JavaDoc e) {
387             throw new ExceptionConverter(e);
388         }
389         img.setOriginalType(Image.ORIGINAL_GIF);
390         img.setOriginalData(fromData);
391         img.setUrl(fromUrl);
392         GifFrame gf = new GifFrame();
393         gf.image = img;
394         gf.ix = ix;
395         gf.iy = iy;
396         frames.add(gf); // add image to frame list
397

398         //resetFrame();
399

400     }
401     
402     protected boolean decodeImageData() throws IOException JavaDoc {
403         int NullCode = -1;
404         int npix = iw * ih;
405         int available, clear, code_mask, code_size, end_of_information, in_code, old_code,
406         bits, code, count, i, datum, data_size, first, top, bi;
407         boolean skipZero = false;
408         
409         if (prefix == null)
410             prefix = new short[MaxStackSize];
411         if (suffix == null)
412             suffix = new byte[MaxStackSize];
413         if (pixelStack == null)
414             pixelStack = new byte[MaxStackSize+1];
415         
416         m_line_stride = (iw * m_bpc + 7) / 8;
417         m_out = new byte[m_line_stride * ih];
418         int pass = 1;
419         int inc = interlace ? 8 : 1;
420         int line = 0;
421         int xpos = 0;
422         
423         // Initialize GIF data stream decoder.
424

425         data_size = in.read();
426         clear = 1 << data_size;
427         end_of_information = clear + 1;
428         available = clear + 2;
429         old_code = NullCode;
430         code_size = data_size + 1;
431         code_mask = (1 << code_size) - 1;
432         for (code = 0; code < clear; code++) {
433             prefix[code] = 0;
434             suffix[code] = (byte) code;
435         }
436         
437         // Decode GIF pixel stream.
438

439         datum = bits = count = first = top = bi = 0;
440         
441         for (i = 0; i < npix; ) {
442             if (top == 0) {
443                 if (bits < code_size) {
444                     // Load bytes until there are enough bits for a code.
445
if (count == 0) {
446                         // Read a new data block.
447
count = readBlock();
448                         if (count <= 0) {
449                             skipZero = true;
450                             break;
451                         }
452                         bi = 0;
453                     }
454                     datum += (((int) block[bi]) & 0xff) << bits;
455                     bits += 8;
456                     bi++;
457                     count--;
458                     continue;
459                 }
460                 
461                 // Get the next code.
462

463                 code = datum & code_mask;
464                 datum >>= code_size;
465                 bits -= code_size;
466                 
467                 // Interpret the code
468

469                 if ((code > available) || (code == end_of_information))
470                     break;
471                 if (code == clear) {
472                     // Reset decoder.
473
code_size = data_size + 1;
474                     code_mask = (1 << code_size) - 1;
475                     available = clear + 2;
476                     old_code = NullCode;
477                     continue;
478                 }
479                 if (old_code == NullCode) {
480                     pixelStack[top++] = suffix[code];
481                     old_code = code;
482                     first = code;
483                     continue;
484                 }
485                 in_code = code;
486                 if (code == available) {
487                     pixelStack[top++] = (byte) first;
488                     code = old_code;
489                 }
490                 while (code > clear) {
491                     pixelStack[top++] = suffix[code];
492                     code = prefix[code];
493                 }
494                 first = ((int) suffix[code]) & 0xff;
495                 
496                 // Add a new string to the string table,
497

498                 if (available >= MaxStackSize)
499                     break;
500                 pixelStack[top++] = (byte) first;
501                 prefix[available] = (short) old_code;
502                 suffix[available] = (byte) first;
503                 available++;
504                 if (((available & code_mask) == 0) && (available < MaxStackSize)) {
505                     code_size++;
506                     code_mask += available;
507                 }
508                 old_code = in_code;
509             }
510             
511             // Pop a pixel off the pixel stack.
512

513             top--;
514             i++;
515             
516             setPixel(xpos, line, pixelStack[top]);
517             ++xpos;
518             if (xpos >= iw) {
519                 xpos = 0;
520                 line += inc;
521                 if (line >= ih) {
522                     if (interlace) {
523                         do {
524                             pass++;
525                             switch (pass) {
526                                 case 2:
527                                     line = 4;
528                                     break;
529                                 case 3:
530                                     line = 2;
531                                     inc = 4;
532                                     break;
533                                 case 4:
534                                     line = 1;
535                                     inc = 2;
536                                     break;
537                                 default: // this shouldn't happen
538
line = ih - 1;
539                                     inc = 0;
540                             }
541                         } while (line >= ih);
542                     }
543                     else {
544                         line = ih - 1; // this shouldn't happen
545
inc = 0;
546                     }
547                 }
548             }
549         }
550         return skipZero;
551     }
552     
553     
554     protected void setPixel(int x, int y, int v) {
555         if (m_bpc == 8) {
556             int pos = x + iw * y;
557             m_out[pos] = (byte)v;
558         }
559         else {
560             int pos = m_line_stride * y + x / (8 / m_bpc);
561             int vout = v << (8 - m_bpc * (x % (8 / m_bpc))- m_bpc);
562             m_out[pos] |= vout;
563         }
564     }
565     
566     /**
567      * Resets frame state for reading next image.
568      */

569     protected void resetFrame() {
570         // it does nothing in the pdf context
571
//boolean transparency = false;
572
//int delay = 0;
573
}
574
575     /**
576      * Reads Graphics Control Extension values
577      */

578     protected void readGraphicControlExt() throws IOException JavaDoc {
579         in.read(); // block size
580
int packed = in.read(); // packed fields
581
dispose = (packed & 0x1c) >> 2; // disposal method
582
if (dispose == 0)
583             dispose = 1; // elect to keep old image if discretionary
584
transparency = (packed & 1) != 0;
585         delay = readShort() * 10; // delay in milliseconds
586
transIndex = in.read(); // transparent color index
587
in.read(); // block terminator
588
}
589     
590     /**
591      * Skips variable length blocks up to and including
592      * next zero length block.
593      */

594     protected void skip() throws IOException JavaDoc {
595         do {
596             readBlock();
597         } while (blockSize > 0);
598     }
599
600     static class GifFrame {
601         Image image;
602         int ix;
603         int iy;
604     }
605 }
606
Popular Tags