javauis/m3g_qt/javasrc/javax/microedition/m3g/Image2D.java
changeset 35 85266cc22c7f
child 40 c6043ea9b06a
equal deleted inserted replaced
26:dc7c549001d5 35:85266cc22c7f
       
     1 /*
       
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package javax.microedition.m3g;
       
    20 
       
    21 import javax.microedition.lcdui.Image;
       
    22 import org.eclipse.swt.graphics.*;
       
    23 import org.eclipse.swt.internal.qt.graphics.*;
       
    24 import com.nokia.mj.impl.nokialcdui.LCDUIInvoker;
       
    25 import org.eclipse.swt.widgets.Internal_PackageSupport;
       
    26 
       
    27 public class Image2D extends Object3D
       
    28 {
       
    29     //------------------------------------------------------------------
       
    30     // Static data
       
    31     //------------------------------------------------------------------
       
    32 
       
    33     public static final int ALPHA               = 96;
       
    34     public static final int LUMINANCE           = 97;
       
    35     public static final int LUMINANCE_ALPHA     = 98;
       
    36     public static final int RGB                 = 99;
       
    37     public static final int RGBA                = 100;
       
    38 
       
    39     static int tempHandle;
       
    40 
       
    41     //------------------------------------------------------------------
       
    42     // Constructor(s)
       
    43     //------------------------------------------------------------------
       
    44 
       
    45     public Image2D(int format, Object image)
       
    46     {
       
    47         // If image is instance of lcdui.Image then checkAndCreate
       
    48         // builds the image and returns the handle to native image,
       
    49         // otherwise throws exception Done this way because class of
       
    50         // image cannot be checked befor calling super()
       
    51         super(Image2D.checkAndCreate(format, image));
       
    52     }
       
    53 
       
    54     public Image2D(int format, int width, int height, byte[] image)
       
    55     {
       
    56         super(createHandle(format, width, height, image));
       
    57     }
       
    58 
       
    59     public Image2D(int format,
       
    60                    int width, int height,
       
    61                    byte[] image,
       
    62                    byte[] palette)
       
    63     {
       
    64         super(createHandle(format, width, height, image, palette));
       
    65     }
       
    66 
       
    67     public Image2D(int format, int width, int height)
       
    68     {
       
    69         super(createHandle(format, width, height));
       
    70     }
       
    71 
       
    72     Image2D(int handle)
       
    73     {
       
    74         super(handle);
       
    75     }
       
    76 
       
    77     //------------------------------------------------------------------
       
    78     // Public methods
       
    79     //------------------------------------------------------------------
       
    80 
       
    81     public void set(int x, int y, int width, int height, byte[] image)
       
    82     {
       
    83         if (image == null)
       
    84         {
       
    85             throw new NullPointerException();
       
    86         }
       
    87         _set(handle, x, y, width, height, image);
       
    88     }
       
    89 
       
    90     public boolean isMutable()
       
    91     {
       
    92         return _isMutable(handle);
       
    93     }
       
    94 
       
    95     public int getFormat()
       
    96     {
       
    97         return _getFormat(handle);
       
    98     }
       
    99 
       
   100     public int getWidth()
       
   101     {
       
   102         return _getWidth(handle);
       
   103     }
       
   104 
       
   105     public int getHeight()
       
   106     {
       
   107         return _getHeight(handle);
       
   108     }
       
   109 
       
   110     //------------------------------------------------------------------
       
   111     // Private methods
       
   112     //------------------------------------------------------------------
       
   113 
       
   114     private static int checkAndCreate(int format, Object image)
       
   115     {
       
   116         if (image == null)
       
   117         {
       
   118             throw new NullPointerException();
       
   119         }
       
   120         if (!(image instanceof javax.microedition.lcdui.Image) &&
       
   121                 !(image instanceof org.eclipse.swt.graphics.Image))
       
   122         {
       
   123             throw new IllegalArgumentException();
       
   124         }
       
   125 
       
   126         final int finalFormat = format;
       
   127         tempHandle = 0;
       
   128 
       
   129         if (image instanceof org.eclipse.swt.graphics.Image)
       
   130         {
       
   131             // get internal image
       
   132             final org.eclipse.swt.internal.qt.graphics.Image cgfxImage = 
       
   133                     Internal_GfxPackageSupport.getImage((org.eclipse.swt.graphics.Image)image);
       
   134             // excute in UI thread
       
   135             Platform.executeInUIThread(
       
   136                 new M3gRunnable()
       
   137             {
       
   138                 void doRun()
       
   139                 {
       
   140                     tempHandle = _ctorImage(Interface.getHandle(), finalFormat, cgfxImage.getHandle());
       
   141                 }
       
   142             });
       
   143         }
       
   144         else if (image instanceof javax.microedition.lcdui.Image)
       
   145         {
       
   146             final org.eclipse.swt.internal.qt.graphics.Image cgfxImage =
       
   147                     Internal_GfxPackageSupport.getImage(
       
   148                             LCDUIInvoker.getEswtImage( (javax.microedition.lcdui.Image)image ) );
       
   149 
       
   150             // excute in UI thread
       
   151             Platform.executeInUIThread(
       
   152                 new M3gRunnable()
       
   153             {
       
   154                 void doRun()
       
   155                 {
       
   156                     tempHandle = _ctorImage(Interface.getHandle(), finalFormat, cgfxImage.getHandle());
       
   157                 }
       
   158             });
       
   159         }
       
   160         return tempHandle;
       
   161     }
       
   162 
       
   163     //Platform.heuristicGC();
       
   164     //ToolkitInvoker invoker = ToolkitInvoker.getToolkitInvoker();
       
   165 
       
   166     // Decide if trueAlpha
       
   167     //Image i = (Image)image;
       
   168     //boolean trueAlpha = !(i.isMutable() && format == ALPHA);
       
   169 
       
   170     //Platform.sync((Image) image);
       
   171 
       
   172 //        Platform.getUIThread().syncExec(
       
   173 //                    new Runnable() {
       
   174 //                        public void run() {
       
   175 //                                               tempHandle = _ctorImage(/*Interface.getEventSourceHandle(),*/ Interface.getHandle(), finalFormat, /*invoker.imageGetHandle(image)*/ 5);
       
   176 //                                          }
       
   177 //                                  });
       
   178 //          return tempHandle;
       
   179 
       
   180 
       
   181     private static int createHandle(int format, int width, int height, byte[] image)
       
   182     {
       
   183         Platform.heuristicGC();
       
   184         return _ctorSizePixels(Interface.getHandle(),
       
   185                                format,
       
   186                                width, height,
       
   187                                image);
       
   188     }
       
   189 
       
   190     private static int createHandle(int format,
       
   191                                     int width, int height,
       
   192                                     byte[] image,
       
   193                                     byte[] palette)
       
   194     {
       
   195         Platform.heuristicGC();
       
   196         return _ctorSizePixelsPalette(Interface.getHandle(),
       
   197                                       format,
       
   198                                       width, height,
       
   199                                       image, palette);
       
   200     }
       
   201 
       
   202     private static int createHandle(int format, int width, int height)
       
   203     {
       
   204         Platform.heuristicGC();
       
   205         return _ctorSize(Interface.getHandle(), format, width, height);
       
   206     }
       
   207 
       
   208     // Native methods
       
   209     private native static int _ctorImage(/*int eventSourceHandle,*/
       
   210         int hInterface,
       
   211         int format,
       
   212         int imageHandle);
       
   213     private native static int _ctorSizePixels(int hInterface,
       
   214             int format,
       
   215             int width, int height,
       
   216             byte[] image);
       
   217     private native static int _ctorSizePixelsPalette(int hInterface,
       
   218             int format,
       
   219             int width, int height,
       
   220             byte[] image,
       
   221             byte[] palette);
       
   222     private native static int _ctorSize(int hInterface,
       
   223                                         int format,
       
   224                                         int width, int height);
       
   225 
       
   226     private native static void _set(int handle, int x, int y, int width,
       
   227                                     int height, byte[] image);
       
   228     private native static boolean _isMutable(int handle);
       
   229     private native static int _getFormat(int handle);
       
   230     private native static int _getWidth(int handle);
       
   231     private native static int _getHeight(int handle);
       
   232 }