1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.microemu.device.swt;
21
22 import org.eclipse.swt.graphics.GC;
23 import org.eclipse.swt.graphics.ImageData;
24 import org.microemu.app.ui.swt.SwtDeviceComponent;
25 import org.microemu.app.ui.swt.SwtGraphics;
26 import org.microemu.device.MutableImage;
27
28
29 public class SwtMutableImage extends MutableImage
30 {
31 public org.eclipse.swt.graphics.Image img;
32
33 private org.eclipse.swt.graphics.GC gc;
34
35
36 public SwtMutableImage(int width, int height)
37 {
38 this.img = SwtDeviceComponent.createImage(width, height);
39 this.gc = new GC(this.img);
40 SwtDisplayGraphics displayGraphics = new SwtDisplayGraphics(new SwtGraphics(gc), this);
41 displayGraphics.setColor(0x00ffffff);
42 displayGraphics.fillRect(0, 0, width, height);
43 }
44
45
46 public javax.microedition.lcdui.Graphics getGraphics()
47 {
48 SwtDisplayGraphics displayGraphics = new SwtDisplayGraphics(new SwtGraphics(gc), this);
49 displayGraphics.setColor(0x00000000);
50 displayGraphics.setClip(0, 0, getWidth(), getHeight());
51 displayGraphics.translate(-displayGraphics.getTranslateX(), -displayGraphics.getTranslateY());
52
53 return displayGraphics;
54 }
55
56
57 public boolean isMutable()
58 {
59 return true;
60 }
61
62
63 public int getHeight()
64 {
65 return img.getBounds().height;
66 }
67
68
69 public org.eclipse.swt.graphics.Image getImage()
70 {
71 return img;
72 }
73
74
75 public int getWidth()
76 {
77 return img.getBounds().width;
78 }
79
80
81 public int[] getData()
82 {
83 byte[] tmp = img.getImageData().data;
84 int[] result = new int[tmp.length];
85
86 for (int i = 0; i < tmp.length; i++) {
87 result[i] = tmp[i];
88 }
89
90 return result;
91 }
92
93
94 public void getRGB(int[] argb, int offset, int scanlength, int x, int y, int width, int height)
95 {
96 if (width <= 0 || height <= 0) {
97 return;
98 }
99 if (x < 0 || y < 0 || x + width > getWidth() || y + height > getHeight()) {
100 throw new IllegalArgumentException("Specified area exceeds bounds of image");
101 }
102 if ((scanlength < 0 ? -scanlength : scanlength) < width) {
103 throw new IllegalArgumentException("abs value of scanlength is less than width");
104 }
105 if (argb == null) {
106 throw new NullPointerException("null rgbData");
107 }
108 if (offset < 0 || offset + width > argb.length) {
109 throw new ArrayIndexOutOfBoundsException();
110 }
111 if (scanlength < 0) {
112 if (offset + scanlength*(height-1) < 0) {
113 throw new ArrayIndexOutOfBoundsException();
114 }
115 } else {
116 if (offset + scanlength*(height-1) + width > argb.length) {
117 throw new ArrayIndexOutOfBoundsException();
118 }
119 }
120
121 ImageData imageData = img.getImageData();
122 for (int i = 0; i < height; i++) {
123 imageData.getPixels(x, y + i, width, argb, offset + i * scanlength);
124 }
125 }
126
127 }