/
ImageUtils.java
281 lines (228 loc) · 7.59 KB
/
ImageUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Gallery Remote - a File Upload Utility for Gallery
*
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2001 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package com.gallery.GalleryRemote;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
/**
* Interface to common image manipulation routines
*
*@author paour
*@created September 1, 2002
*/
public class ImageUtils {
public static final String MODULE = "ImageUtils";
static Vector toDelete = new Vector();
static long totalTime = 0;
static int totalIter = 0;
static boolean useIM = false;
static String imPath = null;
static File tmpDir = null;
public static final int THUMB = 0;
public static final int PREVIEW = 1;
public static final int UPLOAD = 2;
static String[] filterName = new String[3];
static String[] format = new String[3];
/**
* Perform the actual icon loading
*
*@param filename path to the file
*@return Resized icon
*/
public static ImageIcon load( String filename, Dimension d, int usage ) {
ImageIcon r = null;
long start = System.currentTimeMillis();
if (useIM) {
try {
StringBuffer cmdline = new StringBuffer(imPath);
cmdline.append(" -size ");
cmdline.append(d.width);
cmdline.append("x");
cmdline.append(d.height);
if (filterName[usage] != null && filterName[usage].length() > 0) {
cmdline.append(" -filter ");
cmdline.append(filterName[usage]);
}
cmdline.append(" \"");
cmdline.append(filename);
cmdline.append("\" -resize ");
cmdline.append(d.width);
cmdline.append("x");
cmdline.append(d.height);
cmdline.append(" +profile \"*\" ");
File temp = File.createTempFile("thumb", "." + format[usage], tmpDir);
toDelete.add(temp);
cmdline.append(temp.getPath());
Log.log(Log.TRACE, MODULE, "Executing " + cmdline.toString());
Process p = Runtime.getRuntime().exec(cmdline.toString());
p.waitFor();
Log.log(Log.TRACE, MODULE, "Returned with value " + p.exitValue());
if (p.exitValue() != 0) {
Log.log(Log.CRITICAL, MODULE, "ImageMagick doesn't seem to be working. Disabling");
useIM = false;
} else {
r = new ImageIcon(temp.getPath());
}
} catch (IOException e1) {
Log.logException(Log.ERROR, MODULE, e1);
} catch (InterruptedException e2) {
Log.logException(Log.ERROR, MODULE, e2);
}
}
if ( ! useIM && r == null ) {
r = new ImageIcon( filename );
Image scaled = null;
Dimension newD = getSizeKeepRatio(
new Dimension( r.getIconWidth(), r.getIconHeight() ),
d );
scaled = r.getImage().getScaledInstance( newD.width, newD.height, Image.SCALE_FAST );
r.getImage().flush();
r.setImage( scaled );
}
long time = System.currentTimeMillis() - start;
totalTime += time;
totalIter++;
Log.log(Log.TRACE, MODULE, "Time: " + time + " - Avg: " + (totalTime/totalIter) );
return r;
}
public static File resize( String filename, Dimension d ) {
File r = null;
long start = System.currentTimeMillis();
if (useIM) {
try {
StringBuffer cmdline = new StringBuffer(imPath);
cmdline.append(" -size ");
cmdline.append(d.width);
cmdline.append("x");
cmdline.append(d.height);
if (filterName[UPLOAD] != null && filterName[UPLOAD].length() > 0) {
cmdline.append(" -filter ");
cmdline.append(filterName[UPLOAD]);
}
cmdline.append(" \"");
cmdline.append(filename);
cmdline.append("\" -resize \"");
cmdline.append(d.width);
cmdline.append("x");
cmdline.append(d.height);
cmdline.append(">\" ");
//cmdline.append("-gravity SouthEast -draw \"image Over 200,200 0,0 G:\\Projects\\Dev\\gallery_remote10\\2ni.png\" ");
r = File.createTempFile("res"
, "." + GalleryFileFilter.getExtension(filename), tmpDir);
toDelete.add(r);
cmdline.append(r.getPath());
Log.log(Log.TRACE, MODULE, "Executing " + cmdline.toString());
Process p = Runtime.getRuntime().exec(cmdline.toString());
p.waitFor();
Log.log(Log.TRACE, MODULE, "Returned with value " + p.exitValue());
if (p.exitValue() != 0) {
Log.log(Log.CRITICAL, MODULE, "ImageMagick doesn't seem to be working. Disabling");
useIM = false;
r = null;
}
} catch (IOException e1) {
Log.logException(Log.ERROR, MODULE, e1);
} catch (InterruptedException e2) {
Log.logException(Log.ERROR, MODULE, e2);
}
}
if ( ! useIM && r == null ) {
throw new UnsupportedOperationException("IM must be installed for this operation");
}
long time = System.currentTimeMillis() - start;
totalTime += time;
totalIter++;
Log.log(Log.TRACE, MODULE, "Time: " + time + " - Avg: " + (totalTime/totalIter) );
return r;
}
static {
tmpDir = new File(System.getProperty("java.io.tmpdir") + "thumbs");
if (!tmpDir.exists()) {
tmpDir.mkdirs();
}
Log.log(Log.INFO, MODULE, "tmpDir: " + tmpDir.getPath());
try {
PropertiesFile p = new PropertiesFile("imagemagick/im");
useIM = p.getBooleanProperty("enabled");
Log.log(Log.INFO, MODULE, "useIM: " + useIM);
if (useIM) {
imPath = p.getProperty("imConvertPath");
Log.log(Log.INFO, MODULE, "imPath: " + imPath);
if (! new File(imPath).exists()) {
Log.log(Log.CRITICAL, MODULE, "Can't find ImageMagick Convert at the above path");
useIM = false;
}
}
if (useIM) {
filterName[THUMB] = p.getProperty("imThumbnailResizeFilter");
filterName[PREVIEW] = p.getProperty("imPreviewResizeFilter");
filterName[UPLOAD] = p.getProperty("imUploadResizeFilter");
if ( filterName[UPLOAD] == null ) {
filterName[UPLOAD] = filterName[PREVIEW];
}
format[THUMB] = p.getProperty("imThumbnailResizeFormat", "gif");
format[PREVIEW] = p.getProperty("imPreviewResizeFormat", "jpg");
format[UPLOAD] = null;
}
} catch (Exception e) {
Log.logException(Log.CRITICAL, MODULE, e);
useIM = false;
}
}
public static void purgeTemp()
{
Enumeration e = toDelete.elements();
while (e.hasMoreElements()) {
((File) e.nextElement()).delete();
}
}
public static Dimension getSizeKeepRatio(Dimension source, Dimension target)
{
Dimension result = new Dimension();
float sourceRatio = (float) source.width / source.height;
float targetRatio = (float) target.width / target.height;
if (targetRatio > sourceRatio)
{
result.height = target.height;
result.width = (int) source.width * target.height / source.height;
}
else
{
result.width = target.width;
result.height = (int) source.height * target.width / source.width;
}
return result;
}
public static float getRatio(Dimension source, Dimension target)
{
float widthRatio = (float) target.width / source.width;
float heightRatio = (float) target.height / source.height;
if (heightRatio > widthRatio)
{
return widthRatio;
}
else
{
return heightRatio;
}
}
}