/* * Copyright (C) 2001 The Mir-coders group * * This file is part of Mir. * * Mir is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Mir is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Mir; if not, write to the Free Software * 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. */ 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) {} } } }