This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PLPRuntimeImageOps.java
179 lines (167 loc) · 6.92 KB
/
PLPRuntimeImageOps.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
package cop5556sp17;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import java.util.Hashtable;
public class PLPRuntimeImageOps {
static StringBuilder log;
public static void setLog(StringBuilder log){
PLPRuntimeFrame.log = log;
};
public static final String JVMName = "cop5556sp17/PLPRuntimeImageOps";
public final static String getWidthSig = "()I";
// use getWidth method of BufferedImage
public final static String getHeightSig = "()I";
// use getHeight method of BufferedImage
public final static String scaleSig = "(" + PLPRuntimeImageIO.BufferedImageDesc + "I)" + PLPRuntimeImageIO.BufferedImageDesc;
public static BufferedImage scale(BufferedImage image, int factor) {
PLPRuntimeLog.globalLogAddEntry("scale");
int w = image.getWidth();
int h = image.getHeight();
BufferedImage scaledImage = new BufferedImage(w * factor, h * factor, image.getType());
AffineTransform at = new AffineTransform();
at.scale(factor, factor);
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
scaledImage = op.filter(image, scaledImage);
return scaledImage;
}
public static final String addSig = "(" + PLPRuntimeImageIO.BufferedImageDesc
+ PLPRuntimeImageIO.BufferedImageDesc + ")" + PLPRuntimeImageIO.BufferedImageDesc;
public static BufferedImage add(BufferedImage i0, BufferedImage i1) {
PLPRuntimeLog.globalLogAddEntry("add");
int w0 = i0.getWidth();
int w1 = i1.getWidth();
int h0 = i0.getHeight();
int h1 = i0.getHeight();
int w = w0 <= w1 ? w0 : w1;
int h = h0 <= h1 ? h0 : h1;
BufferedImage dest = new BufferedImage(w,h,i0.getType()); for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
Color c0 = new Color(i0.getRGB(j, i));
Color c1 = new Color(i1.getRGB(j, i));
int red = (int) (c0.getRed() + c1.getRed());
int green = (int) (c0.getGreen() + c1.getGreen());
int blue = (int) (c0.getBlue() + c1.getBlue());
red = red > 255 ? 255 : (red < 0 ? 0 : red); // ensure in range
// [0,255]
green = green > 255 ? 255 : (green < 0 ? 0 : green);
blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);
Color newColor = new Color(red, green, blue);
dest.setRGB(j, i, newColor.getRGB());
}
}
return dest;
}
public static final String subSig = "(" + PLPRuntimeImageIO.BufferedImageDesc
+ PLPRuntimeImageIO.BufferedImageDesc + ")" + PLPRuntimeImageIO.BufferedImageDesc;
public static BufferedImage sub(BufferedImage i0, BufferedImage i1) {
PLPRuntimeLog.globalLogAddEntry("sub");
int w0 = i0.getWidth();
int w1 = i1.getWidth();
int h0 = i0.getHeight();
int h1 = i0.getHeight();
int w = w0 <= w1 ? w0 : w1;
int h = h0 <= h1 ? h0 : h1;
BufferedImage dest = new BufferedImage(w,h,i0.getType());
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
Color c0 = new Color(i0.getRGB(j, i));
Color c1 = new Color(i1.getRGB(j, i));
int red = (int) (c0.getRed() - c1.getRed());
int green = (int) (c0.getGreen() - c1.getGreen());
int blue = (int) (c0.getBlue() - c1.getBlue());
red = red > 255 ? 255 : (red < 0 ? 0 : red); // ensure in range
// [0,255]
green = green > 255 ? 255 : (green < 0 ? 0 : green);
blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);
Color newColor = new Color(red, green, blue);
dest.setRGB(j, i, newColor.getRGB());
}
}
return dest;
}
public static final String mulSig = "(" + PLPRuntimeImageIO.BufferedImageDesc
+ "I" + ")" + PLPRuntimeImageIO.BufferedImageDesc;
public static BufferedImage mul(BufferedImage i0, int factor) {
PLPRuntimeLog.globalLogAddEntry("mul");
int w = i0.getWidth();
int h = i0.getHeight();
BufferedImage dest = new BufferedImage(w,h,i0.getType()); for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
Color c0 = new Color(i0.getRGB(j, i));
int red = (int) (c0.getRed() * factor);
int green = (int) (c0.getGreen() * factor);
int blue = (int) (c0.getBlue() * factor);
red = red > 255 ? 255 : (red < 0 ? 0 : red); // ensure in range
// [0,255]
green = green > 255 ? 255 : (green < 0 ? 0 : green);
blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);
Color newColor = new Color(red, green, blue);
dest.setRGB(j, i, newColor.getRGB());
}
}
return dest;
}
public static final String divSig = "(" + PLPRuntimeImageIO.BufferedImageDesc
+ "I" + ")" + PLPRuntimeImageIO.BufferedImageDesc;
public static BufferedImage div(BufferedImage i0, int divisor) {
PLPRuntimeLog.globalLogAddEntry("div");
int w = i0.getWidth();
int h = i0.getHeight();
BufferedImage dest = new BufferedImage(w,h,i0.getType());
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
Color c0 = new Color(i0.getRGB(j, i));
int red = (int) (c0.getRed() / divisor);
int green = (int) (c0.getGreen() / divisor);
int blue = (int) (c0.getBlue() / divisor);
red = red > 255 ? 255 : (red < 0 ? 0 : red); // ensure in range
// [0,255]
green = green > 255 ? 255 : (green < 0 ? 0 : green);
blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);
Color newColor = new Color(red, green, blue);
dest.setRGB(j, i, newColor.getRGB());
}
}
return dest;
}
public static final String modSig = "(" + PLPRuntimeImageIO.BufferedImageDesc
+ "I" + ")" + PLPRuntimeImageIO.BufferedImageDesc;
public static BufferedImage mod(BufferedImage i0, int divisor) {
PLPRuntimeLog.globalLogAddEntry("mod");
int w = i0.getWidth();
int h = i0.getHeight();
BufferedImage dest = new BufferedImage(w,h,i0.getType());
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
Color c0 = new Color(i0.getRGB(j, i));
int red = (int) (c0.getRed() % divisor);
int green = (int) (c0.getGreen() % divisor);
int blue = (int) (c0.getBlue() % divisor);
red = red > 255 ? 255 : (red < 0 ? 0 : red); // ensure in range
// [0,255]
green = green > 255 ? 255 : (green < 0 ? 0 : green);
blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);
Color newColor = new Color(red, green, blue);
dest.setRGB(j, i, newColor.getRGB());
}
}
return dest;
}
public static final String copyImageSig = "("+PLPRuntimeImageIO.BufferedImageDesc+")"+PLPRuntimeImageIO.BufferedImageDesc;
public static BufferedImage copyImage(BufferedImage source){
PLPRuntimeLog.globalLogAddEntry("copyImage");
BufferedImage b = new BufferedImage(source.getWidth(), source.getHeight(), source.getType());
Graphics2D g = (Graphics2D) b.getGraphics();
g.drawImage(source, 0, 0, null);
g.dispose();
return b;
}
}