KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > Image


1 /*
2  * $Id: Image.java 2765 2007-05-20 11:11:16Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 by 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;
52
53 import java.awt.Graphics2D JavaDoc;
54 import java.awt.color.ICC_Profile JavaDoc;
55 import java.awt.image.BufferedImage JavaDoc;
56 import java.io.IOException JavaDoc;
57 import java.io.InputStream JavaDoc;
58 import java.lang.reflect.Constructor JavaDoc;
59 import java.net.MalformedURLException JavaDoc;
60 import java.net.URL JavaDoc;
61 import java.util.ArrayList JavaDoc;
62
63 import com.lowagie.text.pdf.PRIndirectReference;
64 import com.lowagie.text.pdf.PdfArray;
65 import com.lowagie.text.pdf.PdfContentByte;
66 import com.lowagie.text.pdf.PdfDictionary;
67 import com.lowagie.text.pdf.PdfIndirectReference;
68 import com.lowagie.text.pdf.PdfName;
69 import com.lowagie.text.pdf.PdfNumber;
70 import com.lowagie.text.pdf.PdfOCG;
71 import com.lowagie.text.pdf.PdfObject;
72 import com.lowagie.text.pdf.PdfReader;
73 import com.lowagie.text.pdf.PdfTemplate;
74 import com.lowagie.text.pdf.PdfWriter;
75 import com.lowagie.text.pdf.RandomAccessFileOrArray;
76 import com.lowagie.text.pdf.codec.BmpImage;
77 import com.lowagie.text.pdf.codec.CCITTG4Encoder;
78 import com.lowagie.text.pdf.codec.GifImage;
79 import com.lowagie.text.pdf.codec.PngImage;
80 import com.lowagie.text.pdf.codec.TiffImage;
81
82 /**
83  * An <CODE>Image</CODE> is the representation of a graphic element (JPEG, PNG
84  * or GIF) that has to be inserted into the document
85  *
86  * @see Element
87  * @see Rectangle
88  */

