view src/kryshen/tema/functions/ImageConverter.java @ 2:6c41a0b43e58

Tema 0.3 (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 19 Feb 2008 20:32:17 +0300
parents 548a93c24e55
children e9d13c7ffeb1
line source
1 /*
2 * Copyright 2006-2008 Mikhail Kryshen
3 *
4 * This file is part of Tema.
5 *
6 * Tema is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * Tema 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 Lesser General Public License for more details.
15 *
16 * You should have received a copy of the
17 * GNU Lesser General Public License along with Tema.
18 * If not, see <http://www.gnu.org/licenses/>.
19 *
20 * $Id: ImageConverter.java,v 1.13 2008/02/16 17:56:34 mikhail Exp $
21 */
23 package kryshen.tema.functions;
25 import java.util.*;
26 import java.io.*;
27 import java.awt.*;
28 import java.awt.image.*;
29 import java.awt.geom.AffineTransform;
30 import javax.imageio.*;
31 import javax.imageio.stream.*;
33 import kryshen.tema.*;
35 /**
36 * Convert images to specified format.
37 *
38 * @author Mikhail Kryshen
39 */
40 public class ImageConverter extends Function {
42 public static final Function CONVERT_IMAGE = new ImageConverter();
44 public int invoke(FunctionDataParser fdp, Writer out)
45 throws IOException, TemplateException {
47 String arg0 = fdp.getNextArg();
48 String arg1 = fdp.getNextArg();
49 String arg2 = fdp.getNextArg();
50 int arg3 = fdp.hasMoreData() ?
51 Integer.parseInt(fdp.getNextArg()) : -1;
52 int arg4 = fdp.hasMoreData() ?
53 Integer.parseInt(fdp.getNextArg()) : -1;
55 try {
56 convert
57 (fdp.createFile(arg0), /* source file */
58 fdp.createFile(arg1), /* dest file */
59 arg2, /* format */
60 arg3, /* max width */
61 arg4); /* max height */
62 } catch (Exception e) {
63 System.err.println(e);
64 return 0;
65 }
67 out.write(arg1);
68 return 1;
69 }
71 public static void convert(File source, File dest, String format,
72 int maxWidth, int maxHeight)
73 throws IOException, InterruptedException {
75 System.err.print("Converting image " + source + "... ");
77 if (source.lastModified() < dest.lastModified()) {
78 System.err.println(dest + " is up to date.");
79 return;
80 }
82 BufferedImage image = ImageIO.read(source);
84 //int type = image.getType();
85 //final int type = BufferedImage.TYPE_INT_RGB;
86 //ColorModel cm = image.getColorModel();
88 int w = image.getWidth(null);
89 int h = image.getHeight(null);
91 //boolean scale = false;
93 float scale = 1f;
95 if (maxWidth > 0 && w > maxWidth)
96 scale = (float)maxWidth / w;
98 if (maxHeight > 0 && h * scale > maxHeight)
99 scale = (float)maxHeight / h;
101 if (scale != 1f) {
102 w *= scale; h *= scale;
104 // ColorModel cm = image.getColorModel();
105 // boolean alphaPremultiplied = cm.isAlphaPremultiplied();
106 // WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
107 // BufferedImage scaled = new BufferedImage
108 // (cm, raster, alphaPremultiplied, null);
110 // BufferedImage scaled = new BufferedImage(w, h, image.getType());
113 Image scaled = image.getScaledInstance(w, h, Image.SCALE_SMOOTH);
115 ColorModel cm = image.getColorModel();
116 boolean alphaPremultiplied = image.isAlphaPremultiplied();
117 WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
118 image = new BufferedImage(cm, raster, alphaPremultiplied, null);
120 Graphics g = image.getGraphics();
121 g.drawImage(scaled, 0, 0, null);
122 g.dispose();
124 // image = scale(image, scaled, scale);
125 // Graphics2D g = scaled.createGraphics();
126 // g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
127 // RenderingHints.VALUE_INTERPOLATION_BILINEAR);
128 // System.err.print(" " + g.drawImage(image, 0, 0, w, h, null) + " ");
129 // g.dispose();
130 // image = scaled;
131 }
133 File parent = dest.getParentFile();
134 if (parent != null) parent.mkdirs();
136 ImageIO.write(image, format, dest);
137 System.err.println("saved " + dest + ".");
138 }
140 // public static ColorModel getColorModel(Image image)
141 // throws InterruptedException {
143 // PixelGrabber grabby = new PixelGrabber(image, 0, 0, 1, 1, false);
144 // if (!grabby.grabPixels())
145 // throw new RuntimeException("pixel grab fails");
146 // return grabby.getColorModel();
147 // }
149 // private static BufferedImage scale(BufferedImage image,
150 // BufferedImage dest,
151 // float scale) {
153 // AffineTransform tx = new AffineTransform();
154 // tx.scale(scale, scale);
156 // AffineTransformOp op = new AffineTransformOp
157 // (tx, AffineTransformOp.TYPE_BILINEAR);
159 // return op.filter(image, dest);
160 // }
162 // TEST
163 public static void main(String[] args)
164 throws IOException, InterruptedException {
166 convert(new File(args[0]), new File(args[1]), "png", 300, 300);
167 }
170 // public static BufferedImage toBufferedImage(Image image, ColorModel cm) {
171 // if (image instanceof BufferedImage)
172 // return (BufferedImage)image;
174 // int w = image.getWidth(null);
175 // int h = image.getHeight(null);
177 // boolean alphaPremultiplied = cm.isAlphaPremultiplied();
178 // WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
179 // BufferedImage result = new BufferedImage(cm, raster, alphaPremultiplied, null);
180 // Graphics2D g = result.createGraphics();
182 // g.drawImage(image, 0, 0, null);
183 // g.dispose();
185 // return result;
186 // }
187 }