javauis/lcdui_qt/src/javax/microedition/lcdui/Image.java
changeset 23 98ccebc37403
parent 21 2a9601315dfc
child 26 dc7c549001d5
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
     9 * Initial Contributors:
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10 * Nokia Corporation - initial contribution.
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description: 
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 package javax.microedition.lcdui;
    17 package javax.microedition.lcdui;
    18 
    18 
    19 import java.io.*;
    19 import java.io.*;
    27  * <br>
    27  * <br>
    28  * Image path handling is not implemented correctly, for example "standard"
    28  * Image path handling is not implemented correctly, for example "standard"
    29  * LCDUI accepts path "/TestMIDlets/images/100x100.png" but corresponding path
    29  * LCDUI accepts path "/TestMIDlets/images/100x100.png" but corresponding path
    30  * with this image-class must be "/100x100.png".
    30  * with this image-class must be "/100x100.png".
    31  */
    31  */
    32 public class Image {
    32 public class Image
       
    33 {
    33 
    34 
    34     private static Image createImageReturnValue;
    35     private static Image createImageReturnValue;
    35 
    36 
    36     private org.eclipse.swt.graphics.Image eswtImage;
    37     private org.eclipse.swt.graphics.Image eswtImage;
    37     private int eswtImageWidth;
    38     private int eswtImageWidth;
    43     private com.nokia.mj.impl.rt.support.Finalizer finalizer;
    44     private com.nokia.mj.impl.rt.support.Finalizer finalizer;
    44 
    45 
    45     /**
    46     /**
    46      * Constructor.
    47      * Constructor.
    47      */
    48      */
    48     Image(org.eclipse.swt.graphics.Image eswtImage, boolean isMutable) {
    49     Image(org.eclipse.swt.graphics.Image eswtImage, boolean isMutable)
       
    50     {
    49         this.eswtImage = eswtImage;
    51         this.eswtImage = eswtImage;
    50         this.mutable = isMutable;
    52         this.mutable = isMutable;
    51         eswtImageWidth = eswtImage.getBounds().width;
    53         eswtImageWidth = eswtImage.getBounds().width;
    52         eswtImageHeight = eswtImage.getBounds().height;
    54         eswtImageHeight = eswtImage.getBounds().height;
    53         finalizer = ((finalizer != null) ? finalizer
    55         finalizer = ((finalizer != null) ? finalizer
    54                 : new com.nokia.mj.impl.rt.support.Finalizer() {
    56                      : new com.nokia.mj.impl.rt.support.Finalizer()
    55                     public void finalizeImpl() {
    57         {
    56                         if (finalizer != null) {
    58             public void finalizeImpl()
    57                             finalizer = null;
    59             {
    58                             if (!ESWTUIThreadRunner.isDisposed()) {
    60                 if(finalizer != null)
    59                                 dispose();
    61                 {
    60                             }
    62                     finalizer = null;
    61                         }
    63                     if(!ESWTUIThreadRunner.isDisposed())
       
    64                     {
       
    65                         dispose();
    62                     }
    66                     }
    63                 });
    67                 }
       
    68             }
       
    69         });
    64         ESWTUIThreadRunner.update(getClass().getName(), 1);
    70         ESWTUIThreadRunner.update(getClass().getName(), 1);
    65     }
    71     }
    66 
    72 
    67     /**
    73     /**
    68      * Called by finalizer when Image is ready to free its resources.
    74      * Called by finalizer when Image is ready to free its resources.
    69      */
    75      */
    70     void dispose() {
    76     void dispose()
       
    77     {
    71         ESWTUIThreadRunner.update(getClass().getName(), -1);
    78         ESWTUIThreadRunner.update(getClass().getName(), -1);
    72         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
    79         ESWTUIThreadRunner.safeSyncExec(new Runnable()
    73             public void run() {
    80         {
    74                 if (eswtImage != null) {
    81             public void run()
       
    82             {
       
    83                 if(eswtImage != null)
       
    84                 {
    75                     eswtImage.dispose();
    85                     eswtImage.dispose();
    76                     eswtImage = null;
    86                     eswtImage = null;
    77                 }
    87                 }
    78             }
    88             }
    79         });
    89         });
    80     }
    90     }
    81 
    91 
    82     /**
    92     /**
    83      * Return eSWT Image object.
    93      * Return eSWT Image object.
    84      */
    94      */
    85     org.eclipse.swt.graphics.Image getESWTImage() {
    95     org.eclipse.swt.graphics.Image getESWTImage()
       
    96     {
    86         return eswtImage;
    97         return eswtImage;
    87     }
    98     }
    88 
    99 
    89     /**
   100     /**
    90      * Return eSWT Image object.
   101      * Return eSWT Image object.
    91      *
   102      *
    92      * @param img Image
   103      * @param img Image
    93      * @return eSWT Image
   104      * @return eSWT Image
    94      */
   105      */
    95     static org.eclipse.swt.graphics.Image getESWTImage(Image img) {
   106     static org.eclipse.swt.graphics.Image getESWTImage(Image img)
    96         if (img != null) {
   107     {
       
   108         if(img != null)
       
   109         {
    97             return img.eswtImage;
   110             return img.eswtImage;
    98         }
   111         }
    99         return null;
   112         return null;
   100     }
   113     }
   101 
   114 
   105      * @param w The width of the image to be created.
   118      * @param w The width of the image to be created.
   106      * @param h The height of the image to be created.
   119      * @param h The height of the image to be created.
   107      * @return Image created.
   120      * @return Image created.
   108      * @throws IllegalArgumentException if width or height are zero or less.
   121      * @throws IllegalArgumentException if width or height are zero or less.
   109      */
   122      */
   110     public static Image createImage(int w, int h) {
   123     public static Image createImage(int w, int h)
   111         if (w <= 0 || h <= 0) {
   124     {
       
   125         if(w <= 0 || h <= 0)
       
   126         {
   112             throw new IllegalArgumentException(
   127             throw new IllegalArgumentException(
   113                     MsgRepository.IMAGE_EXCEPTION_INVALID_DIMENSIONS);
   128                 MsgRepository.IMAGE_EXCEPTION_INVALID_DIMENSIONS);
   114         }
   129         }
   115 
   130 
   116         final int width = w;
   131         final int width = w;
   117         final int height = h;
   132         final int height = h;
   118         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
   133         ESWTUIThreadRunner.safeSyncExec(new Runnable()
   119             public void run() {
   134         {
       
   135             public void run()
       
   136             {
   120                 org.eclipse.swt.graphics.Image eswtImg =
   137                 org.eclipse.swt.graphics.Image eswtImg =
   121                     eswtCreateImage(width, height);
   138                     eswtCreateImage(width, height);
   122 
   139 
   123                 if (eswtImg != null) {
   140                 if(eswtImg != null)
       
   141                 {
   124                     createImageReturnValue = new Image(eswtImg, true);
   142                     createImageReturnValue = new Image(eswtImg, true);
   125                 }
   143                 }
   126                 else {
   144                 else
       
   145                 {
   127                     createImageReturnValue = null;
   146                     createImageReturnValue = null;
   128                     throw new OutOfMemoryError();
   147                     throw new OutOfMemoryError();
   129                 }
   148                 }
   130             }
   149             }
   131         });
   150         });
   140      * @param width the width of the new image.
   159      * @param width the width of the new image.
   141      * @param height the height of the new image.
   160      * @param height the height of the new image.
   142      * @return the resized Image.
   161      * @return the resized Image.
   143      * @throws NullPointerException if aImage is null.
   162      * @throws NullPointerException if aImage is null.
   144      */
   163      */
   145     static Image createImage(Image srcImage, int width, int height) {
   164     static Image createImage(Image srcImage, int width, int height)
   146         if (srcImage == null) {
   165     {
       
   166         if(srcImage == null)
       
   167         {
   147             throw new NullPointerException(
   168             throw new NullPointerException(
   148                     MsgRepository.IMAGE_EXCEPTION_IS_NULL);
   169                 MsgRepository.IMAGE_EXCEPTION_IS_NULL);
   149         }
   170         }
   150         final Image finalImage = srcImage;
   171         final Image finalImage = srcImage;
   151         final int finalWidth = width;
   172         final int finalWidth = width;
   152         final int finalHeight = height;
   173         final int finalHeight = height;
   153         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
   174         ESWTUIThreadRunner.safeSyncExec(new Runnable()
   154             public void run() {
   175         {
       
   176             public void run()
       
   177             {
   155                 org.eclipse.swt.graphics.Image eswtImg =
   178                 org.eclipse.swt.graphics.Image eswtImg =
   156                     eswtCreateImage(finalImage.eswtImage, finalWidth, finalHeight);
   179                     eswtCreateImage(finalImage.eswtImage, finalWidth, finalHeight);
   157 
   180 
   158                 if (eswtImg != null) {
   181                 if(eswtImg != null)
       
   182                 {
   159                     createImageReturnValue = new Image(eswtImg, false);
   183                     createImageReturnValue = new Image(eswtImg, false);
   160                 }
   184                 }
   161                 else {
   185                 else
       
   186                 {
   162                     createImageReturnValue = null;
   187                     createImageReturnValue = null;
   163                     throw new OutOfMemoryError();
   188                     throw new OutOfMemoryError();
   164                 }
   189                 }
   165             }
   190             }
   166         });
   191         });
   172      *
   197      *
   173      * @param srcImage The source of image.
   198      * @param srcImage The source of image.
   174      * @return Image created.
   199      * @return Image created.
   175      * @throws NullPointerException if source image is null.
   200      * @throws NullPointerException if source image is null.
   176      */
   201      */
   177     public static Image createImage(Image srcImage) {
   202     public static Image createImage(Image srcImage)
   178         if (srcImage == null) {
   203     {
       
   204         if(srcImage == null)
       
   205         {
   179             throw new NullPointerException(
   206             throw new NullPointerException(
   180                     MsgRepository.IMAGE_EXCEPTION_IS_NULL);
   207                 MsgRepository.IMAGE_EXCEPTION_IS_NULL);
   181         }
   208         }
   182 
   209 
   183         if (!srcImage.isMutable()) {
   210         if(!srcImage.isMutable())
       
   211         {
   184             return srcImage;
   212             return srcImage;
   185         }
   213         }
   186 
   214 
   187         final Image finalImage = srcImage;
   215         final Image finalImage = srcImage;
   188         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
   216         ESWTUIThreadRunner.safeSyncExec(new Runnable()
   189             public void run() {
   217         {
       
   218             public void run()
       
   219             {
   190                 org.eclipse.swt.graphics.Image eswtImg =
   220                 org.eclipse.swt.graphics.Image eswtImg =
   191                     eswtCreateImage(finalImage.eswtImage);
   221                     eswtCreateImage(finalImage.eswtImage);
   192 
   222 
   193                 if (eswtImg != null) {
   223                 if(eswtImg != null)
       
   224                 {
   194                     createImageReturnValue = new Image(eswtImg, false);
   225                     createImageReturnValue = new Image(eswtImg, false);
   195                 }
   226                 }
   196                 else {
   227                 else
       
   228                 {
   197                     createImageReturnValue = null;
   229                     createImageReturnValue = null;
   198                     throw new OutOfMemoryError();
   230                     throw new OutOfMemoryError();
   199                 }
   231                 }
   200             }
   232             }
   201         });
   233         });
   209      * @return Image created.
   241      * @return Image created.
   210      * @throws NullPointerException if name is null.
   242      * @throws NullPointerException if name is null.
   211      * @throws IOException If resource doesn't exist, data cannot be loaded or
   243      * @throws IOException If resource doesn't exist, data cannot be loaded or
   212      *             data is invalid.
   244      *             data is invalid.
   213      */
   245      */
   214     public static Image createImage(String name) throws IOException {
   246     public static Image createImage(String name) throws IOException
   215         if (name == null) {
   247     {
       
   248         if(name == null)
       
   249         {
   216             throw new NullPointerException(
   250             throw new NullPointerException(
   217                     MsgRepository.IMAGE_EXCEPTION_FILE_NAME_IS_NULL);
   251                 MsgRepository.IMAGE_EXCEPTION_FILE_NAME_IS_NULL);
   218         }
   252         }
   219 
   253 
   220         String fileName = name;
   254         String fileName = name;
   221         if (!fileName.startsWith("/")) {
   255         if(!fileName.startsWith("/"))
       
   256         {
   222             fileName = "/" + fileName;
   257             fileName = "/" + fileName;
   223         }
   258         }
   224 
   259 
   225         InputStream stream = Image.class.getResourceAsStream(fileName);
   260         InputStream stream = Image.class.getResourceAsStream(fileName);
   226         if (stream == null) {
   261         if(stream == null)
       
   262         {
   227             throw new IOException(MsgRepository.IMAGE_EXCEPTION_IO_ERROR);
   263             throw new IOException(MsgRepository.IMAGE_EXCEPTION_IO_ERROR);
   228         }
   264         }
   229         Image image = createImage(stream);
   265         Image image = createImage(stream);
   230         try {
   266         try
       
   267         {
   231             stream.close();
   268             stream.close();
   232         }
   269         }
   233         catch (IOException e) {
   270         catch(IOException e)
       
   271         {
   234             // ignore
   272             // ignore
   235         }
   273         }
   236         return image;
   274         return image;
   237     }
   275     }
   238 
   276 
   244      * @return Image created.
   282      * @return Image created.
   245      * @throws NullPointerException if stream is null.
   283      * @throws NullPointerException if stream is null.
   246      * @throws java.io.IOException If I/O error occurs or data from stream is
   284      * @throws java.io.IOException If I/O error occurs or data from stream is
   247      *             invalid.
   285      *             invalid.
   248      */
   286      */
   249     public static Image createImage(InputStream stream) throws java.io.IOException {
   287     public static Image createImage(InputStream stream) throws java.io.IOException
   250         if (stream == null) {
   288     {
       
   289         if(stream == null)
       
   290         {
   251             throw new NullPointerException(
   291             throw new NullPointerException(
   252                     MsgRepository.IMAGE_EXCEPTION_FILE_STREAM_IS_NULL);
   292                 MsgRepository.IMAGE_EXCEPTION_FILE_STREAM_IS_NULL);
   253         }
   293         }
   254         final InputStream finalStream = stream;
   294         final InputStream finalStream = stream;
   255         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
   295         ESWTUIThreadRunner.safeSyncExec(new Runnable()
   256             public void run() {
   296         {
       
   297             public void run()
       
   298             {
   257                 org.eclipse.swt.graphics.Image eswtImg = eswtCreateImage(finalStream);
   299                 org.eclipse.swt.graphics.Image eswtImg = eswtCreateImage(finalStream);
   258 
   300 
   259                 if (eswtImg != null) {
   301                 if(eswtImg != null)
       
   302                 {
   260                     createImageReturnValue = new Image(eswtImg, false);
   303                     createImageReturnValue = new Image(eswtImg, false);
   261                 }
   304                 }
   262                 else {
   305                 else
       
   306                 {
   263                     createImageReturnValue = null;
   307                     createImageReturnValue = null;
   264                 }
   308                 }
   265             }
   309             }
   266         });
   310         });
   267         if (createImageReturnValue == null) {
   311         if(createImageReturnValue == null)
       
   312         {
   268             throw new IOException(MsgRepository.IMAGE_EXCEPTION_IO_ERROR);
   313             throw new IOException(MsgRepository.IMAGE_EXCEPTION_IO_ERROR);
   269         }
   314         }
   270         return createImageReturnValue;
   315         return createImageReturnValue;
   271     }
   316     }
   272 
   317 
   280      * @throws ArrayIndexOutOfBoundsException if imgOffset and imgLength
   325      * @throws ArrayIndexOutOfBoundsException if imgOffset and imgLength
   281      *             specifies invalid range.
   326      *             specifies invalid range.
   282      * @throws NullPointerException if imgData is null.
   327      * @throws NullPointerException if imgData is null.
   283      * @throws IllegalArgumentException if imgData content is invalid.
   328      * @throws IllegalArgumentException if imgData content is invalid.
   284      */
   329      */
   285     public static Image createImage(byte[] imgData, int imgOffset, int imgLength) {
   330     public static Image createImage(byte[] imgData, int imgOffset, int imgLength)
   286         if (imgData == null) {
   331     {
       
   332         if(imgData == null)
       
   333         {
   287             throw new NullPointerException(
   334             throw new NullPointerException(
   288                     MsgRepository.IMAGE_EXCEPTION_DATA_IS_NULL);
   335                 MsgRepository.IMAGE_EXCEPTION_DATA_IS_NULL);
   289         }
   336         }
   290         if (imgOffset + imgLength > imgData.length) {
   337         if(imgOffset + imgLength > imgData.length)
       
   338         {
   291             throw new ArrayIndexOutOfBoundsException(
   339             throw new ArrayIndexOutOfBoundsException(
   292                     MsgRepository.IMAGE_EXCEPTION_INVALID_BOUNDS);
   340                 MsgRepository.IMAGE_EXCEPTION_INVALID_BOUNDS);
   293         }
   341         }
   294         if (imgOffset < 0 || imgLength < 0) {
   342         if(imgOffset < 0 || imgLength < 0)
       
   343         {
   295             throw new ArrayIndexOutOfBoundsException(
   344             throw new ArrayIndexOutOfBoundsException(
   296                     MsgRepository.IMAGE_EXCEPTION_INVALID_BOUNDS);
   345                 MsgRepository.IMAGE_EXCEPTION_INVALID_BOUNDS);
   297         }
   346         }
   298 
   347 
   299         InputStream inputStream = new ByteArrayInputStream(imgData, imgOffset, imgLength);
   348         InputStream inputStream = new ByteArrayInputStream(imgData, imgOffset, imgLength);
   300         try {
   349         try
       
   350         {
   301             createImageReturnValue = createImage(inputStream);
   351             createImageReturnValue = createImage(inputStream);
   302         }
   352         }
   303         catch (IOException e) {
   353         catch(IOException e)
       
   354         {
   304             throw new IllegalArgumentException(
   355             throw new IllegalArgumentException(
   305                         MsgRepository.IMAGE_EXCEPTION_INVALID_DATA);
   356                 MsgRepository.IMAGE_EXCEPTION_INVALID_DATA);
   306         }
   357         }
   307         finally {
   358         finally
   308             try {
   359         {
       
   360             try
       
   361             {
   309                 inputStream.close();
   362                 inputStream.close();
   310             }
   363             }
   311             catch (IOException e) {
   364             catch(IOException e)
       
   365             {
   312                 // ignore
   366                 // ignore
   313             }
   367             }
   314         }
   368         }
   315         return createImageReturnValue;
   369         return createImageReturnValue;
   316     }
   370     }
   335     public static Image createImage(Image srcImage,
   389     public static Image createImage(Image srcImage,
   336                                     int x,
   390                                     int x,
   337                                     int y,
   391                                     int y,
   338                                     int w,
   392                                     int w,
   339                                     int h,
   393                                     int h,
   340                                     int transform) {
   394                                     int transform)
   341         if (srcImage == null) {
   395     {
       
   396         if(srcImage == null)
       
   397         {
   342             throw new NullPointerException(
   398             throw new NullPointerException(
   343                     MsgRepository.IMAGE_EXCEPTION_IS_NULL);
   399                 MsgRepository.IMAGE_EXCEPTION_IS_NULL);
   344         }
   400         }
   345         if (w <= 0 || h <= 0) {
   401         if(w <= 0 || h <= 0)
       
   402         {
   346             throw new IllegalArgumentException(
   403             throw new IllegalArgumentException(
   347                     MsgRepository.IMAGE_EXCEPTION_INVALID_DIMENSIONS);
   404                 MsgRepository.IMAGE_EXCEPTION_INVALID_DIMENSIONS);
   348         }
   405         }
   349         if (!validateTransform(transform)) {
   406         if(!validateTransform(transform))
       
   407         {
   350             throw new IllegalArgumentException(
   408             throw new IllegalArgumentException(
   351                     MsgRepository.IMAGE_EXCEPTION_INVALID_TRANSFORM);
   409                 MsgRepository.IMAGE_EXCEPTION_INVALID_TRANSFORM);
   352         }
   410         }
   353         if (!validateRegion(srcImage.getWidth(), srcImage.getHeight(), x, y, w, h)) {
   411         if(!validateRegion(srcImage.getWidth(), srcImage.getHeight(), x, y, w, h))
       
   412         {
   354             throw new IllegalArgumentException(
   413             throw new IllegalArgumentException(
   355                     MsgRepository.IMAGE_EXCEPTION_INVALID_REGION);
   414                 MsgRepository.IMAGE_EXCEPTION_INVALID_REGION);
   356         }
   415         }
   357 
   416 
   358         final Image fImage = srcImage;
   417         final Image fImage = srcImage;
   359         final int fx = x;
   418         final int fx = x;
   360         final int fy = y;
   419         final int fy = y;
   361         final int fw = w;
   420         final int fw = w;
   362         final int fh = h;
   421         final int fh = h;
   363         final int fTransform = transform;
   422         final int fTransform = transform;
   364         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
   423         ESWTUIThreadRunner.safeSyncExec(new Runnable()
   365             public void run() {
   424         {
       
   425             public void run()
       
   426             {
   366                 org.eclipse.swt.internal.qt.graphics.Image srcCgImage =
   427                 org.eclipse.swt.internal.qt.graphics.Image srcCgImage =
   367                     Internal_GfxPackageSupport.getImage(fImage.getESWTImage());
   428                     Internal_GfxPackageSupport.getImage(fImage.getESWTImage());
   368 
   429 
   369                 org.eclipse.swt.internal.qt.graphics.Image cgImage =
   430                 org.eclipse.swt.internal.qt.graphics.Image cgImage =
   370                     new org.eclipse.swt.internal.qt.graphics.Image(
   431                     new org.eclipse.swt.internal.qt.graphics.Image(
   371                             srcCgImage, fx, fy, fw, fh);
   432                     srcCgImage, fx, fy, fw, fh);
   372 
   433 
   373                 cgImage.transform(getCgTransformValue(fTransform));
   434                 cgImage.transform(getCgTransformValue(fTransform));
   374 
   435 
   375                 org.eclipse.swt.graphics.Image eswtImg =
   436                 org.eclipse.swt.graphics.Image eswtImg =
   376                     Internal_GfxPackageSupport.new_Image(
   437                     Internal_GfxPackageSupport.new_Image(
   377                             ESWTUIThreadRunner.getInstance().getDisplay(),
   438                         ESWTUIThreadRunner.getInstance().getDisplay(),
   378                             cgImage);
   439                         cgImage);
   379 
   440 
   380                 if (eswtImg != null) {
   441                 if(eswtImg != null)
       
   442                 {
   381                     createImageReturnValue = new Image(eswtImg, false);
   443                     createImageReturnValue = new Image(eswtImg, false);
   382                 }
   444                 }
   383                 else {
   445                 else
       
   446                 {
   384                     createImageReturnValue = null;
   447                     createImageReturnValue = null;
   385                     throw new OutOfMemoryError();
   448                     throw new OutOfMemoryError();
   386                 }
   449                 }
   387             }
   450             }
   388         });
   451         });
   392     /**
   455     /**
   393      * Validates that a specified region is fully located within the image
   456      * Validates that a specified region is fully located within the image
   394      * bounds. Method is package-private, used by Graphics.drawRegion as well.
   457      * bounds. Method is package-private, used by Graphics.drawRegion as well.
   395      */
   458      */
   396     static boolean validateRegion(int srcWidth, int srcHeight, int x, int y,
   459     static boolean validateRegion(int srcWidth, int srcHeight, int x, int y,
   397             int w, int h) {
   460                                   int w, int h)
       
   461     {
   398         return x >= 0 && y >= 0 && w <= srcWidth && h <= srcHeight;
   462         return x >= 0 && y >= 0 && w <= srcWidth && h <= srcHeight;
   399     }
   463     }
   400 
   464 
   401     /**
   465     /**
   402      * Validates if transform has a valid value. Method is package-private, used
   466      * Validates if transform has a valid value. Method is package-private, used
   403      * by Graphics.drawRegion as well.
   467      * by Graphics.drawRegion as well.
   404      */
   468      */
   405     static boolean validateTransform(int transform) {
   469     static boolean validateTransform(int transform)
       
   470     {
   406         return transform == Sprite.TRANS_NONE
   471         return transform == Sprite.TRANS_NONE
   407             || transform == Sprite.TRANS_MIRROR
   472                || transform == Sprite.TRANS_MIRROR
   408             || transform == Sprite.TRANS_MIRROR_ROT90
   473                || transform == Sprite.TRANS_MIRROR_ROT90
   409             || transform == Sprite.TRANS_MIRROR_ROT180
   474                || transform == Sprite.TRANS_MIRROR_ROT180
   410             || transform == Sprite.TRANS_MIRROR_ROT270
   475                || transform == Sprite.TRANS_MIRROR_ROT270
   411             || transform == Sprite.TRANS_ROT90
   476                || transform == Sprite.TRANS_ROT90
   412             || transform == Sprite.TRANS_ROT180
   477                || transform == Sprite.TRANS_ROT180
   413             || transform == Sprite.TRANS_ROT270;
   478                || transform == Sprite.TRANS_ROT270;
   414     }
   479     }
   415 
   480 
   416     /**
   481     /**
   417      *  Maps LCDUI transform constants to Common Graphics. Method is package-private, used
   482      *  Maps LCDUI transform constants to Common Graphics. Method is package-private, used
   418      *  by Graphics.drawRegion as well.
   483      *  by Graphics.drawRegion as well.
   419      */
   484      */
   420     static int getCgTransformValue(int transform) {
   485     static int getCgTransformValue(int transform)
       
   486     {
   421         int retVal = org.eclipse.swt.internal.qt.graphics.Image.TRANS_NONE;
   487         int retVal = org.eclipse.swt.internal.qt.graphics.Image.TRANS_NONE;
   422         switch (transform) {
   488         switch(transform)
   423             case Sprite.TRANS_NONE:
   489         {
   424                 retVal = org.eclipse.swt.internal.qt.graphics.Image.TRANS_NONE;
   490         case Sprite.TRANS_NONE:
   425                 break;
   491             retVal = org.eclipse.swt.internal.qt.graphics.Image.TRANS_NONE;
   426             case Sprite.TRANS_ROT90:
   492             break;
   427                 retVal = org.eclipse.swt.internal.qt.graphics.Image.TRANS_ROT90;
   493         case Sprite.TRANS_ROT90:
   428                 break;
   494             retVal = org.eclipse.swt.internal.qt.graphics.Image.TRANS_ROT90;
   429             case Sprite.TRANS_ROT180:
   495             break;
   430                 retVal =
   496         case Sprite.TRANS_ROT180:
   431                     org.eclipse.swt.internal.qt.graphics.Image.TRANS_ROT180;
   497             retVal =
   432                 break;
   498                 org.eclipse.swt.internal.qt.graphics.Image.TRANS_ROT180;
   433             case Sprite.TRANS_ROT270:
   499             break;
   434                 retVal =
   500         case Sprite.TRANS_ROT270:
   435                     org.eclipse.swt.internal.qt.graphics.Image.TRANS_ROT270;
   501             retVal =
   436                 break;
   502                 org.eclipse.swt.internal.qt.graphics.Image.TRANS_ROT270;
   437             case Sprite.TRANS_MIRROR:
   503             break;
   438                 retVal =
   504         case Sprite.TRANS_MIRROR:
   439                     org.eclipse.swt.internal.qt.graphics.Image.TRANS_MIRROR;
   505             retVal =
   440                 break;
   506                 org.eclipse.swt.internal.qt.graphics.Image.TRANS_MIRROR;
   441             case Sprite.TRANS_MIRROR_ROT90:
   507             break;
   442                 retVal = org.eclipse.swt.internal.qt.graphics.
   508         case Sprite.TRANS_MIRROR_ROT90:
   443                                             Image.TRANS_MIRROR_ROT90;
   509             retVal = org.eclipse.swt.internal.qt.graphics.
   444                 break;
   510                      Image.TRANS_MIRROR_ROT90;
   445             case Sprite.TRANS_MIRROR_ROT180:
   511             break;
   446                 retVal = org.eclipse.swt.internal.qt.graphics.
   512         case Sprite.TRANS_MIRROR_ROT180:
   447                                             Image.TRANS_MIRROR_ROT180;
   513             retVal = org.eclipse.swt.internal.qt.graphics.
   448                 break;
   514                      Image.TRANS_MIRROR_ROT180;
   449             case Sprite.TRANS_MIRROR_ROT270:
   515             break;
   450                 retVal = org.eclipse.swt.internal.qt.graphics.
   516         case Sprite.TRANS_MIRROR_ROT270:
   451                                             Image.TRANS_MIRROR_ROT270;
   517             retVal = org.eclipse.swt.internal.qt.graphics.
   452                 break;
   518                      Image.TRANS_MIRROR_ROT270;
   453             default:
   519             break;
   454                 break;
   520         default:
       
   521             break;
   455         }
   522         }
   456         return retVal;
   523         return retVal;
   457     }
   524     }
   458 
   525 
   459     /**
   526     /**
   461      *
   528      *
   462      * @param image - image to be copied.
   529      * @param image - image to be copied.
   463      * @return new eSWT image.
   530      * @return new eSWT image.
   464      */
   531      */
   465     private static org.eclipse.swt.graphics.Image eswtCreateImage(
   532     private static org.eclipse.swt.graphics.Image eswtCreateImage(
   466                                     org.eclipse.swt.graphics.Image srcImage) {
   533         org.eclipse.swt.graphics.Image srcImage)
       
   534     {
   467         org.eclipse.swt.graphics.Image ret = null;
   535         org.eclipse.swt.graphics.Image ret = null;
   468         try {
   536         try
       
   537         {
   469             org.eclipse.swt.internal.qt.graphics.Image cgImage =
   538             org.eclipse.swt.internal.qt.graphics.Image cgImage =
   470                 Internal_GfxPackageSupport.getImage(srcImage);
   539                 Internal_GfxPackageSupport.getImage(srcImage);
   471 
   540 
   472             org.eclipse.swt.internal.qt.graphics.Image cgImage2 =
   541             org.eclipse.swt.internal.qt.graphics.Image cgImage2 =
   473                 new org.eclipse.swt.internal.qt.graphics.Image(cgImage);
   542                 new org.eclipse.swt.internal.qt.graphics.Image(cgImage);
   474 
   543 
   475             ret = Internal_GfxPackageSupport.new_Image(
   544             ret = Internal_GfxPackageSupport.new_Image(
   476                     ESWTUIThreadRunner.getInstance().getDisplay(), cgImage2);
   545                       ESWTUIThreadRunner.getInstance().getDisplay(), cgImage2);
   477         }
   546         }
   478         catch (IllegalArgumentException ex) {
   547         catch(IllegalArgumentException ex)
       
   548         {
   479             // Device is null or there's no current device.
   549             // Device is null or there's no current device.
   480             ret = null;
   550             ret = null;
   481         }
   551         }
   482         catch (org.eclipse.swt.SWTException ex) {
   552         catch(org.eclipse.swt.SWTException ex)
   483             ret = null;
   553         {
   484         }
   554             ret = null;
   485         catch (org.eclipse.swt.SWTError err) {
   555         }
       
   556         catch(org.eclipse.swt.SWTError err)
       
   557         {
   486             ret = null;
   558             ret = null;
   487         }
   559         }
   488         return ret;
   560         return ret;
   489     }
   561     }
   490 
   562 
   491     /**
   563     /**
   492      * Creates eSWT image of the specified size from the given image.
   564      * Creates eSWT image of the specified size from the given image.
   493      */
   565      */
   494     static org.eclipse.swt.graphics.Image eswtCreateImage(
   566     static org.eclipse.swt.graphics.Image eswtCreateImage(
   495             org.eclipse.swt.graphics.Image image, int newWidth, int newHeight) {
   567         org.eclipse.swt.graphics.Image image, int newWidth, int newHeight)
       
   568     {
   496         org.eclipse.swt.graphics.Image ret = null;
   569         org.eclipse.swt.graphics.Image ret = null;
   497         try {
   570         try
       
   571         {
   498             ret = new org.eclipse.swt.graphics.Image(
   572             ret = new org.eclipse.swt.graphics.Image(
   499                     ESWTUIThreadRunner.getInstance().getDisplay(),
   573                 ESWTUIThreadRunner.getInstance().getDisplay(),
   500                     image.getImageData().scaledTo(newWidth, newHeight));
   574                 image.getImageData().scaledTo(newWidth, newHeight));
   501         }
   575         }
   502         catch (IllegalArgumentException ex) {
   576         catch(IllegalArgumentException ex)
       
   577         {
   503             // Device is null or there's no current device.
   578             // Device is null or there's no current device.
   504             ret = null;
   579             ret = null;
   505         }
   580         }
   506         catch (org.eclipse.swt.SWTException ex) {
   581         catch(org.eclipse.swt.SWTException ex)
   507             ret = null;
   582         {
   508         }
   583             ret = null;
   509         catch (org.eclipse.swt.SWTError err) {
   584         }
       
   585         catch(org.eclipse.swt.SWTError err)
       
   586         {
   510             ret = null;
   587             ret = null;
   511         }
   588         }
   512         return ret;
   589         return ret;
   513     }
   590     }
   514 
   591 
   515     /**
   592     /**
   516      * Creates eSWT image of the specified size.
   593      * Creates eSWT image of the specified size.
   517      */
   594      */
   518     static org.eclipse.swt.graphics.Image eswtCreateImage(int width, int height) {
   595     static org.eclipse.swt.graphics.Image eswtCreateImage(int width, int height)
       
   596     {
   519         org.eclipse.swt.graphics.Image ret = null;
   597         org.eclipse.swt.graphics.Image ret = null;
   520         try {
   598         try
       
   599         {
   521             ret = new org.eclipse.swt.graphics.Image(
   600             ret = new org.eclipse.swt.graphics.Image(
   522                     ESWTUIThreadRunner.getInstance().getDisplay(), width, height);
   601                 ESWTUIThreadRunner.getInstance().getDisplay(), width, height);
   523         }
   602         }
   524         catch (IllegalArgumentException ex) {
   603         catch(IllegalArgumentException ex)
       
   604         {
   525             // Device is null or there's no current device.
   605             // Device is null or there's no current device.
   526             ret = null;
   606             ret = null;
   527         }
   607         }
   528         catch (org.eclipse.swt.SWTException ex) {
   608         catch(org.eclipse.swt.SWTException ex)
   529             ret = null;
   609         {
   530         }
   610             ret = null;
   531         catch (org.eclipse.swt.SWTError err) {
   611         }
       
   612         catch(org.eclipse.swt.SWTError err)
       
   613         {
   532             ret = null;
   614             ret = null;
   533         }
   615         }
   534         return ret;
   616         return ret;
   535     }
   617     }
   536 
   618 
   537     /**
   619     /**
   538      * Creates and eSWT image out of the InputStream.
   620      * Creates and eSWT image out of the InputStream.
   539      */
   621      */
   540     static org.eclipse.swt.graphics.Image eswtCreateImage(InputStream inputStream) {
   622     static org.eclipse.swt.graphics.Image eswtCreateImage(InputStream inputStream)
       
   623     {
   541         org.eclipse.swt.graphics.Image ret = null;
   624         org.eclipse.swt.graphics.Image ret = null;
   542         try {
   625         try
       
   626         {
   543             ret = new org.eclipse.swt.graphics.Image(
   627             ret = new org.eclipse.swt.graphics.Image(
   544                     ESWTUIThreadRunner.getInstance().getDisplay(), inputStream);
   628                 ESWTUIThreadRunner.getInstance().getDisplay(), inputStream);
   545         }
   629         }
   546         catch (IllegalArgumentException ex) {
   630         catch(IllegalArgumentException ex)
       
   631         {
   547             // Device is null or there's no current device.
   632             // Device is null or there's no current device.
   548             ret = null;
   633             ret = null;
   549         }
   634         }
   550         catch (org.eclipse.swt.SWTException ex) {
   635         catch(org.eclipse.swt.SWTException ex)
   551             ret = null;
   636         {
   552         }
   637             ret = null;
   553         catch (org.eclipse.swt.SWTError err) {
   638         }
       
   639         catch(org.eclipse.swt.SWTError err)
       
   640         {
   554             ret = null;
   641             ret = null;
   555         }
   642         }
   556         return ret;
   643         return ret;
   557     }
   644     }
   558 
   645 
   571      *             match the length of the data array provided.
   658      *             match the length of the data array provided.
   572      */
   659      */
   573     public static Image createRGBImage(int[] rgbData,
   660     public static Image createRGBImage(int[] rgbData,
   574                                        int width,
   661                                        int width,
   575                                        int height,
   662                                        int height,
   576                                        boolean processAlpha) {
   663                                        boolean processAlpha)
   577 
   664     {
   578         if (rgbData == null) {
   665 
       
   666         if(rgbData == null)
       
   667         {
   579             throw new NullPointerException(
   668             throw new NullPointerException(
   580                     MsgRepository.IMAGE_EXCEPTION_DATA_IS_NULL);
   669                 MsgRepository.IMAGE_EXCEPTION_DATA_IS_NULL);
   581         }
   670         }
   582         if (width <= 0 || height <= 0) {
   671         if(width <= 0 || height <= 0)
       
   672         {
   583             throw new IllegalArgumentException(
   673             throw new IllegalArgumentException(
   584                     MsgRepository.IMAGE_EXCEPTION_INVALID_DIMENSIONS);
   674                 MsgRepository.IMAGE_EXCEPTION_INVALID_DIMENSIONS);
   585         }
   675         }
   586         if (width * height > rgbData.length) {
   676         if(width * height > rgbData.length)
       
   677         {
   587             throw new ArrayIndexOutOfBoundsException(
   678             throw new ArrayIndexOutOfBoundsException(
   588                     MsgRepository.IMAGE_EXCEPTION_INVALID_BOUNDS);
   679                 MsgRepository.IMAGE_EXCEPTION_INVALID_BOUNDS);
   589         }
   680         }
   590 
   681 
   591         final int[] rgb = rgbData;
   682         final int[] rgb = rgbData;
   592         final int w = width;
   683         final int w = width;
   593         final int h = height;
   684         final int h = height;
   594         final boolean alpha = processAlpha;
   685         final boolean alpha = processAlpha;
   595         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
   686         ESWTUIThreadRunner.safeSyncExec(new Runnable()
   596             public void run() {
   687         {
       
   688             public void run()
       
   689             {
   597                 org.eclipse.swt.internal.qt.graphics.Image cgImage =
   690                 org.eclipse.swt.internal.qt.graphics.Image cgImage =
   598                     new org.eclipse.swt.internal.qt.graphics.Image(rgb, w, h, alpha);
   691                     new org.eclipse.swt.internal.qt.graphics.Image(rgb, w, h, alpha);
   599 
   692 
   600                 org.eclipse.swt.graphics.Image eswtImg =
   693                 org.eclipse.swt.graphics.Image eswtImg =
   601                     Internal_GfxPackageSupport.new_Image(
   694                     Internal_GfxPackageSupport.new_Image(
   602                             ESWTUIThreadRunner.getInstance().getDisplay(), cgImage);
   695                         ESWTUIThreadRunner.getInstance().getDisplay(), cgImage);
   603 
   696 
   604                 if (eswtImg != null) {
   697                 if(eswtImg != null)
       
   698                 {
   605                     createImageReturnValue = new Image(eswtImg, false);
   699                     createImageReturnValue = new Image(eswtImg, false);
   606                 }
   700                 }
   607                 else {
   701                 else
       
   702                 {
   608                     createImageReturnValue = null;
   703                     createImageReturnValue = null;
   609                     throw new OutOfMemoryError();
   704                     throw new OutOfMemoryError();
   610                 }
   705                 }
   611             }
   706             }
   612         });
   707         });
   629                        int offset,
   724                        int offset,
   630                        int length,
   725                        int length,
   631                        int xPos,
   726                        int xPos,
   632                        int yPos,
   727                        int yPos,
   633                        int width,
   728                        int width,
   634                        int height) {
   729                        int height)
   635 
   730     {
   636         if (rgbData == null) {
   731 
       
   732         if(rgbData == null)
       
   733         {
   637             throw new NullPointerException(
   734             throw new NullPointerException(
   638                     MsgRepository.IMAGE_EXCEPTION_DATA_IS_NULL);
   735                 MsgRepository.IMAGE_EXCEPTION_DATA_IS_NULL);
   639         }
   736         }
   640         if (!validateRegion(this.getWidth(), this.getHeight(),
   737         if(!validateRegion(this.getWidth(), this.getHeight(),
   641              xPos, yPos, width, height)) {
   738                            xPos, yPos, width, height))
       
   739         {
   642             throw new IllegalArgumentException(
   740             throw new IllegalArgumentException(
   643                                 MsgRepository.IMAGE_EXCEPTION_INVALID_BOUNDS);
   741                 MsgRepository.IMAGE_EXCEPTION_INVALID_BOUNDS);
   644         }
   742         }
   645         if (Math.abs(length) < width) {
   743         if(Math.abs(length) < width)
       
   744         {
   646             throw new IllegalArgumentException(
   745             throw new IllegalArgumentException(
   647                     MsgRepository.IMAGE_EXCEPTION_INVALID_SCANLENGTH);
   746                 MsgRepository.IMAGE_EXCEPTION_INVALID_SCANLENGTH);
   648         }
   747         }
   649 
   748 
   650         final int[] localRgbData = rgbData;
   749         final int[] localRgbData = rgbData;
   651         final int localOffset = offset;
   750         final int localOffset = offset;
   652         final int localLength = length;
   751         final int localLength = length;
   653         final int localX = xPos;
   752         final int localX = xPos;
   654         final int localY = yPos;
   753         final int localY = yPos;
   655         final int localW = width;
   754         final int localW = width;
   656         final int localH = height;
   755         final int localH = height;
   657         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
   756         ESWTUIThreadRunner.safeSyncExec(new Runnable()
   658             public void run() {
   757         {
       
   758             public void run()
       
   759             {
   659                 org.eclipse.swt.internal.qt.graphics.Image cgImage = Internal_GfxPackageSupport.getImage(eswtImage);
   760                 org.eclipse.swt.internal.qt.graphics.Image cgImage = Internal_GfxPackageSupport.getImage(eswtImage);
   660                 cgImage.getRGB(localRgbData, localOffset, localLength,
   761                 cgImage.getRGB(localRgbData, localOffset, localLength,
   661                         localX, localY, localW, localH);
   762                                localX, localY, localW, localH);
   662             }
   763             }
   663         });
   764         });
   664     }
   765     }
   665 
   766 
   666     /**
   767     /**
   667      * Creates a new Graphics-objects that is able to draw to this image.
   768      * Creates a new Graphics-objects that is able to draw to this image.
   668      *
   769      *
   669      * @return New Graphics-object.
   770      * @return New Graphics-object.
   670      * @throws IllegalStateException if image is immutable.
   771      * @throws IllegalStateException if image is immutable.
   671      */
   772      */
   672     public Graphics getGraphics() {
   773     public Graphics getGraphics()
   673         if (mutable) {
   774     {
   674             if (imageGraphics == null) {
   775         if(mutable)
   675                 ESWTUIThreadRunner.safeSyncExec(new Runnable() {
   776         {
   676                     public void run() {
   777             if(imageGraphics == null)
       
   778             {
       
   779                 ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   780                 {
       
   781                     public void run()
       
   782                     {
   677                         // instantiate the Graphics object
   783                         // instantiate the Graphics object
   678                         imageGraphics = new Graphics();
   784                         imageGraphics = new Graphics();
   679                         // bind the GC to the Image target
   785                         // bind the GC to the Image target
   680                         imageGraphics.eswtSetParentImage(Image.this);
   786                         imageGraphics.eswtSetParentImage(Image.this);
   681                     }
   787                     }
   689     /**
   795     /**
   690      * Gets width of the image.
   796      * Gets width of the image.
   691      *
   797      *
   692      * @return Image width.
   798      * @return Image width.
   693      */
   799      */
   694     public int getWidth() {
   800     public int getWidth()
       
   801     {
   695         return eswtImageWidth;
   802         return eswtImageWidth;
   696     }
   803     }
   697 
   804 
   698     /**
   805     /**
   699      * Gets height of the image.
   806      * Gets height of the image.
   700      *
   807      *
   701      * @return Image height.
   808      * @return Image height.
   702      */
   809      */
   703     public int getHeight() {
   810     public int getHeight()
       
   811     {
   704         return eswtImageHeight;
   812         return eswtImageHeight;
   705     }
   813     }
   706 
   814 
   707     /**
   815     /**
   708      * Check is it possible to modify this image.
   816      * Check is it possible to modify this image.
   709      *
   817      *
   710      * @return true, if image is mutable.
   818      * @return true, if image is mutable.
   711      */
   819      */
   712     public boolean isMutable() {
   820     public boolean isMutable()
       
   821     {
   713         return mutable;
   822         return mutable;
   714     }
   823     }
   715 
   824 
   716 }
   825 }