changeset 89:6d35c945a5d6

New version 1.8b4. ImageLoader uses RandomAccessFile to setup ImageInputStream.
author robcast
date Mon, 17 Mar 2003 15:28:13 +0100
parents 398d39ee1014
children 8058d3b3466a
files servlet/src/digilib/image/ImageLoaderDocuImage.java servlet/src/digilib/image/JAIImageLoaderDocuImage.java
diffstat 2 files changed, 116 insertions(+), 114 deletions(-) [+]
line wrap: on
line diff
--- a/servlet/src/digilib/image/ImageLoaderDocuImage.java	Mon Mar 17 15:24:55 2003 +0100
+++ b/servlet/src/digilib/image/ImageLoaderDocuImage.java	Mon Mar 17 15:28:13 2003 +0100
@@ -29,6 +29,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.RandomAccessFile;
 import java.util.Iterator;
 
 import javax.imageio.ImageIO;
@@ -115,17 +116,13 @@
 	/** Get an ImageReader for the image file.
 	 * 
 	 */
-	public void preloadImage(File f) throws FileOpException {
+	public void preloadImage(File f) throws IOException {
 		System.gc();
-		try {
-			ImageInputStream istream = ImageIO.createImageInputStream(f);
-			Iterator readers = ImageIO.getImageReaders(istream);
-			reader = (ImageReader) readers.next();
-			reader.setInput(istream);
-		} catch (IOException e) {
-			util.dprintln(3, "ERROR(loadImage): unable to load file");
-			throw new FileOpException("Unable to load File!" + e);
-		}
+		RandomAccessFile rf = new RandomAccessFile(f, "r");
+		ImageInputStream istream = ImageIO.createImageInputStream(rf);
+		Iterator readers = ImageIO.getImageReaders(istream);
+		reader = (ImageReader) readers.next();
+		reader.setInput(istream);
 		if (reader == null) {
 			util.dprintln(3, "ERROR(loadImage): unable to load file");
 			throw new FileOpException("Unable to load File!");
@@ -258,7 +255,8 @@
 					+ ")");
 			return;
 		}
-		RescaleOp scaleOp = new RescaleOp(rgbOrdered(rgbm), rgbOrdered(rgba), null);
+		RescaleOp scaleOp =
+			new RescaleOp(rgbOrdered(rgbm), rgbOrdered(rgba), null);
 		scaleOp.filter(img, img);
 	}
 
@@ -280,78 +278,78 @@
 		return fb;
 	}
 
