view src/kryshen/tema/functions/ImageConverter.java @ 15:e9d13c7ffeb1

Update header comment.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Mar 2009 18:51:47 +0300
parents 6c41a0b43e58
children
line source
1 /*
2 * Copyright 2006-2009 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 */
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 Kryshen
37 */
38 public class ImageConverter extends Function {
40 public static final Function CONVERT_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 try {
54 convert
55 (fdp.createFile(arg0), /* source file */
56 fdp.createFile(arg1), /* dest file */
57 arg2, /* format */
58 arg3, /* max width */
59 arg4); /* max height */
60 } catch (Exception e) {
61 System.err.println(e);
62 return 0;
63 }
65 out.write(arg1);
66 return 1;
67 }
69 public static void convert(File source, File dest, String format,
70 int maxWidth, int maxHeight)
71 throws IOException, InterruptedException {
73 System.err.print("Converting image " + source + "... ");
75 if (source.lastModified() < dest.lastModified()) {
76 System.err.println(dest + " is up to date.");
77 return;
78 }
80 BufferedImage image = ImageIO.read(source);
82 //int type = image.getType();
83 //final int type = BufferedImage.TYPE_INT_RGB;
84 //ColorModel cm = image.getColorModel();
86 int w = image.getWidth(null);
87 int h = image.getHeight(null);
89 //boolean scale = false;
91 float scale = 1f;
93 if (maxWidth > 0 && w > maxWidth)
94 scale = (float)maxWidth / w;
96 if (maxHeight > 0 && h * scale > maxHeight)
97 scale = (float)maxHeight / h;
99 if (scale != 1f) {
100 w *= scale; h *= scale;
102 // ColorModel cm = image.getColorModel();
103 // boolean alphaPremultiplied = cm.isAlphaPremultiplied();
104 // WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
105 // BufferedImage scaled = new BufferedImage
106 // (cm, raster, alphaPremultiplied, null);
108 // BufferedImage scaled = new BufferedImage(w, h, image.getType());
111 Image scaled = image.getScaledInstance(w, h, Image.SCALE_SMOOTH);
113 ColorModel cm = image.getColorModel();
114 boolean alphaPremultiplied = image.isAlphaPremultiplied();
115 WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
116 image = new BufferedImage(cm, raster, alphaPremultiplied, null);
118 Graphics g = image.getGraphics();
119 g.drawImage(scaled, 0, 0, null);
120 g.dispose();
122 // image = scale(image, scaled, scale);
123 // Graphics2D g = scaled.createGraphics();
124 // g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
125 // RenderingHints.VALUE_INTERPOLATION_BILINEAR);
126 // System.err.print(" " + g.drawImage(image, 0, 0, w, h, null) + " ");
127 // g.dispose();
128 // image = scaled;
129 }
131 File parent = dest.getParentFile();
132 if (parent != null) parent.mkdirs();
134 ImageIO.write(image, format, dest);
135 System.err.println("saved " + dest + ".");
136 }
138 // public static ColorModel getColorModel(Image image)
139 // throws InterruptedException {
141 // PixelGrabber grabby = new PixelGrabber(image, 0, 0, 1, 1, false);
142 // if (!grabby.grabPixels())
143 // throw new RuntimeException("pixel grab fails");
144 // return grabby.getColorModel();
145 // }
147 // private static BufferedImage scale(BufferedImage image,
148 // BufferedImage dest,
149 // float scale) {
151 // AffineTransform tx = new AffineTransform();
152 // tx.scale(scale, scale);
154 // AffineTransformOp op = new AffineTransformOp
155 // (tx, AffineTransformOp.TYPE_BILINEAR);
157 // return op.filter(image, dest);
158 // }
160 // TEST
161 public static void main(String[] args)
162 throws IOException, InterruptedException {
164 convert(new File(args[0]), new File(args[1]), "png", 300, 300);
165 }
168 // public static BufferedImage toBufferedImage(Image image, ColorModel cm) {
169 // if (image instanceof BufferedImage)
170 // return (BufferedImage)image;
172 // int w = image.getWidth(null);
173 // int h = image.getHeight(null);
175 // boolean alphaPremultiplied = cm.isAlphaPremultiplied();
176 // WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
177 // BufferedImage result = new BufferedImage(cm, raster, alphaPremultiplied, null);
178 // Graphics2D g = result.createGraphics();
180 // g.drawImage(image, 0, 0, null);
181 // g.dispose();
183 // return result;
184 // }
185 }