Results 1 to 7 of 7
  1. #1
    Federico is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default interpolation of intARGB

    Hey there guys, this is my first thread post here so bare with me.
    I have been working on this project where i'm asked to interpolate a colo square with the NW corner being composed of green, the NE corner red, SW corner black and SE corner of the bufferedImage blue and the colors are supposed to interpolate with one another so as the image dispayed shows a gradient from each corner of different colors.

    I have trouble generating this.... getRGB doesn't seem to be giving me what I want... I casted to long without any luck... here's the meat of the code:


    private void BilinearGradient()
    {
    int NW = 0xFF00FF00; // green
    int NE = 0xFFFF0000; // red
    int SW = 0xFF000000; // black
    int SE = 0xFF0000FF; // blue
    int i, j, red, green, blue, red2, green2, blue2;
    long intARGB1, intARGB2;
    int argb;

    // prompt user for height == width
    String input = promptUser();
    // convert string to int
    int imageSize = Integer.parseInt(input);

    // set up buffered image
    BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_INT_ARGB);
    // setup corners to start off
    image.setRGB(0, 0, NW);
    image.setRGB(imageSize-1, 0, NE);
    image.setRGB(0, imageSize-1, SW);
    image.setRGB(imageSize-1, imageSize-1, SE);

    for ( i = 0; i <= imageSize/2; i++ ) {
    for ( j = 0; j <= imageSize/2; j++ ) {

    // extraction
    intARGB1 = image.getRGB(j,i);
    red = (int)(intARGB1 >>> 16) & 0x000000FF;
    green = (int)(intARGB1 >>> 8) & 0x000000FF;
    blue = (int)intARGB1 & 0x000000FF;

    intARGB2 = image.getRGB(imageSize-1-j, i);
    red2 = (int)(intARGB2 >>> 16) & 0x000000FF;
    green2 = (int)(intARGB2 >>> 8) & 0x000000FF;
    blue2 = (int)intARGB2 & 0x000000FF;

    // interpolation
    red = (red - red2) / imageSize;
    green = (green - green2) / imageSize;
    blue = (blue - blue2) / imageSize;

    argb = (red << 16 ) | (green << 8 ) | blue;

    image.setRGB( j, i, argb);
    image.setRGB( imageSize-1-j, i, argb);

    }
    }

    // display image
    displayBufferedImage( image );
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    getRGB doesn't seem to be giving me what I want.
    Can you explain what it gives you and what you want and how they are different.

    Have you tried debugging your code by printing out the values to see if you are generating them correctly? The Integer.toHexString() method is useful here.

  3. #3
    Federico is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    I get really weird values, like -4708340 and such, when I'm expecting hex TYPE_INT_ARGB...
    how does Interger.toHexString() work exactly?

    thakns for the quick reply

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    What it does is print an int in hex characters. Instead of gettting -4708340 you would get ffb8280c

  5. #5
    Federico is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    it doesn't seem to work either... i get 0 values when i print them out...
    I think the whole loop is erroneous....i just can't think of a better way to implement the concept.

    The idea is to interpolate each color component separately, then convert it back to an intARGB to plot in the right location based on the distances of the colors... I feel I'm missing something.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    i get 0 values when i print them out..
    What variables are you printing out? The value returned by: getRGB(imageSize-1-j, i)
    Or the results of your computations?

    Where do you ever set any colors in the image you've created? Its virgin.
    Last edited by Norm; 09-09-2010 at 04:03 PM.

  7. #7
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    If you are going to interpolate among NW, NE, SW, and SE, then surely those values should be used somewhere in your loop?

    I also want to point out that stuff like this

    Java Code:
    (red - red2) / imageSize;
    is integer arithmetic. When you subtract two ints and divide by another you get an int back. And in particular, since you are dividing by imageSize the expression will pretty much always evaluate to 0.
    Last edited by Maxideon; 09-10-2010 at 05:36 AM.

Similar Threads

  1. Bilinear Interpolation Scaling help
    By Xpectro in forum Java 2D
    Replies: 1
    Last Post: 06-15-2010, 08:12 PM
  2. how to use javax.media.jai.Interpolation
    By MINGxDOG in forum Java 2D
    Replies: 0
    Last Post: 11-16-2009, 09:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •