KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > Images


1 /*
2   Copyright (C) 2004 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.awt.Dimension JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import org.apache.log4j.Logger;
26
27 /**
28  * Contains various methods related to image files
29  */

30
31 public class Images {
32     static Logger logger = Logger.getLogger("images");
33
34     protected static final byte[]
35         PNG_SIG = new byte[] {-119, 'P', 'N', 'G', 13, 10, 26, 10};
36     protected static final byte[]
37         GIF87A_SIG = new byte[] {'G', 'I', 'F', '8', '7', 'a' };
38     protected static final byte[]
39         GIF89A_SIG = new byte[] {'G', 'I', 'F', '8', '9', 'a' };
40     protected static final byte[]
41         JFIF_SIG = new byte[] {-1, -40, -1, -32, 0, 0, 'J', 'F', 'I', 'F'};
42
43
44     /**
45      * Tells the size of an image. Supported formats are GIF and PNG.
46      *
47      * @param img the file of the image
48      * @return The size of the image, null if the image format is unhandled
49      *
50      * @see #getImageSize(InputStream)
51      */

52     public static Dimension JavaDoc getImageFileSize(File img)
53         throws IOException JavaDoc
54     {
55         return getImageSize(
56             Images.class.getClassLoader().getResourceAsStream(
57                 img.getPath()));
58     }
59
60     /**
61      * Tells the size of an image. Supported formats are GIF and PNG.
62      *
63      * @param img the data of the image
64      * @return The size of the image, null if the image format is unhandled
65      *
66      * @see #getImageFileSize(File)
67      */

68     public static Dimension JavaDoc getImageSize(InputStream JavaDoc img)
69         throws IOException JavaDoc
70     {
71
72         int read;
73         byte [] buf = new byte[24];
74         if ((read=img.read(buf,0,24))>=10) {
75             if (ExtArrays.equals(buf, 0, GIF87A_SIG, 0, 6) ||
76                 ExtArrays.equals(buf, 0, GIF89A_SIG, 0, 6)) {
77                 return new Dimension JavaDoc(
78                     buf[7]*(2^8)+buf[6],
79                     buf[9]*(2^8)+buf[8]);
80             } else if (ExtArrays.equals(buf,0,PNG_SIG,0,8)) {
81                 if (read==24) {
82                     return new Dimension JavaDoc(
83                         buf[16]*2^24+buf[17]*2^16+buf[18]*2^8+buf[19],
84                         buf[20]*2^24+buf[21]*2^16+buf[22]*2^8+buf[23]);
85                 } else {
86                     throw new RuntimeException JavaDoc(
87                         "getImageSize: not enough data");
88                 }
89             } else if (ExtArrays.equals(buf,0,JFIF_SIG,0,4) &&
90                        ExtArrays.equals(buf,6,JFIF_SIG,6,4)) {
91                 logger.debug("getImageSize: JPEG is not supported yet");
92                 return null;
93             }
94             logger.warn("Unrecognized signature"+ExtArrays.asList(buf));
95             return null;
96         } else {
97             throw new RuntimeException JavaDoc("getImageSize: not enough data");
98         }
99     }
100 }
101
Popular Tags