SWT et redimensionnement d’images

Juste une petite note sur les redimensionnement d’images en SWT/jFace :

Il existe 2 techniques :

  • en utilisant ImageData.scaleTo (basse qualité)
  • en utilisant un GC sur une Image et en appelant GC.drawImage (haute qualité)

La deuxième solutions alloue des ressources systèmes, mais bénéficie du coup des accélérations matérielles et du anti-aliasing :
Pour définir l’anti-aliasing, utilisez GC.setAntialias(SWT.ON ou SWT.DEFAULT ou SWT.NONE); La qualité se règle avec GC.setInterpolation(SWT.DEFAULT ou SWT.NONE ou SWT.LOW ou SWT.HIGH);

Attention aux valeurs défaut qui ne semble-t-il ne sont pas les même en fonction de la plateforme… En tout cas, sur windows : DEFAULT = NONE.

   /**
   * Returns a new scaled image. new Image must be disposed after use.
   *
   * @param image
   * @param width
   * @param height
   * @return
   */
   public static Image resize(Image image, int width, int height) {

      if (image == null)
         return null;

      final Image scaled = new Image(Display.getDefault(), width, height);
      GC gc = new GC(scaled);
      gc.setAntialias(SWT.ON);
      gc.setInterpolation(SWT.HIGH);
      gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, width, height);
      gc.dispose();

      return scaled;
   }

   public static ImageData resize(ImageData imageData, int width, int height, boolean antiAliasing) {

      if (imageData == null)
         return null;

      if (imageData.width == width && imageData.height == height)
         return imageData;

      if (antiAliasing) {
         Image tmpImage = null;
         Image fullImage = new Image(Display.getCurrent(), imageData);
         ImageData result = null;
         tmpImage = resize(fullImage, width, height);

         result = tmpImage.getImageData();
         tmpImage.dispose();
         fullImage.dispose();
         return result;
      }
      return imageData.scaledTo(width, height);
   }

Je suis sûr que ça pourra aider quelqu’un un de ces jour :-)




  • Justin Dolezy
    Now I like that idea! ;) More features is good. I'll try to put together a simple snippet at the weekend.
  • The solution could be a better support for overlay icons in the Gallery widget. Can you send me a simple snippet showing the problem, I will see what I can do.
  • Justin Dolezy
    Hi Nicolas,

    I need to resize the image as I'm creating an overlaid image to be used in a Gallery! The post didn't show that, but you can see what I mean here: http://neckdiagrams.com/features/files/collage_... <- look at the image on the right that has a star in the bottom right corner. I'm using a JFace DecorationOverlayIcon to do the overlay, but I need to resize the base image (guitar) appropriately so e.g. I don't get a large star on a small guitar! The Gallery takes care of resizing the regular non-overlaid images, but the overlay I need to handle myself..
    What do you mean by "turn your image into an alpha layered one"? At the SWT level? The images are png's.

    Cheers,
    Justin
  • Hi Justin,

    I didn't try to resize images with an alpha layer yet. Resizing this kind of images with antialiasing will be difficult because the transparent pixels tends to become something else (don't stick to the right color).

    Ideas :
    - Why do you need to resize images ? if it's for painting on a GC, try to resize while drawing, not before
    - Resize the image data, not the image (low quality)
    - Turn your image to an alpha layered one, and use the same method to resize the layer. The black border should become transparent. I'm not sure this one will work, but it is the thing I would try if I had this problem.

    Good luck,
    Nicolas
  • Justin Dolezy
    Hi Nicolas,

    Have you tried resizing images with alpha transparency?! This doesn't work so well with that kind of image as the scaled GC gets a white background by default. I actually posted a question on the SWT newsgroups but got no response :( ! I worked around it by finding an unused colour in the source image and setting the new pixel data to that with alpha and also setting the transparentPixel, however I get a black border around the source image... http://dev.eclipse.org/newslists/news.eclipse.p...
    I imagine the problem is an easy one for someone with a bit more insight?! Any ideas greatly appreciated!

    Cheers,
    Justin
blog comments powered by Disqus