-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathPixels.java
More file actions
158 lines (123 loc) · 4.81 KB
/
Copy pathPixels.java
File metadata and controls
158 lines (123 loc) · 4.81 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
package clojure2d.java;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import fastmath.vector.Vec4;
public final class Pixels {
public static BufferedImage imageFromPixels(int[] p, int w, int h) {
BufferedImage img = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
setImagePixels(img,p);
return img;
}
public static int[] getRasterPixels(Raster raster, int x, int y, int w, int h) {
int[] buff = new int[4 * w * h];
return raster.getPixels(x,y,w,h,buff);
}
public static int[] getRasterPixels(Raster raster) {
return getRasterPixels(raster, 0, 0, raster.getWidth(), raster.getHeight());
}
public static int[] getImagePixels(BufferedImage img, int x, int y, int w, int h) {
return getRasterPixels(img.getRaster(),x,y,w,h);
}
public static int[] getImagePixels(BufferedImage img) {
return getRasterPixels(img.getRaster(), 0, 0, img.getWidth(), img.getHeight());
}
public static void setRasterPixels(WritableRaster raster, int x, int y, int w, int h, int[] p) {
raster.setPixels(x,y,w,h,p);
}
public static void setRasterPixels(WritableRaster raster, int[] p) {
setRasterPixels(raster, 0, 0, raster.getWidth(), raster.getHeight(), p);
}
public static void setImagePixels(BufferedImage img, int x, int y, int w, int h, int[] p) {
img.getRaster().setPixels(x,y,w,h,p);
}
public static void setImagePixels(BufferedImage img, int[] p) {
setImagePixels(img, 0, 0, img.getWidth(), img.getHeight(), p);
}
public static int[] getChannel(int[] p, int ch) {
int[] buff = new int[p.length >> 2];
for(int i=ch, iter=0;i<p.length;i+=4,iter++) {
buff[iter] = p[i];
}
return buff;
}
public static void setChannel(int[] p, int ch, int[] buff) {
for(int i=ch, iter=0;i<p.length;i+=4,iter++) {
p[i] = buff[iter];
}
}
public static int getValue(int[] p, int ch, int x, int y, int w, int h, int edge) {
if( (x<0) || (x>=w) || (y<0) || (y>=h)) {
switch(edge) {
case -1: return p[((constrain(y,0,h-1) * w + constrain(x,0,w-1)) << 2) + ch];
case -2: return p[((mod(y,h) * w + mod(x,w)) << 2) + ch];
default: return edge;
}
} else {
return p[((y * w + x) << 2) + ch];
}
}
public static int unsafeGetValue(int[] p, int ch, int x, int y, int w) {
return p[((y * w + x) << 2) + ch];
}
public static int getValue(int[] p, int ch, int idx) {
return p[(idx << 2) + ch];
}
public static void setValue(int[] p, int ch, int x, int y, int w, int v) {
p[((y * w + x) << 2) + ch]=v;
}
public static void setValue(int[] p, int ch, int idx, int v) {
p[(idx << 2) + ch]=v;
}
public static Vec4 getColor(int[] p, int x, int y, int w, int h, int edge) {
int idx=0;
if( (x<0) || (x>=w) || (y<0) || (y>=h)) {
switch(edge) {
case -1: idx=(constrain(y,0,h-1) * w + constrain(x,0,w-1)) << 2; break;
case -2: idx=(mod(y,h) * w + mod(x,w)) << 2; break;
default: return new Vec4(edge,edge,edge,255);
}
} else {
idx=(y * w + x) << 2;
}
return new Vec4(p[idx],p[idx+1],p[idx+2],p[idx+3]);
}
public static Vec4 getColor(int[] p, int idx) {
int i = idx << 2;
return new Vec4(p[i],p[i+1],p[i+2],p[i+3]);
}
public static Vec4 unsafeGetColor(int[] p, int x, int y, int w) {
int idx=(y * w + x) << 2;
return new Vec4(p[idx],p[idx+1],p[idx+2],p[idx+3]);
}
public static void setColor(int [] p, int x, int y, int w, Vec4 c) {
int idx = (y * w + x) << 2;
p[idx] = constrain((int)(c.x+0.5),0,255);
p[idx+1] = constrain((int)(c.y+0.5),0,255);
p[idx+2] = constrain((int)(c.z+0.5),0,255);
p[idx+3] = constrain((int)(c.w+0.5),0,255);
}
public static void setColor(int [] p, int idx, Vec4 c) {
int i = idx << 2;
p[i] = constrain((int)(c.x+0.5),0,255);
p[i+1] = constrain((int)(c.y+0.5),0,255);
p[i+2] = constrain((int)(c.z+0.5),0,255);
p[i+3] = constrain((int)(c.w+0.5),0,255);
}
// some basic utilities
public static int constrain(int v, int mn, int mx) {
return v < mn ? mn : (v > mx ? mx : v);
}
public static double smoothStep(double a, double b, double x) {
if (x < a)
return 0;
if (x >= b)
return 1;
double xx = (x - a) / (b - a);
return xx*xx * (3.0 - (xx+xx));
}
public static int mod(int x, int y) {
int result = x % y;
return result < 0 ? result + y : result;
}
}