Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Scaling images  (Read 859 times)
0 Members and 1 Guest are viewing this topic.
Offline Porlus

Junior Member





« Posted 2012-12-12 22:09:55 »

Hi all, I know there are simpler ways to do this in Java but I'm writing a method to scale an image represented by pixel array. This is what I've managed to knock up so far with guess work:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public void scale(double factor) {
      int newWidth = (int) Math.abs(width * factor);
      int newHeight = (int) Math.abs(height * factor);
      int[] temp = new int[newWidth * newHeight];
      for(int y = 0; y < height; y++) {
         for(int x = 0; x < width; x++) {
            int newX = (int) (x * factor);
            int newY = (int) (y * factor);
            if(newX < 0) newX += newWidth;
            if(newY < 0) newY += newHeight;
            if(newX < 0 || newY < 0 || newX >= newWidth || newY >= newHeight) continue;
            temp[(int) (newY * newWidth + newX)] = pixels[y * width + x];
         }
      }
      width = newWidth;
      height = newHeight;
      pixels = temp.clone();
   }


This is inside a bitmap class which has its own pixel array. This method creates a new pixel array of the correct size, then loops through every pixel and scales each pixel according to the scale factor. It works fine scaling down, but when scaling up it creates a grid-like effect where pixel aren't being filled as it doesn't scale up. I tried this the other way round by looping through each pixel in the new array and mapping pixels from the old array, which works fine when mapping up, but doesn't work for scaling down.
Does anyone know how I could modify this algorithm to cater for scaling in both directions? Thanks

MODIFIED:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public void TESTscale(double factor) {
      int newWidth = (int) Math.abs(width * factor);
      int newHeight = (int) Math.abs(height * factor);
      int[] temp = new int[newWidth * newHeight];
      double xRatio = width / (double) newWidth;
      double yRatio = height / (double) newHeight;
      double newX, newY;
      for(int y = 0; y < newHeight; y++) {
         for(int x = 0; x < newWidth; x++) {
            newX = Math.floor(x * xRatio);
            newY = Math.floor(y * yRatio);
            temp[y * newWidth + x] = pixels[(int) (newY * width + newX)];
         }
      }
      width = newWidth;
      height = newHeight;
      pixels = temp.clone();
   }


Paul
Offline davedes
« Reply #1 - Posted 2012-12-12 22:41:48 »

Nearest-neighbour scaling:
http://tech-algorithm.com/articles/nearest-neighbor-image-scaling/

Of course the best thing would be to use BufferedImage, which leads to far fewer lines of code, potentially hardware-accelerated scaling, and much greater flexibility (i.e. different image types, different interpolation algorithms, etc).

Offline Porlus

Junior Member





« Reply #2 - Posted 2012-12-12 23:02:33 »

Hi dave, thanks for the quick reply! I tried the first solution on that web page to check that it would work, but it still appears to have a grid effect. Sad I've updated my original post with the updated code. Any ideas what I've done wrong?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Porlus

Junior Member





« Reply #3 - Posted 2012-12-12 23:07:13 »

Ah, apologies dave. I was still calling the unmodified method. Tongue Works great. Although it doesn't allow me to scale the image by negative numbers, which I was planning to use for mirroring an image. Any idea how I would add that functionality?
Offline Porlus

Junior Member





« Reply #4 - Posted 2012-12-13 00:02:12 »

Ah, got it working. Smiley For anyone who's interested here's what I have:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public void TESTscale(double factor) {
      int newWidth = (int) Math.abs(width * factor);
      int newHeight = (int) Math.abs(height * factor);
      int[] temp = new int[newWidth * newHeight];
      double xRatio = width / (double) newWidth;
      double yRatio = height / (double) newHeight;
      double newX, newY;
      for(int y = 0; y < newHeight; y++) {
         for(int x = 0; x < newWidth; x++) {
            newX = Math.floor(x / factor);
            newY = Math.floor(y / factor);
            if(newX < 0) newX += width;
            if(newY < 0) newY += height;
            if(newX<0 || newY<0 || newX >= width || newY >= height) continue;
            temp[y * newWidth + x] = pixels[(int) (newY * width + newX)];
         }
      }
      width = newWidth;
      height = newHeight;
      pixels = temp.clone();
   }


Also if anyone can think of a more elegant way to write it, I'd appreciate anymore help. Tongue

Paul
Offline Rorkien
« Reply #5 - Posted 2012-12-13 00:26:48 »

I have this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   public static Image scale(Image i, int scale) {
      Image img = new Image(i.width * scale, i.height * scale);
     
      for (int y = 0; y < img.height; y++) {  
         for (int x = 0; x < img.width; x++) {
            int src = i.pixels[(x / scale) + (y / scale) * i.width];
            if (src < 0) img.pixels[x + y * img.width] = src;
         }
      }
     
      return img;
   }


With "Image" being your usual pixel-array.
But i'm pretty sure it doesn't work when scaling images down (who needs it anyways?)
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (35 views)
2014-04-15 18:08:23

BurntPizza (31 views)
2014-04-15 03:46:01

UprightPath (46 views)
2014-04-14 17:39:50

UprightPath (29 views)
2014-04-14 17:35:47

Porlus (46 views)
2014-04-14 15:48:38

tom_mai78101 (67 views)
2014-04-10 04:04:31

BurntPizza (127 views)
2014-04-08 23:06:04

tom_mai78101 (227 views)
2014-04-05 13:34:39

trollwarrior1 (192 views)
2014-04-04 12:06:45

CJLetsGame (199 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!