whoops
[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.ByteArrayOutputStream;
35 import java.io.File;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.OutputStream;
39
40 import javax.media.jai.InterpolationBilinear;
41 import javax.media.jai.JAI;
42 import javax.media.jai.ParameterBlockJAI;
43 import javax.media.jai.PlanarImage;
44
45 import mir.log.LoggerWrapper;
46
47 import com.sun.media.jai.codec.ByteArraySeekableStream;
48 import com.sun.media.jai.codec.FileSeekableStream;
49 import com.sun.media.jai.codec.SeekableStream;
50
51 /**
52  *
53  * <p>Title: Image processor</p>
54  * <p>Description: Temporary image processor class. (Made for the immediate needs of CMI brasil.
55  *                 Will become obsolete when mh's media handler rewrite is finished. </p>
56  * @author Zapata
57  * @version 1.0
58  */
59
60 public class ImageProcessor {
61   static final LoggerWrapper logger = new LoggerWrapper("media");
62
63   private PlanarImage image;
64   private PlanarImage scaledImage;
65
66   private byte[] iconData;
67   private byte[] imageData;
68   private int iconWidth;
69   private int iconHeight;
70   private String imageType;
71
72 //  public ImageScaler(
73
74   public ImageProcessor(SeekableStream anImageStream, String anImageType) throws IOException {
75     image = JAI.create("stream", anImageStream);
76     scaledImage = image;
77     imageType = anImageType;
78   }
79
80   public ImageProcessor(File aFile, String anImageType) throws IOException {
81     this(new FileSeekableStream(aFile), anImageType);
82   }
83
84   public ImageProcessor(byte[] anImageData, String anImageType) throws IOException {
85     this(new ByteArraySeekableStream(anImageData), anImageType);
86   }
87
88   public void descaleImage(int aMaxSize) throws java.io.IOException {
89     descaleImage(aMaxSize, 0);
90   }
91
92   public void descaleImage(int aMaxSize, float aMinDescale) throws java.io.IOException {
93     descaleImage(aMaxSize, aMaxSize, aMinDescale, 0);
94   }
95
96   public void descaleImage(int aMaxSize, int aMinResize) throws java.io.IOException {
97     descaleImage(aMaxSize, aMaxSize, 0, aMinResize);
98   }
99
100   public void descaleImage(int aMaxSize, float aMinDescale, int aMinResize) throws java.io.IOException {
101     descaleImage(aMaxSize, aMaxSize, aMinDescale, aMinResize);
102   }
103
104   /**
105    *
106    * Resizes an image to fit inside <code>aMaxWidth</code> and <code>aMaxHeight</code>, provided
107    *    this requires at least <code>aMinResize</code> pixels will be removed from either the width or
108    *    the height
109    *
110    * @param aMaxWidth
111    * @param aMaxHeight
112    * @param aMinDescale
113    * @param aMinResize
114    * @throws java.io.IOException
115    */
116   public void descaleImage(int aMaxWidth, int aMaxHeight, float aMinDescale, int aMinResize) throws java.io.IOException {
117     float scale;
118     scaledImage = image;
119
120     if ((aMaxWidth>0 && image.getWidth()>aMaxWidth+aMinResize-1) || (aMaxHeight>0 && image.getHeight()>aMaxHeight+aMinResize-1))
121     {
122       logger.info("Scaling image");
123
124       scale=1;
125
126       if (aMaxWidth>0 && image.getWidth()>aMaxWidth) {
127         scale = Math.min(scale, (float) aMaxWidth / (float) image.getWidth());
128       }
129       if (aMaxHeight>0 && image.getHeight()>aMaxHeight) {
130         scale = Math.min(scale, (float) aMaxHeight / (float) image.getHeight());
131       }
132
133       if (1-scale>aMinDescale) {
134         scaleImage(scale);
135       }
136     }
137   }
138
139   public void scaleImage(float aScalingFactor) throws java.io.IOException {
140     ParameterBlockJAI params = new ParameterBlockJAI("scale");
141     params.addSource(image);
142
143     params.setParameter("xScale", aScalingFactor);
144     params.setParameter("yScale", aScalingFactor);
145     params.setParameter("xTrans", 0.0F);
146     params.setParameter("yTrans", 0.0F);
147     params.setParameter("interpolation", new InterpolationBilinear());
148     scaledImage = JAI.create("scale", params);
149   }
150
151   public int getWidth() {
152     return image.getWidth();
153   }
154
155   public int getHeight() {
156     return image.getHeight();
157   }
158
159   public int getScaledWidth() {
160     return scaledImage.getWidth();
161   }
162
163   public int getScaledHeight() {
164     return scaledImage.getHeight();
165   }
166
167   public void writeScaledData(OutputStream aStream) {
168     JAI.create("encode", scaledImage, aStream, imageType, null);
169   }
170
171   public byte[] getScaledData() {
172     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
173     writeScaledData(outputStream);
174     return outputStream.toByteArray();
175   }
176
177   public void writeScaledData(File aFile) throws IOException {
178     writeScaledData(new FileOutputStream(aFile));
179   }
180 }