www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: eye1 on February 09, 2007, 02:36:19 PM

Title: Texture resizing
Post by: eye1 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?
Title: Texture resizing
Post by: cyberkilla 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.
Title: Texture resizing
Post by: eye1 on February 09, 2007, 05:05:52 PM
I figurated it out.


  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;
 }
Title: Texture resizing
Post by: cyberkilla on February 09, 2007, 05:34:50 PM
This works too.


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


If you change the SCALE_ to something else, you can get a higher quality scale.
Title: Texture resizing
Post by: eye1 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.
Title: Texture resizing
Post by: cyberkilla 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.


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;
 }
Title: Texture resizing
Post by: eye1 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:
Title: Re: Texture resizing
Post by: eye1 on March 14, 2007, 04:06:04 PM
I changed scaling to how you suggested. It resizes texture much smoother.



   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;
  }

Title: Re: Texture resizing
Post by: cyberkilla on March 15, 2007, 03:32:47 AM
I'm glad it works for you:).