Image handler that stores imagedata outside the database.
[mir.git] / source / mircoders / media / ImageProcessor.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.media;
33
34 import java.io.*;
35 import java.awt.image.renderable.ParameterBlock;
36 import javax.media.jai.*;
37 import com.sun.media.jai.codec.*;
38
39 import mir.log.*;
40
41 /**
42  *
43  * <p>Title: Image processor</p>
44  * <p>Description: Temporary image processor class. (Made for the immediate needs of CMI brasil.
45  *                 Will become obsolete when mh's media handler rewrite is finished. </p>
46  * @author Zapata
47  * @version 1.0
48  */
49
50 public class ImageProcessor {
51   static final LoggerWrapper logger = new LoggerWrapper("media");
52
53   private static final int DEFAULT_MAX_ICON_SIZE=120;
54   private static final int DEFAULT_MAX_IMAGE_SIZE=640;
55
56   private PlanarImage image;
57   private PlanarImage scaledImage;
58
59   private byte[] iconData;
60   private byte[] imageData;
61   private int iconWidth;
62   private int iconHeight;
63   private String imageType;
64
65 //  public ImageScaler(
66
67   public ImageProcessor(SeekableStream anImageStream, String anImageType) throws IOException {
68     image = JAI.create("stream", anImageStream);
69     scaledImage = image;
70     imageType = anImageType;
71   }
72
73   public ImageProcessor(File aFile, String anImageType) throws IOException {
74     this(new FileSeekableStream(aFile), anImageType);
75   }
76
77   public ImageProcessor(byte[] anImageData, String anImageType) throws IOException {
78     this(new ByteArraySeekableStream(anImageData), anImageType);
79   }
80
81   public void descaleImage(int aMaxSize) throws java.io.IOException {
82     descaleImage(aMaxSize, 0);
83   }
84
85   public void descaleImage(int aMaxSize, float aMinDescale) throws java.io.IOException {
86     descaleImage(aMaxSize, aMaxSize, aMinDescale);
87   }
88
89   public void descaleImage(int aMaxWidth, int aMaxHeight, float aMinDescale) throws java.io.IOException {
90     float scale;
91     scaledImage = image;
92
93     if ((aMaxWidth>0 && image.getWidth()>aMaxWidth) || (aMaxHeight>0 && image.getHeight()>aMaxHeight))
94     {
95       logger.info("Scaling image");
96
97       scale=1;
98
99       if (aMaxWidth>0 && image.getWidth()>aMaxWidth) {
100         scale = Math.min(scale, (float) aMaxWidth / (float) image.getWidth());
101       }
102       if (aMaxHeight>0 && image.getHeight()>aMaxHeight) {
103         scale = Math.min(scale, (float) aMaxHeight / (float) image.getHeight());
104       }
105
106       if (1-scale>aMinDescale) {
107         scaleImage(scale);
108       }
109     }
110   }
111
112   public void scaleImage(float aScalingFactor) throws java.io.IOException {
113     ParameterBlockJAI params = new ParameterBlockJAI("scale");
114     params.addSource(image);
115
116     params.setParameter("xScale", aScalingFactor);
117     params.setParameter("yScale", aScalingFactor);
118     params.setParameter("xTrans", 0.0F);
119     params.setParameter("yTrans", 0.0F);
120     params.setParameter("interpolation", new InterpolationBilinear());
121     scaledImage = JAI.create("scale", params);
122   }
123
124   public int getWidth() {
125     return image.getWidth();
126   }
127
128   public int getHeight() {
129     return image.getWidth();
130   }
131
132   public int getScaledWidth() {
133     return scaledImage.getWidth();
134   }
135
136   public int getScaledHeight() {
137     return scaledImage.getWidth();
138   }
139
140   public void writeScaledData(OutputStream aStream) {
141     JAI.create("encode", scaledImage, aStream, imageType, null);
142   }
143
144   public byte[] getScaledData() {
145     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
146     writeScaledData(outputStream);
147     return outputStream.toByteArray();
148   }
149
150   public void writeScaledData(File aFile) throws IOException {
151     writeScaledData(new FileOutputStream(aFile));
152   }
153 }