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 package com.nokia.mid.ui;
29
30 import javax.microedition.lcdui.Graphics;
31 import javax.microedition.lcdui.Image;
32 import javax.microedition.lcdui.game.Sprite;
33
34 import org.microemu.device.DisplayGraphics;
35 import org.microemu.device.MutableImage;
36
37
38
39 public class DirectGraphicsImp implements DirectGraphics{
40
41
42 private Graphics graphics;
43 private int alphaComponent;
44
45
46
47
48 public DirectGraphicsImp(Graphics g) {
49 graphics = g;
50 }
51
52
53
54
55
56
57
58
59 public void drawImage(Image img, int x, int y, int anchor, int manipulation) {
60 if(img == null) {
61 throw new NullPointerException();
62 }
63 int transform;
64 switch (manipulation) {
65 case FLIP_HORIZONTAL:
66 transform = Sprite.TRANS_MIRROR_ROT180;
67 break;
68 case FLIP_VERTICAL:
69 transform = Sprite.TRANS_MIRROR;
70 break;
71 case ROTATE_90:
72 transform = Sprite.TRANS_ROT90;
73 break;
74 case ROTATE_180:
75 transform = Sprite.TRANS_ROT180;
76 break;
77 case ROTATE_270:
78 transform = Sprite.TRANS_ROT270;
79 break;
80 default:
81 transform = -1;
82 }
83 if(anchor >= 64 || transform == -1) {
84 throw new IllegalArgumentException();
85 } else {
86 graphics.drawRegion(
87 img,
88 x + graphics.getTranslateX(), y + graphics.getTranslateY(),
89 img.getWidth(), img.getHeight(),
90 transform,
91 x + graphics.getTranslateX(), y + graphics.getTranslateY(), anchor);
92 return;
93 }
94 }
95
96
97
98
99 public void setARGBColor(int argb) {
100 alphaComponent=(argb >> 24 & 0xff);
101 graphics.setColor(argb & 0xffffff);
102 }
103
104
105
106
107 public int getAlphaComponent() {
108 return alphaComponent;
109 }
110
111
112
113
114 public int getNativePixelFormat() {
115 return TYPE_BYTE_1_GRAY;
116 }
117
118
119
120
121
122
123
124
125
126 public void drawPolygon(int xPoints[], int xOffset, int yPoints[], int yOffset, int nPoints, int argbColor) {
127 System.out.println("public void drawPolygon(int xPoints[], int xOffset, int yPoints[], int yOffset, int nPoints, int argbColor)");
128 }
129
130
131
132
133
134
135
136
137
138
139 public void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int argbColor) {
140 System.out.println("public void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int argbColor)");
141 }
142
143
144
145
146
147
148
149
150
151 public void fillPolygon(int xPoints[], int xOffset, int yPoints[], int yOffset, int nPoints, int argbColor) {
152 System.out.println("public void fillPolygon(int xPoints[], int xOffset, int yPoints[], int yOffset, int nPoints, int argbColor)");
153 }
154
155
156
157
158
159
160
161
162
163
164 public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int argbColor) {
165 System.out.println("public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int argbColor)");
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181 public void drawPixels(byte[] pix, byte[] alpha, int off, int scanlen, int x, int y, int width, int height, int manipulation, int format) {
182 System.out.println("public void drawPixels(byte[] pix, byte[] alpha, int off, int scanlen, int x, int y, int width, int height, int manipulation, int format)");
183 if (pix == null) {
184 throw new NullPointerException();
185 }
186 if (width < 0 || height < 0) {
187 throw new IllegalArgumentException();
188 }
189
190 Graphics g=graphics;
191 int c;
192
193 if (format == TYPE_BYTE_1_GRAY) {
194
195 int b=7;
196
197 for (int yj = 0; yj < height; yj++) {
198 int line = off + yj * scanlen;
199 int ypos = yj * width;
200 for (int xj = 0; xj < width; xj++) {
201 c=doAlpha(pix, alpha, (line + xj) /8,b);
202 if((c >> 24 & 0xff)!=0)
203 {
204 if (g.getColor()!=c) g.setColor(c);
205 g.drawLine(xj+x,yj+y,xj+x,yj+y);
206 }
207 b--;
208 if(b<0)b=7;
209 }
210 }
211 }
212 else if (format == TYPE_BYTE_1_GRAY_VERTICAL) {
213 int ods = off / scanlen;
214 int oms = off % scanlen;
215 int b=0;
216 for (int yj = 0; yj < height; yj++) {
217 int ypos = yj * width;
218 int tmp = (ods + yj) /8 * scanlen+oms;
219 for (int xj = 0; xj < width; xj++) {
220 c=doAlpha(pix, alpha, tmp + xj, b);
221 if (g.getColor()!=c) g.setColor(c);
222 if((c >> 24 & 0xff)!=0)
223 g.drawLine(xj+x,yj+y,xj+x,yj+y);
224 }
225 b++;
226 if(b>7) b=0;
227 }
228 } else
229 throw new IllegalArgumentException();
230 }
231
232
233
234
235
236
237
238
239
240
241
242
243
244 public void drawPixels(short pix[], boolean trans, int off, int scanlen, int x, int y, int width, int height, int manipulation, int format) {
245
246 if (format != TYPE_USHORT_4444_ARGB) {
247 throw new IllegalArgumentException("Illegal format: " + format);
248 }
249
250
251 Graphics g = graphics;
252
253 for (int iy = 0; iy < height; iy++) {
254 for (int ix = 0; ix < width; ix ++) {
255 int c=toARGB(pix[off + ix + iy * scanlen],TYPE_USHORT_4444_ARGB);
256 if (!isTransparent(c)) {
257 g.setColor(c);
258 g.drawLine(x + ix, y + iy,x + ix, y + iy);
259 }
260 }
261 }
262
263
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277
278 public void drawPixels(int pix[], boolean trans, int off, int scanlen, int x, int y, int width, int height, int manipulation, int format) {
279 System.out.println("public void drawPixels(int pix[], boolean trans, int off, int scanlen, int x, int y, int width, int height, int manipulation, int format)");
280 throw new IllegalArgumentException();
281 }
282
283
284
285
286
287
288
289
290
291
292
293
294 public void getPixels(byte pix[], byte alpha[], int offset, int scanlen, int x, int y, int width, int height, int format) {
295 System.out.println("public void getPixels(byte pix[], byte alpha[], int offset, int scanlen, int x, int y, int width, int height, int format)");
296 throw new IllegalArgumentException();
297 }
298
299
300
301
302
303
304
305
306
307
308
309 public void getPixels(short pix[], int offset, int scanlen, int x, int y, int width, int height, int format) {
310
311 switch (format) {
312 case TYPE_USHORT_4444_ARGB: {
313
314
315 MutableImage img=((DisplayGraphics)graphics).getImage();
316
317 int [] data=img.getData();
318
319 for (int iy = 0; iy < height; iy++) {
320 for (int ix = 0; ix < width; ix++) {
321
322
323 pix[offset + ix + iy * scanlen]=(short)fromARGB(data[ix+iy*width],TYPE_USHORT_4444_ARGB);
324 }
325 }
326 break;
327 }
328 case TYPE_USHORT_444_RGB: {
329
330
331 MutableImage img=((DisplayGraphics)graphics).getImage();
332
333 int [] data=img.getData();
334
335 for (int iy = 0; iy < height; iy++) {
336 for (int ix = 0; ix < width; ix++) {
337
338
339 pix[offset + ix + iy * scanlen]=(short)fromARGB(data[ix+iy*width],TYPE_USHORT_444_RGB);
340 }
341 }
342 break;
343 }
344 default: throw new IllegalArgumentException("Illegal format: " + format);
345 }
346 }
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361 public void getPixels(int pix[], int offset, int scanlen, int x, int y, int width, int height, int format) {
362 System.out.println("public void getPixels(int pix[], int offset, int scanlen, int x, int y, int width, int height, int format");
363 throw new IllegalArgumentException();
364 }
365
366
367 private static int doAlpha(byte[] pix, byte[] alpha, int pos, int shift) {
368 int p;
369 int a;
370 if (isBitSet(pix[pos],shift))
371 p=0;
372 else
373 p=0x00FFFFFF;
374 if (alpha == null || isBitSet(alpha[pos],shift))
375 a=0xFF000000;
376 else
377 a=0;
378 return p|a;
379 }
380
381
382 private static boolean isBitSet(byte b, int pos) {
383 return ((b & (byte)(1 << pos)) != 0);
384 }
385
386
387 private static int toARGB(int s, int type) {
388 switch (type) {
389 case TYPE_USHORT_4444_ARGB: {
390 int a=((s)&0xF000)>>>12;
391 int r=((s)&0x0F00)>>>8;
392 int g=((s)&0x00F0)>>>4;
393 int b=((s)&0x000F);
394
395
396 s=((a*15)<<24)|((r*15)<<16)|((g*15)<<8)|(b*15);
397 break;
398 }
399 case TYPE_USHORT_444_RGB: {
400 int r=((s)&0x0F00)>>>8;
401 int g=((s)&0x00F0)>>>4;
402 int b=((s)&0x000F);
403
404
405 s=((r*15)<<16)|((g*15)<<8)|(b*15);
406 break;
407 }
408 }
409 return s;
410 }
411
412 private static int fromARGB(int s, int type) {
413 switch (type) {
414 case TYPE_USHORT_4444_ARGB: {
415 int a=((s)&0xFF000000)>>>24;
416 int r=((s)&0x00FF0000)>>>16;
417 int g=((s)&0x0000FF00)>>>8;
418 int b=((s)&0x000000FF);
419 s=((a/15)<<12)|((r/15)<<8)|((g/15)<<4)|(b/15);
420 break;
421 }
422 case TYPE_USHORT_444_RGB: {
423 int r=((s)&0x00FF0000)>>>16;
424 int g=((s)&0x0000FF00)>>>8;
425 int b=((s)&0x000000FF);
426 s=((r/15)<<8)|((g/15)<<4)|(b/15);
427 break;
428 }
429 }
430 return s;
431 }
432
433 private static boolean isTransparent(int s) {
434 return (s&0xFF000000)==0;
435 }
436 }