109 /** |
109 /** |
110 * Specifies 1 bit per pixel image format. |
110 * Specifies 1 bit per pixel image format. |
111 */ |
111 */ |
112 public static final int FORMAT_IMG_MONO = 8; |
112 public static final int FORMAT_IMG_MONO = 8; |
113 |
113 |
|
114 /** |
|
115 * Specifies native image type QImage. This type |
|
116 * is recommended when the image is modified frequently |
|
117 * and especially if it's pixels need to be accessed. |
|
118 */ |
|
119 public static final int IMAGE_TYPE_QIMAGE = 0; |
|
120 |
|
121 /** |
|
122 * Specifies native image type QPixmap. This type is |
|
123 * recommended if the image is not modified at all |
|
124 * or rarely, i.e. optimal for immutable images. |
|
125 */ |
|
126 public static final int IMAGE_TYPE_QPIXMAP = 1; |
|
127 |
|
128 |
|
129 |
114 // Native image handle |
130 // Native image handle |
115 int handle; |
131 int handle; |
116 // Handle of native QPixmap wrapped by image |
132 // Handle of native QPixmap wrapped by image |
117 int pixmapHandle; |
133 int pixmapHandle; |
118 // Status of native peer |
134 // Status of native peer |
172 if (width <= 0 || height <= 0) { |
189 if (width <= 0 || height <= 0) { |
173 throw new IllegalArgumentException("width or height is equal or less than 0"); |
190 throw new IllegalArgumentException("width or height is equal or less than 0"); |
174 } |
191 } |
175 |
192 |
176 // Construct image in native side and store the handle |
193 // Construct image in native side and store the handle |
177 handle = OS.image_create(width, height, Config.IMAGE_DEFAULT_FILL_COLOR); // may throw outOfMemoryError |
194 handle = OS.image_create(width, height, Config.IMAGE_DEFAULT_FILL_COLOR, Config.IMAGE_DEFAULT_TYPE ); |
178 // set dimensions |
195 // set dimensions |
179 updateSize(); |
196 updateSize(); |
180 } |
197 } |
181 |
198 |
182 /** |
199 /** |
183 * Constructs new image with specified width, height and fill color. |
200 * Constructs new image with specified width, height and type. |
184 * The RGB values passed in is interpreted with the least significant eight bits giving the blue |
|
185 * component, the next eight more significant bits giving the green component, and the next eight |
|
186 * bits giving the red component. The high order byte of this value, i.e. alpha, is ignored. |
|
187 * |
201 * |
188 * @param width The width of new image |
202 * @param width The width of new image |
189 * @param height The height of new image |
203 * @param height The height of new image |
190 * @param fillColor The initial fill color for the image in format #00RRGGBB. |
|
191 * @throws IllegalArgumetException if width or height is equal or less than zero |
204 * @throws IllegalArgumetException if width or height is equal or less than zero |
|
205 * @throws IllegalArgumetException if type is not either IMAGE_TYPE_QIMAGE or IMAGE_TYPE_QPIXMAP |
192 * @throws OutOfMemoryError if memory allocation for new image fails |
206 * @throws OutOfMemoryError if memory allocation for new image fails |
193 */ |
207 */ |
194 public Image(int width, int height, int fillColor) { |
208 public Image(int width, int height, int type) { |
195 Utils.validateUiThread(); |
209 Utils.validateUiThread(); |
196 // check width and height |
|
197 if (width <= 0 || height <= 0) { |
210 if (width <= 0 || height <= 0) { |
198 throw new IllegalArgumentException("width or height is equal or less than 0"); |
211 throw new IllegalArgumentException("width or height is equal or less than 0"); |
199 } |
212 } |
|
213 if((type != IMAGE_TYPE_QIMAGE) && (type != IMAGE_TYPE_QPIXMAP)) { |
|
214 throw new IllegalArgumentException("Invalid type"); |
|
215 } |
200 |
216 |
201 // Construct image in native side and store the handle |
217 // Construct image in native side and store the handle |
202 handle = OS.image_create(width, height, fillColor); // may throw outOfMemoryError |
218 handle = OS.image_create(width, height, Config.IMAGE_DEFAULT_FILL_COLOR, type); // may throw outOfMemoryError |
203 // set dimensions |
219 // set dimensions |
204 updateSize(); |
220 updateSize(); |
205 } |
221 } |
206 |
222 |
207 /** |
223 /** |
208 * Constructs a new instance of this class based on given image. |
224 * Constructs a new instance of this class based on given image. |
|
225 * The new instance will have the same type as the original. |
209 * |
226 * |
210 * @param sourceImage The image used as source |
227 * @param sourceImage The image used as source |
211 * @throws NullPointerException if sourceImage is null |
228 * @throws NullPointerException if sourceImage is null |
212 * @throws OutOfMemoryError if memory allocation for new image fails |
229 * @throws OutOfMemoryError if memory allocation for new image fails |
213 */ |
230 */ |
215 this(sourceImage, 0, 0, 0, 0); |
232 this(sourceImage, 0, 0, 0, 0); |
216 } |
233 } |
217 |
234 |
218 /** |
235 /** |
219 * Constructs a new instance of this class based on given image. |
236 * Constructs a new instance of this class based on given image. |
|
237 * The new instance will have the type specified by the typeOfCopy. |
|
238 * |
|
239 * @param sourceImage The image used as source |
|
240 * @param typeOfCopy The native type of the copy, either IMAGE_TYPE_QIMAGE or IMAGE_TYPE_QPIXMAP |
|
241 * @throws NullPointerException if sourceImage is null |
|
242 * @throws IllegalArgumetException if type is not either IMAGE_TYPE_QIMAGE or IMAGE_TYPE_QPIXMAP |
|
243 * @throws OutOfMemoryError if memory allocation for new image fails |
|
244 */ |
|
245 public Image(Image sourceImage, int typeOfCopy) { |
|
246 this(sourceImage, 0, 0, 0, 0, typeOfCopy); |
|
247 } |
|
248 |
|
249 /** |
|
250 * Constructs a new instance of this class based on given image. |
220 * If the given copy region is empty, the whole image is copied. |
251 * If the given copy region is empty, the whole image is copied. |
|
252 * The new instance will have the same native type as the original. |
221 * |
253 * |
222 * @param sourceImage The image used as source |
254 * @param sourceImage The image used as source |
223 * @param x The top-left x coordinate of the copy region. |
255 * @param x The top-left x coordinate of the copy region. |
224 * @param y The top-left y coordinate of the copy region. |
256 * @param y The top-left y coordinate of the copy region. |
225 * @param width The width of the copy region. |
257 * @param width The width of the copy region. |
227 * @throws NullPointerException if sourceImage is null |
259 * @throws NullPointerException if sourceImage is null |
228 */ |
260 */ |
229 public Image(Image sourceImage, int x, int y, int width, int height) { |
261 public Image(Image sourceImage, int x, int y, int width, int height) { |
230 // validate sourceImage |
262 // validate sourceImage |
231 if (sourceImage == null) { |
263 if (sourceImage == null) { |
232 throw new NullPointerException("img is null"); |
264 throw new NullPointerException("sourceImage is null"); |
233 } |
265 } |
234 createCopy(sourceImage, x, y, width, height); |
266 createCopy(sourceImage, x, y, width, height); |
235 } |
267 } |
236 |
268 |
237 /** |
269 /** |
238 * Creates a new image from given ImageData |
270 * Constructs a new instance of this class based on given image. |
|
271 * If the given copy region is empty, the whole image is copied. |
|
272 * The new instance will have the native type speficied by typeOfCopy. |
|
273 * |
|
274 * @param sourceImage The image used as source |
|
275 * @param x The top-left x coordinate of the copy region. |
|
276 * @param y The top-left y coordinate of the copy region. |
|
277 * @param width The width of the copy region. |
|
278 * @param height The height of the copy region. |
|
279 * @param typeOfCopy The native type of the copy, either IMAGE_TYPE_QIMAGE or IMAGE_TYPE_QPIXMAP |
|
280 * @throws NullPointerException if sourceImage is null |
|
281 * @throws IllegalArgumetException if type is not either IMAGE_TYPE_QIMAGE or IMAGE_TYPE_QPIXMAP |
|
282 */ |
|
283 public Image(Image sourceImage, int x, int y, int width, int height, int typeOfCopy) { |
|
284 // validate sourceImage |
|
285 if (sourceImage == null) { |
|
286 throw new NullPointerException("sourceImage is null"); |
|
287 } |
|
288 if((typeOfCopy != IMAGE_TYPE_QIMAGE) && (typeOfCopy != IMAGE_TYPE_QPIXMAP)) { |
|
289 throw new IllegalArgumentException("Invalid typeOfCopy"); |
|
290 } |
|
291 handle = OS.image_create(sourceImage.handle, x, y, width, height, typeOfCopy); |
|
292 updateSize(); |
|
293 } |
|
294 |
|
295 /** |
|
296 * Creates a new image from given ImageData. |
|
297 * Image will have the default type defined by Config.IMAGE_DEFAULT_TYPE. |
239 * @param imageData |
298 * @param imageData |
240 * @throws NullPointerException if imageData is null |
299 * @throws NullPointerException if imageData is null |
241 */ |
300 */ |
242 public Image(ImageData imageData) { |
301 public Image(ImageData imageData) { |
243 if (imageData == null) { |
302 if (imageData == null) { |
244 throw new NullPointerException("imageData is null"); |
303 throw new NullPointerException("imageData is null"); |
245 } |
304 } |
246 handle = OS.image_create(imageData); |
305 handle = OS.image_create(imageData, Config.IMAGE_DEFAULT_TYPE); |
247 // set dimensions |
306 // set dimensions |
248 updateSize(); |
307 updateSize(); |
249 } |
308 } |
250 |
309 |
251 /** |
310 /** |
252 * Constructs a new instance of this class based on the given rgb data. |
311 * Constructs a new instance of this class based on the given rgb data. |
|
312 * Image will have the type IMAGE_TYPE_QPIXMAP. |
253 * |
313 * |
254 * @param argbData a RGB data array where one pixel is specified as 0xAARRGGBB. |
314 * @param argbData a RGB data array where one pixel is specified as 0xAARRGGBB. |
255 * @param width The width of the image |
315 * @param width The width of the image |
256 * @param height The height of the image |
316 * @param height The height of the image |
257 * @param hasAlpha If true the rgb data has also an alpha channel, |
317 * @param hasAlpha If true the rgb data has also an alpha channel, |
262 public Image(int[] argbData, int width, int height, boolean hasAlpha) { |
322 public Image(int[] argbData, int width, int height, boolean hasAlpha) { |
263 // input validation |
323 // input validation |
264 if(argbData == null) { |
324 if(argbData == null) { |
265 throw new NullPointerException("argbData is null"); |
325 throw new NullPointerException("argbData is null"); |
266 } |
326 } |
267 // Construct an ge in native side and store the handle |
327 // Construct image in native side and store the handle |
268 handle = OS.image_create(argbData, width, height, hasAlpha); |
328 handle = OS.image_create(argbData, width, height, hasAlpha, IMAGE_TYPE_QPIXMAP); |
269 // set dimensions |
329 // set dimensions |
270 updateSize(); |
330 updateSize(); |
271 } |
331 } |
272 |
332 |
273 /** |
333 /** |
399 } |
468 } |
400 } |
469 } |
401 if((scanlength > 0 ? scanlength : -scanlength) < width) { |
470 if((scanlength > 0 ? scanlength : -scanlength) < width) { |
402 throw new IllegalArgumentException("scanlength is less than width"); |
471 throw new IllegalArgumentException("scanlength is less than width"); |
403 } |
472 } |
404 |
|
405 |
|
406 OS.image_getRGB(handle, argbData, offset, scanlength, x, y, width, height); |
473 OS.image_getRGB(handle, argbData, offset, scanlength, x, y, width, height); |
407 } |
474 } |
408 |
|
409 |
475 |
410 /**TODO: change comments |
476 /**TODO: change comments |
411 * Copies image data to given integer array from rectangle specified |
477 * Copies image data to given integer array from rectangle specified |
412 * by x, y, width and height. |
478 * by x, y, width and height. |
413 * |
479 * |
528 } |
592 } |
529 } |
593 } |
530 if((scanlength > 0 ? scanlength : -scanlength) < width) { |
594 if((scanlength > 0 ? scanlength : -scanlength) < width) { |
531 throw new IllegalArgumentException("scanlength is less than width"); |
595 throw new IllegalArgumentException("scanlength is less than width"); |
532 } |
596 } |
533 |
|
534 |
|
535 OS.image_getRGB(handle, argbData, offset, scanlength, x, y, width, height, format); |
597 OS.image_getRGB(handle, argbData, offset, scanlength, x, y, width, height, format); |
536 } |
598 } |
537 |
599 |
538 /** |
600 /** |
539 * Pixel level collision detection. |
601 * Pixel level collision detection. |
540 */ |
602 */ |
541 public static boolean detectCollision(Image image1, int transform1, int p1x, int p1y, int r1x1, int r1y1, int r1x2, int r1y2, |
603 public static boolean detectCollision(Image image1, int transform1, int p1x, int p1y, int r1x1, int r1y1, int r1x2, int r1y2, |
542 Image image2, int transform2, int p2x, int p2y, int r2x1, int r2y1, int r2x2, int r2y2) { |
604 Image image2, int transform2, int p2x, int p2y, int r2x1, int r2y1, int r2x2, int r2y2) { |
543 |
|
544 return OS.image_detectCollision( |
605 return OS.image_detectCollision( |
545 image1.getNativePixmapHandle(), transform1, p1x, p1y, r1x1, r1y1, r1x2, r1y2, |
606 image1.getHandle(), transform1, p1x, p1y, r1x1, r1y1, r1x2, r1y2, |
546 image2.getNativePixmapHandle(), transform2, p2x, p2y, r2x1, r2y1, r2x2, r2y2); |
607 image2.getHandle(), transform2, p2x, p2y, r2x1, r2y1, r2x2, r2y2); |
547 } |
608 } |
548 |
609 |
549 /** |
610 /** |
550 * Transforms image with given transform. |
611 * Transforms image with given transform. |
551 * |
612 * |
601 |
662 |
602 /** |
663 /** |
603 * Returns native side handle. |
664 * Returns native side handle. |
604 * @return the native handle. |
665 * @return the native handle. |
605 */ |
666 */ |
606 public int getHandle() |
667 public int getHandle() { |
607 { |
|
608 checkState(); |
668 checkState(); |
609 return handle; |
669 return handle; |
610 } |
670 } |
611 |
671 |
612 /** |
|
613 * Returns native side QPixmap handle. |
|
614 * @return the native QPixmap handle. |
|
615 */ |
|
616 synchronized public int getNativePixmapHandle() |
|
617 { |
|
618 checkState(); |
|
619 if(pixmapHandle == 0) |
|
620 { |
|
621 // In the current implementation this will return |
|
622 // pointer to the already existing QPixmap (which |
|
623 // will be destroyed with the object), so the |
|
624 // handle does not need to be released manually. |
|
625 pixmapHandle = OS.image_getPixmapHandle(handle); |
|
626 } |
|
627 return pixmapHandle; |
|
628 } |
|
629 |
|
630 /** |
|
631 /** |
672 /** |
632 * Creates <code>ImageData</code> objects |
673 * Creates <code>ImageData</code> objects |
633 * @return New object |
674 * @return New object |
634 */ |
675 */ |
635 public ImageData getImageData() { |
676 public ImageData getImageData() { |
636 checkState(); |
677 checkState(); |
637 return OS.image_getImageData(handle); |
678 return OS.image_getImageData(handle); |
638 } |
679 } |
639 |
680 |
|
681 /** |
|
682 * Gets the native type of this Image instance. |
|
683 * @return Either Image.IMAGE_TYPE_QIMAGE or Image.IMAGE_TYPE_QPIXMAP |
|
684 */ |
|
685 public int getNativeType() { |
|
686 checkState(); |
|
687 return OS.image_getType(handle); |
|
688 } |
|
689 |
|
690 /** |
|
691 * Gets the handle of the QPaintDevice of contained |
|
692 * QImage or QPixmap instance. |
|
693 * @return handle to native QPaintDevice |
|
694 */ |
|
695 public int getQPainDeviceHandle() { |
|
696 checkState(); |
|
697 return OS.image_getQPaintDeviceHandle(handle); |
|
698 } |
|
699 |
640 /** |
700 /** |
641 * Private helper to check the state of the current instance. |
701 * Private helper to check the state of the current instance. |
642 */ |
702 */ |
643 private void checkState() { |
703 private void checkState() { |
644 Utils.validateUiThread(); |
704 Utils.validateUiThread(); |
652 */ |
712 */ |
653 private void updateSize() { |
713 private void updateSize() { |
654 w = OS.image_getWidth(handle); |
714 w = OS.image_getWidth(handle); |
655 h = OS.image_getHeight(handle); |
715 h = OS.image_getHeight(handle); |
656 } |
716 } |
657 |
|
658 /** |
|
659 * Contructs an instance of Image based on the given <code>imageData</code>. |
|
660 * |
|
661 * @param imageData Image data @see org.eclipse.swt.graphics.ImageData |
|
662 * @return Instance of loaded image |
|
663 * @throws IllegalArgumentException if <code>imageData</code> is null. |
|
664 */ |
|
665 public static Image createImage(ImageData imageData) { |
|
666 if (imageData == null) { |
|
667 throw new IllegalArgumentException("imageData is null"); |
|
668 } |
|
669 int imageHandle = OS.image_create(imageData); |
|
670 return new Image(imageHandle); |
|
671 } |
|
672 |
717 |
673 /** |
718 /** |
674 * Constructs new image with given native QPixmap handle. |
719 * Constructs new image with given native QPixmap handle. |
|
720 * The native type if new image instance will be IMAGE_TYPE_QPIXMAP. |
675 * @param pixmapHandle Handle of native QPixmap. |
721 * @param pixmapHandle Handle of native QPixmap. |
676 * @return Instance of loaded image. |
722 * @return new image instance |
677 */ |
723 */ |
678 public static Image createImageFromPixmap(int pixmapHandle) { |
724 public static Image createImageFromPixmap(int pixmapHandle) { |
679 // input validation |
725 // input validation |
680 if(pixmapHandle <= 0) { |
726 if(pixmapHandle <= 0) { |
681 throw new IllegalArgumentException("Invalid pixmap handle"); |
727 throw new IllegalArgumentException("Invalid pixmap handle"); |