391 * @throws NullPointerException if img is null. |
391 * @throws NullPointerException if img is null. |
392 * @throws IllegalArgumentException if Region specified exceeds the source |
392 * @throws IllegalArgumentException if Region specified exceeds the source |
393 * image or if w or h is zero or less or if transform is not one |
393 * image or if w or h is zero or less or if transform is not one |
394 * defined in Sprite-class. |
394 * defined in Sprite-class. |
395 */ |
395 */ |
396 public static Image createImage(Image srcImage, |
396 public static Image createImage(Image aImage, |
397 int x, |
397 int aX, |
398 int y, |
398 int aY, |
399 int w, |
399 int aWidth, |
400 int h, |
400 int aHeight, |
401 int transform) |
401 int aTransform) |
402 { |
402 { |
403 if(srcImage == null) |
403 if(aImage == null) |
404 { |
404 { |
405 throw new NullPointerException( |
405 throw new NullPointerException( |
406 MsgRepository.IMAGE_EXCEPTION_IS_NULL); |
406 MsgRepository.IMAGE_EXCEPTION_IS_NULL); |
407 } |
407 } |
408 if(w <= 0 || h <= 0) |
408 if(aWidth <= 0 || aHeight <= 0) |
409 { |
409 { |
410 throw new IllegalArgumentException( |
410 throw new IllegalArgumentException( |
411 MsgRepository.IMAGE_EXCEPTION_INVALID_DIMENSIONS); |
411 MsgRepository.IMAGE_EXCEPTION_INVALID_DIMENSIONS); |
412 } |
412 } |
413 if(!validateTransform(transform)) |
413 if(!validateTransform(aTransform)) |
414 { |
414 { |
415 throw new IllegalArgumentException( |
415 throw new IllegalArgumentException( |
416 MsgRepository.IMAGE_EXCEPTION_INVALID_TRANSFORM); |
416 MsgRepository.IMAGE_EXCEPTION_INVALID_TRANSFORM); |
417 } |
417 } |
418 if(!validateRegion(srcImage.getWidth(), srcImage.getHeight(), x, y, w, h)) |
418 |
|
419 if(!validateRegion(aImage.getWidth(), aImage.getHeight(), aX, aY, aWidth, aHeight)) |
419 { |
420 { |
420 throw new IllegalArgumentException( |
421 throw new IllegalArgumentException( |
421 MsgRepository.IMAGE_EXCEPTION_INVALID_REGION); |
422 MsgRepository.IMAGE_EXCEPTION_INVALID_REGION); |
422 } |
423 } |
423 |
424 |
424 final Image fImage = srcImage; |
425 final Image fImage = aImage; |
425 final int fx = x; |
426 final int fx = aX; |
426 final int fy = y; |
427 final int fy = aY; |
427 final int fw = w; |
428 final int fw = aWidth; |
428 final int fh = h; |
429 final int fh = aHeight; |
429 final int fTransform = transform; |
430 final int fTransform = aTransform; |
430 ESWTUIThreadRunner.safeSyncExec(new Runnable() |
431 ESWTUIThreadRunner.safeSyncExec(new Runnable() |
431 { |
432 { |
432 public void run() |
433 public void run() |
433 { |
434 { |
434 org.eclipse.swt.internal.qt.graphics.Image srcCgImage = |
435 org.eclipse.swt.internal.qt.graphics.Image srcCgImage = |
461 |
462 |
462 /** |
463 /** |
463 * Validates that a specified region is fully located within the image |
464 * Validates that a specified region is fully located within the image |
464 * bounds. Method is package-private, used by Graphics.drawRegion as well. |
465 * bounds. Method is package-private, used by Graphics.drawRegion as well. |
465 */ |
466 */ |
466 static boolean validateRegion(int srcWidth, int srcHeight, int x, int y, |
467 static boolean validateRegion(int aSrcWidth, |
467 int w, int h) |
468 int aSrcHeight, |
468 { |
469 int aX, |
469 return x >= 0 && y >= 0 && w <= srcWidth && h <= srcHeight; |
470 int aY, |
470 } |
471 int aWidth, |
471 |
472 int aHeight) |
|
473 { |
|
474 boolean result = true; |
|
475 |
|
476 final int width = aSrcWidth; |
|
477 final int height = aSrcHeight; |
|
478 |
|
479 final int sx1 = aX; // left column |
|
480 final int sx2 = aX + aWidth; // right column |
|
481 |
|
482 final int sy1 = aY; // top row |
|
483 final int sy2 = aY + aHeight; // bottom row (exclusive) |
|
484 |
|
485 // |
|
486 // Check source x range lies within source image |
|
487 // |
|
488 final boolean xRangeError = (sx1 < 0) || (sx1 >= width) || (sx2 < 0) || |
|
489 (sx2 > width); |
|
490 final boolean yRangeError = (sy1 < 0) || (sy1 >= height) || (sy2 < 0) || |
|
491 (sy2 > height); |
|
492 |
|
493 if (xRangeError || yRangeError) |
|
494 { |
|
495 result = false; |
|
496 } |
|
497 return result; |
|
498 } |
|
499 |
472 /** |
500 /** |
473 * Validates if transform has a valid value. Method is package-private, used |
501 * Validates if transform has a valid value. Method is package-private, used |
474 * by Graphics.drawRegion as well. |
502 * by Graphics.drawRegion as well. |
475 */ |
503 */ |
476 static boolean validateTransform(int transform) |
504 static boolean validateTransform(int transform) |