KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > ImageUtils


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.util;
17
18 import java.io.File JavaDoc;
19 import java.io.FileNotFoundException JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23
24 /**
25  * @author <a HREF="mailto:tcurdt@apache.org">Torsten Curdt</a>
26  * @author <a HREF="mailto:balld@webslingerZ.com">Donald A. Ball Jr.</a>
27  * @version CVS $Id: ImageUtils.java 30932 2004-07-29 17:35:38Z vgritsenko $
28  */

29 final public class ImageUtils {
30
31     final public static ImageProperties getImageProperties(File JavaDoc file) throws FileNotFoundException JavaDoc, IOException JavaDoc, FileFormatException {
32         String JavaDoc type = MIMEUtils.getMIMEType(file);
33
34         if ("image/gif".equals(type)) {
35              return (getGifProperties(file));
36         }
37         else if ("image/jpeg".equals(type)) {
38             return (getJpegProperties(file));
39         }
40         else {
41             return (null);
42         }
43     }
44
45     final public static ImageProperties getJpegProperties(File JavaDoc file) throws FileNotFoundException JavaDoc, IOException JavaDoc, FileFormatException {
46         BufferedInputStream JavaDoc in = null;
47         try {
48             in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
49
50             // check for "magic" header
51
byte[] buf = new byte[2];
52             int count = in.read(buf, 0, 2);
53             if (count < 2) {
54                 throw new FileFormatException("Not a valid Jpeg file!");
55             }
56             if ((buf[0]) != (byte) 0xFF || (buf[1]) != (byte) 0xD8) {
57                 throw new FileFormatException("Not a valid Jpeg file!");
58             }
59
60             int width = 0;
61             int height = 0;
62             char[] comment = null;
63
64             boolean hasDims = false;
65             boolean hasComment = false;
66             int ch = 0;
67
68             while (ch != 0xDA && !(hasDims && hasComment)) {
69                 /* Find next marker (JPEG markers begin with 0xFF) */
70                 while (ch != 0xFF) {
71                     ch = in.read();
72                 }
73                 /* JPEG markers can be padded with unlimited 0xFF's */
74                 while (ch == 0xFF) {
75                     ch = in.read();
76                 }
77                 /* Now, ch contains the value of the marker. */
78
79                 int length = 256 * in.read();
80                 length += in.read();
81                 if (length < 2) {
82                     throw new FileFormatException("Not a valid Jpeg file!");
83                 }
84                 /* Now, length contains the length of the marker. */
85
86                 if (ch >= 0xC0 && ch <= 0xC3) {
87                     in.read();
88                     height = 256 * in.read();
89                     height += in.read();
90                     width = 256 * in.read();
91                     width += in.read();
92                     for (int foo = 0; foo < length - 2 - 5; foo++) {
93                         in.read();
94                     }
95                     hasDims = true;
96                 }
97                 else if (ch == 0xFE) {
98                     // that's the comment marker
99
comment = new char[length-2];
100                     for (int foo = 0; foo < length - 2; foo++)
101                         comment[foo] = (char) in.read();
102                     hasComment = true;
103                 }
104                 else {
105                     // just skip marker
106
for (int foo = 0; foo < length - 2; foo++) {
107                         in.read();
108                     }
109                 }
110             }
111             return (new ImageProperties(width, height, comment, "jpeg"));
112
113         }
114         finally {
115             if (in != null) {
116                 try {
117                     in.close();
118                 }
119                 catch (IOException JavaDoc e) {
120                 }
121             }
122         }
123     }
124
125     final public static ImageProperties getGifProperties(File JavaDoc file) throws FileNotFoundException JavaDoc, IOException JavaDoc, FileFormatException {
126         BufferedInputStream JavaDoc in = null;
127         try {
128             in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
129             byte[] buf = new byte[10];
130             int count = in.read(buf, 0, 10);
131             if (count < 10) {
132                 throw new FileFormatException("Not a valid Gif file!");
133             }
134             if ((buf[0]) != (byte) 'G' || (buf[1]) != (byte) 'I' || (buf[2]) != (byte) 'F') {
135                 throw new FileFormatException("Not a valid Gif file!");
136             }
137
138             int w1 = (buf[6] & 0xff) | (buf[6] & 0x80);
139             int w2 = (buf[7] & 0xff) | (buf[7] & 0x80);
140             int h1 = (buf[8] & 0xff) | (buf[8] & 0x80);
141             int h2 = (buf[9] & 0xff) | (buf[9] & 0x80);
142
143             int width = w1 + (w2 << 8);
144             int height = h1 + (h2 << 8);
145
146             return (new ImageProperties(width, height, null,"gif"));
147
148         }
149         finally {
150             if (in != null) {
151                 try {
152                     in.close();
153                 }
154                 catch (IOException JavaDoc e) {
155                 }
156             }
157         }
158     }
159 }
160
Popular Tags