Source Code Cross Referenced for GifImage.java in  » PDF » pdf-itext » com » lowagie » text » pdf » codec » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » PDF » pdf itext » com.lowagie.text.pdf.codec 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2003 by Paulo Soares.
003:         *
004:         * The contents of this file are subject to the Mozilla Public License Version 1.1
005:         * (the "License"); you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at http://www.mozilla.org/MPL/
007:         *
008:         * Software distributed under the License is distributed on an "AS IS" basis,
009:         * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010:         * for the specific language governing rights and limitations under the License.
011:         *
012:         * The Original Code is 'iText, a free JAVA-PDF library'.
013:         *
014:         * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
015:         * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
016:         * All Rights Reserved.
017:         * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
018:         * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
019:         *
020:         * Contributor(s): all the names of the contributors are added in the source code
021:         * where applicable.
022:         *
023:         * Alternatively, the contents of this file may be used under the terms of the
024:         * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
025:         * provisions of LGPL are applicable instead of those above.  If you wish to
026:         * allow use of your version of this file only under the terms of the LGPL
027:         * License and not to allow others to use your version of this file under
028:         * the MPL, indicate your decision by deleting the provisions above and
029:         * replace them with the notice and other provisions required by the LGPL.
030:         * If you do not delete the provisions above, a recipient may use your version
031:         * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
032:         *
033:         * This library is free software; you can redistribute it and/or modify it
034:         * under the terms of the MPL as stated above or under the terms of the GNU
035:         * Library General Public License as published by the Free Software Foundation;
036:         * either version 2 of the License, or any later version.
037:         *
038:         * This library is distributed in the hope that it will be useful, but WITHOUT
039:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
040:         * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
041:         * details.
042:         *
043:         * If you didn't download this code from the following link, you should check if
044:         * you aren't using an obsolete version:
045:         * http://www.lowagie.com/iText/
046:         */
047:        package com.lowagie.text.pdf.codec;
048:
049:        import java.io.BufferedInputStream;
050:        import java.io.ByteArrayInputStream;
051:        import java.io.DataInputStream;
052:        import java.io.IOException;
053:        import java.io.InputStream;
054:        import java.net.URL;
055:        import java.util.ArrayList;
056:
057:        import com.lowagie.text.ExceptionConverter;
058:        import com.lowagie.text.Image;
059:        import com.lowagie.text.ImgRaw;
060:        import com.lowagie.text.Utilities;
061:        import com.lowagie.text.pdf.PdfArray;
062:        import com.lowagie.text.pdf.PdfDictionary;
063:        import com.lowagie.text.pdf.PdfName;
064:        import com.lowagie.text.pdf.PdfNumber;
065:        import com.lowagie.text.pdf.PdfString;
066:
067:        /** Reads gif images of all types. All the images in a gif are read in the constructors
068:         * and can be retrieved with other methods.
069:         * @author Paulo Soares (psoares@consiste.pt)
070:         */
071:        public class GifImage {
072:
073:            protected DataInputStream in;
074:            protected int width; // full image width
075:            protected int height; // full image height
076:            protected boolean gctFlag; // global color table used
077:
078:            protected int bgIndex; // background color index
079:            protected int bgColor; // background color
080:            protected int pixelAspect; // pixel aspect ratio
081:
082:            protected boolean lctFlag; // local color table flag
083:            protected boolean interlace; // interlace flag
084:            protected int lctSize; // local color table size
085:
086:            protected int ix, iy, iw, ih; // current image rectangle
087:
088:            protected byte[] block = new byte[256]; // current data block
089:            protected int blockSize = 0; // block size
090:
091:            // last graphic control extension info
092:            protected int dispose = 0; // 0=no action; 1=leave in place; 2=restore to bg; 3=restore to prev
093:            protected boolean transparency = false; // use transparent color
094:            protected int delay = 0; // delay in milliseconds
095:            protected int transIndex; // transparent color index
096:
097:            protected static final int MaxStackSize = 4096; // max decoder pixel stack size
098:
099:            // 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 fromUrl;
114:
115:            protected ArrayList frames = new ArrayList(); // frames read from current file
116:
117:            /** Reads gif images from an URL.
118:             * @param url the URL
119:             * @throws IOException on error
120:             */
121:            public GifImage(URL url) throws IOException {
122:                fromUrl = url;
123:                InputStream is = null;
124:                try {
125:                    is = url.openStream();
126:                    process(is);
127:                } finally {
128:                    if (is != null) {
129:                        is.close();
130:                    }
131:                }
132:            }
133:
134:            /** Reads gif images from a file.
135:             * @param file the file
136:             * @throws IOException on error
137:             */
138:            public GifImage(String file) throws IOException {
139:                this (Utilities.toURL(file));
140:            }
141:
142:            /** Reads gif images from a byte array.
143:             * @param data the byte array
144:             * @throws IOException on error
145:             */
146:            public GifImage(byte data[]) throws IOException {
147:                fromData = data;
148:                InputStream is = null;
149:                try {
150:                    is = new ByteArrayInputStream(data);
151:                    process(is);
152:                } finally {
153:                    if (is != null) {
154:                        is.close();
155:                    }
156:                }
157:            }
158:
159:            /** Reads gif images from a stream. The stream is not closed.
160:             * @param is the stream
161:             * @throws IOException on error
162:             */
163:            public GifImage(InputStream is) throws IOException {
164:                process(is);
165:            }
166:
167:            /** Gets the number of frames the gif has.
168:             * @return the number of frames the gif has
169:             */
170:            public int getFrameCount() {
171:                return frames.size();
172:            }
173:
174:            /** Gets the image from a frame. The first frame is 1.
175:             * @param frame the frame to get the image from
176:             * @return the image
177:             */
178:            public Image getImage(int frame) {
179:                GifFrame gf = (GifFrame) frames.get(frame - 1);
180:                return gf.image;
181:            }
182:
183:            /** Gets the [x,y] position of the frame in reference to the
184:             * logical screen.
185:             * @param frame the frame
186:             * @return the [x,y] position of the frame
187:             */
188:            public int[] getFramePosition(int frame) {
189:                GifFrame gf = (GifFrame) frames.get(frame - 1);
190:                return new int[] { gf.ix, gf.iy };
191:
192:            }
193:
194:            /** Gets the logical screen. The images may be smaller and placed
195:             * in some position in this screen to playback some animation.
196:             * No image will be be bigger that this.
197:             * @return the logical screen dimensions as [x,y]
198:             */
199:            public int[] getLogicalScreen() {
200:                return new int[] { width, height };
201:            }
202:
203:            void process(InputStream is) throws IOException {
204:                in = new DataInputStream(new BufferedInputStream(is));
205:                readHeader();
206:                readContents();
207:                if (frames.isEmpty())
208:                    throw new IOException(
209:                            "The file does not contain any valid image.");
210:            }
211:
212:            /**
213:             * Reads GIF file header information.
214:             */
215:            protected void readHeader() throws IOException {
216:                String id = "";
217:                for (int i = 0; i < 6; i++)
218:                    id += (char) in.read();
219:                if (!id.startsWith("GIF8")) {
220:                    throw new IOException("Gif signature nor found.");
221:                }
222:
223:                readLSD();
224:                if (gctFlag) {
225:                    m_global_table = readColorTable(m_gbpc);
226:                }
227:            }
228:
229:            /**
230:             * Reads Logical Screen Descriptor
231:             */
232:            protected void readLSD() throws IOException {
233:
234:                // logical screen size
235:                width = readShort();
236:                height = readShort();
237:
238:                // packed fields
239:                int packed = in.read();
240:                gctFlag = (packed & 0x80) != 0; // 1   : global color table flag
241:                m_gbpc = (packed & 7) + 1;
242:                bgIndex = in.read(); // background color index
243:                pixelAspect = in.read(); // pixel aspect ratio
244:            }
245:
246:            /**
247:             * Reads next 16-bit value, LSB first
248:             */
249:            protected int readShort() throws IOException {
250:                // read 16-bit value, LSB first
251:                return in.read() | (in.read() << 8);
252:            }
253:
254:            /**
255:             * Reads next variable length block from input.
256:             *
257:             * @return number of bytes stored in "buffer"
258:             */
259:            protected int readBlock() throws IOException {
260:                blockSize = in.read();
261:                if (blockSize <= 0)
262:                    return blockSize = 0;
263:                for (int k = 0; k < blockSize; ++k) {
264:                    int v = in.read();
265:                    if (v < 0) {
266:                        return blockSize = k;
267:                    }
268:                    block[k] = (byte) v;
269:                }
270:                return blockSize;
271:            }
272:
273:            protected byte[] readColorTable(int bpc) throws IOException {
274:                int ncolors = 1 << bpc;
275:                int nbytes = 3 * ncolors;
276:                bpc = newBpc(bpc);
277:                byte table[] = new byte[(1 << bpc) * 3];
278:                in.readFully(table, 0, nbytes);
279:                return table;
280:            }
281:
282:            static protected int newBpc(int bpc) {
283:                switch (bpc) {
284:                case 1:
285:                case 2:
286:                case 4:
287:                    break;
288:                case 3:
289:                    return 4;
290:                default:
291:                    return 8;
292:                }
293:                return bpc;
294:            }
295:
296:            protected void readContents() throws IOException {
297:                // read GIF file content blocks
298:                boolean done = false;
299:                while (!done) {
300:                    int code = in.read();
301:                    switch (code) {
302:
303:                    case 0x2C: // image separator
304:                        readImage();
305:                        break;
306:
307:                    case 0x21: // extension
308:                        code = in.read();
309:                        switch (code) {
310:
311:                        case 0xf9: // graphics control extension
312:                            readGraphicControlExt();
313:                            break;
314:
315:                        case 0xff: // application extension
316:                            readBlock();
317:                            skip(); // don't care
318:                            break;
319:
320:                        default: // uninteresting extension
321:                            skip();
322:                        }
323:                        break;
324:
325:                    default:
326:                        done = true;
327:                        break;
328:                    }
329:                }
330:            }
331:
332:            /**
333:             * Reads next frame image
334:             */
335:            protected void readImage() throws IOException {
336:                ix = readShort(); // (sub)image position & size
337:                iy = readShort();
338:                iw = readShort();
339:                ih = readShort();
340:
341:                int packed = in.read();
342:                lctFlag = (packed & 0x80) != 0; // 1 - local color table flag
343:                interlace = (packed & 0x40) != 0; // 2 - interlace flag
344:                // 3 - sort flag
345:                // 4-5 - reserved
346:                lctSize = 2 << (packed & 7); // 6-8 - local color table size
347:                m_bpc = newBpc(m_gbpc);
348:                if (lctFlag) {
349:                    m_curr_table = readColorTable((packed & 7) + 1); // read table
350:                    m_bpc = newBpc((packed & 7) + 1);
351:                } else {
352:                    m_curr_table = m_global_table;
353:                }
354:                if (transparency && transIndex >= m_curr_table.length / 3)
355:                    transparency = false;
356:                if (transparency && m_bpc == 1) { // Acrobat 5.05 doesn't like this combination
357:                    byte tp[] = new byte[12];
358:                    System.arraycopy(m_curr_table, 0, tp, 0, 6);
359:                    m_curr_table = tp;
360:                    m_bpc = 2;
361:                }
362:                boolean skipZero = decodeImageData(); // decode pixel data
363:                if (!skipZero)
364:                    skip();
365:
366:                Image img = null;
367:                try {
368:                    img = new ImgRaw(iw, ih, 1, m_bpc, m_out);
369:                    PdfArray colorspace = new PdfArray();
370:                    colorspace.add(PdfName.INDEXED);
371:                    colorspace.add(PdfName.DEVICERGB);
372:                    int len = m_curr_table.length;
373:                    colorspace.add(new PdfNumber(len / 3 - 1));
374:                    colorspace.add(new PdfString(m_curr_table));
375:                    PdfDictionary ad = new PdfDictionary();
376:                    ad.put(PdfName.COLORSPACE, colorspace);
377:                    img.setAdditional(ad);
378:                    if (transparency) {
379:                        img
380:                                .setTransparency(new int[] { transIndex,
381:                                        transIndex });
382:                    }
383:                } catch (Exception e) {
384:                    throw new ExceptionConverter(e);
385:                }
386:                img.setOriginalType(Image.ORIGINAL_GIF);
387:                img.setOriginalData(fromData);
388:                img.setUrl(fromUrl);
389:                GifFrame gf = new GifFrame();
390:                gf.image = img;
391:                gf.ix = ix;
392:                gf.iy = iy;
393:                frames.add(gf); // add image to frame list
394:
395:                //resetFrame();
396:
397:            }
398:
399:            protected boolean decodeImageData() throws IOException {
400:                int NullCode = -1;
401:                int npix = iw * ih;
402:                int available, clear, code_mask, code_size, end_of_information, in_code, old_code, bits, code, count, i, datum, data_size, first, top, bi;
403:                boolean skipZero = false;
404:
405:                if (prefix == null)
406:                    prefix = new short[MaxStackSize];
407:                if (suffix == null)
408:                    suffix = new byte[MaxStackSize];
409:                if (pixelStack == null)
410:                    pixelStack = new byte[MaxStackSize + 1];
411:
412:                m_line_stride = (iw * m_bpc + 7) / 8;
413:                m_out = new byte[m_line_stride * ih];
414:                int pass = 1;
415:                int inc = interlace ? 8 : 1;
416:                int line = 0;
417:                int xpos = 0;
418:
419:                //  Initialize GIF data stream decoder.
420:
421:                data_size = in.read();
422:                clear = 1 << data_size;
423:                end_of_information = clear + 1;
424:                available = clear + 2;
425:                old_code = NullCode;
426:                code_size = data_size + 1;
427:                code_mask = (1 << code_size) - 1;
428:                for (code = 0; code < clear; code++) {
429:                    prefix[code] = 0;
430:                    suffix[code] = (byte) code;
431:                }
432:
433:                //  Decode GIF pixel stream.
434:
435:                datum = bits = count = first = top = bi = 0;
436:
437:                for (i = 0; i < npix;) {
438:                    if (top == 0) {
439:                        if (bits < code_size) {
440:                            //  Load bytes until there are enough bits for a code.
441:                            if (count == 0) {
442:                                // Read a new data block.
443:                                count = readBlock();
444:                                if (count <= 0) {
445:                                    skipZero = true;
446:                                    break;
447:                                }
448:                                bi = 0;
449:                            }
450:                            datum += (((int) block[bi]) & 0xff) << bits;
451:                            bits += 8;
452:                            bi++;
453:                            count--;
454:                            continue;
455:                        }
456:
457:                        //  Get the next code.
458:
459:                        code = datum & code_mask;
460:                        datum >>= code_size;
461:                        bits -= code_size;
462:
463:                        //  Interpret the code
464:
465:                        if ((code > available) || (code == end_of_information))
466:                            break;
467:                        if (code == clear) {
468:                            //  Reset decoder.
469:                            code_size = data_size + 1;
470:                            code_mask = (1 << code_size) - 1;
471:                            available = clear + 2;
472:                            old_code = NullCode;
473:                            continue;
474:                        }
475:                        if (old_code == NullCode) {
476:                            pixelStack[top++] = suffix[code];
477:                            old_code = code;
478:                            first = code;
479:                            continue;
480:                        }
481:                        in_code = code;
482:                        if (code == available) {
483:                            pixelStack[top++] = (byte) first;
484:                            code = old_code;
485:                        }
486:                        while (code > clear) {
487:                            pixelStack[top++] = suffix[code];
488:                            code = prefix[code];
489:                        }
490:                        first = ((int) suffix[code]) & 0xff;
491:
492:                        //  Add a new string to the string table,
493:
494:                        if (available >= MaxStackSize)
495:                            break;
496:                        pixelStack[top++] = (byte) first;
497:                        prefix[available] = (short) old_code;
498:                        suffix[available] = (byte) first;
499:                        available++;
500:                        if (((available & code_mask) == 0)
501:                                && (available < MaxStackSize)) {
502:                            code_size++;
503:                            code_mask += available;
504:                        }
505:                        old_code = in_code;
506:                    }
507:
508:                    //  Pop a pixel off the pixel stack.
509:
510:                    top--;
511:                    i++;
512:
513:                    setPixel(xpos, line, pixelStack[top]);
514:                    ++xpos;
515:                    if (xpos >= iw) {
516:                        xpos = 0;
517:                        line += inc;
518:                        if (line >= ih) {
519:                            if (interlace) {
520:                                do {
521:                                    pass++;
522:                                    switch (pass) {
523:                                    case 2:
524:                                        line = 4;
525:                                        break;
526:                                    case 3:
527:                                        line = 2;
528:                                        inc = 4;
529:                                        break;
530:                                    case 4:
531:                                        line = 1;
532:                                        inc = 2;
533:                                        break;
534:                                    default: // this shouldn't happen
535:                                        line = ih - 1;
536:                                        inc = 0;
537:                                    }
538:                                } while (line >= ih);
539:                            } else {
540:                                line = ih - 1; // this shouldn't happen
541:                                inc = 0;
542:                            }
543:                        }
544:                    }
545:                }
546:                return skipZero;
547:            }
548:
549:            protected void setPixel(int x, int y, int v) {
550:                if (m_bpc == 8) {
551:                    int pos = x + iw * y;
552:                    m_out[pos] = (byte) v;
553:                } else {
554:                    int pos = m_line_stride * y + x / (8 / m_bpc);
555:                    int vout = v << (8 - m_bpc * (x % (8 / m_bpc)) - m_bpc);
556:                    m_out[pos] |= vout;
557:                }
558:            }
559:
560:            /**
561:             * Resets frame state for reading next image.
562:             */
563:            protected void resetFrame() {
564:                // it does nothing in the pdf context
565:                //boolean transparency = false;
566:                //int delay = 0;
567:            }
568:
569:            /**
570:             * Reads Graphics Control Extension values
571:             */
572:            protected void readGraphicControlExt() throws IOException {
573:                in.read(); // block size
574:                int packed = in.read(); // packed fields
575:                dispose = (packed & 0x1c) >> 2; // disposal method
576:                if (dispose == 0)
577:                    dispose = 1; // elect to keep old image if discretionary
578:                transparency = (packed & 1) != 0;
579:                delay = readShort() * 10; // delay in milliseconds
580:                transIndex = in.read(); // transparent color index
581:                in.read(); // block terminator
582:            }
583:
584:            /**
585:             * Skips variable length blocks up to and including
586:             * next zero length block.
587:             */
588:            protected void skip() throws IOException {
589:                do {
590:                    readBlock();
591:                } while (blockSize > 0);
592:            }
593:
594:            static class GifFrame {
595:                Image image;
596:                int ix;
597:                int iy;
598:            }
599:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.