View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  It is licensed under the following two licenses as alternatives:
6    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
7    *    2. Apache License (the "AL") Version 2.0
8    *
9    *  You may not use this file except in compliance with at least one of
10   *  the above two licenses.
11   *
12   *  You may obtain a copy of the LGPL at
13   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
14   *
15   *  You may obtain a copy of the AL at
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the LGPL or the AL for the specific language governing permissions and
22   *  limitations.
23   *
24   *  Contributor(s):
25   *    Andres Navarro
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                  // Fix for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4905411 (Java < 1.5)
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                 // Fix for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4905411 (Java < 1.5)
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 }