89
90 public abstract class Image extends Rectangle {
91
92     // static final membervariables
93

94     /** this is a kind of image alignment. */
95     public static final int DEFAULT = 0;
96
97     /** this is a kind of image alignment. */
98     public static final int RIGHT = 2;
99
100     /** this is a kind of image alignment. */
101     public static final int LEFT = 0;
102
103     /** this is a kind of image alignment. */
104     public static final int MIDDLE = 1;
105
106     /** this is a kind of image alignment. */
107     public static final int TEXTWRAP = 4;
108
109     /** this is a kind of image alignment. */
110     public static final int UNDERLYING = 8;
111
112     /** This represents a coordinate in the transformation matrix. */
113     public static final int AX = 0;
114
115     /** This represents a coordinate in the transformation matrix. */
116     public static final int AY = 1;
117
118     /** This represents a coordinate in the transformation matrix. */
119     public static final int BX = 2;
120
121     /** This represents a coordinate in the transformation matrix. */
122     public static final int BY = 3;
123
124     /** This represents a coordinate in the transformation matrix. */
125     public static final int CX = 4;
126
127     /** This represents a coordinate in the transformation matrix. */
128     public static final int CY = 5;
129
130     /** This represents a coordinate in the transformation matrix. */
131     public static final int DX = 6;
132
133     /** This represents a coordinate in the transformation matrix. */
134     public static final int DY = 7;
135
136     /** type of image */
137     public static final int ORIGINAL_NONE = 0;
138
139     /** type of image */
140     public static final int ORIGINAL_JPEG = 1;
141
142     /** type of image */
143     public static final int ORIGINAL_PNG = 2;
144
145     /** type of image */
146     public static final int ORIGINAL_GIF = 3;
147
148     /** type of image */
149     public static final int ORIGINAL_BMP = 4;
150
151     /** type of image */
152     public static final int ORIGINAL_TIFF = 5;
153
154     /** type of image */
155     public static final int ORIGINAL_WMF = 6;
156
157     /** type of image */
158     public static final int ORIGINAL_PS = 7;
159
160     // member variables
161

162     /** The imagetype. */
163     protected int type;
164
165     /** The URL of the image. */
166     protected URL JavaDoc url;
167
168     /** The raw data of the image. */
169     protected byte rawData[];
170
171     /** The bits per component of the raw image. It also flags a CCITT image. */
172     protected int bpc = 1;
173     
174     /** The template to be treated as an image. */
175     protected PdfTemplate template[] = new PdfTemplate[1];
176
177     /** The alignment of the Image. */
178     protected int alignment;
179
180     /** Text that can be shown instead of the image. */
181     protected String JavaDoc alt;
182
183     /** This is the absolute X-position of the image. */
184     protected float absoluteX = Float.NaN;
185
186     /** This is the absolute Y-position of the image. */
187     protected float absoluteY = Float.NaN;
188
189     /** This is the width of the image without rotation. */
190     protected float plainWidth;
191
192     /** This is the width of the image without rotation. */
193     protected float plainHeight;
194
195     /** This is the scaled width of the image taking rotation into account. */
196     protected float scaledWidth;
197
198     /** This is the original height of the image taking rotation into account. */
199     protected float scaledHeight;
200
201     /** an iText attributed unique id for this image. */
202     protected Long JavaDoc mySerialId = getSerialId();
203
204     // image from file or URL
205

206     /**
207      * Constructs an <CODE>Image</CODE> -object, using an <VAR>url </VAR>.
208      *
209      * @param url
210      * the <CODE>URL</CODE> where the image can be found.
211      */

212     public Image(URL JavaDoc url) {
213         super(0, 0);
214         this.url = url;
215         this.alignment = DEFAULT;
216         rotationRadians = 0;
217     }
218
219     /**
220      * Gets an instance of an Image.
221      *
222      * @param url
223      * an URL
224      * @return an Image
225      * @throws BadElementException
226      * @throws MalformedURLException
227      * @throws IOException
228      */

229     public static Image getInstance(URL JavaDoc url) throws BadElementException,
230             MalformedURLException JavaDoc, IOException JavaDoc {
231         InputStream JavaDoc is = null;
232         try {
233             is = url.openStream();
234             int c1 = is.read();
235             int c2 = is.read();
236             int c3 = is.read();
237             int c4 = is.read();
238             is.close();
239
240             is = null;
241             if (c1 == 'G' && c2 == 'I' && c3 == 'F') {
242                 GifImage gif = new GifImage(url);
243                 Image img = gif.getImage(1);
244                 return img;
245             }
246             if (c1 == 0xFF && c2 == 0xD8) {
247                 return new Jpeg(url);
248             }
249             if (c1 == PngImage.PNGID[0] && c2 == PngImage.PNGID[1]
250                     && c3 == PngImage.PNGID[2] && c4 == PngImage.PNGID[3]) {
251                 return PngImage.getImage(url);
252             }
253             if (c1 == 0xD7 && c2 == 0xCD) {
254                 return new ImgWMF(url);
255             }
256             if (c1 == 'B' && c2 == 'M') {
257                 return BmpImage.getImage(url);
258             }
259             if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42)
260                     || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) {
261                 RandomAccessFileOrArray ra = null;
262                 try {
263                     if (url.getProtocol().equals("file")) {
264                         String JavaDoc file = url.getFile();
265                         file = Utilities.unEscapeURL(file);
266                         ra = new RandomAccessFileOrArray(file);
267                     } else
268                         ra = new RandomAccessFileOrArray(url);
269                     Image img = TiffImage.getTiffImage(ra, 1);
270                     img.url = url;
271                     return img;
272                 } finally {
273                     if (ra != null)
274                         ra.close();
275                 }
276
277             }
278             throw new IOException JavaDoc(url.toString()
279                     + " is not a recognized imageformat.");
280         } finally {
281             if (is != null) {
282                 is.close();
283             }
284         }
285     }
286
287     /**
288      * Gets an instance of an Image.
289      *
290      * @param filename
291      * a filename
292      * @return an object of type <CODE>Gif</CODE>,<CODE>Jpeg</CODE> or
293      * <CODE>Png</CODE>
294      * @throws BadElementException
295      * @throws MalformedURLException
296      * @throws IOException
297      */

298     public static Image getInstance(String JavaDoc filename)
299             throws BadElementException, MalformedURLException JavaDoc, IOException JavaDoc {
300         return getInstance(Utilities.toURL(filename));
301     }
302     
303     /**
304      * gets an instance of an Image
305      *
306      * @param imgb
307      * raw image date
308      * @return an Image object
309      * @throws BadElementException
310      * @throws MalformedURLException
311      * @throws IOException
312      */

