2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
31 package mircoders.media;
33 import java.io.ByteArrayOutputStream;
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;
44 import java.awt.RenderingHints;
45 import java.awt.image.ColorModel;
46 import java.awt.image.DataBuffer;
47 import java.awt.image.PixelInterleavedSampleModel;
49 import com.sun.media.jai.codec.ByteArraySeekableStream;
50 import com.sun.media.jai.codec.FileSeekableStream;
51 import com.sun.media.jai.codec.SeekableStream;
53 import mir.log.LoggerWrapper;
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>
64 public class ImageProcessor {
65 static final LoggerWrapper logger = new LoggerWrapper("media");
67 private PlanarImage image;
68 private PlanarImage scaledImage;
70 private byte[] iconData;
71 private byte[] imageData;
72 private int iconWidth;
73 private int iconHeight;
75 public ImageProcessor(SeekableStream anImageStream) throws IOException {
76 PlanarImage tempImage = JAI.create("stream", anImageStream);
77 ParameterBlockJAI params = new ParameterBlockJAI("format");
82 params.addSource(tempImage);
83 params.setParameter("dataType", DataBuffer.TYPE_BYTE);
85 ImageLayout layout = new ImageLayout();
86 nrComponents = tempImage.getColorModel().getNumColorComponents();
88 bands = new int[nrComponents];
89 for (int i=0; i<nrComponents; i++)
92 layout.setColorModel(ColorModel.getRGBdefault());
93 layout.setSampleModel(
94 new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,
96 tempImage.getHeight(),
98 nrComponents*tempImage.getWidth(),
101 RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
103 image = JAI.create("format", params, hints);
108 public ImageProcessor(File aFile) throws IOException {
109 this(new FileSeekableStream(aFile));
112 public ImageProcessor(byte[] anImageData) throws IOException {
113 this(new ByteArraySeekableStream(anImageData));
116 public void descaleImage(int aMaxSize) throws java.io.IOException {
117 descaleImage(aMaxSize, 0);
120 public void descaleImage(int aMaxSize, float aMinDescale) throws java.io.IOException {
121 descaleImage(aMaxSize, aMaxSize, aMinDescale, 0);
124 public void descaleImage(int aMaxSize, int aMinResize) throws java.io.IOException {
125 descaleImage(aMaxSize, aMaxSize, 0, aMinResize);
128 public void descaleImage(int aMaxSize, float aMinDescale, int aMinResize) throws java.io.IOException {
129 descaleImage(aMaxSize, aMaxSize, aMinDescale, aMinResize);
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
142 * @throws java.io.IOException
144 public void descaleImage(int aMaxWidth, int aMaxHeight, float aMinDescale, int aMinResize) throws java.io.IOException {
148 if ((aMaxWidth>0 && image.getWidth()>aMaxWidth+aMinResize-1) || (aMaxHeight>0 && image.getHeight()>aMaxHeight+aMinResize-1))
150 logger.info("Scaling image");
154 if (aMaxWidth>0 && image.getWidth()>aMaxWidth) {
155 scale = Math.min(scale, (float) aMaxWidth / (float) image.getWidth());
157 if (aMaxHeight>0 && image.getHeight()>aMaxHeight) {
158 scale = Math.min(scale, (float) aMaxHeight / (float) image.getHeight());
161 if (1-scale>aMinDescale) {
167 public void scaleImage(float aScalingFactor) throws java.io.IOException {
168 ParameterBlockJAI params = new ParameterBlockJAI("scale");
169 params.addSource(image);
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);
179 public int getWidth() {
180 return image.getWidth();
183 public int getHeight() {
184 return image.getHeight();
187 public int getScaledWidth() {
188 return scaledImage.getWidth();
191 public int getScaledHeight() {
192 return scaledImage.getHeight();
195 public void writeScaledData(OutputStream aStream, String anImageType) {
196 JAI.create("encode", scaledImage, aStream, anImageType, null);
199 public byte[] getScaledData(String anImageType) {
200 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
201 writeScaledData(outputStream, anImageType);
202 return outputStream.toByteArray();
205 public void writeScaledData(File aFile, String anImageType) throws IOException {
206 writeScaledData(new FileOutputStream(aFile), anImageType);