diff servlet/src/digilib/image/ImageLoaderDocuImage.java @ 79:63c8186455c1

Servlet version 1.6b. Further cleanup and new functionality: * mirroring * contrast/brightness * rotation (not finished)
author robcast
date Mon, 03 Feb 2003 16:04:53 +0100
parents 3b8797fc3e90
children 4e6757e8ccd4
line wrap: on
line diff
--- a/servlet/src/digilib/image/ImageLoaderDocuImage.java	Fri Jan 24 22:13:49 2003 +0100
+++ b/servlet/src/digilib/image/ImageLoaderDocuImage.java	Mon Feb 03 16:04:53 2003 +0100
@@ -25,9 +25,9 @@
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
+import java.io.OutputStream;
 
 import javax.imageio.ImageIO;
-import javax.servlet.ServletResponse;
 
 import digilib.Utils;
 import digilib.io.FileOpException;
@@ -35,128 +35,122 @@
 /** Implementation of DocuImage using the ImageLoader API of Java 1.4 and Java2D. */
 public class ImageLoaderDocuImage extends DocuImageImpl {
 
-  private BufferedImage img;
+	private BufferedImage img;
+
+	private int scaleInt;
+
+	public ImageLoaderDocuImage() {
+	}
+
+	public ImageLoaderDocuImage(Utils u) {
+		util = u;
+	}
 
-  public ImageLoaderDocuImage() {
-  }
-
-  public ImageLoaderDocuImage(Utils u) {
-    util = u;
-  }
+	/**
+	 *  load image file
+	 */
+	public void loadImage(File f) throws FileOpException {
+		util.dprintln(10, "loadImage!");
+		System.gc();
+		try {
+			img = ImageIO.read(f);
+			if (img == null) {
+				util.dprintln(3, "ERROR(loadImage): unable to load file");
+				throw new FileOpException("Unable to load File!");
+			}
+		} catch (IOException e) {
+			throw new FileOpException("Error reading image.");
+		}
+	}
 
-  /**
-   *  load image file
-   */
-  public void loadImage(File f) throws FileOpException {
-    util.dprintln(10, "loadImage!");
-    System.gc();
-    try {
-      for (int i = 0; i < ImageIO.getReaderFormatNames().length; i++) {
-    System.out.println("ImageLoader reader:"+ImageIO.getReaderFormatNames()[i]);
-      }
-      for (int i = 0; i < ImageIO.getWriterFormatNames().length; i++) {
-    System.out.println("ImageLoader writer:"+ImageIO.getWriterFormatNames()[i]);
-      }
-      img = ImageIO.read(f);
-      if (img == null) {
-        util.dprintln(3, "ERROR(loadImage): unable to load file");
-        throw new FileOpException("Unable to load File!");
-      }
-    }
-    catch (IOException e) {
-      throw new FileOpException("Error reading image.");
-    }
-  }
+	/**
+	 *  write image of type mt to Stream
+	 */
+	public void writeImage(String mt, OutputStream ostream)
+		throws FileOpException {
+		util.dprintln(10, "writeImage!");
+		try {
+			// setup output
+			String type = "png";
+			if (mt == "image/jpeg") {
+				type = "jpeg";
+			} else if (mt == "image/png") {
+				type = "png";
+			} else {
+				// unknown mime type
+				util.dprintln(2, "ERROR(writeImage): Unknown mime type " + mt);
+				throw new FileOpException("Unknown mime type: " + mt);
+			}
+			// render output
+			if (ImageIO.write(img, type, ostream)) {
+				// writing was OK
+				return;
+			} else {
+				throw new FileOpException("Error writing image: Unknown image format!");
+			}
+		} catch (IOException e) {
+			// e.printStackTrace();
+			throw new FileOpException("Error writing image.");
+		}
+	}
 
-  /**
-   *  write image of type mt to Stream
-   */
-  public void writeImage(String mt, ServletResponse res)
-         throws FileOpException {
-    util.dprintln(10, "writeImage!");
-    try {
-      // setup output
-      String type = "png";
-      if (mt == "image/jpeg") {
-        type = "jpeg";
-      } else if (mt == "image/png") {
-        type = "png";
-      } else {
-        // unknown mime type
-        util.dprintln(2, "ERROR(writeImage): Unknown mime type "+mt);
-        throw new FileOpException("Unknown mime type: "+mt);
-      }
-      res.setContentType(mt);
-      // render output
-      if (ImageIO.write(img, type, res.getOutputStream())) {
-        // writing was OK
-        return;
-      } else {
-        throw new FileOpException("Error writing image: Unknown image format!");
-      }
-    } catch (IOException e) {
-      // e.printStackTrace();
-      throw new FileOpException("Error writing image.");
-    }
-  }
+	public int getWidth() {
+		if (img != null) {
+			return img.getWidth();
+		}
+		return 0;
+	}
+
+	public int getHeight() {
+		if (img != null) {
+			return img.getHeight();
+		}
+		return 0;
+	}
 
-  public int getWidth() {
-    if (img != null) {
-      return img.getWidth();
-    }
-    return 0;
-  }
+	public void scale(double scale) throws ImageOpException {
+		// setup scale
+		AffineTransformOp scaleOp =
+			new AffineTransformOp(
+				AffineTransform.getScaleInstance(scale, scale),
+				scaleInt);
+		BufferedImage scaledImg = scaleOp.filter(img, null);
+
+		if (scaledImg == null) {
+			util.dprintln(2, "ERROR(cropAndScale): error in scale");
+			throw new ImageOpException("Unable to scale");
+		}
+		img = scaledImg;
+	}
 
-  public int getHeight() {
-    if (img != null) {
-      return img.getHeight();
-    }
-    return 0;
-  }
+	public void crop(int x_off, int y_off, int width, int height)
+		throws ImageOpException {
+		// setup Crop
+		BufferedImage croppedImg = img.getSubimage(x_off, y_off, width, height);
 
-  /**
-   *  crop and scale image
-   *    take rectangle width,height at position x_off,y_off
-   *    and scale by scale
-   */
-   public void cropAndScale(int x_off, int y_off, int width, int height,
-         float scale, int qual) throws ImageOpException {
-    util.dprintln(10, "cropAndScale!");
-
-    int scaleInt = 0;
-    // setup interpolation quality
-    if (qual > 0) {
-      util.dprintln(4, "quality q1");
-      scaleInt = AffineTransformOp.TYPE_BILINEAR;
-    } else {
-      util.dprintln(4, "quality q0");
-      scaleInt = AffineTransformOp.TYPE_NEAREST_NEIGHBOR;
-    }
+		util.dprintln(
+			3,
+			"CROP:" + croppedImg.getWidth() + "x" + croppedImg.getHeight());
+		//DEBUG
+		//    util.dprintln(2, "  time "+(System.currentTimeMillis()-startTime)+"ms");
 
-    // setup Crop
-    BufferedImage croppedImg = img.getSubimage(x_off, y_off, width, height);
-
-    img = null; // free img
-    util.dprintln(3, "CROP:"+croppedImg.getWidth()+"x"+croppedImg.getHeight()); //DEBUG
-//    util.dprintln(2, "  time "+(System.currentTimeMillis()-startTime)+"ms");
-
-    if (croppedImg == null) {
-      util.dprintln(2, "ERROR(cropAndScale): error in crop");
-      throw new ImageOpException("Unable to crop");
-    }
+		if (croppedImg == null) {
+			util.dprintln(2, "ERROR(cropAndScale): error in crop");
+			throw new ImageOpException("Unable to crop");
+		}
+		img = croppedImg;
+	}
 
-    // setup scale
-    AffineTransformOp scaleOp = new AffineTransformOp(
-                      AffineTransform.getScaleInstance(scale, scale),
-                      scaleInt);
-    BufferedImage scaledImg = scaleOp.filter(croppedImg, null);
-    croppedImg = null; // free opCrop
-
-    if (scaledImg == null) {
-      util.dprintln(2, "ERROR(cropAndScale): error in scale");
-      throw new ImageOpException("Unable to scale");
-    }
-    img = scaledImg;
-  }
+	public void setQuality(int qual) {
+		quality = qual;
+		// setup interpolation quality
+		if (qual > 0) {
+			util.dprintln(4, "quality q1");
+			scaleInt = AffineTransformOp.TYPE_BILINEAR;
+		} else {
+			util.dprintln(4, "quality q0");
+			scaleInt = AffineTransformOp.TYPE_NEAREST_NEIGHBOR;
+		}
+	}
 
 }