-public void rotate(double angle) throws ImageOpException {
-	// setup rotation
-	double rangle = Math.toRadians(angle);
-	double x = getWidth() / 2;
-	double y = getHeight() / 2;
-	AffineTransformOp rotOp =
-		new AffineTransformOp(
-			AffineTransform.getRotateInstance(rangle, x, y),
-			interpol);
-	BufferedImage rotImg = rotOp.filter(img, null);
+	public void rotate(double angle) throws ImageOpException {
+		// setup rotation
+		double rangle = Math.toRadians(angle);
+		double x = getWidth() / 2;
+		double y = getHeight() / 2;
+		AffineTransformOp rotOp =
+			new AffineTransformOp(
+				AffineTransform.getRotateInstance(rangle, x, y),
+				interpol);
+		BufferedImage rotImg = rotOp.filter(img, null);
 
-	if (rotImg == null) {
-		util.dprintln(2, "ERROR: error in rotate");
-		throw new ImageOpException("Unable to rotate");
+		if (rotImg == null) {
+			util.dprintln(2, "ERROR: error in rotate");
+			throw new ImageOpException("Unable to rotate");
+		}
+		img = rotImg;
 	}
-	img = rotImg;
-}
 
-public void mirror(double angle) throws ImageOpException {
-	// setup mirror
-	double mx = 1;
-	double my = 1;
-	double tx = 0;
-	double ty = 0;
-	if (Math.abs(angle - 0) < epsilon) {
-		// 0 degree
-		mx = -1;
-		tx = getWidth();
-	} else if (Math.abs(angle - 90) < epsilon) {
-		// 90 degree
-		my = -1;
-		ty = getHeight();
-	} else if (Math.abs(angle - 180) < epsilon) {
-		// 180 degree
-		mx = -1;
-		tx = getWidth();
-	} else if (Math.abs(angle - 270) < epsilon) {
-		// 270 degree
-		my = -1;
-		ty = getHeight();
-	} else if (Math.abs(angle - 360) < epsilon) {
-		// 360 degree
-		mx = -1;
-		tx = getWidth();
+	public void mirror(double angle) throws ImageOpException {
+		// setup mirror
+		double mx = 1;
+		double my = 1;
+		double tx = 0;
+		double ty = 0;
+		if (Math.abs(angle - 0) < epsilon) {
+			// 0 degree
+			mx = -1;
+			tx = getWidth();
+		} else if (Math.abs(angle - 90) < epsilon) {
+			// 90 degree
+			my = -1;
+			ty = getHeight();
+		} else if (Math.abs(angle - 180) < epsilon) {
+			// 180 degree
+			mx = -1;
+			tx = getWidth();
+		} else if (Math.abs(angle - 270) < epsilon) {
+			// 270 degree
+			my = -1;
+			ty = getHeight();
+		} else if (Math.abs(angle - 360) < epsilon) {
+			// 360 degree
+			mx = -1;
+			tx = getWidth();
+		}
+		AffineTransformOp mirOp =
+			new AffineTransformOp(
+				new AffineTransform(mx, 0, 0, my, tx, ty),
+				interpol);
+		BufferedImage mirImg = mirOp.filter(img, null);
+
+		if (mirImg == null) {
+			util.dprintln(2, "ERROR: error in mirror");
+			throw new ImageOpException("Unable to mirror");
+		}
+		img = mirImg;
 	}
-	AffineTransformOp mirOp =
-		new AffineTransformOp(
-			new AffineTransform(mx, 0, 0, my, tx, ty),
-			interpol);
-	BufferedImage mirImg = mirOp.filter(img, null);
 
-	if (mirImg == null) {
-		util.dprintln(2, "ERROR: error in mirror");
-		throw new ImageOpException("Unable to mirror");
+	/* check image size and type and store in DocuFile f */
+	public boolean checkFile(DocuFile f) throws IOException {
+		// see if f is already loaded
+		if ((reader == null) || (imgFile != f.getFile())) {
+			preloadImage(f.getFile());
+		}
+		Dimension d = new Dimension();
+		d.setSize(reader.getWidth(0), reader.getHeight(0));
+		f.setSize(d);
+		String t = reader.getFormatName();
+		t = FileOps.mimeForFile(f.getFile());
+		f.setMimetype(t);
+		f.setChecked(true);
+		return true;
 	}
-	img = mirImg;
-}
-
-/* check image size and type and store in DocuFile f */
-public boolean checkFile(DocuFile f) throws IOException {
-	// see if f is already loaded
-	if ((reader == null) || (imgFile != f.getFile())) {
-		preloadImage(f.getFile());
-	}
-	Dimension d = new Dimension();
-	d.setSize(reader.getWidth(0), reader.getHeight(0));
-	f.setSize(d);
-//	String t = reader.getFormatName();
-	String t = FileOps.mimeForFile(f.getFile());
-	f.setMimetype(t);
-	f.setChecked(true);
-	return true;
-}
 
 }
--- a/servlet/src/digilib/image/JAIImageLoaderDocuImage.java	Mon Mar 17 15:24:55 2003 +0100
+++ b/servlet/src/digilib/image/JAIImageLoaderDocuImage.java	Mon Mar 17 15:28:13 2003 +0100
@@ -20,11 +20,13 @@
 
 package digilib.image;
 
+import java.awt.Dimension;
 import java.awt.Rectangle;
 import java.awt.image.renderable.ParameterBlock;
 import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.RandomAccessFile;
 import java.util.Iterator;
 
 import javax.imageio.ImageIO;
@@ -32,23 +34,21 @@
 import javax.imageio.ImageReader;
 import javax.imageio.stream.ImageInputStream;
 import javax.media.jai.JAI;
+import javax.media.jai.ParameterBlockJAI;
 
-import com.sun.media.jai.operator.ImageReadDescriptor;
-
+import digilib.io.DocuFile;
 import digilib.io.FileOpException;
+import digilib.io.FileOps;
 
 /** DocuImage implementation using the Java Advanced Imaging API and the ImageLoader
  * API of Java 1.4.
  */
 public class JAIImageLoaderDocuImage extends JAIDocuImage {
 
-	// ImageIO image reader
-	ImageReader reader;
-
-	/* preload is supported. */
-	public boolean isPreloadSupported() {
-		return true;
-	}
+	/** ImageIO image reader */
+	protected ImageReader reader;
+	/** current image file */
+	protected File imgFile;
 
 	/* loadSubimage is supported. */
 	public boolean isSubimageSupported() {
@@ -98,29 +98,18 @@
 		throws FileOpException {
 		System.gc();
 		try {
-			if (reader == null) {
+			if ((reader == null) || (imgFile != f)) {
 				preloadImage(f);
 			}
 			ImageInputStream istream = (ImageInputStream) reader.getInput();
 			ImageReadParam readParam = reader.getDefaultReadParam();
 			readParam.setSourceRegion(region);
 			readParam.setSourceSubsampling(prescale, prescale, 0, 0);
-			/* Parameter for ImageRead operation:
-				Input, ImageChoice, ReadMetadata, ReadThumbnails, VerifyInput,
-				Listeners, Locale, ReadParam, Reader, RenderingHints
-			 */
-			img =
-				ImageReadDescriptor.create(
-					istream,
-					new Integer(0),
-					Boolean.TRUE,
-					Boolean.FALSE,
-					Boolean.FALSE,
-					null,
-					null,
-					readParam,
-					reader,
-					null);
+			ParameterBlockJAI pb = new ParameterBlockJAI("imageread");
+			pb.setParameter("Input", istream);
+			pb.setParameter("ReadParam", readParam);
+			pb.setParameter("Reader", reader);
+			img = JAI.create("imageread", pb);
 		} catch (IOException e) {
 			util.dprintln(3, "ERROR(loadImage): unable to load file");
 			throw new FileOpException("Unable to load File!");
@@ -129,20 +118,17 @@
 			util.dprintln(3, "ERROR(loadImage): unable to load file");
 			throw new FileOpException("Unable to load File!");
 		}
+		imgFile = f;
 	}
 
 	/* Get an ImageReader for the image file. */
-	public void preloadImage(File f) throws FileOpException {
+	public void preloadImage(File f) throws IOException {
 		System.gc();
-		try {
-			ImageInputStream istream = ImageIO.createImageInputStream(f);
-			Iterator readers = ImageIO.getImageReaders(istream);
-			reader = (ImageReader) readers.next();
-			reader.setInput(istream);
-		} catch (IOException e) {
-			util.dprintln(3, "ERROR(loadImage): unable to load file");
-			throw new FileOpException("Unable to load File!" + e);
-		}
+		RandomAccessFile rf = new RandomAccessFile(f, "r");
+		ImageInputStream istream = ImageIO.createImageInputStream(rf);
+		Iterator readers = ImageIO.getImageReaders(istream);
+		reader = (ImageReader) readers.next();
+		reader.setInput(istream);
 		if (reader == null) {
 			util.dprintln(3, "ERROR(loadImage): unable to load file");
 			throw new FileOpException("Unable to load File!");
@@ -174,4 +160,22 @@
 		}
 	}
 
+	/* (non-Javadoc)
+	 * @see digilib.image.DocuImage#checkFile(digilib.io.DocuFile)
+	 */
+	public boolean checkFile(DocuFile f) throws IOException {
+		// see if f is already loaded
+		if ((reader == null) || (imgFile != f.getFile())) {
+			preloadImage(f.getFile());
+		}
+		Dimension d = new Dimension();
+		d.setSize(reader.getWidth(0), reader.getHeight(0));
+		f.setSize(d);
+		//	String t = reader.getFormatName();
+		String t = FileOps.mimeForFile(f.getFile());
+		f.setMimetype(t);
+		f.setChecked(true);
+		return true;
+	}
+
 }