2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\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
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
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
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
31 package mir.media.image;
\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
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
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
51 public class JAIImageProcessor implements ImageProcessor {
\r
52 static final LoggerWrapper logger = new LoggerWrapper("media.image.jai");
\r
54 private RenderedImage image;
\r
55 private RenderedImage scaledImage;
\r
57 public JAIImageProcessor(RenderedImage anImage) {
\r
59 scaledImage = anImage;
\r
62 public JAIImageProcessor(SeekableStream anImageStream) {
\r
63 RenderedImage tempImage = JAI.create("stream", anImageStream);
\r
64 ParameterBlockJAI params = new ParameterBlockJAI("format");
\r
69 params.addSource(tempImage);
\r
70 params.setParameter("dataType", DataBuffer.TYPE_BYTE);
\r
72 ImageLayout layout = new ImageLayout();
\r
73 nrComponents = tempImage.getColorModel().getNumColorComponents();
\r
75 bands = new int[nrComponents];
\r
76 for (int i=0; i<nrComponents; i++)
\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
85 nrComponents*tempImage.getWidth(),
\r
88 RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
\r
90 image = JAI.create("format", params, hints);
\r
92 scaledImage = image;
\r
95 public JAIImageProcessor(File aFile) throws IOException {
\r
96 this(new FileSeekableStream(aFile));
\r
99 public JAIImageProcessor(byte[] anImageData) throws IOException {
\r
100 this(new ByteArraySeekableStream(anImageData));
\r
103 public void descaleImage(int aMaxSize) throws MediaExc {
\r
104 descaleImage(aMaxSize, 0);
\r
107 public void descaleImage(int aMaxSize, float aMinDescale) throws MediaExc {
\r
108 descaleImage(aMaxSize, aMaxSize, aMinDescale, 0);
\r
111 public void descaleImage(int aMaxSize, int aMinResize) throws MediaExc {
\r
112 descaleImage(aMaxSize, aMaxSize, 0, aMinResize);
\r
115 public void descaleImage(int aMaxSize, float aMinDescale, int aMinResize) throws MediaExc {
\r
116 descaleImage(aMaxSize, aMaxSize, aMinDescale, aMinResize);
\r
122 public void descaleImage(int aMaxWidth, int aMaxHeight, float aMinDescale, int aMinResize) throws MediaExc {
\r
124 scaledImage = image;
\r
126 if ((aMaxWidth>0 && image.getWidth()>aMaxWidth+aMinResize-1) || (aMaxHeight>0 && image.getHeight()>aMaxHeight+aMinResize-1))
\r
128 logger.info("Scaling image");
\r
132 if (aMaxWidth>0 && image.getWidth()>aMaxWidth) {
\r
133 scale = Math.min(scale, (float) aMaxWidth / (float) image.getWidth());
\r
135 if (aMaxHeight>0 && image.getHeight()>aMaxHeight) {
\r
136 scale = Math.min(scale, (float) aMaxHeight / (float) image.getHeight());
\r
139 if (1-scale>aMinDescale) {
\r
145 public void scaleImage(float aScalingFactor) throws MediaExc {
\r
146 ParameterBlockJAI params = new ParameterBlockJAI("scale");
\r
147 params.addSource(image);
\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
157 public int getWidth() {
\r
158 return image.getWidth();
\r
161 public int getHeight() {
\r
162 return image.getHeight();
\r
165 public int getScaledWidth() {
\r
166 return scaledImage.getWidth();
\r
169 public int getScaledHeight() {
\r
170 return scaledImage.getHeight();
\r
173 public void writeScaledData(OutputStream aStream, String anImageType) {
\r
174 JAI.create("encode", scaledImage, aStream, anImageType, null);
\r
177 public byte[] getScaledData(String anImageType) {
\r
178 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
\r
179 writeScaledData(outputStream, anImageType);
\r
180 return outputStream.toByteArray();
\r
183 public void writeScaledData(File aFile, String anImageType) throws MediaExc {
\r
185 writeScaledData(new BufferedOutputStream(new FileOutputStream(aFile),8192), anImageType);
\r
187 catch (FileNotFoundException f) {
\r
188 throw new MediaFailure(f);
\r