313     public static Image getInstance(byte imgb[]) throws BadElementException,
314             MalformedURLException JavaDoc, IOException JavaDoc {
315         InputStream JavaDoc is = null;
316         try {
317             is = new java.io.ByteArrayInputStream JavaDoc(imgb);
318             int c1 = is.read();
319             int c2 = is.read();
320             int c3 = is.read();
321             int c4 = is.read();
322             is.close();
323
324             is = null;
325             if (c1 == 'G' && c2 == 'I' && c3 == 'F') {
326                 GifImage gif = new GifImage(imgb);
327                 return gif.getImage(1);
328             }
329             if (c1 == 0xFF && c2 == 0xD8) {
330                 return new Jpeg(imgb);
331             }
332             if (c1 == PngImage.PNGID[0] && c2 == PngImage.PNGID[1]
333                     && c3 == PngImage.PNGID[2] && c4 == PngImage.PNGID[3]) {
334                 return PngImage.getImage(imgb);
335             }
336             if (c1 == 0xD7 && c2 == 0xCD) {
337                 return new ImgWMF(imgb);
338             }
339             if (c1 == 'B' && c2 == 'M') {
340                 return BmpImage.getImage(imgb);
341             }
342             if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42)
343                     || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) {
344                 RandomAccessFileOrArray ra = null;
345                 try {
346                     ra = new RandomAccessFileOrArray(imgb);
347                     Image img = TiffImage.getTiffImage(ra, 1);
348                     if (img.getOriginalData() == null)
349                         img.setOriginalData(imgb);
350                     return img;
351                 } finally {
352                     if (ra != null)
353                         ra.close();
354                 }
355
356             }
357             throw new IOException JavaDoc(
358                     "The byte array is not a recognized imageformat.");
359         } finally {
360             if (is != null) {
361                 is.close();
362             }
363         }
364     }
365
366     /**
367      * Gets an instance of an Image in raw mode.
368      *
369      * @param width
370      * the width of the image in pixels
371      * @param height
372      * the height of the image in pixels
373      * @param components
374      * 1,3 or 4 for GrayScale, RGB and CMYK
375      * @param data
376      * the image data
377      * @param bpc
378      * bits per component
379      * @return an object of type <CODE>ImgRaw</CODE>
380      * @throws BadElementException
381      * on error
382      */

383     public static Image getInstance(int width, int height, int components,
384             int bpc, byte data[]) throws BadElementException {
385         return Image.getInstance(width, height, components, bpc, data, null);
386     }
387
388     /**
389      * Creates an Image with CCITT G3 or G4 compression. It assumes that the
390      * data bytes are already compressed.
391      *
392      * @param width
393      * the exact width of the image
394      * @param height
395      * the exact height of the image
396      * @param reverseBits
397      * reverses the bits in <code>data</code>. Bit 0 is swapped
398      * with bit 7 and so on
399      * @param typeCCITT
400      * the type of compression in <code>data</code>. It can be
401      * CCITTG4, CCITTG31D, CCITTG32D
402      * @param parameters
403      * parameters associated with this stream. Possible values are
404      * CCITT_BLACKIS1, CCITT_ENCODEDBYTEALIGN, CCITT_ENDOFLINE and
405      * CCITT_ENDOFBLOCK or a combination of them
406      * @param data
407      * the image data
408      * @return an Image object
409      * @throws BadElementException
410      * on error
411      */

412     public static Image getInstance(int width, int height, boolean reverseBits,
413             int typeCCITT, int parameters, byte[] data)
414             throws BadElementException {
415         return Image.getInstance(width, height, reverseBits, typeCCITT,
416                 parameters, data, null);
417     }
418
419     /**
420      * Creates an Image with CCITT G3 or G4 compression. It assumes that the
421      * data bytes are already compressed.
422      *
423      * @param width
424      * the exact width of the image
425      * @param height
426      * the exact height of the image
427      * @param reverseBits
428      * reverses the bits in <code>data</code>. Bit 0 is swapped
429      * with bit 7 and so on
430      * @param typeCCITT
431      * the type of compression in <code>data</code>. It can be
432      * CCITTG4, CCITTG31D, CCITTG32D
433      * @param parameters
434      * parameters associated with this stream. Possible values are
435      * CCITT_BLACKIS1, CCITT_ENCODEDBYTEALIGN, CCITT_ENDOFLINE and
436      * CCITT_ENDOFBLOCK or a combination of them
437      * @param data
438      * the image data
439      * @param transparency
440      * transparency information in the Mask format of the image
441      * dictionary
442      * @return an Image object
443      * @throws BadElementException
444      * on error
445      */

