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 org.microemu.device.j2se;
29
30 import java.awt.Image;
31 import java.awt.Toolkit;
32 import java.awt.image.ImageObserver;
33
34 import org.microemu.log.Logger;
35
36 public class J2SEImmutableImage extends javax.microedition.lcdui.Image {
37 private Image img;
38
39 private int width;
40
41 private int height;
42
43 public J2SEImmutableImage(Image image) {
44 this.img = image;
45 this.width = -1;
46 this.height = -1;
47 }
48
49 public J2SEImmutableImage(J2SEMutableImage image) {
50 img = Toolkit.getDefaultToolkit().createImage(image.getImage().getSource());
51 this.width = -1;
52 this.height = -1;
53 }
54
55 public int getHeight() {
56 if (height == -1) {
57 ImageObserver observer = new ImageObserver() {
58 public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
59 if ((infoflags & ImageObserver.WIDTH) != 0) {
60 J2SEImmutableImage.this.width = width;
61 }
62 if ((infoflags & ImageObserver.HEIGHT) != 0) {
63 synchronized (this) {
64 J2SEImmutableImage.this.height = height;
65 this.notify();
66 }
67 return false;
68 }
69
70 return true;
71 }
72 };
73 synchronized (observer) {
74
75 try {
76 height = img.getHeight(observer);
77 } catch (NullPointerException ex) {
78 }
79 if (height == -1) {
80 try {
81 observer.wait();
82 } catch (InterruptedException ex) {
83 }
84 }
85 }
86 }
87
88 return height;
89 }
90
91 public Image getImage() {
92 return img;
93 }
94
95 public int getWidth() {
96 if (width == -1) {
97 ImageObserver observer = new ImageObserver() {
98 public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
99 if ((infoflags & ImageObserver.HEIGHT) != 0) {
100 J2SEImmutableImage.this.height = height;
101 }
102 if ((infoflags & ImageObserver.WIDTH) != 0) {
103 synchronized (this) {
104 J2SEImmutableImage.this.width = width;
105 this.notify();
106 }
107 return false;
108 }
109
110 return true;
111 }
112 };
113 synchronized (observer) {
114
115 try {
116 width = img.getWidth(observer);
117 } catch (NullPointerException ex) {
118 }
119 if (width == -1) {
120 try {
121 observer.wait();
122 } catch (InterruptedException ex) {
123 }
124 }
125 }
126 }
127
128 return width;
129 }
130
131 public void getRGB(int[] argb, int offset, int scanlength, int x, int y, int width, int height) {
132
133 if (width <= 0 || height <= 0)
134 return;
135 if (x < 0 || y < 0 || x + width > getWidth() || y + height > getHeight())
136 throw new IllegalArgumentException("Specified area exceeds bounds of image");
137 if ((scanlength < 0 ? -scanlength : scanlength) < width)
138 throw new IllegalArgumentException("abs value of scanlength is less than width");
139 if (argb == null)
140 throw new NullPointerException("null rgbData");
141 if (offset < 0 || offset + width > argb.length)
142 throw new ArrayIndexOutOfBoundsException();
143 if (scanlength < 0) {
144 if (offset + scanlength * (height - 1) < 0)
145 throw new ArrayIndexOutOfBoundsException();
146 } else {
147 if (offset + scanlength * (height - 1) + width > argb.length)
148 throw new ArrayIndexOutOfBoundsException();
149 }
150
151 try {
152 (new java.awt.image.PixelGrabber(img, x, y, width, height, argb, offset, scanlength)).grabPixels();
153 } catch (InterruptedException e) {
154 Logger.error(e);
155 }
156 }
157
158 }