KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Jpeg.java 2752 2007-05-15 14:58:33Z 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.io.IOException JavaDoc;
54 import java.io.InputStream JavaDoc;
55 import java.net.URL JavaDoc;
56
57 /**
58  * An <CODE>Jpeg</CODE> is the representation of a graphic element (JPEG)
59  * that has to be inserted into the document
60  *
61  * @see Element
62  * @see Image
63  */

64
65 public class Jpeg extends Image {
66     
67     // public static final membervariables
68

69     /** This is a type of marker. */
70     public static final int NOT_A_MARKER = -1;
71     
72     /** This is a type of marker. */
73     public static final int VALID_MARKER = 0;
74     
75     /** Acceptable Jpeg markers. */
76     public static final int[] VALID_MARKERS = {0xC0, 0xC1, 0xC2};
77     
78     /** This is a type of marker. */
79     public static final int UNSUPPORTED_MARKER = 1;
80     
81     /** Unsupported Jpeg markers. */
82     public static final int[] UNSUPPORTED_MARKERS = {0xC3, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF};
83     
84     /** This is a type of marker. */
85     public static final int NOPARAM_MARKER = 2;
86     
87     /** Jpeg markers without additional parameters. */
88     public static final int[] NOPARAM_MARKERS = {0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0x01};
89     
90     /** Marker value */
91     public static final int M_APP0 = 0xE0;
92     /** Marker value */
93     public static final int M_APPE = 0xEE;
94     
95     /** sequence that is used in all Jpeg files */
96     public static final byte JFIF_ID[] = {0x4A, 0x46, 0x49, 0x46, 0x00};
97     // Constructors
98

99     Jpeg(Image image) {
100         super(image);
101     }
102
103     /**
104      * Constructs a <CODE>Jpeg</CODE>-object, using an <VAR>url</VAR>.
105      *
106      * @param url the <CODE>URL</CODE> where the image can be found
107      * @throws BadElementException
108      * @throws IOException
109      */

110     public Jpeg(URL JavaDoc url) throws BadElementException, IOException JavaDoc {
111         super(url);
112         processParameters();
113     }
114     
115     /**
116      * Constructs a <CODE>Jpeg</CODE>-object from memory.
117      *
118      * @param img the memory image
119      * @throws BadElementException
120      * @throws IOException
121      */

122     
123     public Jpeg(byte[] img) throws BadElementException, IOException JavaDoc {
124         super((URL JavaDoc)null);
125         rawData = img;
126         originalData = img;
127         processParameters();
128     }
129     
130     /**
131      * Constructs a <CODE>Jpeg</CODE>-object from memory.
132      *
133      * @param img the memory image.
134      * @param width the width you want the image to have
135      * @param height the height you want the image to have
136      * @throws BadElementException
137      * @throws IOException
138      */

139     
140     public Jpeg(byte[] img, float width, float height) throws BadElementException, IOException JavaDoc {
141         this(img);
142         scaledWidth = width;
143         scaledHeight = height;
144     }
145     
146     // private static methods
147

148     /**
149      * Reads a short from the <CODE>InputStream</CODE>.
150      *
151      * @param is the <CODE>InputStream</CODE>
152      * @return an int
153      * @throws IOException
154      */

155     private static final int getShort(InputStream JavaDoc is) throws IOException JavaDoc {
156         return (is.read() << 8) + is.read();
157     }
158     
159     /**
160      * Returns a type of marker.
161      *
162      * @param marker an int
163      * @return a type: <VAR>VALID_MARKER</CODE>, <VAR>UNSUPPORTED_MARKER</VAR> or <VAR>NOPARAM_MARKER</VAR>
164      */

165     private static final int marker(int marker) {
166         for (int i = 0; i < VALID_MARKERS.length; i++) {
167             if (marker == VALID_MARKERS[i]) {
168                 return VALID_MARKER;
169             }
170         }
171         for (int i = 0; i < NOPARAM_MARKERS.length; i++) {
172             if (marker == NOPARAM_MARKERS[i]) {
173                 return NOPARAM_MARKER;
174             }
175         }
176         for (int i = 0; i < UNSUPPORTED_MARKERS.length; i++) {
177             if (marker == UNSUPPORTED_MARKERS[i]) {
178                 return UNSUPPORTED_MARKER;
179             }
180         }
181         return NOT_A_MARKER;
182     }
183     
184     // private methods
185

186     /**
187      * This method checks if the image is a valid JPEG and processes some parameters.
188      * @throws BadElementException
189      * @throws IOException
190      */

191     private void processParameters() throws BadElementException, IOException JavaDoc {
192         type = JPEG;
193         originalType = ORIGINAL_JPEG;
194         InputStream JavaDoc is = null;
195         try {
196             String JavaDoc errorID;
197             if (rawData == null){
198                 is = url.openStream();
199                 errorID = url.toString();
200             }
201             else{
202                 is = new java.io.ByteArrayInputStream JavaDoc(rawData);
203                 errorID = "Byte array";
204             }
205             if (is.read() != 0xFF || is.read() != 0xD8) {
206                 throw new BadElementException(errorID + " is not a valid JPEG-file.");
207             }
208             boolean firstPass = true;
209             int len;
210             while (true) {
211                 int v = is.read();
212                 if (v < 0)
213                     throw new IOException JavaDoc("Premature EOF while reading JPG.");
214                 if (v == 0xFF) {
215                     int marker = is.read();
216                     if (firstPass && marker == M_APP0) {
217                         firstPass = false;
218                         len = getShort(is);
219                         if (len < 16) {
220                             Utilities.skip(is, len - 2);
221                             continue;
222                         }
223                         byte bcomp[] = new byte[JFIF_ID.length];
224                         int r = is.read(bcomp);
225                         if (r != bcomp.length)
226                             throw new BadElementException(errorID + " corrupted JFIF marker.");
227                         boolean found = true;
228                         for (int k = 0; k < bcomp.length; ++k) {
229                             if (bcomp[k] != JFIF_ID[k]) {
230                                 found = false;
231                                 break;
232                             }
233                         }
234                         if (!found) {
235                             Utilities.skip(is, len - 2 - bcomp.length);
236                             continue;
237                         }
238                         Utilities.skip(is, 2);
239                         int units = is.read();
240                         int dx = getShort(is);
241                         int dy = getShort(is);
242                         if (units == 1) {
243                             dpiX = dx;
244                             dpiY = dy;
245                         }
246                         else if (units == 2) {
247                             dpiX = (int)((float)dx * 2.54f + 0.5f);
248                             dpiY = (int)((float)dy * 2.54f + 0.5f);
249                         }
250                         Utilities.skip(is, len - 2 - bcomp.length - 7);
251                         continue;
252                     }
253                     if (marker == M_APPE) {
254                         len = getShort(is) - 2;
255                         byte[] byteappe = new byte[len];
256                         for (int k = 0; k < len; ++k) {
257                             byteappe[k] = (byte)is.read();
258                         }
259                         if (byteappe.length >= 12) {
260                             String JavaDoc appe = new String JavaDoc(byteappe, 0, 5, "ISO-8859-1");
261                             if (appe.equals("Adobe")) {
262                                 invert = true;
263                             }
264                         }
265                         continue;
266                     }
267                     firstPass = false;
268                     int markertype = marker(marker);
269                     if (markertype == VALID_MARKER) {
270                         Utilities.skip(is, 2);
271                         if (is.read() != 0x08) {
272                             throw new BadElementException(errorID + " must have 8 bits per component.");
273                         }
274                         scaledHeight = getShort(is);
275                         setTop(scaledHeight);
276                         scaledWidth = getShort(is);
277                         setRight(scaledWidth);
278                         colorspace = is.read();
279                         bpc = 8;
280                         break;
281                     }
282                     else if (markertype == UNSUPPORTED_MARKER) {
283                         throw new BadElementException(errorID + ": unsupported JPEG marker: " + marker);
284                     }
285                     else if (markertype != NOPARAM_MARKER) {
286                         Utilities.skip(is, getShort(is) - 2);
287                     }
288                 }
289             }
290         }
291         finally {
292             if (is != null) {
293                 is.close();
294             }
295             plainWidth = getWidth();
296             plainHeight = getHeight();
297         }
298     }
299 }
300
Popular Tags