view src/kryshen/tema/functions/ImageConverter.java @ 1:548a93c24e55

Tema 0.1jk - Javakonkurs edition (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Thu, 14 Dec 2006 23:22:05 +0300
parents
children 6c41a0b43e58
line source
1 /*
2 * Copyright (C) 2005, 2006 Mikhail A. Kryshen
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * $Id: ImageConverter.java,v 1.4 2006/12/14 19:44:32 mikhail Exp $
19 */
21 package kryshen.tema.functions;
23 import java.util.*;
24 import java.io.*;
25 import java.awt.*;
26 import java.awt.image.*;
27 import java.awt.geom.AffineTransform;
28 import javax.imageio.*;
29 import javax.imageio.stream.*;
31 import kryshen.tema.*;
33 /**
34 * Convert images to specified format.
35 *
36 * @author Mikhail A. Kryshen
37 */
38 public class ImageConverter extends Function {
40 public static final Function IMAGE = new ImageConverter();
42 public int invoke(FunctionDataParser fdp, Writer out)
43 throws IOException, TemplateException {
45 String arg0 = fdp.getNextArg();
46 String arg1 = fdp.getNextArg();
47 String arg2 = fdp.getNextArg();
48 int arg3 = fdp.hasMoreData() ?
49 Integer.parseInt(fdp.getNextArg()) : -1;
50 int arg4 = fdp.hasMoreData() ?
51 Integer.parseInt(fdp.getNextArg()) : -1;
53 File source = new File
54 (Tema.getProperty("resource_base"), arg0);
56 try {
57 convert
58 (source, /* source file */
59 new File(arg1), /* dest file */
60 arg2, /* format */
61 arg3, /* max width */
62 arg4); /* max height */
63 } catch (Exception e) {
64 System.err.println(e);
65 return 0;
66 }
68 out.write(arg1);
69 return 1;
70 }
72 public static void convert(File source, File dest, String format,
73 int maxWidth, int maxHeight)
74 throws IOException, InterruptedException {
76 System.err.print("Converting image " + source + "... ");
78 if (source.lastModified() < dest.lastModified()) {
79 System.err.println(dest + " is up to date.");
80 return;
81 }
83 BufferedImage image = ImageIO.read(source);
85 //int type = image.getType();
86 //final int type = BufferedImage.TYPE_INT_RGB;
87 //ColorModel cm = image.getColorModel();
89 int w = image.getWidth(null);
90 int h = image.getHeight(null);
92 //boolean scale = false;
94 float scale = 1f;
96 if (maxWidth > 0 && w > maxWidth)
97 scale = (float)maxWidth / w;
99 if (maxHeight > 0 && h * scale > maxHeight)
100 scale = (float)maxHeight / h;
102 if (scale != 1f) {
103 w *= scale; h *= scale;
105 // ColorModel cm = image.getColorModel();
106 // boolean alphaPremultiplied = cm.isAlphaPremultiplied();
107 // WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
108 // BufferedImage scaled = new BufferedImage
109 // (cm, raster, alphaPremultiplied, null);
111 // BufferedImage scaled = new BufferedImage(w, h, image.getType());
114 Image scaled = image.getScaledInstance(w, h, Image.SCALE_SMOOTH);
116 ColorModel cm = image.getColorModel();
117 boolean alphaPremultiplied = image.isAlphaPremultiplied();
118 WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
119 image = new BufferedImage(cm, raster, alphaPremultiplied, null);
121 Graphics g = image.getGraphics();
122 g.drawImage(scaled, 0, 0, null);
123 g.dispose();
125 // image = scale(image, scaled, scale);
126 // Graphics2D g = scaled.createGraphics();
127 // g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
128 // RenderingHints.VALUE_INTERPOLATION_BILINEAR);
129 // System.err.print(" " + g.drawImage(image, 0, 0, w, h, null) + " ");
130 // g.dispose();
131 // image = scaled;
132 }
134 File parent = dest.getParentFile();
135 if (parent != null) parent.mkdirs();
137 ImageIO.write(image, format, dest);
138 System.err.println("saved " + dest + ".");
139 }
141 // public static ColorModel getColorModel(Image image)
142 // throws InterruptedException {
144 // PixelGrabber grabby = new PixelGrabber(image, 0, 0, 1, 1, false);
145 // if (!grabby.grabPixels())
146 // throw new RuntimeException("pixel grab fails");
147 // return grabby.getColorModel();
148 // }
150 // private static BufferedImage scale(BufferedImage image,
151 // BufferedImage dest,
152 // float scale) {
154 // AffineTransform tx = new AffineTransform();
155 // tx.scale(scale, scale);
157 // AffineTransformOp op = new AffineTransformOp
158 // (tx, AffineTransformOp.TYPE_BILINEAR);
160 // return op.filter(image, dest);
161 // }
163 // TEST
164 public static void main(String[] args)
165 throws IOException, InterruptedException {
167 convert(new File(args[0]), new File(args[1]), "png", 300, 300);
168 }
171 // public static BufferedImage toBufferedImage(Image image, ColorModel cm) {
172 // if (image instanceof BufferedImage)
173 // return (BufferedImage)image;
175 // int w = image.getWidth(null);
176 // int h = image.getHeight(null);
178 // boolean alphaPremultiplied = cm.isAlphaPremultiplied();
179 // WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
180 // BufferedImage result = new BufferedImage(cm, raster, alphaPremultiplied, null);
181 // Graphics2D g = result.createGraphics();
183 // g.drawImage(image, 0, 0, null);
184 // g.dispose();
186 // return result;
187 // }
188 }