001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.util;
018:
019: import java.io.File;
020: import java.io.FileNotFoundException;
021: import java.io.IOException;
022: import java.io.BufferedInputStream;
023: import java.io.FileInputStream;
024:
025: /**
026: * @author <a href="mailto:tcurdt@apache.org">Torsten Curdt</a>
027: * @author <a href="mailto:balld@webslingerZ.com">Donald A. Ball Jr.</a>
028: * @version CVS $Id: ImageUtils.java 433543 2006-08-22 06:22:54Z crossley $
029: */
030: final public class ImageUtils {
031:
032: final public static ImageProperties getImageProperties(File file)
033: throws FileNotFoundException, IOException,
034: FileFormatException {
035: String type = MIMEUtils.getMIMEType(file);
036:
037: if ("image/gif".equals(type)) {
038: return (getGifProperties(file));
039: } else if ("image/jpeg".equals(type)) {
040: return (getJpegProperties(file));
041: } else {
042: return (null);
043: }
044: }
045:
046: final public static ImageProperties getJpegProperties(File file)
047: throws FileNotFoundException, IOException,
048: FileFormatException {
049: BufferedInputStream in = null;
050: try {
051: in = new BufferedInputStream(new FileInputStream(file));
052:
053: // check for "magic" header
054: byte[] buf = new byte[2];
055: int count = in.read(buf, 0, 2);
056: if (count < 2) {
057: throw new FileFormatException("Not a valid Jpeg file!");
058: }
059: if ((buf[0]) != (byte) 0xFF || (buf[1]) != (byte) 0xD8) {
060: throw new FileFormatException("Not a valid Jpeg file!");
061: }
062:
063: int width = 0;
064: int height = 0;
065: char[] comment = null;
066:
067: boolean hasDims = false;
068: boolean hasComment = false;
069: int ch = 0;
070:
071: while (ch != 0xDA && !(hasDims && hasComment)) {
072: /* Find next marker (JPEG markers begin with 0xFF) */
073: while (ch != 0xFF) {
074: ch = in.read();
075: }
076: /* JPEG markers can be padded with unlimited 0xFF's */
077: while (ch == 0xFF) {
078: ch = in.read();
079: }
080: /* Now, ch contains the value of the marker. */
081:
082: int length = 256 * in.read();
083: length += in.read();
084: if (length < 2) {
085: throw new FileFormatException(
086: "Not a valid Jpeg file!");
087: }
088: /* Now, length contains the length of the marker. */
089:
090: if (ch >= 0xC0 && ch <= 0xC3) {
091: in.read();
092: height = 256 * in.read();
093: height += in.read();
094: width = 256 * in.read();
095: width += in.read();
096: for (int foo = 0; foo < length - 2 - 5; foo++) {
097: in.read();
098: }
099: hasDims = true;
100: } else if (ch == 0xFE) {
101: // that's the comment marker
102: comment = new char[length - 2];
103: for (int foo = 0; foo < length - 2; foo++)
104: comment[foo] = (char) in.read();
105: hasComment = true;
106: } else {
107: // just skip marker
108: for (int foo = 0; foo < length - 2; foo++) {
109: in.read();
110: }
111: }
112: }
113: return (new ImageProperties(width, height, comment, "jpeg"));
114:
115: } finally {
116: if (in != null) {
117: try {
118: in.close();
119: } catch (IOException e) {
120: }
121: }
122: }
123: }
124:
125: final public static ImageProperties getGifProperties(File file)
126: throws FileNotFoundException, IOException,
127: FileFormatException {
128: BufferedInputStream in = null;
129: try {
130: in = new BufferedInputStream(new FileInputStream(file));
131: byte[] buf = new byte[10];
132: int count = in.read(buf, 0, 10);
133: if (count < 10) {
134: throw new FileFormatException("Not a valid Gif file!");
135: }
136: if ((buf[0]) != (byte) 'G' || (buf[1]) != (byte) 'I'
137: || (buf[2]) != (byte) 'F') {
138: throw new FileFormatException("Not a valid Gif file!");
139: }
140:
141: int w1 = (buf[6] & 0xff) | (buf[6] & 0x80);
142: int w2 = (buf[7] & 0xff) | (buf[7] & 0x80);
143: int h1 = (buf[8] & 0xff) | (buf[8] & 0x80);
144: int h2 = (buf[9] & 0xff) | (buf[9] & 0x80);
145:
146: int width = w1 + (w2 << 8);
147: int height = h1 + (h2 << 8);
148:
149: return (new ImageProperties(width, height, null, "gif"));
150:
151: } finally {
152: if (in != null) {
153: try {
154: in.close();
155: } catch (IOException e) {
156: }
157: }
158: }
159: }
160: }
|