ImageHelper.java
11.8 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.image.Layer
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.commons.io.FileUtils
* org.apache.commons.io.IOUtils
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.crx.packaging.gfx;
import com.day.image.Layer;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ImageHelper {
private static final Logger log = LoggerFactory.getLogger(ImageHelper.class);
private static Map<String, String> typeFromExt = new HashMap<String, String>();
private static Map<String, String> extFromType = new HashMap<String, String>();
public static Rectangle getCropRect(String rectCSV, String path) {
block4 : {
if (rectCSV != null && rectCSV.length() > 0) {
try {
int ratioPos = rectCSV.indexOf("/");
if (ratioPos >= 0) {
rectCSV = rectCSV.substring(0, ratioPos);
}
String[] cords = rectCSV.split(",");
int x1 = Integer.parseInt(cords[0]);
int y1 = Integer.parseInt(cords[1]);
int x2 = Integer.parseInt(cords[2]);
int y2 = Integer.parseInt(cords[3]);
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
}
catch (Exception e) {
if (path == null) break block4;
log.warn("cropRect at {} is not valid: {}", (Object)path, (Object)e.toString());
}
}
}
return null;
}
public static Layer createLayer(Node node, String imageName, String refName) throws RepositoryException, IOException {
if (node.hasNode(imageName)) {
return ImageHelper.createLayer((Item)node.getNode(imageName));
}
String imageRef = node.hasProperty(refName) ? node.getProperty(refName).getString() : "";
return ImageHelper.createLayer(node.getSession(), imageRef);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static Layer createLayer(ClassLoader cl, String resource) {
InputStream in = null;
try {
in = cl.getResourceAsStream(resource);
Layer layer = new Layer(in);
return layer;
}
catch (IOException e) {
log.error("Error while creating layer for {}", (Object)resource);
Layer layer = null;
return layer;
}
finally {
IOUtils.closeQuietly((InputStream)in);
}
}
public static Layer createLayer(Session session, String path) throws RepositoryException, IOException {
if (path.length() > 0 && session.itemExists(path)) {
return ImageHelper.createLayer(session.getItem(path));
}
return null;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static Layer createLayer(Item item) throws RepositoryException, IOException {
Node node;
InputStream in = null;
if (item.isNode()) {
node = (Node)item;
if (node.hasNode("{http://www.jcp.org/jcr/1.0}content")) {
node = node.getNode("{http://www.jcp.org/jcr/1.0}content");
}
if (node.hasProperty("{http://www.jcp.org/jcr/1.0}data")) {
in = node.getProperty("{http://www.jcp.org/jcr/1.0}data").getStream();
}
} else {
in = ((Property)item).getStream();
}
if (in == null) {
node = null;
return node;
}
Layer layer = new Layer(in);
if (layer.getImage() == null) {
layer = null;
}
Layer e = layer;
return e;
finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {}
}
}
}
public static /* varargs */ int parseFontStyle(String ... styles) {
int style = 0;
for (String s : styles) {
for (String st : s.split("[, |]+")) {
if ("bold".equals(st = st.toLowerCase())) {
style |= 1;
continue;
}
if ("italic".equals(st)) {
style |= 2;
continue;
}
if ("underline".equals(st)) {
style |= 4;
continue;
}
if (!"strikeout".equals(st)) continue;
style |= 8;
}
}
return style;
}
public static Layer resize(Layer layer, Dimension d, Dimension min, Dimension max) {
int maxHeight;
int width = d == null ? 0 : (int)d.getWidth();
int height = d == null ? 0 : (int)d.getHeight();
int minWidth = min == null ? 0 : (int)min.getWidth();
int minHeight = min == null ? 0 : (int)min.getHeight();
int maxWidth = max == null ? 0 : (int)max.getWidth();
int n = maxHeight = max == null ? 0 : (int)max.getHeight();
if (maxWidth == 0) {
maxWidth = Integer.MAX_VALUE;
}
if (maxHeight == 0) {
maxHeight = Integer.MAX_VALUE;
}
int ratioW = layer.getWidth();
int ratioH = layer.getHeight();
int loopProtect = 32;
while (loopProtect-- > 0) {
if (width == 0 && height == 0) {
width = layer.getWidth();
height = layer.getHeight();
continue;
}
if (width == 0) {
if (height < minHeight) {
height = minHeight;
} else if (height > maxHeight) {
height = maxHeight;
}
width = height * ratioW / ratioH;
continue;
}
if (height == 0) {
if (width < minWidth) {
width = minWidth;
} else if (width > maxWidth) {
width = maxWidth;
}
height = width * ratioH / ratioW;
continue;
}
ratioW = width;
ratioH = height;
if (width < minWidth) {
width = minWidth;
height = 0;
continue;
}
if (width > maxWidth) {
width = maxWidth;
height = 0;
continue;
}
if (height < minHeight) {
height = minHeight;
width = 0;
continue;
}
if (height <= maxHeight) break;
height = maxHeight;
width = 0;
}
if (loopProtect > 0 && (width != layer.getWidth() || height != layer.getHeight())) {
layer.resize(width, height);
return layer;
}
return null;
}
public static Color parseColor(String s) {
try {
int i = Integer.decode(s);
return new Color(i >> 16 & 255, i >> 8 & 255, i & 255, i >> 24 & 255);
}
catch (NumberFormatException e) {
return new Color(0, 0, 0, 0);
}
}
public static Color parseColor(String s, int alpha) {
try {
int i = Integer.decode(s);
return new Color(i >> 16 & 255, i >> 8 & 255, i & 255, alpha);
}
catch (NumberFormatException e) {
return new Color(0, 0, 0, 0);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static Node saveLayer(Layer layer, String type, double quality, Node parent, String filename, boolean replace) throws RepositoryException, IOException {
Node fileNode = null;
if (parent.hasNode(filename)) {
if (replace) {
parent.getNode(filename).remove();
} else {
fileNode = parent.getNode(filename);
}
}
if (fileNode == null) {
fileNode = parent.addNode(filename, "{http://www.jcp.org/jcr/nt/1.0}file");
}
Node content = fileNode.hasNode("{http://www.jcp.org/jcr/1.0}content") ? fileNode.getNode("{http://www.jcp.org/jcr/1.0}content") : fileNode.addNode("{http://www.jcp.org/jcr/1.0}content", "{http://www.jcp.org/jcr/nt/1.0}resource");
content.setProperty("{http://www.jcp.org/jcr/1.0}mimeType", type);
content.setProperty("{http://www.jcp.org/jcr/1.0}lastModified", Calendar.getInstance());
FileOutputStream out = null;
FileInputStream in = null;
File tmpFile = null;
try {
tmpFile = File.createTempFile("imgheler", "img");
out = FileUtils.openOutputStream((File)tmpFile);
layer.write(type, quality, (OutputStream)out);
out.close();
out = null;
in = FileUtils.openInputStream((File)tmpFile);
content.setProperty("{http://www.jcp.org/jcr/1.0}data", (InputStream)in);
parent.save();
}
finally {
IOUtils.closeQuietly((OutputStream)out);
IOUtils.closeQuietly((InputStream)in);
if (tmpFile != null) {
tmpFile.delete();
}
}
return fileNode;
}
public static String getTypeFromExtension(String ext) {
return ext == null ? null : typeFromExt.get(ext);
}
public static String getExtensionFromType(String type) {
return type == null ? null : extFromType.get(type);
}
public static boolean handleIfModifiedSince(HttpServletRequest req, HttpServletResponse resp, Node node) {
Calendar lastMod = null;
try {
while (node.getDepth() > 0 && lastMod == null) {
if (node.hasProperty("{http://www.jcp.org/jcr/1.0}lastModified")) {
lastMod = node.getProperty("{http://www.jcp.org/jcr/1.0}lastModified").getDate();
continue;
}
node = node.getParent();
}
}
catch (RepositoryException e) {
log.warn("Error while searching for last modified property: " + (Object)e);
return false;
}
if (lastMod != null) {
long ims;
long modTime = lastMod.getTimeInMillis() / 1000;
if (modTime <= (ims = req.getDateHeader("if-modified-since") / 1000)) {
resp.setStatus(304);
return true;
}
resp.setDateHeader("last-modified", lastMod.getTimeInMillis());
}
return false;
}
static {
typeFromExt.put("png", "image/png");
extFromType.put("image/png", "png");
typeFromExt.put("jpg", "image/jpg");
typeFromExt.put("jpeg", "image/jpeg");
extFromType.put("image/jpg", "jpg");
extFromType.put("image/jpeg", "jpg");
typeFromExt.put("gif", "image/gif");
extFromType.put("image/gif", "gif");
}
}