446     public static Image getInstance(int width, int height, boolean reverseBits,
447             int typeCCITT, int parameters, byte[] data, int transparency[])
448             throws BadElementException {
449         if (transparency != null && transparency.length != 2)
450             throw new BadElementException(
451                     "Transparency length must be equal to 2 with CCITT images");
452         Image img = new ImgCCITT(width, height, reverseBits, typeCCITT,
453                 parameters, data);
454         img.transparency = transparency;
455         return img;
456     }
457
458     /**
459      * Gets an instance of an Image in raw mode.
460      *
461      * @param width
462      * the width of the image in pixels
463      * @param height
464      * the height of the image in pixels
465      * @param components
466      * 1,3 or 4 for GrayScale, RGB and CMYK
467      * @param data
468      * the image data
469      * @param bpc
470      * bits per component
471      * @param transparency
472      * transparency information in the Mask format of the image
473      * dictionary
474      * @return an object of type <CODE>ImgRaw</CODE>
475      * @throws BadElementException
476      * on error
477      */

478     public static Image getInstance(int width, int height, int components,
479             int bpc, byte data[], int transparency[])
480             throws BadElementException {
481         if (transparency != null && transparency.length != components * 2)
482             throw new BadElementException(
483                     "Transparency length must be equal to (componentes * 2)");
484         if (components == 1 && bpc == 1) {
485             byte g4[] = CCITTG4Encoder.compress(data, width, height);
486             return Image.getInstance(width, height, false, Image.CCITTG4,
487                     Image.CCITT_BLACKIS1, g4, transparency);
488         }
489         Image img = new ImgRaw(width, height, components, bpc, data);
490         img.transparency = transparency;
491         return img;
492     }
493
494     // images from a PdfTemplate
495

496     /**
497      * gets an instance of an Image
498      *
499      * @param template
500      * a PdfTemplate that has to be wrapped in an Image object
501      * @return an Image object
502      * @throws BadElementException
503      */

504     public static Image getInstance(PdfTemplate template)
505             throws BadElementException {
506         return new ImgTemplate(template);
507     }
508     
509     // images from a java.awt.Image
510

511     /**
512      * Gets an instance of an Image from a java.awt.Image.
513      *
514      * @param image
515      * the <CODE>java.awt.Image</CODE> to convert
516      * @param color
517      * if different from <CODE>null</CODE> the transparency pixels
518      * are replaced by this color
519      * @param forceBW
520      * if <CODE>true</CODE> the image is treated as black and white
521      * @return an object of type <CODE>ImgRaw</CODE>
522      * @throws BadElementException
523      * on error
524      * @throws IOException
525      * on error
526      */

