Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/nokia/mid/ui/DirectGraphicsImp.java


1   /*
2    *  Nokia API for MicroEmulator
3    *  Copyright (C) 2003 Markus Heberling <markus@heberling.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   *
19   *  Contributor(s):
20   *    Bartek Teodorczyk <barteo@barteo.net>
21   */
22  
23  package com.nokia.mid.ui;
24  
25  import javax.microedition.lcdui.Image;
26  import javax.microedition.lcdui.Graphics;
27  import com.barteo.emulator.device.DeviceFactory;
28  import com.barteo.emulator.device.MutableImage;
29  import com.barteo.emulator.device.DeviceDisplay;
30  import com.barteo.emulator.device.DisplayGraphics;
31  
32  
33  public class DirectGraphicsImp implements DirectGraphics{
34      
35      
36      private Graphics graphics;
37      private int alphaComponent;
38      
39      /**
40       * @param g
41       */
42      public DirectGraphicsImp(Graphics g) {
43          graphics = g;
44      }
45      
46      /**
47       * @param img
48       * @param x
49       * @param y
50       * @param anchor
51       * @param manipulation ignored, since manipulations are not supported at the moment
52       */
53      public void drawImage(Image img, int x, int y, int anchor, int manipulation) {
54          System.out.println("public void drawImage(Image img, int x, int y, int anchor, int manipulation)");
55          if(img == null) {
56              throw new NullPointerException();
57          }
58          if(anchor >= 64 || manipulation != 0) {
59              throw new IllegalArgumentException();
60          } else {
61              graphics.drawImage(img, x + graphics.getTranslateX(), y + graphics.getTranslateY(), anchor);
62              return;
63          }
64      }
65      
66      /**
67       * @param argb
68       */
69      public void setARGBColor(int argb) {
70          alphaComponent=(argb >> 24 & 0xff);
71          graphics.setColor(argb & 0xffffff);
72      }
73      
74      /**
75       * @return
76       */
77      public int getAlphaComponent() {
78          return alphaComponent;
79      }
80      
81      /**
82       * @return
83       */
84      public int getNativePixelFormat() {
85          return TYPE_BYTE_1_GRAY;
86      }
87      
88      /** Not supported
89       * @param xPoints
90       * @param xOffset
91       * @param yPoints
92       * @param yOffset
93       * @param nPoints
94       * @param argbColor
95       */
96      public void drawPolygon(int xPoints[], int xOffset, int yPoints[], int yOffset, int nPoints, int argbColor) {
97          System.out.println("public void drawPolygon(int xPoints[], int xOffset, int yPoints[], int yOffset, int nPoints, int argbColor)");
98      }
99      
100     /** Not supported
101      * @param x1
102      * @param y1
103      * @param x2
104      * @param y2
105      * @param x3
106      * @param y3
107      * @param argbColor
108      */
109     public void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int argbColor) {
110         System.out.println("public void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int argbColor)");
111     }
112     
113     /** Not supported
114      * @param xPoints
115      * @param xOffset
116      * @param yPoints
117      * @param yOffset
118      * @param nPoints
119      * @param argbColor
120      */
121     public void fillPolygon(int xPoints[], int xOffset, int yPoints[], int yOffset, int nPoints, int argbColor) {
122         System.out.println("public void fillPolygon(int xPoints[], int xOffset, int yPoints[], int yOffset, int nPoints, int argbColor)");
123     }
124     
125     /** Not supported
126      * @param x1
127      * @param y1
128      * @param x2
129      * @param y2
130      * @param x3
131      * @param y3
132      * @param argbColor
133      */
134     public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int argbColor) {
135         System.out.println("public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int argbColor)");
136     }
137     
138     //manipulations are not supported!
139     /**
140      * @param pix
141      * @param alpha
142      * @param off
143      * @param scanlen
144      * @param x
145      * @param y
146      * @param width
147      * @param height
148      * @param manipulation ignored, since manipulations are not supported at the moment
149      * @param format
150      */
151     public void drawPixels(byte[] pix, byte[] alpha, int off, int scanlen, int x, int y, int width, int height, int manipulation, int format) {
152         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)");
153         if (pix == null) {
154             throw new NullPointerException();
155         }
156         if (width < 0 || height < 0) {
157             throw new IllegalArgumentException();
158         }
159         
160         Graphics g=graphics;
161         int c;
162         
163         if (format == TYPE_BYTE_1_GRAY) {
164             
165             int b=7;
166             
167             for (int yj = 0; yj < height; yj++) {
168                 int line = off + yj * scanlen;
169                 int ypos = yj * width;
170                 for (int xj = 0; xj < width; xj++) {
171                     c=doAlpha(pix, alpha, (line + xj) /8,b);
172                     if((c >> 24 & 0xff)!=0)//alpha
173                     {
174                         if (g.getColor()!=c) g.setColor(c);
175                         g.drawLine(xj+x,yj+y,xj+x,yj+y);
176                     }
177                     b--;
178                     if(b<0)b=7;
179                 }
180             }
181         }
182         else if (format == TYPE_BYTE_1_GRAY_VERTICAL) {
183             int ods = off / scanlen;
184             int oms = off % scanlen;
185             int b=0;
186             for (int yj = 0; yj < height; yj++) {
187                 int ypos = yj * width;
188                 int tmp = (ods + yj) /8 * scanlen+oms;
189                 for (int xj = 0; xj < width; xj++) {
190                     c=doAlpha(pix, alpha, tmp + xj, b);
191                     if (g.getColor()!=c) g.setColor(c);
192                     if((c >> 24 & 0xff)!=0) //alpha
193                         g.drawLine(xj+x,yj+y,xj+x,yj+y);
194                 }
195                 b++;
196                 if(b>7) b=0;
197             }
198         } else
199             throw new IllegalArgumentException();
200     }
201     
202     /** Only TYPE_USHORT_4444_ARGB format supported
203      * @param pix
204      * @param trans
205      * @param off
206      * @param scanlen
207      * @param x
208      * @param y
209      * @param width
210      * @param height
211      * @param manipulation
212      * @param format
213      */
214     public void drawPixels(short pix[], boolean trans, int off, int scanlen, int x, int y, int width, int height, int manipulation, int format) {
215         //        System.out.println("public void drawPixels(short pix[], boolean trans, int off, int scanlen, int x, int y, int width, int height, int manipulation, int format)");
216         if (format != TYPE_USHORT_4444_ARGB) {
217             throw new IllegalArgumentException("Illegal format: " + format);
218         }
219         
220         //Graphics g = image.getGraphics();
221         Graphics g = graphics;
222         
223         for (int iy = 0; iy < height; iy++) {
224             for (int ix = 0; ix < width; ix ++) {
225                 int c=toARGB(pix[off + ix + iy * scanlen],TYPE_USHORT_4444_ARGB);
226                 if (!isTransparent(c)) {
227                     g.setColor(c);
228                     g.drawLine(x + ix, y + iy,x + ix, y + iy);
229                 }
230             }
231         }
232         
233         //graphics.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
234     }
235     
236     /** Not supported
237      * @param pix
238      * @param trans
239      * @param off
240      * @param scanlen
241      * @param x
242      * @param y
243      * @param width
244      * @param height
245      * @param manipulation
246      * @param format
247      */
248     public void drawPixels(int pix[], boolean trans, int off, int scanlen, int x, int y, int width, int height, int manipulation, int format) {
249         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)");
250         throw new IllegalArgumentException();
251     }
252     
253     /** Not supported
254      * @param pix
255      * @param alpha
256      * @param offset
257      * @param scanlen
258      * @param x
259      * @param y
260      * @param width
261      * @param height
262      * @param format
263      */
264     public void getPixels(byte pix[], byte alpha[], int offset, int scanlen, int x, int y, int width, int height, int format) {
265         System.out.println("public void getPixels(byte pix[], byte alpha[], int offset, int scanlen, int x, int y, int width, int height, int format)");
266         throw new IllegalArgumentException();
267     }
268     
269     /** Only TYPE_USHORT_4444_ARGB format supported
270      * @param pix
271      * @param offset
272      * @param scanlen
273      * @param x
274      * @param y
275      * @param width
276      * @param height
277      * @param format
278      */
279     public void getPixels(short pix[], int offset, int scanlen, int x, int y, int width, int height, int format) {
280         //        System.out.println("public void getPixels(short pix[], int offset, int scanlen, int x, int y, int width, int height, int format)");
281         switch (format) {
282             case TYPE_USHORT_4444_ARGB: {
283                 //DeviceDisplay dd = DeviceFactory.getDevice().getDeviceDisplay();
284                 //MutableImage img = (MutableImage)dd.getDisplayImage();
285                 MutableImage img=((DisplayGraphics)graphics).getImage();
286                 
287                 int [] data=img.getData();
288                 
289                 for (int iy = 0; iy < height; iy++) {
290                     for (int ix = 0; ix < width; ix++) {
291                         //pix[offset + ix + iy * scanlen] = (short) img.getPixel(x + ix, y + iy);
292                         //System.out.println(data[ix+iy*width]+" "+a+" "+r+" "+g+" "+b);
293                         pix[offset + ix + iy * scanlen]=(short)fromARGB(data[ix+iy*width],TYPE_USHORT_4444_ARGB);
294                     }
295                 }
296                 break;
297             }
298             case TYPE_USHORT_444_RGB: {
299                 //DeviceDisplay dd = DeviceFactory.getDevice().getDeviceDisplay();
300                 //MutableImage img = (MutableImage)dd.getDisplayImage();
301                 MutableImage img=((DisplayGraphics)graphics).getImage();
302                 
303                 int [] data=img.getData();
304                 
305                 for (int iy = 0; iy < height; iy++) {
306                     for (int ix = 0; ix < width; ix++) {
307                         //pix[offset + ix + iy * scanlen] = (short) img.getPixel(x + ix, y + iy);
308                         //System.out.println(data[ix+iy*width]+" "+a+" "+r+" "+g+" "+b);
309                         pix[offset + ix + iy * scanlen]=(short)fromARGB(data[ix+iy*width],TYPE_USHORT_444_RGB);
310                     }
311                 }
312                 break;
313             }            
314             default: throw new IllegalArgumentException("Illegal format: " + format);
315         }
316     }
317     
318     
319     
320     
321     /** Not supported
322      * @param pix
323      * @param offset
324      * @param scanlen
325      * @param x
326      * @param y
327      * @param width
328      * @param height
329      * @param format
330      */
331     public void getPixels(int pix[], int offset, int scanlen, int x, int y, int width, int height, int format) {
332         System.out.println("public void getPixels(int pix[], int offset, int scanlen, int x, int y, int width, int height, int format");
333         throw new IllegalArgumentException();
334     }
335     
336     
337     private static int doAlpha(byte[] pix, byte[] alpha, int pos, int shift) {
338         int p;
339         int a;
340         if (isBitSet(pix[pos],shift))
341             p=0;
342         else
343             p=0x00FFFFFF;
344         if (alpha == null || isBitSet(alpha[pos],shift))
345             a=0xFF000000;
346         else
347             a=0;
348         return p|a;
349     }
350     
351     
352     private static boolean isBitSet(byte b, int pos) {
353         return ((b & (byte)(1 << pos)) != 0);
354     }
355     
356     
357     private static int toARGB(int s, int type) {
358         switch (type) {
359             case TYPE_USHORT_4444_ARGB: {
360                 int a=((s)&0xF000)>>>12;
361                 int r=((s)&0x0F00)>>>8;
362                 int g=((s)&0x00F0)>>>4;
363                 int b=((s)&0x000F);
364                 
365                 //System.out.println("t"+a+" "+r+" "+g+" "+b);
366                 s=((a*15)<<24)|((r*15)<<16)|((g*15)<<8)|(b*15);
367                 break;
368             }
369             case TYPE_USHORT_444_RGB: {
370                 int r=((s)&0x0F00)>>>8;
371                 int g=((s)&0x00F0)>>>4;
372                 int b=((s)&0x000F);
373                 
374                 //System.out.println("t"+a+" "+r+" "+g+" "+b);
375                 s=((r*15)<<16)|((g*15)<<8)|(b*15);
376                 break;
377             }
378         }
379         return s;
380     }
381     
382     private static int fromARGB(int s, int type) {
383         switch (type) {
384             case TYPE_USHORT_4444_ARGB: {
385                 int a=((s)&0xFF000000)>>>24;
386                 int r=((s)&0x00FF0000)>>>16;
387                 int g=((s)&0x0000FF00)>>>8;
388                 int b=((s)&0x000000FF);
389                 s=((a/15)<<12)|((r/15)<<8)|((g/15)<<4)|(b/15);
390                 break;
391             }
392             case TYPE_USHORT_444_RGB: {
393                 int r=((s)&0x00FF0000)>>>16;
394                 int g=((s)&0x0000FF00)>>>8;
395                 int b=((s)&0x000000FF);
396                 s=((r/15)<<8)|((g/15)<<4)|(b/15);
397                 break;
398             }
399         }
400         return s;
401     }
402     
403     private static boolean isTransparent(int s) {
404         return (s&0xFF000000)==0;
405     }
406 }