281
|
1 /* ImageOps -- convenience methods for images
|
|
2
|
|
3 Digital Image Library servlet components
|
|
4
|
|
5 Copyright (C) 2004 Robert Casties (robcast@mail.berlios.de)
|
|
6
|
|
7 This program is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2 of the License, or (at your
|
|
10 option) any later version.
|
|
11
|
|
12 Please read license.txt for the full details. A copy of the GPL
|
|
13 may be found at http://www.gnu.org/copyleft/lgpl.html
|
|
14
|
|
15 You should have received a copy of the GNU General Public License
|
|
16 along with this program; if not, write to the Free Software
|
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18
|
|
19 * Created on 13.10.2004
|
|
20 */
|
|
21 package digilib.image;
|
|
22
|
|
23 import java.io.File;
|
|
24 import java.io.IOException;
|
|
25 import java.io.RandomAccessFile;
|
|
26 import java.util.Iterator;
|
|
27
|
|
28 import javax.imageio.ImageIO;
|
|
29 import javax.imageio.ImageReader;
|
|
30 import javax.imageio.stream.ImageInputStream;
|
|
31
|
|
32 import org.apache.log4j.Logger;
|
|
33 import org.marcoschmidt.image.ImageInfo;
|
|
34
|
|
35 import digilib.io.FileOpException;
|
|
36 import digilib.io.FileOps;
|
|
37 import digilib.io.ImageFile;
|
|
38 import digilib.io.ImageFileset;
|
|
39
|
|
40 /**
|
|
41 * convenience methods for images
|
|
42 *
|
|
43 * @author casties
|
|
44 */
|
|
45 public class ImageOps {
|
|
46
|
|
47 private static Logger logger = Logger.getLogger(ImageOps.class);
|
|
48
|
352
|
49 public static int TYPE_AUTO = 0;
|
|
50 public static int TYPE_JPEG = 1;
|
|
51 public static int TYPE_PNG = 2;
|
|
52
|
|
53
|
281
|
54 /** Check image size and type and store in ImageFile f */
|
|
55 public static boolean checkFile(ImageFile imgf) throws IOException {
|
|
56 // fileset to store the information
|
|
57 ImageFileset imgfs = imgf.getParent();
|
|
58 File f = imgf.getFile();
|
|
59 if (f == null) {
|
|
60 throw new IOException("File not found!");
|
|
61 }
|
|
62 RandomAccessFile raf = new RandomAccessFile(f, "r");
|
|
63 // set up ImageInfo object
|
|
64 ImageInfo iif = new ImageInfo();
|
|
65 iif.setInput(raf);
|
|
66 iif.setCollectComments(false);
|
|
67 iif.setDetermineImageNumber(false);
|
|
68 logger.debug("identifying (ImageInfo) " + f);
|
|
69 // try with ImageInfo first
|
|
70 if (iif.check()) {
|
|
71 ImageSize d = new ImageSize(iif.getWidth(), iif.getHeight());
|
|
72 imgf.setSize(d);
|
|
73 imgf.setMimetype(iif.getMimeType());
|
|
74 //logger.debug(" format:"+iif.getFormatName());
|
|
75 if (imgfs != null) {
|
|
76 imgfs.setAspect(d);
|
|
77 }
|
|
78 raf.close();
|
|
79 iif = null;
|
|
80 } else {
|
|
81 iif = null;
|
|
82 logger.debug("identifying (ImageIO) " + f);
|
|
83 /*
|
|
84 * else use ImageReader
|
|
85 */
|
|
86 ImageInputStream istream = ImageIO.createImageInputStream(raf);
|
|
87 Iterator readers = ImageIO.getImageReaders(istream);
|
|
88 if ((readers == null) || (!readers.hasNext())) {
|
|
89 throw new FileOpException("ERROR: unknown image file format!");
|
|
90 }
|
|
91 ImageReader reader = (ImageReader) readers.next();
|
|
92 /* are there more readers? */
|
|
93 logger.debug("ImageIO: this reader: " + reader.getClass());
|
|
94 while (readers.hasNext()) {
|
|
95 logger.debug("ImageIO: next reader: "
|
|
96 + readers.next().getClass());
|
|
97 }
|
|
98 reader.setInput(istream);
|
|
99 ImageSize d = new ImageSize(reader.getWidth(0), reader.getHeight(0));
|
|
100 imgf.setSize(d);
|
|
101 //String t = reader.getFormatName();
|
|
102 String t = FileOps.mimeForFile(f);
|
|
103 imgf.setMimetype(t);
|
|
104 //logger.debug(" format:"+t);
|
|
105 if (imgfs != null) {
|
|
106 imgfs.setAspect(d);
|
|
107 }
|
|
108 // dispose the reader to free resources
|
|
109 reader.dispose();
|
|
110 raf.close();
|
|
111 reader = null;
|
|
112 }
|
|
113 logger.debug("image size: " + imgf.getSize());
|
|
114 return true;
|
|
115 }
|
|
116
|
|
117 }
|