JMagick reference implementation. not to be used officially.
authormh <mh>
Fri, 19 Oct 2001 13:44:19 +0000 (13:44 +0000)
committermh <mh>
Fri, 19 Oct 2001 13:44:19 +0000 (13:44 +0000)
jmagick/README [new file with mode: 0755]
jmagick/WebdbImage.java.jmagick [new file with mode: 0755]

diff --git a/jmagick/README b/jmagick/README
new file mode 100755 (executable)
index 0000000..0f1110f
--- /dev/null
@@ -0,0 +1,22 @@
+This directory contains an implementation of mir.misc.WedbImage that uses
+JMagick for image manipulation as an alternative to Sun's JAI ( Java 
+Advanced Imaging). JMagick is the java interface to good'ol ImageMagick.
+This code is for JMagick-5.33. You need to dowload JMagick from the ImageMagick
+site.
+
+There are no plans to replace the JAI implementation with this. JAI is better
+maintained (the last JMagick release was November 2000) and is more powerful.
+
+You absolutely need to have the Jmagick shared library in your LD_LIBRARY_PATH
+var for it to work.
+
+The only advantage it has over JAI is that it is open source (AFAIK, the JAI
+source is not available, please correct me if I'm wrong). 
+
+Also, it provides a native implementation (the c shared library) for all 
+platforms as one has the source to compile it... However, my testing shows
+that the pure Java JAI is *faster* than the native JMagick/ImageMagick, at
+least on the PPC architecture (G3) tested against JAI 1.1.1.
+
+-mh <heckmann@hbe.ca>
+2001.10.19
diff --git a/jmagick/WebdbImage.java.jmagick b/jmagick/WebdbImage.java.jmagick
new file mode 100755 (executable)
index 0000000..731570b
--- /dev/null
@@ -0,0 +1,177 @@
+package mir.misc;
+
+/**
+ * Title:
+ * Description:
+ * Copyright:    Copyright (c) 2001
+ * Company:      Indymedia
+ * @author
+ * @version 1.0
+ */
+
+import java.io.*;
+//import java.awt.image.renderable.ParameterBlock;
+import java.awt.Dimension;
+import java.awt.Rectangle;
+import magick.*;
+
+public class WebdbImage
+{
+
+    private Logfile theLog = Logfile.getInstance(MirConfig.getPropWithHome("HTMLTemplateProcessor.Logfile"));
+       // imageTypes
+       public final static int        WEBDB_JPG=0;
+       public final static int        WEBDB_GIF=1;
+
+       // default values for scaling
+       private int               maxIconSize=120;
+       private int               maxImageSize=640;
+
+       private byte[]            iconData;
+       private byte[]            imageData;
+       private int               imageType;
+       private int               iconWidth;
+       private int               iconHeight;
+
+       // internal representation of the image
+       private MagickImage       magickImage;
+
+
+       // constructor
+       public WebdbImage(byte[] image, int type)
+               throws IOException
+       {
+               imageType=type;
+        try {
+            magickImage = new MagickImage(new ImageInfo(), image);
+        } catch (MagickException e) {theLog.printDebugInfo("fail: "+e.toString());}
+               scaleImage();
+       }
+
+       public WebdbImage(byte[] image, int type, int iconMax)
+               throws IOException
+       {
+               imageType=type;
+               maxIconSize=iconMax;
+        try {
+            magickImage = new MagickImage(new ImageInfo(), image);
+        } catch (MagickException e) {theLog.printDebugInfo("fail: "+e.toString());}
+               scaleImage();
+       }
+
+       public WebdbImage(byte[] image, int type, int iconMax, int imageMax)
+               throws IOException
+       {
+               imageType=type;
+               maxIconSize=iconMax;
+               maxImageSize=imageMax;
+        try {
+            magickImage = new MagickImage(new ImageInfo(), image);
+        } catch (MagickException e) {theLog.printDebugInfo("fail: "+e.toString());}
+               scaleImage();
+       }
+
+
+       // acc3ssor-methods
+       public int getIconWidth() throws IOException {
+               if (iconData==null) scaleIcon();
+               return iconWidth;
+       }
+
+       public int getIconHeight() throws IOException {
+               if (iconData==null) scaleIcon();
+               return iconHeight;
+       }
+
+       public int getImageWidth() {
+        try {
+            return (int)magickImage.getDimension().getWidth();
+        } catch (MagickException e) { return -1;}
+       }
+
+       public int getImageHeight() {
+        int gg;
+        try {
+                   return (int)magickImage.getDimension().getHeight();
+        } catch (MagickException e) { return -1;}
+       }
+
+       public byte[] getImage() {
+               if (imageData==null) {
+            try {
+                ImageInfo imageInfo = new ImageInfo();
+                imageInfo.init();
+
+                switch (imageType) {
+                    case WEBDB_JPG:
+                        imageInfo.setMagickMember("JPG");
+                    case WEBDB_GIF:
+                        imageInfo.setMagickMember("JPG");
+                }
+
+                imageData = magickImage.imageToBlob(imageInfo);
+            } catch (MagickException e) {
+                theLog.printDebugInfo("getImage: magick "+e.toString());
+            }
+        }
+        return imageData;
+       }
+
+       public byte[] getIcon()
+               throws IOException
+       {
+               if (iconData == null) scaleIcon();
+               return iconData;
+       }
+
+       private void scaleImage()
+               throws java.io.IOException
+       {
+               if (maxImageSize>0 && ( getImageHeight()> maxImageSize|| getImageWidth() >maxImageSize))
+               {
+                       float scale;
+                       if (getImageHeight() > getImageWidth())
+                               scale = (float)maxImageSize / (float)getImageHeight();
+                       else
+                               scale = (float)maxImageSize / (float)getImageWidth();
+
+            try {
+                           magickImage = magickImage.scaleImage((int)scale*getImageWidth(), (int)scale*getImageHeight());
+            } catch (MagickException e) {}
+               }
+       }
+
+       private void scaleIcon()
+               throws java.io.IOException
+       {
+               if (iconData==null) {
+                       float scale;
+                       if (getImageHeight() > getImageWidth())
+                               scale = (float)maxIconSize / (float)getImageHeight();
+                       else
+                               scale = (float)maxIconSize / (float)getImageWidth();
+
+            try {
+                           MagickImage temp = magickImage.scaleImage((int)(scale*getImageWidth()), (int)(scale*getImageHeight()));
+                ImageInfo imageInfo = new ImageInfo();
+                imageInfo.init();
+                /** @todo gif */
+                switch (imageType) {
+                    case WEBDB_JPG:
+                        imageInfo.setMagickMember("JPG");
+                    case WEBDB_GIF:
+                        imageInfo.setMagickMember("JPG");
+                }
+                iconWidth=(int)temp.getDimension().getWidth();
+                iconHeight=(int)temp.getDimension().getHeight();
+                // Put a black rectangle around the border
+                DrawInfo drawInfo = new DrawInfo(imageInfo);
+                drawInfo.setPrimitive("Rectangle 0 0 "+(iconWidth-1)+" "+(iconHeight-1));
+                drawInfo.setStroke(PixelPacket.queryColorDatabase("black"));
+                temp.drawImage(drawInfo);
+                iconData = temp.imageToBlob(imageInfo);
+            } catch (MagickException e) {}
+               }
+       }
+
+}