527     public static Image getInstance(java.awt.Image JavaDoc image, java.awt.Color JavaDoc color,
528             boolean forceBW) throws BadElementException, IOException JavaDoc {
529         
530         if(image instanceof BufferedImage JavaDoc){
531             BufferedImage JavaDoc bi = (BufferedImage JavaDoc) image;
532             if(bi.getType()==BufferedImage.TYPE_BYTE_BINARY) {
533                 forceBW=true;
534             }
535         }
536         
537         java.awt.image.PixelGrabber JavaDoc pg = new java.awt.image.PixelGrabber JavaDoc(image,
538                 0, 0, -1, -1, true);
539         try {
540             pg.grabPixels();
541         } catch (InterruptedException JavaDoc e) {
542             throw new IOException JavaDoc(
543                     "java.awt.Image Interrupted waiting for pixels!");
544         }
545         if ((pg.getStatus() & java.awt.image.ImageObserver.ABORT) != 0) {
546             throw new IOException JavaDoc("java.awt.Image fetch aborted or errored");
547         }
548         int w = pg.getWidth();
549         int h = pg.getHeight();
550         int[] pixels = (int[]) pg.getPixels();
551         if (forceBW) {
552             int byteWidth = (w / 8) + ((w & 7) != 0 ? 1 : 0);
553             byte[] pixelsByte = new byte[byteWidth * h];
554
555             int index = 0;
556             int size = h * w;
557             int transColor = 1;
558             if (color != null) {
559                 transColor = (color.getRed() + color.getGreen()
560                         + color.getBlue() < 384) ? 0 : 1;
561             }
562             int transparency[] = null;
563             int cbyte = 0x80;
564             int wMarker = 0;
565             int currByte = 0;
566             if (color != null) {
567                 for (int j = 0; j < size; j++) {
568                     int alpha = (pixels[j] >> 24) & 0xff;
569                     if (alpha < 250) {
570                         if (transColor == 1)
571                             currByte |= cbyte;
572                     } else {
573                         if ((pixels[j] & 0x888) != 0)
574                             currByte |= cbyte;
575                     }
576                     cbyte >>= 1;
577                     if (cbyte == 0 || wMarker + 1 >= w) {
578                         pixelsByte[index++] = (byte) currByte;
579                         cbyte = 0x80;
580                         currByte = 0;
581                     }
582                     ++wMarker;
583                     if (wMarker >= w)
584                         wMarker = 0;
585                 }
586             } else {
587                 for (int j = 0; j < size; j++) {
588                     if (transparency == null) {
589                         int alpha = (pixels[j] >> 24) & 0xff;
590                         if (alpha == 0) {
591                             transparency = new int[2];
592                             transparency[0] = transparency[1] = ((pixels[j] & 0x888) != 0) ? 1
593                                     : 0;
594                         }
595                     }
596                     if ((pixels[j] & 0x888) != 0)
597                         currByte |= cbyte;
598                     cbyte >>= 1;
599                     if (cbyte == 0 || wMarker + 1 >= w) {
600                         pixelsByte[index++] = (byte) currByte;
601                         cbyte = 0x80;
602                         currByte = 0;
603                     }
604                     ++wMarker;
605                     if (wMarker >= w)
606                         wMarker = 0;
607                 }
608             }
609             return Image.getInstance(w, h, 1, 1, pixelsByte, transparency);
610         } else {
611             byte[] pixelsByte = new byte[w * h * 3];
612             byte[] smask = null;
613
614             int index = 0;
615             int size = h * w;
616             int red = 255;
617             int green = 255;
618             int blue = 255;
619             if (color != null) {
620                 red = color.getRed();
621                 green = color.getGreen();
622                 blue = color.getBlue();
623             }
624             int transparency[] = null;
625             if (color != null) {
626                 for (int j = 0; j < size; j++) {
627                     int alpha = (pixels[j] >> 24) & 0xff;
628                     if (alpha < 250) {
629                         pixelsByte[index++] = (byte) red;
630                         pixelsByte[index++] = (byte) green;
631                         pixelsByte[index++] = (byte) blue;
632                     } else {
633                         pixelsByte[index++] = (byte) ((pixels[j] >> 16) & 0xff);
634                         pixelsByte[index++] = (byte) ((pixels[j] >> 8) & 0xff);
635                         pixelsByte[index++] = (byte) ((pixels[j]) & 0xff);
636                     }
637                 }
638             } else {
639                 int transparentPixel = 0;
640                 smask = new byte[w * h];
641                 boolean shades = false;
642                 for (int j = 0; j < size; j++) {
643                     byte alpha = smask[j] = (byte) ((pixels[j] >> 24) & 0xff);
644                     /* bugfix by Chris Nokleberg */
645                     if (!shades) {
646                         if (alpha != 0 && alpha != -1) {
647                             shades = true;
648                         } else if (transparency == null) {
649                             if (alpha == 0) {
650                                 transparentPixel = pixels[j] & 0xffffff;
651                                 transparency = new int[6];
652                                 transparency[0] = transparency[1] = (transparentPixel >> 16) & 0xff;
653                                 transparency[2] = transparency[3] = (transparentPixel >> 8) & 0xff;
654                                 transparency[4] = transparency[5] = transparentPixel & 0xff;
655                             }
656                         } else if ((pixels[j] & 0xffffff) != transparentPixel) {
657                             shades = true;
658                         }
659                     }
660                     pixelsByte[index++] = (byte) ((pixels[j] >> 16) & 0xff);
661                     pixelsByte[index++] = (byte) ((pixels[j] >> 8) & 0xff);
662                     pixelsByte[index++] = (byte) ((pixels[j]) & 0xff);
663                 }
664                 if (shades)
665                     transparency = null;
666                 else
667                     smask = null;
668             }
669             Image img = Image.getInstance(w, h, 3, 8, pixelsByte, transparency);
670             if (smask != null) {
671                 Image sm = Image.getInstance(w, h, 1, 8, smask);
672                 try {
673                     sm.makeMask();
674                     img.setImageMask(sm);
675                 } catch (DocumentException de) {
676                     throw new ExceptionConverter(de);
677                 }
678             }
679             return img;
680         }
681     }
682
683     /**
684      * Gets an instance of an Image from a java.awt.Image.
685      *
686      * @param image
687      * the <CODE>java.awt.Image</CODE> to convert
688      * @param color
689      * if different from <CODE>null</CODE> the transparency pixels
690      * are replaced by this color
691      * @return an object of type <CODE>ImgRaw</CODE>
692      * @throws BadElementException
693      * on error
694      * @throws IOException
695      * on error
696      */

