40f152e6cf8a83dce2a14f8f2f9ead402728ed66
[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  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30
31 package mircoders.media;
32
33 import java.io.ByteArrayOutputStream;
34 import java.io.File;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.OutputStream;
38 import javax.media.jai.ImageLayout;
39 import javax.media.jai.InterpolationBilinear;
40 import javax.media.jai.JAI;
41 import javax.media.jai.ParameterBlockJAI;
42 import javax.media.jai.PlanarImage;
43
44 import java.awt.RenderingHints;
45 import java.awt.image.ColorModel;
46 import java.awt.image.DataBuffer;
47 import java.awt.image.PixelInterleavedSampleModel;
48
49 import com.sun.media.jai.codec.ByteArraySeekableStream;
50 import com.sun.media.jai.codec.FileSeekableStream;
51 import com.sun.media.jai.codec.SeekableStream;
52
53 import mir.log.LoggerWrapper;
54
55 /**
56  *
57  * <p>Title: Image processor</p>
58  * <p>Description: Temporary image processor class. (Made for the immediate needs of CMI brasil.
59  *                 Will become obsolete when mh's media handler rewrite is finished. </p>
60  * @author Zapata
61  * @version 1.0
62  */
63
64 public class ImageProcessor {
65   static final LoggerWrapper logger = new LoggerWrapper("media");
66
67   private PlanarImage image;
68   private PlanarImage scaledImage;
69
70   private byte[] iconData;
71   private byte[] imageData;
72   private int iconWidth;
73   private int iconHeight;
74
75   public ImageProcessor(SeekableStream anImageStream) throws IOException {
76     PlanarImage tempImage = JAI.create("stream", anImageStream);
77     ParameterBlockJAI params = new ParameterBlockJAI("format");
78     int bands[];
79     int nrComponents;
80
81
82     params.addSource(tempImage);
83     params.setParameter("dataType", DataBuffer.TYPE_BYTE);
84
85     ImageLayout layout = new ImageLayout();
86     nrComponents = tempImage.getColorModel().getNumColorComponents();
87
88     bands = new int[nrComponents];
89     for (int i=0; i<nrComponents; i++)
90       bands[i]=i;
91
92     layout.setColorModel(ColorModel.getRGBdefault());
93     layout.setSampleModel(
94         new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,
95         tempImage.getWidth(),
96         tempImage.getHeight(),
97         nrComponents,
98         nrComponents*tempImage.getWidth(),
99         bands));
100
101     RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
102
103     image = JAI.create("format", params, hints);
104
105     scaledImage = image;
106   }
107
108   public ImageProcessor(File aFile) throws IOException {
109     this(new FileSeekableStream(aFile));
110   }
111
112   public ImageProcessor(byte[] anImageData) throws IOException {
113     this(new ByteArraySeekableStream(anImageData));
114   }
115
116   public void descaleImage(int aMaxSize) throws java.io.IOException {
117     descaleImage(aMaxSize, 0);
118   }
119
120   public void descaleImage(int aMaxSize, float aMinDescale) throws java.io.IOException {
121     descaleImage(aMaxSize, aMaxSize, aMinDescale, 0);
122   }
123
124   public void descaleImage(int aMaxSize, int aMinResize) throws java.io.IOException {
125     descaleImage(aMaxSize, aMaxSize, 0, aMinResize);
126   }
127
128   public void descaleImage(int aMaxSize, float aMinDescale, int aMinResize) throws java.io.IOException {
129     descaleImage(aMaxSize, aMaxSize, aMinDescale, aMinResize);
130   }
131
132   /**
133    *
134    * Resizes an image to fit inside <code>aMaxWidth</code> and <code>aMaxHeight</code>, provided
135    *    this requires at least <code>aMinResize</code> pixels will be removed from either the width or
136    *    the height
137    *
138    * @param aMaxWidth
139    * @param aMaxHeight
140    * @param aMinDescale
141    * @param aMinResize
142    * @throws java.io.IOException
143    */
144   public void descaleImage(int aMaxWidth, int aMaxHeight, float aMinDescale, int aMinResize) throws java.io.IOException {
145     float scale;
146     scaledImage = image;
147
148     if ((aMaxWidth>0 && image.getWidth()>aMaxWidth+aMinResize-1) || (aMaxHeight>0 && image.getHeight()>aMaxHeight+aMinResize-1))
149     {
150       logger.info("Scaling image");
151
152       scale=1;
153
154       if (aMaxWidth>0 && image.getWidth()>aMaxWidth) {
155         scale = Math.min(scale, (float) aMaxWidth / (float) image.getWidth());
156       }
157       if (aMaxHeight>0 && image.getHeight()>aMaxHeight) {
158         scale = Math.min(scale, (float) aMaxHeight / (float) image.getHeight());
159       }
160
161       if (1-scale>aMinDescale) {
162         scaleImage(scale);
163       }
164     }
165   }
166
167   public void scaleImage(float aScalingFactor) throws java.io.IOException {
168     ParameterBlockJAI params = new ParameterBlockJAI("scale");
169     params.addSource(image);
170
171     params.setParameter("xScale", aScalingFactor);
172     params.setParameter("yScale", aScalingFactor);
173     params.setParameter("xTrans", 0.0F);
174     params.setParameter("yTrans", 0.0F);
175     params.setParameter("interpolation", new InterpolationBilinear());
176     scaledImage = JAI.create("scale", params);
177   }
178
179   public int getWidth() {
180     return image.getWidth();
181   }
182
183   public int getHeight() {
184     return image.getHeight();
185   }
186
187   public int getScaledWidth() {
188     return scaledImage.getWidth();
189   }
190
191   public int getScaledHeight() {
192     return scaledImage.getHeight();
193   }
194
195   public void writeScaledData(OutputStream aStream, String anImageType) {
196     JAI.create("encode", scaledImage, aStream, anImageType, null);
197   }
198
199   public byte[] getScaledData(String anImageType) {
200     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
201     writeScaledData(outputStream, anImageType);
202     return outputStream.toByteArray();
203   }
204
205   public void writeScaledData(File aFile, String anImageType) throws IOException {
206     writeScaledData(new FileOutputStream(aFile), anImageType);
207   }
208 }
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228