Author Topic: Texture resizing  (Read 7003 times)

Offline eye1

  • int
  • **
  • Posts: 68
    • View Profile
Texture resizing
« on: February 09, 2007, 02:36:19 pm »
Hello!

I would like to do next thing.

Currently i blit some textures on screen. The textures are designed for highest resolution application will support. I would like to have/program method which will, when i start application, resize the Texture to a size, which will fit configured resolution. Since the Textures must be n^2 in size. The actuall size of image shouldn't be resized. Only content of image. I'll blit then only the part of texture.

Any hints?

Offline cyberkilla

  • float
  • ****
  • Posts: 413
    • View Profile
    • http://futurerp.net
Texture resizing
« Reply #1 on: February 09, 2007, 03:32:57 pm »
If you are creating the textures from images, you can use Java2D scaling functions to do that.
http://futurerp.net - Text Based MMORPG
http://beta.rpwar.com - 3D Isometric MMORPG

Offline eye1

  • int
  • **
  • Posts: 68
    • View Profile
Texture resizing
« Reply #2 on: February 09, 2007, 05:05:52 pm »
I figurated it out.

Code: [Select]

   public static BufferedImage scaleImage(BufferedImage image, double factorW, double factorH)
   {
      BufferedImage scaled = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
      AffineTransformOp op = new AffineTransformOp(
      AffineTransform.getScaleInstance(factorW, factorH), null);
      image = op.filter(image, null);
      scaled.getGraphics().drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
      return scaled;
  }

Offline cyberkilla

  • float
  • ****
  • Posts: 413
    • View Profile
    • http://futurerp.net
Texture resizing
« Reply #3 on: February 09, 2007, 05:34:50 pm »
This works too.

Code: [Select]

Image image = imageBig.getScaledInstance(newWidth, -1,Image.SCALE_FAST);


If you change the SCALE_ to something else, you can get a higher quality scale.
http://futurerp.net - Text Based MMORPG
http://beta.rpwar.com - 3D Isometric MMORPG

Offline eye1

  • int
  • **
  • Posts: 68
    • View Profile
Texture resizing
« Reply #4 on: February 09, 2007, 07:38:38 pm »
The thing is I needed to maintain original size of image and make smaller content. The part with no content is left opaque.

Offline cyberkilla

  • float
  • ****
  • Posts: 413
    • View Profile
    • http://futurerp.net
Texture resizing
« Reply #5 on: February 10, 2007, 12:49:32 am »
I'm sure your way will work fine:)

I would just have a feeling the method I suggested would be slightly faster.
If you got rid of the affine transform, you could just paint it onto the buffered image.

Code: [Select]

public static BufferedImage scaleImage(BufferedImage image, int scaleWidth, int scaleHeight)
   {
int width = image.getWidth();
int height = image.getHeight();
      BufferedImage scaled = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);

Image imageSmall = image.getScaledInstance(scaleWidth, scaleHeight,Image.SCALE_FAST);

      scaled.getGraphics().drawImage(imageSmall, 0, 0, null);

      return scaled;
  }
http://futurerp.net - Text Based MMORPG
http://beta.rpwar.com - 3D Isometric MMORPG

Offline eye1

  • int
  • **
  • Posts: 68
    • View Profile
Texture resizing
« Reply #6 on: February 10, 2007, 07:04:23 pm »
Speed is not a factor here, since I only scale a few images at initialization. Works fast already. So I'll stick with how it is. ;). No need for optimization      :mrgreen:

Offline eye1

  • int
  • **
  • Posts: 68
    • View Profile
Re: Texture resizing
« Reply #7 on: March 14, 2007, 04:06:04 pm »
I changed scaling to how you suggested. It resizes texture much smoother.

Code: [Select]

   public static Image scaleImage(BufferedImage image, double factorW, double factorH)
   {
      BufferedImage scaled = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
      int width = (int)(factorW*image.getWidth());
      int height = (int)(factorH * image.getHeight());
      Image img = image.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
      scaled.getGraphics().drawImage(img, 0, 0, width, height, null);
      return scaled;
  }


Offline cyberkilla

  • float
  • ****
  • Posts: 413
    • View Profile
    • http://futurerp.net
Re: Texture resizing
« Reply #8 on: March 15, 2007, 03:32:47 am »
I'm glad it works for you:).
http://futurerp.net - Text Based MMORPG
http://beta.rpwar.com - 3D Isometric MMORPG