javauis/eswt_qt/org.eclipse.swt/Eclipse_SWT_PI/qt/org/eclipse/swt/internal/qt/graphics/ImageLoader.java
changeset 80 d6dafc5d983f
parent 78 71ad690e91f5
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
    44 
    44 
    45     /**
    45     /**
    46      * Status of native peer
    46      * Status of native peer
    47      */
    47      */
    48     private boolean disposed;
    48     private boolean disposed;
       
    49     
       
    50     /**
       
    51      * Default native type of loaded images
       
    52      */
       
    53     private int nativeImageType = Config.IMAGE_DEFAULT_TYPE;
    49 
    54 
    50     /**
    55     /**
    51      * Creates an instance of this class and initializes native
    56      * Creates an instance of this class and initializes native
    52      * image loader.
    57      * image loader. Type of native image created is set to 
       
    58      * the default type defined Config.IMAGE_DEFAULT_TYPE;
    53      */
    59      */
    54     public ImageLoader() {
    60     public ImageLoader() {
    55     	Utils.validateUiThread();
    61         Utils.validateUiThread();
    56         handle = OS.imageLoader_init();
    62         handle = OS.imageLoader_init(nativeImageType);
    57         if (handle == 0) {
    63         if (handle == 0) {
    58             throw new OutOfMemoryError();
    64             throw new OutOfMemoryError();
    59         }
    65         }
    60         disposed = false;
    66         disposed = false;
    61     }
    67     }
    62 
    68 
    63     /**
    69     /**
       
    70      * Creates an instance of this class and initializes native
       
    71      * image loader with specified target image type. 
       
    72      * @param resultImageType The native type of image for loaded images,
       
    73      *                        either Image.IMAGE_TYPE_QIMAGE or Image.IMAGE_TYPE_QPIXMAP.
       
    74      */
       
    75     public ImageLoader(int resultImageType) {
       
    76         Utils.validateUiThread();
       
    77         if((resultImageType != Image.IMAGE_TYPE_QIMAGE) &&
       
    78            (resultImageType != Image.IMAGE_TYPE_QPIXMAP)) {
       
    79             throw new IllegalArgumentException("Illegal resultImageType");
       
    80         }
       
    81         nativeImageType = resultImageType;
       
    82         handle = OS.imageLoader_init(nativeImageType);
       
    83         if (handle == 0) {
       
    84             throw new OutOfMemoryError();
       
    85         }
       
    86         disposed = false;
       
    87     }
       
    88     
       
    89     /**
    64      * Disposes native image, i.e. frees resources.
    90      * Disposes native image, i.e. frees resources.
    65      */
    91      */
    66     public void dispose() {
    92     public void dispose() {
    67     	Utils.validateUiThread();
    93         Utils.validateUiThread();
    68         if (disposed) {
    94         if (disposed) {
    69             return;
    95             return;
    70         } else {
    96         } else {
    71             OS.imageLoader_dispose(handle);
    97             OS.imageLoader_dispose(handle);
    72             handle = 0;
    98             handle = 0;
    73             disposed = true;
    99             disposed = true;
    74         }
   100         }
    75     }
   101     }
    76 
   102 
       
   103     /**
       
   104      * Returns current result native image type.
       
   105      * @return Native image type, either Image.IMAGE_TYPE_QIMAGE or Image.IMAGE_TYPE_QPIXMAP
       
   106      */
       
   107     public int getResultImageType() {
       
   108         return nativeImageType;
       
   109     }
       
   110     
    77     /**
   111     /**
    78      * Gets the status of this image loader.
   112      * Gets the status of this image loader.
    79      *
   113      *
    80      * @return True if this instance has been already disposed otherwise false
   114      * @return True if this instance has been already disposed otherwise false
    81      */
   115      */
   112 
   146 
   113     /**
   147     /**
   114      * Loads image from given byte array.
   148      * Loads image from given byte array.
   115      *
   149      *
   116      * @param data The array containing image data
   150      * @param data The array containing image data
   117      * @param offset The offset from beginnig of data where image data starts
   151      * @param offset The offset from beginning of data where image data starts
   118      * @param length The length of data beginning form offset
   152      * @param length The length of data beginning form offset
   119      * @return Instance of loaded image
   153      * @return Instance of loaded image
   120      * @throws NullPointerException if data is null
   154      * @throws NullPointerException if data is null
   121      * @throws IllegalArgumentException if offset is less than 0
   155      * @throws IllegalArgumentException if offset is less than 0
   122      * @throws IllegalArgumentException if length is equal or less than 0
   156      * @throws IllegalArgumentException if length is equal or less than 0
   123      * @throws ArrayIndexOutOfBoundsException if offset and length define an invalid range
   157      * @throws ArrayIndexOutOfBoundsException if offset and length define an invalid range
   124      * @throws IOException if image cannot be loaded or it is unsupported format
   158      * @throws IllegalArgumentException if image cannot be loaded or it is unsupported format
   125      * @throws OutOfMemoryError if native buffer allocation fails
   159      * @throws OutOfMemoryError if native buffer allocation fails
   126      */
   160      */
   127     public Image loadImage(byte[] data, int offset, int length) throws IOException {
   161     public Image loadImage(byte[] data, int offset, int length) throws IOException {
   128         checkState();
   162         checkState();
   129 
   163 
   145         OS.imageLoader_append(handle, length, offset, data);
   179         OS.imageLoader_append(handle, length, offset, data);
   146 
   180 
   147         return new Image(OS.imageLoader_endStream(handle));
   181         return new Image(OS.imageLoader_endStream(handle));
   148     }
   182     }
   149 
   183 
   150 	/**
   184     /**
   151 	 * Loads image from file directly using the native APIs. Note that Java
   185      * Loads image from file directly using the native APIs. Note that Java
   152 	 * security checks are not done.
   186      * security checks are not done.
   153 	 *
   187      *
   154 	 * @param filename The filename to pass to the native APIs.
   188      * @param filename The filename to pass to the native APIs.
   155 	 * @return Image The loaded image.
   189      * @return Image The loaded image.
   156 	 * @throws IOException
   190      * @throws IOException
   157 	 */
   191      */
   158     public Image nativelyLoadImageFromFileNoSecurity(String filename) throws IOException {
   192     public Image nativelyLoadImageFromFileNoSecurity(String filename) throws IOException {
   159         checkState();
   193         checkState();
   160         if (filename == null) {
   194         if (filename == null) {
   161             throw new NullPointerException("data is null");
   195             throw new NullPointerException("data is null");
   162         }
   196         }
   163         return new Image(OS.imageLoader_load(handle, filename));
   197         return new Image(OS.imageLoader_load(handle, filename));
   164     }
   198     }
   165 
   199 
   166     /**
   200     /**
   167      * Contructs an instance of Image by reading image data from given InputStream.
   201      * Constructs an instance of Image by reading image data from given InputStream.
   168      *
   202      *
   169      * @param is The InputStream from where to read the image data from
   203      * @param is The InputStream from where to read the image data from
   170      * @return Instance of loaded image
   204      * @return Instance of loaded image
   171      * @throws NullPointerException if InputStream is is null
   205      * @throws NullPointerException if InputStream is is null
   172      * @throws IOException if image cannot be loaded
   206      * @throws IllegalArgumentException if image cannot be loaded
   173      * @throws OutOfMemoryError if allocation of native buffer of image creation fails
   207      * @throws OutOfMemoryError if allocation of native buffer of image creation fails
   174      * @throws IllegalStateException if image data is invalid
   208      * @throws IllegalStateException if image data is invalid
   175      * @throws IllegalArgumentException if image format is not supported
   209      * @throws IllegalArgumentException if image format is not supported
   176      */
   210      */
   177     public Image loadImage(InputStream is) throws IOException {
   211     public Image loadImage(InputStream is) throws IOException {
   225      * SVG images.
   259      * SVG images.
   226      * @param width The width to scale to
   260      * @param width The width to scale to
   227      * @param height The height to scale to
   261      * @param height The height to scale to
   228      */
   262      */
   229     public void setLoadSize(int width, int height) {
   263     public void setLoadSize(int width, int height) {
   230     	OS.imageLoader_setLoadSize(handle, width, height);
   264         OS.imageLoader_setLoadSize(handle, width, height);
   231     }
   265     }
   232 
   266 
       
   267     /**
       
   268      * Sets the result image type, i.e. the native type for the images
       
   269      * loaded. 
       
   270      * 
       
   271      * @param resultImageType The type of loaded image, either Image.IMAGE_TYPE_QIMAGE or 
       
   272      *                  Image.IMAGE_TYPE_QPIXMAP 
       
   273      * @throws IllegalArgumentException if imageType is not one of Image.
       
   274      *                                  IMAGE_TYPE_QIMAGE or Image.IMAGE_TYPE QPIXMAP
       
   275      */
       
   276     public void setResultImageType(int resultImageType) {
       
   277         if((resultImageType != Image.IMAGE_TYPE_QIMAGE) && 
       
   278            (resultImageType != Image.IMAGE_TYPE_QPIXMAP)) {
       
   279             throw new IllegalArgumentException("Illegal resultImageType");
       
   280         }
       
   281         nativeImageType = resultImageType;
       
   282         OS.imageloader_setResultImageType(handle, nativeImageType);
       
   283     }
       
   284     
   233     /**
   285     /**
   234      * Returns the bounds of an Image without creating an actual Image instance.
   286      * Returns the bounds of an Image without creating an actual Image instance.
   235      *
   287      *
   236      * @param is The InputStream from where to read the image data from
   288      * @param is The InputStream from where to read the image data from
   237      * @return Bounds of the image
   289      * @return Bounds of the image
   259 
   311 
   260     /**
   312     /**
   261      * Private helper to check the state of the current instance.
   313      * Private helper to check the state of the current instance.
   262      */
   314      */
   263     private void checkState() {
   315     private void checkState() {
   264     	Utils.validateUiThread();
   316         Utils.validateUiThread();
   265         if (disposed) {
   317         if (disposed) {
   266             throw new IllegalStateException("Image loader already disposed");
   318             throw new IllegalStateException("Image loader already disposed");
   267         }
   319         }
   268     }
   320     }
   269 }
   321 }