Mercurial > hg > digilib-old
comparison servlet/src/digilib/image/JAIImageLoaderDocuImage.java @ 85:4e6757e8ccd4
New enhanced ImageLoader stuff.
Now uses Subsampling and image regions on read.
Now implements enhance, rotate and mirror for ImageLoader/Java2D
author | robcast |
---|---|
date | Thu, 27 Feb 2003 15:07:29 +0100 |
parents | 63c8186455c1 |
children | 6d35c945a5d6 |
comparison
equal
deleted
inserted
replaced
84:ed1b698b4f0a | 85:4e6757e8ccd4 |
---|---|
1 /* JAIImageLoaderDocuImage -- Image class implementation using JAI's ImageLoader Plugin | 1 /* JAIImageLoaderDocuImage -- Image class implementation using JAI's ImageLoader Plugin |
2 | 2 |
3 Digital Image Library servlet components | 3 Digital Image Library servlet components |
4 | 4 |
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de) | 5 Copyright (C) 2002, 2003 Robert Casties (robcast@mail.berlios.de) |
6 | 6 |
7 This program is free software; you can redistribute it and/or modify it | 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 | 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 | 9 Free Software Foundation; either version 2 of the License, or (at your |
10 option) any later version. | 10 option) any later version. |
18 | 18 |
19 */ | 19 */ |
20 | 20 |
21 package digilib.image; | 21 package digilib.image; |
22 | 22 |
23 import java.awt.Rectangle; | |
23 import java.awt.image.renderable.ParameterBlock; | 24 import java.awt.image.renderable.ParameterBlock; |
24 import java.io.File; | 25 import java.io.File; |
25 import java.io.IOException; | 26 import java.io.IOException; |
26 import java.io.OutputStream; | 27 import java.io.OutputStream; |
28 import java.util.Iterator; | |
27 | 29 |
30 import javax.imageio.ImageIO; | |
31 import javax.imageio.ImageReadParam; | |
32 import javax.imageio.ImageReader; | |
33 import javax.imageio.stream.ImageInputStream; | |
28 import javax.media.jai.JAI; | 34 import javax.media.jai.JAI; |
35 | |
36 import com.sun.media.jai.operator.ImageReadDescriptor; | |
29 | 37 |
30 import digilib.io.FileOpException; | 38 import digilib.io.FileOpException; |
31 | 39 |
32 /** DocuImage implementation using the Java Advanced Imaging API and the ImageLoader | 40 /** DocuImage implementation using the Java Advanced Imaging API and the ImageLoader |
33 * API of Java 1.4. | 41 * API of Java 1.4. |
34 */ | 42 */ |
35 public class JAIImageLoaderDocuImage extends JAIDocuImage { | 43 public class JAIImageLoaderDocuImage extends JAIDocuImage { |
36 | 44 |
45 // ImageIO image reader | |
46 ImageReader reader; | |
47 | |
48 /* preload is supported. */ | |
49 public boolean isPreloadSupported() { | |
50 return true; | |
51 } | |
52 | |
53 /* loadSubimage is supported. */ | |
54 public boolean isSubimageSupported() { | |
55 return true; | |
56 } | |
57 | |
58 public int getHeight() { | |
59 int h = 0; | |
60 try { | |
61 if (img == null) { | |
62 h = reader.getHeight(0); | |
63 } else { | |
64 h = img.getHeight(); | |
65 } | |
66 } catch (IOException e) { | |
67 e.printStackTrace(); | |
68 } | |
69 return h; | |
70 } | |
71 | |
72 public int getWidth() { | |
73 int w = 0; | |
74 try { | |
75 if (img == null) { | |
76 w = reader.getWidth(0); | |
77 } else { | |
78 w = img.getWidth(); | |
79 } | |
80 } catch (IOException e) { | |
81 e.printStackTrace(); | |
82 } | |
83 return w; | |
84 } | |
37 | 85 |
38 /* Load an image file into the Object. */ | 86 /* Load an image file into the Object. */ |
39 public void loadImage(File f) throws FileOpException { | 87 public void loadImage(File f) throws FileOpException { |
40 System.gc(); | 88 System.gc(); |
41 img = JAI.create("ImageRead", f.getAbsolutePath()); | 89 img = JAI.create("ImageRead", f.getAbsolutePath()); |
42 if (img == null) { | 90 if (img == null) { |
91 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
92 throw new FileOpException("Unable to load File!"); | |
93 } | |
94 } | |
95 | |
96 /* Load an image file into the Object. */ | |
97 public void loadSubimage(File f, Rectangle region, int prescale) | |
98 throws FileOpException { | |
99 System.gc(); | |
100 try { | |
101 if (reader == null) { | |
102 preloadImage(f); | |
103 } | |
104 ImageInputStream istream = (ImageInputStream) reader.getInput(); | |
105 ImageReadParam readParam = reader.getDefaultReadParam(); | |
106 readParam.setSourceRegion(region); | |
107 readParam.setSourceSubsampling(prescale, prescale, 0, 0); | |
108 /* Parameter for ImageRead operation: | |
109 Input, ImageChoice, ReadMetadata, ReadThumbnails, VerifyInput, | |
110 Listeners, Locale, ReadParam, Reader, RenderingHints | |
111 */ | |
112 img = | |
113 ImageReadDescriptor.create( | |
114 istream, | |
115 new Integer(0), | |
116 Boolean.TRUE, | |
117 Boolean.FALSE, | |
118 Boolean.FALSE, | |
119 null, | |
120 null, | |
121 readParam, | |
122 reader, | |
123 null); | |
124 } catch (IOException e) { | |
125 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
126 throw new FileOpException("Unable to load File!"); | |
127 } | |
128 if (img == null) { | |
129 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
130 throw new FileOpException("Unable to load File!"); | |
131 } | |
132 } | |
133 | |
134 /* Get an ImageReader for the image file. */ | |
135 public void preloadImage(File f) throws FileOpException { | |
136 System.gc(); | |
137 try { | |
138 ImageInputStream istream = ImageIO.createImageInputStream(f); | |
139 Iterator readers = ImageIO.getImageReaders(istream); | |
140 reader = (ImageReader) readers.next(); | |
141 reader.setInput(istream); | |
142 } catch (IOException e) { | |
143 util.dprintln(3, "ERROR(loadImage): unable to load file"); | |
144 throw new FileOpException("Unable to load File!" + e); | |
145 } | |
146 if (reader == null) { | |
43 util.dprintln(3, "ERROR(loadImage): unable to load file"); | 147 util.dprintln(3, "ERROR(loadImage): unable to load file"); |
44 throw new FileOpException("Unable to load File!"); | 148 throw new FileOpException("Unable to load File!"); |
45 } | 149 } |
46 } | 150 } |
47 | 151 |