-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBitmap.java
326 lines (271 loc) · 5.21 KB
/
Bitmap.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
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
import java.awt.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
//import java.util.Arrays;
public class Bitmap extends Component
{
public int w, h;
public int cl, cr, ct, cb;
public int data[];
public int row[];
public Bitmap(int ww, int hh)
{
int i;
w = ww;
h = hh;
cl = 0;
ct = 0;
cr = w - 1;
cb = h - 1;
data = new int[w * h];
row = new int[h];
for(i = 0; i < h; i++)
row[i] = w * i;
//Arrays.fill(data, Col.makeRGB(0, 0, 0));
}
public Bitmap(String s)
{
int i;
BufferedImage src = null;
try
{
src = ImageIO.read(getClass().getResource(s));
}
catch (IOException e)
{
}
w = src.getWidth(null);
h = src.getHeight(null);
cl = 0;
ct = 0;
cr = w - 1;
cb = h - 1;
data = new int[w * h];
row = new int[h];
for(i = 0; i < h; i++)
row[i] = w * i;
int x, y;
for(y = 0; y < h; y++)
for(x = 0; x < w; x++)
data[x + row[y]] = src.getRGB(x, y) | 0xFF000000;
}
public void clear(int c)
{
int i;
for(i = 0; i < w * h; i++)
data[i] = c;
//Arrays.fill(data, c);
}
public void setpixel(int x, int y, int c, int t)
{
if(x < cl || x > cr || y < ct || y > cb)
return;
data[x + row[y]] = Blend.current(data[x + row[y]], c, t);
}
public int getpixel(int x, int y)
{
if(x < cl || x > cr || y < ct || y > cb)
return 0;
return data[x + row[y]];
}
public void hline(int x1, int y, int x2, int c, int t)
{
int swap;
if(x2 < x1)
{
swap = x1;
x1 = x2;
x2 = swap;
}
if(x1 < cl)
x1 = cl;
if(x2 > cr)
x2 = cr;
if(y < ct)
return;
if(y > cb)
return;
while(x1 <= x2)
{
setpixel(x1, y, c, t);
x1++;
}
}
public void rect(int x1, int y1, int x2, int y2, int c, int t)
{
int swap;
if(x2 < x1)
{
swap = x1;
x1 = x2;
x2 = swap;
}
if(y2 < y1)
{
swap = y1;
y1 = y2;
y2 = swap;
}
if(x1 < cl)
x1 = cl;
if(y1 < ct)
y1 = ct;
if(x2 > cr)
x2 = cr;
if(y2 > cb)
y2 = cb;
hline(x1, y1, x2, c, t);
while(y1 < y2)
{
setpixel(x1, y1, c, t);
setpixel(x2, y1, c, t);
y1++;
}
hline(x1, y1, x2, c, t);
}
public void rectfill(int x1, int y1, int x2, int y2, int c, int t)
{
int swap;
if(x2 < x1) {
swap = x1;
x1 = x2;
x2 = swap;
}
if(y2 < y1) {
swap = y1;
y1 = y2;
y2 = swap;
}
if(x1 < cl)
x1 = cl;
if(y1 < ct)
y1 = ct;
if(x2 > cr)
x2 = cr;
if(y2 > cb)
y2 = cb;
while(y1 <= y2)
{
hline(x1, y1, x2, c, t);
y1++;
}
}
// warning: does not clip
public void stretchBlit(Bitmap dest,
int xs1, int ys1, int xs2, int ys2,
int xd1, int yd1, int xd2, int yd2)
{
xs2 += xs1;
xs2--;
ys2 += ys1;
ys2--;
xd2 += xd1;
xd2--;
yd2 += yd1;
yd2--;
int dx = Int.abs(yd2 - yd1);
int dy = Int.abs(ys2 - ys1) << 1;
int sx = Int.sign(yd2 - yd1);
int sy = Int.sign(ys2 - ys1);
int e = dy - dx;
int dx2 = dx << 1;
int d;
for(d = 0; d <= dx; d++)
{
int ddx = Int.abs(xd2 - xd1);
int ddy = Int.abs(xs2 - xs1) << 1;
int ssx = Int.sign(xd2 - xd1);
int ssy = Int.sign(xs2 - xs1);
int ee = ddy - ddx;
int ddx2 = ddx << 1;
int p = dest.row[yd1] + xd1;
int q = row[ys1] + xs1;
int dd;
for(dd = 0; dd <= ddx; dd++)
{
dest.data[p] = data[q];
while(ee >= 0)
{
q += ssy;
ee -= ddx2;
}
p += ssx;
ee += ddy;
}
while(e >= 0)
{
ys1 += sy;
e -= dx2;
}
yd1 += sx;
e += dy;
}
}
public void blit(Bitmap dest,
int sx, int sy, int dx, int dy, int ww, int hh)
{
int x, y;
if((sx >= w) || (sy >= h) ||
(dx >= dest.cr) || (dy >= dest.cb))
return;
// clip src left
if(sx < 0)
{
ww += sx;
dx -= sx;
sx = 0;
}
// clip src top
if(sy < 0)
{
hh += sy;
dy -= sy;
sy = 0;
}
// clip src right
if((sx + ww) > w)
ww = w - sx;
// clip src bottom
if((sy + hh) > h)
hh = h - sy;
// clip dest left
if(dx < dest.cl)
{
dx -= dest.cl;
ww += dx;
sx -= dx;
dx = dest.cl;
}
// clip dest top
if(dy < dest.ct)
{
dy -= dest.ct;
hh += dy;
sy -= dy;
dy = dest.ct;
}
// clip dest right
if((dx + ww - 1) > dest.cr)
ww = dest.cr - dx;
// clip dest bottom
if((dy + hh - 1) > dest.cb)
hh = dest.cb - dy;
if(ww < 1 || hh < 1)
return;
int sy1 = sy;
int dy1 = dy;
for(y = 0; y < hh; y++)
{
int sx1 = sx + row[sy1];
int dx1 = dx + dest.row[dy1];
for(x = 0; x < ww; x++, sx1++, dx1++)
dest.data[dx1] = data[sx1];
sy1++;
dy1++;
// could maybe use this but its a function call
//System.arraycopy(data, sx + row[sy + y],
// dest.data, dx + dest.row[dy + y], ww);
}
}
}