1.1 restoration
[mir.git] / source / mircoders / media / ImageProcessor.java
index 95c22af..2c7877e 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2001, 2002  The Mir-coders group
+ * Copyright (C) 2001, 2002 The Mir-coders group
  *
  * This file is part of Mir.
  *
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with the com.oreilly.servlet library, any library
- * licensed under the Apache Software License, The Sun (tm) Java Advanced
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of
- * the above that use the same license as the above), and distribute linked
- * combinations including the two.  You must obey the GNU General Public
- * License in all respects for all of the code used other than the above
- * mentioned libraries.  If you modify this file, you may extend this exception
- * to your version of the file, but you are not obligated to do so.  If you do
- * not wish to do so, delete this exception statement from your version.
+ * the code of this program with  any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
  */
 
 package mircoders.media;
 
-import java.io.*;
-import java.awt.image.renderable.ParameterBlock;
-import javax.media.jai.*;
-import com.sun.media.jai.codec.*;
-
-import mir.log.*;
+import java.awt.RenderingHints;
+import java.awt.image.ColorModel;
+import java.awt.image.DataBuffer;
+import java.awt.image.PixelInterleavedSampleModel;
+import java.awt.image.RenderedImage;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.media.jai.ImageLayout;
+import javax.media.jai.InterpolationBilinear;
+import javax.media.jai.JAI;
+import javax.media.jai.ParameterBlockJAI;
+
+import mir.log.LoggerWrapper;
+
+import com.sun.media.jai.codec.ByteArraySeekableStream;
+import com.sun.media.jai.codec.FileSeekableStream;
+import com.sun.media.jai.codec.SeekableStream;
 
 /**
  *
@@ -50,29 +65,53 @@ import mir.log.*;
 public class ImageProcessor {
   static final LoggerWrapper logger = new LoggerWrapper("media");
 
-  private PlanarImage image;
-  private PlanarImage scaledImage;
+  private RenderedImage image;
+  private RenderedImage scaledImage;
+
+  public ImageProcessor(RenderedImage anImage) throws IOException {
+    image = anImage;
+    scaledImage = anImage;
+  }
+
+  public ImageProcessor(SeekableStream anImageStream) throws IOException {
+    RenderedImage tempImage = JAI.create("stream", anImageStream);
+    ParameterBlockJAI params = new ParameterBlockJAI("format");
+    int bands[];
+    int nrComponents;
+
+
+    params.addSource(tempImage);
+    params.setParameter("dataType", DataBuffer.TYPE_BYTE);
+
+    ImageLayout layout = new ImageLayout();
+    nrComponents = tempImage.getColorModel().getNumColorComponents();
+
+    bands = new int[nrComponents];
+    for (int i=0; i<nrComponents; i++)
+      bands[i]=i;
+
+    layout.setColorModel(ColorModel.getRGBdefault());
+    layout.setSampleModel(
+        new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,
+        tempImage.getWidth(),
+        tempImage.getHeight(),
+        nrComponents,
+        nrComponents*tempImage.getWidth(),
+        bands));
 
-  private byte[] iconData;
-  private byte[] imageData;
-  private int iconWidth;
-  private int iconHeight;
-  private String imageType;
+    RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
 
-//  public ImageScaler(
+    image = JAI.create("format", params, hints);
 
-  public ImageProcessor(SeekableStream anImageStream, String anImageType) throws IOException {
-    image = JAI.create("stream", anImageStream);
     scaledImage = image;
-    imageType = anImageType;
   }
 
-  public ImageProcessor(File aFile, String anImageType) throws IOException {
-    this(new FileSeekableStream(aFile), anImageType);
+  public ImageProcessor(File aFile) throws IOException {
+    this(new FileSeekableStream(aFile));
   }
 
-  public ImageProcessor(byte[] anImageData, String anImageType) throws IOException {
-    this(new ByteArraySeekableStream(anImageData), anImageType);
+  public ImageProcessor(byte[] anImageData) throws IOException {
+    this(new ByteArraySeekableStream(anImageData));
   }
 
   public void descaleImage(int aMaxSize) throws java.io.IOException {
@@ -80,14 +119,34 @@ public class ImageProcessor {
   }
 
   public void descaleImage(int aMaxSize, float aMinDescale) throws java.io.IOException {
-    descaleImage(aMaxSize, aMaxSize, aMinDescale);
+    descaleImage(aMaxSize, aMaxSize, aMinDescale, 0);
   }
 
-  public void descaleImage(int aMaxWidth, int aMaxHeight, float aMinDescale) throws java.io.IOException {
+  public void descaleImage(int aMaxSize, int aMinResize) throws java.io.IOException {
+    descaleImage(aMaxSize, aMaxSize, 0, aMinResize);
+  }
+
+  public void descaleImage(int aMaxSize, float aMinDescale, int aMinResize) throws java.io.IOException {
+    descaleImage(aMaxSize, aMaxSize, aMinDescale, aMinResize);
+  }
+
+  /**
+   *
+   * Resizes an image to fit inside <code>aMaxWidth</code> and <code>aMaxHeight</code>, provided
+   *    this requires at least <code>aMinResize</code> pixels will be removed from either the width or
+   *    the height
+   *
+   * @param aMaxWidth
+   * @param aMaxHeight
+   * @param aMinDescale
+   * @param aMinResize
+   * @throws java.io.IOException
+   */
+  public void descaleImage(int aMaxWidth, int aMaxHeight, float aMinDescale, int aMinResize) throws java.io.IOException {
     float scale;
     scaledImage = image;
 
-    if ((aMaxWidth>0 && image.getWidth()>aMaxWidth) || (aMaxHeight>0 && image.getHeight()>aMaxHeight))
+    if ((aMaxWidth>0 && image.getWidth()>aMaxWidth+aMinResize-1) || (aMaxHeight>0 && image.getHeight()>aMaxHeight+aMinResize-1))
     {
       logger.info("Scaling image");
 
@@ -134,17 +193,37 @@ public class ImageProcessor {
     return scaledImage.getHeight();
   }
 
-  public void writeScaledData(OutputStream aStream) {
-    JAI.create("encode", scaledImage, aStream, imageType, null);
+  public void writeScaledData(OutputStream aStream, String anImageType) {
+    JAI.create("encode", scaledImage, aStream, anImageType, null);
   }
 
-  public byte[] getScaledData() {
+  public byte[] getScaledData(String anImageType) {
     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-    writeScaledData(outputStream);
+    writeScaledData(outputStream, anImageType);
     return outputStream.toByteArray();
   }
 
-  public void writeScaledData(File aFile) throws IOException {
-    writeScaledData(new FileOutputStream(aFile));
+  public void writeScaledData(File aFile, String anImageType) throws IOException {
+    writeScaledData(new BufferedOutputStream(new FileOutputStream(aFile),8192), anImageType);
   }
 }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+