annotate servlet/src/digilib/image/ImageLoaderDocuImage.java @ 863:1641fab276ee stream

fixing bugs from cleanup...
author robcast
date Wed, 09 Mar 2011 11:03:29 +0100
parents 74062b7d9b22
children 83e747b2a98f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
1 /* ImageLoaderDocuImage -- Image class implementation using JDK 1.4 ImageLoader
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
2
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
3 Digital Image Library servlet components
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
4
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
5 Copyright (C) 2002 - 2011 Robert Casties (robcast@mail.berlios.de)
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
6
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
7 This program is free software; you can redistribute it and/or modify it
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
8 under the terms of the GNU General Public License as published by the
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
9 Free Software Foundation; either version 2 of the License, or (at your
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
10 option) any later version.
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
11
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
12 Please read license.txt for the full details. A copy of the GPL
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
13 may be found at http://www.gnu.org/copyleft/lgpl.html
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
14
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
15 You should have received a copy of the GNU General Public License
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
16 along with this program; if not, write to the Free Software
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
18 */
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
19
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
20 package digilib.image;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
21
496
1760b19df530 Added getImage method to ImageLoaderDocuImage;
cmielack
parents: 469
diff changeset
22 import java.awt.Image;
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
23 import java.awt.Rectangle;
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
24 import java.awt.RenderingHints;
829
a630d0303cce new servlet operation colop=grayscale.
robcast
parents: 827
diff changeset
25 import java.awt.color.ColorSpace;
73
3b8797fc3e90 New servlet version 1.5b.
robcast
parents: 1
diff changeset
26 import java.awt.geom.AffineTransform;
101
78f52a1876fe digilib V1.9b1
robcast
parents: 89
diff changeset
27 import java.awt.geom.Rectangle2D;
73
3b8797fc3e90 New servlet version 1.5b.
robcast
parents: 1
diff changeset
28 import java.awt.image.AffineTransformOp;
860
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
29 import java.awt.image.BandCombineOp;
73
3b8797fc3e90 New servlet version 1.5b.
robcast
parents: 1
diff changeset
30 import java.awt.image.BufferedImage;
834
c07c21ac78f9 color invert operation. doesn't work with all image types yet...
robcast
parents: 831
diff changeset
31 import java.awt.image.ByteLookupTable;
829
a630d0303cce new servlet operation colop=grayscale.
robcast
parents: 827
diff changeset
32 import java.awt.image.ColorConvertOp;
838
dd13310dbe29 another try at invert. (works on osx...)
robcast
parents: 834
diff changeset
33 import java.awt.image.ColorModel;
144
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
34 import java.awt.image.ConvolveOp;
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
35 import java.awt.image.Kernel;
834
c07c21ac78f9 color invert operation. doesn't work with all image types yet...
robcast
parents: 831
diff changeset
36 import java.awt.image.LookupOp;
c07c21ac78f9 color invert operation. doesn't work with all image types yet...
robcast
parents: 831
diff changeset
37 import java.awt.image.LookupTable;
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
38 import java.awt.image.RescaleOp;
863
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
39 import java.awt.image.WritableRaster;
73
3b8797fc3e90 New servlet version 1.5b.
robcast
parents: 1
diff changeset
40 import java.io.IOException;
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
41 import java.io.OutputStream;
89
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
42 import java.io.RandomAccessFile;
464
f1f710ff14ad Servlet version 1.10b
robcast
parents: 462
diff changeset
43 import java.util.Arrays;
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
44 import java.util.Iterator;
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
45
352
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
46 import javax.imageio.IIOImage;
73
3b8797fc3e90 New servlet version 1.5b.
robcast
parents: 1
diff changeset
47 import javax.imageio.ImageIO;
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
48 import javax.imageio.ImageReadParam;
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
49 import javax.imageio.ImageReader;
352
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
50 import javax.imageio.ImageWriteParam;
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
51 import javax.imageio.ImageWriter;
220
c4ee5f0c3a77 servlet version 1.18b12
robcast
parents: 207
diff changeset
52 import javax.imageio.stream.FileImageInputStream;
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
53 import javax.imageio.stream.ImageInputStream;
352
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
54 import javax.imageio.stream.ImageOutputStream;
570
fd2ef7e46119 more cleanup, set version to 1.8.2
robcast
parents: 565
diff changeset
55 import javax.servlet.ServletException;
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
56
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
57 import digilib.io.FileOpException;
464
f1f710ff14ad Servlet version 1.10b
robcast
parents: 462
diff changeset
58 import digilib.io.FileOps;
579
efd7a223f819 try: ImageInput as interface, ImageFile inherits from Dirent and implements ImageInput
robcast
parents: 574
diff changeset
59 import digilib.io.ImageInput;
596
2b58d2783ef0 small cleanup
robcast
parents: 590
diff changeset
60 import digilib.util.ImageSize;
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
61
73
3b8797fc3e90 New servlet version 1.5b.
robcast
parents: 1
diff changeset
62 /** Implementation of DocuImage using the ImageLoader API of Java 1.4 and Java2D. */
564
4c51d71aef13 more reshuffling of classes
robcast
parents: 563
diff changeset
63 public class ImageLoaderDocuImage extends ImageInfoDocuImage {
590
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
64
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
65 /** image object */
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
66 protected BufferedImage img;
570
fd2ef7e46119 more cleanup, set version to 1.8.2
robcast
parents: 565
diff changeset
67
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
68 /** interpolation type */
838
dd13310dbe29 another try at invert. (works on osx...)
robcast
parents: 834
diff changeset
69 protected RenderingHints renderHint = null;
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
70
834
c07c21ac78f9 color invert operation. doesn't work with all image types yet...
robcast
parents: 831
diff changeset
71 /** convolution kernels for blur() */
590
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
72 protected static Kernel[] convolutionKernels = {
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
73 null,
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
74 new Kernel(1, 1, new float[] {1f}),
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
75 new Kernel(2, 2, new float[] {0.25f, 0.25f, 0.25f, 0.25f}),
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
76 new Kernel(3, 3, new float[] {1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f})
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
77 };
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
78
854
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
79 /* lookup tables for inverting images (byte) */
841
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
80 protected static LookupTable invertSingleByteTable;
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
81 protected static LookupTable invertRgbaByteTable;
854
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
82 protected static boolean needsInvertRgba = false;
860
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
83 /* RescaleOp for contrast/brightness operation */
854
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
84 protected static boolean needsRescaleRgba = false;
860
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
85 /* lookup tables for false-color */
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
86 protected static LookupTable mapBgrByteTable;
854
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
87
834
c07c21ac78f9 color invert operation. doesn't work with all image types yet...
robcast
parents: 831
diff changeset
88 static {
c07c21ac78f9 color invert operation. doesn't work with all image types yet...
robcast
parents: 831
diff changeset
89 byte[] invertByte = new byte[256];
c07c21ac78f9 color invert operation. doesn't work with all image types yet...
robcast
parents: 831
diff changeset
90 byte[] orderedByte = new byte[256];
841
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
91 byte[] nullByte = new byte[256];
860
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
92 byte[] mapR = new byte[256];
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
93 byte[] mapG = new byte[256];
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
94 byte[] mapB = new byte[256];
834
c07c21ac78f9 color invert operation. doesn't work with all image types yet...
robcast
parents: 831
diff changeset
95 for (int i = 0; i < 256; ++i) {
860
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
96 // counting down
834
c07c21ac78f9 color invert operation. doesn't work with all image types yet...
robcast
parents: 831
diff changeset
97 invertByte[i] = (byte) (256 - i);
860
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
98 // counting up
834
c07c21ac78f9 color invert operation. doesn't work with all image types yet...
robcast
parents: 831
diff changeset
99 orderedByte[i] = (byte) i;
860
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
100 // constant 0
841
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
101 nullByte[i] = 0;
860
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
102 // three overlapping slopes
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
103 if (i < 64) {
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
104 mapR[i] = 0;
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
105 mapG[i] = (byte) (4 * i);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
106 mapB[i] = (byte) 255;
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
107 } else if (i >= 64 && i < 192) {
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
108 mapR[i] = (byte) (2 * (i - 64));
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
109 mapG[i] = (byte) 255;
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
110 mapB[i] = (byte) (255 - 2 * (i - 64));
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
111 } else {
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
112 mapR[i] = (byte) 255;
861
74062b7d9b22 fixed colop=map_gray_bgr.
robcast
parents: 860
diff changeset
113 mapG[i] = (byte) (255 - (4 * (i - 192)));
860
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
114 mapB[i] = 0;
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
115 }
834
c07c21ac78f9 color invert operation. doesn't work with all image types yet...
robcast
parents: 831
diff changeset
116 }
841
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
117 // should(!) work for all color models
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
118 invertSingleByteTable = new ByteLookupTable(0, invertByte);
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
119 // but doesn't work with alpha channel on all platforms
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
120 String ver = System.getProperty("java.version");
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
121 String os = System.getProperty("os.name");
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
122 logger.debug("os="+os+" ver="+ver);
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
123 if (os.startsWith("Linux") && ver.startsWith("1.6")) {
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
124 // GRAB(WTF?) works in Linux JDK1.6 with transparency
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
125 invertRgbaByteTable = new ByteLookupTable(0, new byte[][] {
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
126 invertByte, invertByte, orderedByte, invertByte});
854
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
127 needsInvertRgba = true;
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
128 needsRescaleRgba = true;
841
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
129 } else {
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
130 invertRgbaByteTable = invertSingleByteTable;
3519cfa11a0b organised special juju for invert on linux jdk 1.6.
robcast
parents: 839
diff changeset
131 }
860
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
132 // this hopefully works for all
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
133 mapBgrByteTable = new ByteLookupTable(0, new byte[][] {
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
134 mapR, mapG, mapB});
834
c07c21ac78f9 color invert operation. doesn't work with all image types yet...
robcast
parents: 831
diff changeset
135 }
c07c21ac78f9 color invert operation. doesn't work with all image types yet...
robcast
parents: 831
diff changeset
136
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
137 /** the size of the current image */
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
138 protected ImageSize imageSize;
590
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
139
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
140
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
141 /* loadSubimage is supported. */
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
142 public boolean isSubimageSupported() {
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
143 return true;
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
144 }
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
145
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
146 public void setQuality(int qual) {
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
147 quality = qual;
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
148 renderHint = new RenderingHints(null);
352
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
149 // hint.put(RenderingHints.KEY_ANTIALIASING,
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
150 // RenderingHints.VALUE_ANTIALIAS_OFF);
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
151 // setup interpolation quality
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
152 if (qual > 0) {
181
afe7ff98bb71 Servlet version 1.18b1
robcast
parents: 170
diff changeset
153 logger.debug("quality q1");
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
154 renderHint.put(RenderingHints.KEY_INTERPOLATION,
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
155 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
156 } else {
181
afe7ff98bb71 Servlet version 1.18b1
robcast
parents: 170
diff changeset
157 logger.debug("quality q0");
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
158 renderHint.put(RenderingHints.KEY_INTERPOLATION,
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
159 RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
160 }
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
161 }
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
162
570
fd2ef7e46119 more cleanup, set version to 1.8.2
robcast
parents: 565
diff changeset
163 /* returns the size of the current image */
fd2ef7e46119 more cleanup, set version to 1.8.2
robcast
parents: 565
diff changeset
164 public ImageSize getSize() {
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
165 if (imageSize == null) {
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
166 int h = 0;
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
167 int w = 0;
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
168 try {
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
169 if (img == null) {
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
170 ImageReader reader = getReader(input);
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
171 // get size from ImageReader
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
172 h = reader.getHeight(0);
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
173 w = reader.getWidth(0);
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
174 } else {
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
175 // get size from image
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
176 h = img.getHeight();
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
177 w = img.getWidth();
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
178 }
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
179 imageSize = new ImageSize(w, h);
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
180 } catch (IOException e) {
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
181 logger.debug("error in getSize:", e);
570
fd2ef7e46119 more cleanup, set version to 1.8.2
robcast
parents: 565
diff changeset
182 }
fd2ef7e46119 more cleanup, set version to 1.8.2
robcast
parents: 565
diff changeset
183 }
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
184 return imageSize;
570
fd2ef7e46119 more cleanup, set version to 1.8.2
robcast
parents: 565
diff changeset
185 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
186
464
f1f710ff14ad Servlet version 1.10b
robcast
parents: 462
diff changeset
187 /* returns a list of supported image formats */
531
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
188 public Iterator<String> getSupportedFormats() {
464
f1f710ff14ad Servlet version 1.10b
robcast
parents: 462
diff changeset
189 String[] formats = ImageIO.getReaderFormatNames();
f1f710ff14ad Servlet version 1.10b
robcast
parents: 462
diff changeset
190 return Arrays.asList(formats).iterator();
f1f710ff14ad Servlet version 1.10b
robcast
parents: 462
diff changeset
191 }
f1f710ff14ad Servlet version 1.10b
robcast
parents: 462
diff changeset
192
588
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
193 /* Check image size and type and store in ImageInput */
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
194 public ImageInput identify(ImageInput input) throws IOException {
462
03ff7238c9d4 second try for flashpix support (doesn't work currently...)
robcast
parents: 402
diff changeset
195 // try parent method first
588
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
196 ImageInput ii = super.identify(input);
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
197 if (ii != null) {
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
198 return ii;
462
03ff7238c9d4 second try for flashpix support (doesn't work currently...)
robcast
parents: 402
diff changeset
199 }
588
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
200 logger.debug("identifying (ImageIO) " + input);
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
201 ImageReader reader = null;
590
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
202 try {
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
203 /*
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
204 * try ImageReader
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
205 */
590
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
206 reader = getReader(input);
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
207 // set size
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
208 ImageSize d = new ImageSize(reader.getWidth(0), reader.getHeight(0));
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
209 input.setSize(d);
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
210 // set mime type
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
211 if (input.getMimetype() == null) {
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
212 if (input.hasFile()) {
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
213 String t = FileOps.mimeForFile(input.getFile());
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
214 input.setMimetype(t);
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
215 } else {
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
216 // FIXME: is format name a mime type???
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
217 String t = reader.getFormatName();
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
218 input.setMimetype(t);
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
219 }
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
220 }
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
221 return input;
590
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
222 } catch (FileOpException e) {
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
223 // maybe just our class doesn't know what to do
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
224 logger.error("ImageLoaderDocuimage unable to identify:", e);
590
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
225 return null;
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
226 } finally {
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
227 if (reader != null) {
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
228 reader.dispose();
588
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
229 }
462
03ff7238c9d4 second try for flashpix support (doesn't work currently...)
robcast
parents: 402
diff changeset
230 }
03ff7238c9d4 second try for flashpix support (doesn't work currently...)
robcast
parents: 402
diff changeset
231 }
03ff7238c9d4 second try for flashpix support (doesn't work currently...)
robcast
parents: 402
diff changeset
232
03ff7238c9d4 second try for flashpix support (doesn't work currently...)
robcast
parents: 402
diff changeset
233 /* load image file */
588
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
234 public void loadImage(ImageInput ii) throws FileOpException {
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
235 logger.debug("loadImage: " + ii);
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
236 this.input = ii;
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
237 try {
588
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
238 if (ii.hasImageInputStream()) {
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
239 img = ImageIO.read(ii.getImageInputStream());
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
240 } else if (ii.hasFile()) {
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
241 img = ImageIO.read(ii.getFile());
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
242 }
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
243 } catch (IOException e) {
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
244 throw new FileOpException("Error reading image.");
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
245 }
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
246 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
247
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
248 /**
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
249 * Get an ImageReader for the image file.
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
250 *
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
251 * @return
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
252 */
587
720d061a1b30 more work on stream input
robcast
parents: 574
diff changeset
253 public ImageReader getReader(ImageInput input) throws IOException {
720d061a1b30 more work on stream input
robcast
parents: 574
diff changeset
254 logger.debug("get ImageReader for " + input);
720d061a1b30 more work on stream input
robcast
parents: 574
diff changeset
255 ImageInputStream istream = null;
720d061a1b30 more work on stream input
robcast
parents: 574
diff changeset
256 if (input.hasImageInputStream()) {
720d061a1b30 more work on stream input
robcast
parents: 574
diff changeset
257 // stream input
720d061a1b30 more work on stream input
robcast
parents: 574
diff changeset
258 istream = input.getImageInputStream();
720d061a1b30 more work on stream input
robcast
parents: 574
diff changeset
259 } else if (input.hasFile()) {
720d061a1b30 more work on stream input
robcast
parents: 574
diff changeset
260 // file only input
720d061a1b30 more work on stream input
robcast
parents: 574
diff changeset
261 RandomAccessFile rf = new RandomAccessFile(input.getFile(), "r");
720d061a1b30 more work on stream input
robcast
parents: 574
diff changeset
262 istream = new FileImageInputStream(rf);
720d061a1b30 more work on stream input
robcast
parents: 574
diff changeset
263 } else {
720d061a1b30 more work on stream input
robcast
parents: 574
diff changeset
264 throw new FileOpException("Unable to get data from ImageInput");
148
837a633a0407 fixed bugs when using lots of prescaled images
robcast
parents: 145
diff changeset
265 }
565
8beefd1142b2 more cleanup
robcast
parents: 564
diff changeset
266 Iterator<ImageReader> readers;
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
267 String mt = null;
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
268 if (input.hasMimetype()) {
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
269 // check hasMimetype first or we might get into a loop
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
270 mt = input.getMimetype();
821
6c752969f9e8 more tries and catches...
robcast
parents: 819
diff changeset
271 } else {
6c752969f9e8 more tries and catches...
robcast
parents: 819
diff changeset
272 // try file extension
6c752969f9e8 more tries and catches...
robcast
parents: 819
diff changeset
273 mt = FileOps.mimeForFile(input.getFile());
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
274 }
565
8beefd1142b2 more cleanup
robcast
parents: 564
diff changeset
275 if (mt == null) {
8beefd1142b2 more cleanup
robcast
parents: 564
diff changeset
276 logger.debug("No mime-type. Trying automagic.");
8beefd1142b2 more cleanup
robcast
parents: 564
diff changeset
277 readers = ImageIO.getImageReaders(istream);
8beefd1142b2 more cleanup
robcast
parents: 564
diff changeset
278 } else {
8beefd1142b2 more cleanup
robcast
parents: 564
diff changeset
279 logger.debug("File type:" + mt);
8beefd1142b2 more cleanup
robcast
parents: 564
diff changeset
280 readers = ImageIO.getImageReadersByMIMEType(mt);
8beefd1142b2 more cleanup
robcast
parents: 564
diff changeset
281 }
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
282 if (!readers.hasNext()) {
565
8beefd1142b2 more cleanup
robcast
parents: 564
diff changeset
283 throw new FileOpException("Can't find Reader to load File!");
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
284 }
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
285 ImageReader reader = readers.next();
149
04ad64b2137a Servlet version 1.14b1
robcast
parents: 148
diff changeset
286 /* are there more readers? */
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
287 logger.debug("ImageIO: this reader: " + reader.getClass());
564
4c51d71aef13 more reshuffling of classes
robcast
parents: 563
diff changeset
288 /* while (readers.hasNext()) {
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
289 logger.debug("ImageIO: next reader: " + readers.next().getClass());
564
4c51d71aef13 more reshuffling of classes
robcast
parents: 563
diff changeset
290 } */
89
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
291 reader.setInput(istream);
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
292 return reader;
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
293 }
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
294
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
295 /* Load an image file into the Object. */
588
aee436f0549d more work on stream input
robcast
parents: 587
diff changeset
296 public void loadSubimage(ImageInput ii, Rectangle region, int prescale)
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
297 throws FileOpException {
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
298 logger.debug("loadSubimage");
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
299 this.input = ii;
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
300 ImageReader reader = null;
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
301 try {
590
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
302 reader = getReader(ii);
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
303 // set up reader parameters
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
304 ImageReadParam readParam = reader.getDefaultReadParam();
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
305 readParam.setSourceRegion(region);
220
c4ee5f0c3a77 servlet version 1.18b12
robcast
parents: 207
diff changeset
306 if (prescale > 1) {
c4ee5f0c3a77 servlet version 1.18b12
robcast
parents: 207
diff changeset
307 readParam.setSourceSubsampling(prescale, prescale, 0, 0);
c4ee5f0c3a77 servlet version 1.18b12
robcast
parents: 207
diff changeset
308 }
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
309 // read image
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
310 logger.debug("loading..");
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
311 img = reader.read(0, readParam);
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
312 logger.debug("loaded");
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
313 } catch (IOException e) {
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
314 throw new FileOpException("Unable to load File!");
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
315 } finally {
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
316 if (reader != null) {
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
317 reader.dispose();
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
318 }
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
319 }
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
320 }
170
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
321
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
322 /* write image of type mt to Stream */
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
323 public void writeImage(String mt, OutputStream ostream)
570
fd2ef7e46119 more cleanup, set version to 1.8.2
robcast
parents: 565
diff changeset
324 throws ImageOpException, ServletException {
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
325 logger.debug("writeImage");
353
25095f85ba7c Servlet version 1.5.9c
robcast
parents: 352
diff changeset
326 // setup output
25095f85ba7c Servlet version 1.5.9c
robcast
parents: 352
diff changeset
327 ImageWriter writer = null;
25095f85ba7c Servlet version 1.5.9c
robcast
parents: 352
diff changeset
328 ImageOutputStream imgout = null;
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
329 try {
353
25095f85ba7c Servlet version 1.5.9c
robcast
parents: 352
diff changeset
330 imgout = ImageIO.createImageOutputStream(ostream);
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
331 if (mt == "image/jpeg") {
352
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
332 /*
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
333 * JPEG doesn't do transparency so we have to convert any RGBA
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
334 * image to RGB :-( *Java2D BUG*
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
335 */
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
336 if (img.getColorModel().hasAlpha()) {
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
337 logger.debug("BARF: JPEG with transparency!!");
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
338 int w = img.getWidth();
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
339 int h = img.getHeight();
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
340 // BufferedImage.TYPE_INT_RGB seems to be fastest (JDK1.4.1,
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
341 // OSX)
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
342 int destType = BufferedImage.TYPE_INT_RGB;
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
343 BufferedImage img2 = new BufferedImage(w, h, destType);
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
344 img2.createGraphics().drawImage(img, null, 0, 0);
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
345 img = img2;
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
346 }
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
347 writer = (ImageWriter) ImageIO.getImageWritersByFormatName(
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
348 "jpeg").next();
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
349 if (writer == null) {
570
fd2ef7e46119 more cleanup, set version to 1.8.2
robcast
parents: 565
diff changeset
350 throw new ImageOpException("Unable to get JPEG writer");
352
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
351 }
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
352 ImageWriteParam param = writer.getDefaultWriteParam();
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
353 if (quality > 1) {
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
354 // change JPEG compression quality
353
25095f85ba7c Servlet version 1.5.9c
robcast
parents: 352
diff changeset
355 param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
469
11e11fe4d680 Improved performance of JAIDocuImage for large images
robcast
parents: 464
diff changeset
356 //logger.debug("JPEG qual before: "
11e11fe4d680 Improved performance of JAIDocuImage for large images
robcast
parents: 464
diff changeset
357 // + Float.toString(param.getCompressionQuality()));
352
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
358 param.setCompressionQuality(0.9f);
469
11e11fe4d680 Improved performance of JAIDocuImage for large images
robcast
parents: 464
diff changeset
359 //logger.debug("JPEG qual now: "
11e11fe4d680 Improved performance of JAIDocuImage for large images
robcast
parents: 464
diff changeset
360 // + Float.toString(param.getCompressionQuality()));
352
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
361 }
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
362 writer.setOutput(imgout);
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
363 // render output
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
364 logger.debug("writing");
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
365 writer.write(null, new IIOImage(img, null, null), param);
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
366 } else if (mt == "image/png") {
352
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
367 // render output
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
368 writer = (ImageWriter) ImageIO.getImageWritersByFormatName(
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
369 "png").next();
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
370 if (writer == null) {
570
fd2ef7e46119 more cleanup, set version to 1.8.2
robcast
parents: 565
diff changeset
371 throw new ImageOpException("Unable to get PNG writer");
352
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
372 }
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
373 writer.setOutput(imgout);
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
374 logger.debug("writing");
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
375 writer.write(img);
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
376 } else {
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
377 // unknown mime type
570
fd2ef7e46119 more cleanup, set version to 1.8.2
robcast
parents: 565
diff changeset
378 throw new ImageOpException("Unknown mime type: " + mt);
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
379 }
140
c878ea574c29 Servlet Version 1.12b2
robcast
parents: 122
diff changeset
380
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
381 } catch (IOException e) {
570
fd2ef7e46119 more cleanup, set version to 1.8.2
robcast
parents: 565
diff changeset
382 logger.error("Error writing image:", e);
fd2ef7e46119 more cleanup, set version to 1.8.2
robcast
parents: 565
diff changeset
383 throw new ServletException("Error writing image:", e);
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
384 }
801
72662bb585ba remove all ServletOutputStream.flush(). (stupid me ;-)
robcast
parents: 596
diff changeset
385 // TODO: should we: finally { writer.dispose(); }
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
386 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
387
149
04ad64b2137a Servlet version 1.14b1
robcast
parents: 148
diff changeset
388 public void scale(double scale, double scaleY) throws ImageOpException {
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
389 logger.debug("scale: " + scale);
170
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
390 /* for downscaling in high quality the image is blurred first */
144
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
391 if ((scale <= 0.5) && (quality > 1)) {
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
392 int bl = (int) Math.floor(1 / scale);
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
393 blur(bl);
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
394 }
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
395 /* then scaled */
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
396 AffineTransformOp scaleOp = new AffineTransformOp(AffineTransform
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
397 .getScaleInstance(scale, scale), renderHint);
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
398 logger.debug("scaling...");
857
456408791331 rendering code cleanup.
robcast
parents: 854
diff changeset
399 img = scaleOp.filter(img, null);
456408791331 rendering code cleanup.
robcast
parents: 854
diff changeset
400 logger.debug("SCALE: " + scale + " ->" + img.getWidth() + "x"
863
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
401 + img.getHeight() + " type=" + img);
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
402 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
403
144
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
404 public void blur(int radius) throws ImageOpException {
181
afe7ff98bb71 Servlet version 1.18b1
robcast
parents: 170
diff changeset
405 logger.debug("blur: " + radius);
144
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
406 // minimum radius is 2
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
407 int klen = Math.max(radius, 2);
590
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
408 Kernel blur = null;
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
409 if (klen < convolutionKernels.length) {
863
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
410 // use precalculated Kernel
590
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
411 blur = convolutionKernels[klen];
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
412 } else {
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
413 // calculate our own kernel
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
414 int ksize = klen * klen;
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
415 // kernel is constant 1/k
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
416 float f = 1f / ksize;
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
417 float[] kern = new float[ksize];
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
418 for (int i = 0; i < ksize; ++i) {
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
419 kern[i] = f;
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
420 }
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
421 blur = new Kernel(klen, klen, kern);
144
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
422 }
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
423 // blur with convolve operation
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
424 ConvolveOp blurOp = new ConvolveOp(blur, ConvolveOp.EDGE_NO_OP,
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
425 renderHint);
863
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
426 /* blur needs explicit destination image type for color *Java2D BUG* */
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
427 BufferedImage dest = null;
145
d76006c12e89 corrected bugs with Java2D on Linux...
robcast
parents: 144
diff changeset
428 if (img.getType() == BufferedImage.TYPE_3BYTE_BGR) {
863
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
429 logger.debug("blur: fixing destination image type for "+img);
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
430 dest = new BufferedImage(img.getWidth(), img.getHeight(), img
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
431 .getType());
863
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
432 }
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
433 img = blurOp.filter(img, dest);
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
434 logger.debug("blurred: "+img);
144
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
435 }
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
436
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
437 public void crop(int x_off, int y_off, int width, int height)
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
438 throws ImageOpException {
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
439 // setup Crop
857
456408791331 rendering code cleanup.
robcast
parents: 854
diff changeset
440 img = img.getSubimage(x_off, y_off, width, height);
456408791331 rendering code cleanup.
robcast
parents: 854
diff changeset
441 logger.debug("CROP:" + img.getWidth() + "x"
456408791331 rendering code cleanup.
robcast
parents: 854
diff changeset
442 + img.getHeight());
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
443 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
444
170
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
445 public void enhance(float mult, float add) throws ImageOpException {
843
7718c6e832b0 enhance (cont, brgt) operation now with just one float.
robcast
parents: 841
diff changeset
446 RescaleOp op = null;
854
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
447 logger.debug("enhance: cm=" + img.getColorModel());
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
448 if (needsRescaleRgba) {
857
456408791331 rendering code cleanup.
robcast
parents: 854
diff changeset
449 /*
456408791331 rendering code cleanup.
robcast
parents: 854
diff changeset
450 * Only one constant should work regardless of the number of bands
456408791331 rendering code cleanup.
robcast
parents: 854
diff changeset
451 * according to the JDK spec. Doesn't work on JDK 1.4 for OSX and
456408791331 rendering code cleanup.
robcast
parents: 854
diff changeset
452 * Linux (at least).
456408791331 rendering code cleanup.
robcast
parents: 854
diff changeset
453 *
854
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
454 * The number of constants must match the number of bands in the
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
455 * image.
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
456 */
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
457 int ncol = img.getColorModel().getNumComponents();
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
458 float[] dm = new float[ncol];
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
459 float[] da = new float[ncol];
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
460 for (int i = 0; i < ncol; i++) {
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
461 dm[i] = (float) mult;
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
462 da[i] = (float) add;
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
463 }
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
464 op = new RescaleOp(dm, da, null);
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
465 } else {
1e2e9599d84c (slightly) fixed enhance and colop in linux.
robcast
parents: 843
diff changeset
466 op = new RescaleOp(mult, add, renderHint);
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
467 }
843
7718c6e832b0 enhance (cont, brgt) operation now with just one float.
robcast
parents: 841
diff changeset
468 op.filter(img, img);
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
469 }
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
470
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
471 public void enhanceRGB(float[] rgbm, float[] rgba) throws ImageOpException {
170
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
472 /*
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
473 * The number of constants must match the number of bands in the image.
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
474 * We do only 3 (RGB) bands.
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
475 */
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
476 int ncol = img.getColorModel().getNumColorComponents();
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
477 if ((ncol != 3) || (rgbm.length != 3) || (rgba.length != 3)) {
863
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
478 logger.error("enhanceRGB: unknown number of color bands or coefficients ("
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
479 + ncol + ")");
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
480 return;
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
481 }
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
482 RescaleOp scaleOp = new RescaleOp(rgbOrdered(rgbm), rgbOrdered(rgba),
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
483 null);
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
484 scaleOp.filter(img, img);
170
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
485 }
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
486
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
487 /**
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
488 * Ensures that the array f is in the right order to map the images RGB
590
69bc69381ac4 more work on stream input and more cleanup
robcast
parents: 589
diff changeset
489 * components. (not sure what happens otherwise)
170
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
490 */
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
491 public float[] rgbOrdered(float[] fa) {
170
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
492 /*
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
493 * TODO: this is UGLY, UGLY!!
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
494 */
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
495 float[] fb;
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
496 int t = img.getType();
170
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
497 if (img.getColorModel().hasAlpha()) {
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
498 fb = new float[4];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
499 if ((t == BufferedImage.TYPE_INT_ARGB)
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
500 || (t == BufferedImage.TYPE_INT_ARGB_PRE)) {
170
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
501 // RGB Type
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
502 fb[0] = fa[0];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
503 fb[1] = fa[1];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
504 fb[2] = fa[2];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
505 fb[3] = 1f;
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
506 } else {
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
507 // this isn't tested :-(
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
508 fb[0] = 1f;
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
509 fb[1] = fa[0];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
510 fb[2] = fa[1];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
511 fb[3] = fa[2];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
512 }
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
513 } else {
170
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
514 fb = new float[3];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
515 if (t == BufferedImage.TYPE_3BYTE_BGR) {
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
516 // BGR Type (actually it looks like RBG...)
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
517 fb[0] = fa[0];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
518 fb[1] = fa[2];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
519 fb[2] = fa[1];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
520 } else {
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
521 fb[0] = fa[0];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
522 fb[1] = fa[1];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
523 fb[2] = fa[2];
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
524 }
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
525 }
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
526 return fb;
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
527 }
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
528
860
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
529 /*
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
530 * (non-Javadoc)
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
531 *
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
532 * @see
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
533 * digilib.image.DocuImageImpl#colorOp(digilib.image.DocuImage.ColorOps)
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
534 */
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
535 public void colorOp(ColorOp colop) throws ImageOpException {
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
536 if (colop == ColorOp.GRAYSCALE) {
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
537 /*
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
538 * convert image to grayscale
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
539 */
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
540 logger.debug("Color op: grayscaling");
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
541 ColorModel cm = img.getColorModel();
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
542 if (cm.getNumColorComponents() == 1) {
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
543 // grayscale already
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
544 return;
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
545 }
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
546 ColorConvertOp op = new ColorConvertOp(
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
547 ColorSpace.getInstance(ColorSpace.CS_GRAY), renderHint);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
548 // let filter create new image
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
549 img = op.filter(img, null);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
550 } else if (colop == ColorOp.NTSC_GRAY) {
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
551 /*
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
552 * convert image to grayscale NTSC-style: luminance = 0.2989*red +
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
553 * 0.5870*green + 0.1140*blue
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
554 */
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
555 logger.debug("Color op: NTSC gray");
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
556 ColorModel cm = img.getColorModel();
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
557 if (cm.getNumColorComponents() == 1) {
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
558 // grayscale already
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
559 return;
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
560 }
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
561 float[][] combineFn = new float[1][4];
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
562 combineFn[0] = rgbOrdered(new float[] { 0.299f, 0.114f, 0.587f, 0f });
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
563 BandCombineOp op = new BandCombineOp(combineFn, renderHint);
863
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
564 // BandCombineOp only works on Rasters so we create a
860
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
565 // new image and use its Raster
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
566 BufferedImage dest = new BufferedImage(img.getWidth(),
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
567 img.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
568 op.filter(img.getRaster(), dest.getRaster());
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
569 img = dest;
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
570 } else if (colop == ColorOp.INVERT) {
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
571 /*
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
572 * invert colors i.e. invert every channel
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
573 */
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
574 logger.debug("Color op: inverting");
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
575 LookupTable invtbl = null;
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
576 ColorModel cm = img.getColorModel();
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
577 if (needsInvertRgba && cm.hasAlpha()) {
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
578 /*
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
579 * should work with one array for all channels, but JDK 1.6 in
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
580 * Linux (at least) is broken :-(
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
581 */
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
582 invtbl = invertRgbaByteTable;
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
583 } else {
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
584 invtbl = invertSingleByteTable;
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
585 }
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
586 LookupOp op = new LookupOp(invtbl, renderHint);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
587 logger.debug("colop: image=" + img + " colormodel=" + cm);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
588 op.filter(img, img);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
589 } else if (colop == ColorOp.MAP_GRAY_BGR) {
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
590 /*
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
591 * false color image from grayscale (0: blue, 128: green, 255: red)
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
592 */
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
593 logger.debug("Color op: map_gray_bgr");
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
594 // convert to grayscale
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
595 ColorConvertOp grayOp = new ColorConvertOp(
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
596 ColorSpace.getInstance(ColorSpace.CS_GRAY), renderHint);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
597 // create new 3-byte image
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
598 BufferedImage dest = new BufferedImage(img.getWidth(),
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
599 img.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
600 img = grayOp.filter(img, dest);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
601 logger.debug("map_gray: image=" + img);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
602 // convert to false color
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
603 LookupOp mapOp = new LookupOp(mapBgrByteTable, renderHint);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
604 mapOp.filter(img, img);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
605 logger.debug("mapped image=" + img);
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
606 }
fe58284a8dd9 implemented colop= ntsc_gray and map_gray_bgr.
robcast
parents: 857
diff changeset
607 }
829
a630d0303cce new servlet operation colop=grayscale.
robcast
parents: 827
diff changeset
608
140
c878ea574c29 Servlet Version 1.12b2
robcast
parents: 122
diff changeset
609 public void rotate(double angle) throws ImageOpException {
89
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
610 // setup rotation
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
611 double rangle = Math.toRadians(angle);
170
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
612 // create offset to make shure the rotated image has no negative
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
613 // coordinates
101
78f52a1876fe digilib V1.9b1
robcast
parents: 89
diff changeset
614 double w = img.getWidth();
78f52a1876fe digilib V1.9b1
robcast
parents: 89
diff changeset
615 double h = img.getHeight();
103
0e49645f98c8 Servlet V1.9b2
robcast
parents: 101
diff changeset
616 AffineTransform trafo = new AffineTransform();
170
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
617 // center of rotation
103
0e49645f98c8 Servlet V1.9b2
robcast
parents: 101
diff changeset
618 double x = (w / 2);
0e49645f98c8 Servlet V1.9b2
robcast
parents: 101
diff changeset
619 double y = (h / 2);
101
78f52a1876fe digilib V1.9b1
robcast
parents: 89
diff changeset
620 trafo.rotate(rangle, x, y);
103
0e49645f98c8 Servlet V1.9b2
robcast
parents: 101
diff changeset
621 // try rotation to see how far we're out of bounds
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
622 AffineTransformOp rotOp = new AffineTransformOp(trafo, renderHint);
103
0e49645f98c8 Servlet V1.9b2
robcast
parents: 101
diff changeset
623 Rectangle2D rotbounds = rotOp.getBounds2D(img);
0e49645f98c8 Servlet V1.9b2
robcast
parents: 101
diff changeset
624 double xoff = rotbounds.getX();
0e49645f98c8 Servlet V1.9b2
robcast
parents: 101
diff changeset
625 double yoff = rotbounds.getY();
0e49645f98c8 Servlet V1.9b2
robcast
parents: 101
diff changeset
626 // move image back in line
565
8beefd1142b2 more cleanup
robcast
parents: 564
diff changeset
627 trafo.preConcatenate(AffineTransform.getTranslateInstance(-xoff, -yoff));
101
78f52a1876fe digilib V1.9b1
robcast
parents: 89
diff changeset
628 // transform image
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
629 rotOp = new AffineTransformOp(trafo, renderHint);
863
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
630 img = rotOp.filter(img, null);
101
78f52a1876fe digilib V1.9b1
robcast
parents: 89
diff changeset
631 // calculate new bounding box
352
5ded9e06cd57 Servlet version 1.5.9b
robcast
parents: 293
diff changeset
632 // Rectangle2D bounds = rotOp.getBounds2D(img);
101
78f52a1876fe digilib V1.9b1
robcast
parents: 89
diff changeset
633 // crop new image (with self-made rounding)
170
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
634 /*
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
635 * img = rotImg.getSubimage( (int) (bounds.getX()+0.5), (int)
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
636 * (bounds.getY()+0.5), (int) (bounds.getWidth()+0.5), (int)
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
637 * (bounds.getHeight()+0.5));
d40922628e4a Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents: 159
diff changeset
638 */
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
639 }
85
4e6757e8ccd4 New enhanced ImageLoader stuff.
robcast
parents: 79
diff changeset
640
89
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
641 public void mirror(double angle) throws ImageOpException {
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
642 // setup mirror
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
643 double mx = 1;
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
644 double my = 1;
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
645 double tx = 0;
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
646 double ty = 0;
144
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
647 if (Math.abs(angle - 0) < epsilon) { // 0 degree
89
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
648 mx = -1;
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
649 tx = getWidth();
144
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
650 } else if (Math.abs(angle - 90) < epsilon) { // 90 degree
89
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
651 my = -1;
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
652 ty = getHeight();
144
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
653 } else if (Math.abs(angle - 180) < epsilon) { // 180 degree
89
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
654 mx = -1;
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
655 tx = getWidth();
144
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
656 } else if (Math.abs(angle - 270) < epsilon) { // 270 degree
89
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
657 my = -1;
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
658 ty = getHeight();
144
d87a7e2515af Sevlet version 1.12b5
robcast
parents: 140
diff changeset
659 } else if (Math.abs(angle - 360) < epsilon) { // 360 degree
89
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
660 mx = -1;
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
661 tx = getWidth();
6d35c945a5d6 New version 1.8b4.
robcast
parents: 86
diff changeset
662 }
279
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
663 AffineTransformOp mirOp = new AffineTransformOp(new AffineTransform(mx,
2739fd945499 Servlet version 1.22b1
robcast
parents: 256
diff changeset
664 0, 0, my, tx, ty), renderHint);
863
1641fab276ee fixing bugs from cleanup...
robcast
parents: 861
diff changeset
665 img = mirOp.filter(img, null);
79
63c8186455c1 Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents: 73
diff changeset
666 }
86
997ba69afb81 New version 1.8b1.
robcast
parents: 85
diff changeset
667
207
1a65d8e43620 Servlet version 1.18b9 -- cleanup and bugfixes
robcast
parents: 181
diff changeset
668 public void dispose() {
819
a23c4c15a6a8 clean up possible resource leaks. better behaviour with unknown image types.
robcast
parents: 801
diff changeset
669 // is this necessary?
148
837a633a0407 fixed bugs when using lots of prescaled images
robcast
parents: 145
diff changeset
670 img = null;
837a633a0407 fixed bugs when using lots of prescaled images
robcast
parents: 145
diff changeset
671 }
837a633a0407 fixed bugs when using lots of prescaled images
robcast
parents: 145
diff changeset
672
533
87cb3dc2aa12 improved pdf generation (not done yet)
robcast
parents: 531
diff changeset
673 public Image getAwtImage(){
496
1760b19df530 Added getImage method to ImageLoaderDocuImage;
cmielack
parents: 469
diff changeset
674 return (Image) img;
1760b19df530 Added getImage method to ImageLoaderDocuImage;
cmielack
parents: 469
diff changeset
675 }
1760b19df530 Added getImage method to ImageLoaderDocuImage;
cmielack
parents: 469
diff changeset
676
1760b19df530 Added getImage method to ImageLoaderDocuImage;
cmielack
parents: 469
diff changeset
677
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
678 }