986a3f30fbb498a6405bb11e198c107d55e7117f
[mir.git] / source / mir / media / image / JAIImageProcessor.java
1 /*\r
2  * Copyright (C) 2001, 2002 The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with  any library licensed under the Apache Software License,\r
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
23  * (or with modified versions of the above that use the same license as the above),\r
24  * and distribute linked combinations including the two.  You must obey the\r
25  * GNU General Public License in all respects for all of the code used other than\r
26  * the above mentioned libraries.  If you modify this file, you may extend this\r
27  * exception to your version of the file, but you are not obligated to do so.\r
28  * If you do not wish to do so, delete this exception statement from your version.\r
29  */\r
30 \r
31 package mir.media.image;\r
32 \r
33 import com.sun.media.jai.codec.ByteArraySeekableStream;\r
34 import com.sun.media.jai.codec.FileSeekableStream;\r
35 import com.sun.media.jai.codec.SeekableStream;\r
36 import mir.log.LoggerWrapper;\r
37 import mir.media.MediaExc;\r
38 import mir.media.MediaFailure;\r
39 \r
40 import javax.media.jai.ImageLayout;\r
41 import javax.media.jai.InterpolationBilinear;\r
42 import javax.media.jai.JAI;\r
43 import javax.media.jai.ParameterBlockJAI;\r
44 import java.awt.*;\r
45 import java.awt.image.ColorModel;\r
46 import java.awt.image.DataBuffer;\r
47 import java.awt.image.PixelInterleavedSampleModel;\r
48 import java.awt.image.RenderedImage;\r
49 import java.io.*;\r
50 \r
51 public class JAIImageProcessor implements ImageProcessor {\r
52   static final LoggerWrapper logger = new LoggerWrapper("media.image.jai");\r
53 \r
54   private RenderedImage image;\r
55   private RenderedImage scaledImage;\r
56 \r
57   public JAIImageProcessor(RenderedImage anImage) {\r
58     image = anImage;\r
59     scaledImage = anImage;\r
60   }\r
61 \r
62   public JAIImageProcessor(SeekableStream anImageStream) {\r
63     RenderedImage tempImage = JAI.create("stream", anImageStream);\r
64     ParameterBlockJAI params = new ParameterBlockJAI("format");\r
65     int bands[];\r
66     int nrComponents;\r
67 \r
68 \r
69     params.addSource(tempImage);\r
70     params.setParameter("dataType", DataBuffer.TYPE_BYTE);\r
71 \r
72     ImageLayout layout = new ImageLayout();\r
73     nrComponents = tempImage.getColorModel().getNumColorComponents();\r
74 \r
75     bands = new int[nrComponents];\r
76     for (int i=0; i<nrComponents; i++)\r
77       bands[i]=i;\r
78 \r
79     layout.setColorModel(ColorModel.getRGBdefault());\r
80     layout.setSampleModel(\r
81         new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,\r
82         tempImage.getWidth(),\r
83         tempImage.getHeight(),\r
84         nrComponents,\r
85         nrComponents*tempImage.getWidth(),\r
86         bands));\r
87 \r
88     RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);\r
89 \r
90     image = JAI.create("format", params, hints);\r
91 \r
92     scaledImage = image;\r
93   }\r
94 \r
95   public JAIImageProcessor(File aFile) throws IOException {\r
96     this(new FileSeekableStream(aFile));\r
97   }\r
98 \r
99   public JAIImageProcessor(byte[] anImageData) throws IOException {\r
100     this(new ByteArraySeekableStream(anImageData));\r
101   }\r
102 \r
103   public void descaleImage(int aMaxSize) throws MediaExc {\r
104     descaleImage(aMaxSize, 0);\r
105   }\r
106 \r
107   public void descaleImage(int aMaxSize, float aMinDescale) throws MediaExc {\r
108     descaleImage(aMaxSize, aMaxSize, aMinDescale, 0);\r
109   }\r
110 \r
111   public void descaleImage(int aMaxSize, int aMinResize) throws MediaExc {\r
112     descaleImage(aMaxSize, aMaxSize, 0, aMinResize);\r
113   }\r
114 \r
115   public void descaleImage(int aMaxSize, float aMinDescale, int aMinResize) throws MediaExc {\r
116     descaleImage(aMaxSize, aMaxSize, aMinDescale, aMinResize);\r
117   }\r
118 \r
119   /**\r
120    * {@inheritDoc}\r
121    */\r
122   public void descaleImage(int aMaxWidth, int aMaxHeight, float aMinDescale, int aMinResize) throws MediaExc {\r
123     float scale;\r
124     scaledImage = image;\r
125 \r
126     if ((aMaxWidth>0 && image.getWidth()>aMaxWidth+aMinResize-1) || (aMaxHeight>0 && image.getHeight()>aMaxHeight+aMinResize-1))\r
127     {\r
128       logger.info("Scaling image");\r
129 \r
130       scale=1;\r
131 \r
132       if (aMaxWidth>0 && image.getWidth()>aMaxWidth) {\r
133         scale = Math.min(scale, (float) aMaxWidth / (float) image.getWidth());\r
134       }\r
135       if (aMaxHeight>0 && image.getHeight()>aMaxHeight) {\r
136         scale = Math.min(scale, (float) aMaxHeight / (float) image.getHeight());\r
137       }\r
138 \r
139       if (1-scale>aMinDescale) {\r
140         scaleImage(scale);\r
141       }\r
142     }\r
143   }\r
144 \r
145   public void scaleImage(float aScalingFactor) throws MediaExc {\r
146     ParameterBlockJAI params = new ParameterBlockJAI("scale");\r
147     params.addSource(image);\r
148 \r
149     params.setParameter("xScale", aScalingFactor);\r
150     params.setParameter("yScale", aScalingFactor);\r
151     params.setParameter("xTrans", 0.0F);\r
152     params.setParameter("yTrans", 0.0F);\r
153     params.setParameter("interpolation", new InterpolationBilinear());\r
154     scaledImage = JAI.create("scale", params);\r
155   }\r
156 \r
157   public int getWidth() {\r
158     return image.getWidth();\r
159   }\r
160 \r
161   public int getHeight() {\r
162     return image.getHeight();\r
163   }\r
164 \r
165   public int getScaledWidth() {\r
166     return scaledImage.getWidth();\r
167   }\r
168 \r
169   public int getScaledHeight() {\r
170     return scaledImage.getHeight();\r
171   }\r
172 \r
173   public void writeScaledData(OutputStream aStream, String anImageType) {\r
174     JAI.create("encode", scaledImage, aStream, anImageType, null);\r
175   }\r
176 \r
177   public byte[] getScaledData(String anImageType) {\r
178     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();\r
179     writeScaledData(outputStream, anImageType);\r
180     return outputStream.toByteArray();\r
181   }\r
182 \r
183   public void writeScaledData(File aFile, String anImageType) throws MediaExc {\r
184     try {\r
185       writeScaledData(new BufferedOutputStream(new FileOutputStream(aFile),8192), anImageType);\r
186     }\r
187     catch (FileNotFoundException f) {\r
188       throw new MediaFailure(f);\r
189     }\r
190   }\r
191 }\r
192 \r
193 \r
194 \r
195 \r
196 \r
197 \r
198 \r
199 \r
200 \r
201 \r
202 \r
203 \r
204 \r
205 \r
206 \r
207 \r
208 \r
209 \r
210 \r
211 \r