697     public static Image getInstance(java.awt.Image JavaDoc image, java.awt.Color JavaDoc color)
698             throws BadElementException, IOException JavaDoc {
699         return Image.getInstance(image, color, false);
700     }
701     
702     /**
703      * Gets an instance of a Image from a java.awt.Image.
704      * The image is added as a JPEG with a userdefined quality.
705      *
706      * @param writer
707      * the <CODE>PdfWriter</CODE> object to which the image will be added
708      * @param awtImage
709      * the <CODE>java.awt.Image</CODE> to convert
710      * @param quality
711      * a float value between 0 and 1
712      * @return an object of type <CODE>PdfTemplate</CODE>
713      * @throws BadElementException
714      * on error
715      * @throws IOException
716      */

717     public static Image getInstance(PdfWriter writer, java.awt.Image JavaDoc awtImage, float quality) throws BadElementException, IOException JavaDoc {
718         return getInstance(new PdfContentByte(writer), awtImage, quality);
719     }
720     
721     /**
722      * Gets an instance of a Image from a java.awt.Image.
723      * The image is added as a JPEG with a userdefined quality.
724      *
725      * @param cb
726      * the <CODE>PdfContentByte</CODE> object to which the image will be added
727      * @param awtImage
728      * the <CODE>java.awt.Image</CODE> to convert
729      * @param quality
730      * a float value between 0 and 1
731      * @return an object of type <CODE>PdfTemplate</CODE>
732      * @throws BadElementException
733      * on error
734      * @throws IOException
735      */

736     public static Image getInstance(PdfContentByte cb, java.awt.Image JavaDoc awtImage, float quality) throws BadElementException, IOException JavaDoc {
737         java.awt.image.PixelGrabber JavaDoc pg = new java.awt.image.PixelGrabber JavaDoc(awtImage,
738                 0, 0, -1, -1, true);
739         try {
740             pg.grabPixels();
741         } catch (InterruptedException JavaDoc e) {
742             throw new IOException JavaDoc(
743                     "java.awt.Image Interrupted waiting for pixels!");
744         }
745         if ((pg.getStatus() & java.awt.image.ImageObserver.ABORT) != 0) {
746             throw new IOException JavaDoc("java.awt.Image fetch aborted or errored");
747         }
748         int w = pg.getWidth();
749         int h = pg.getHeight();
750         PdfTemplate tp = cb.createTemplate(w, h);
751         Graphics2D JavaDoc g2d = tp.createGraphics(w, h, true, quality);
752         g2d.drawImage(awtImage, 0, 0, null);
753         g2d.dispose();
754         return getInstance(tp);
755     }
756
757     // image from indirect reference
758

759     /**
760      * Holds value of property directReference.
761      * An image is embedded into a PDF as an Image XObject.
762      * This object is referenced by a PdfIndirectReference object.
763      */

764     private PdfIndirectReference directReference;
765     
766     /**
767      * Getter for property directReference.
768