aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes negative color Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "negative color" Watch "negative color" New topic
Author

negative color

Sophie Bell
Greenhorn

Joined: Oct 23, 2011
Posts: 11
Hi all,
I'm trying to define a simple method that takes an image and returns its negative. It appears I have some trouble with the method that returns the negative of the pixel color (only black becomes white, but every other color is returned as yellow).
Here's the code:

private GImage makeColorNegative(GImage image) {
int[][]array = image.getPixelArray();
for(int row = 0; row < array.length; row++) {
for(int col = 0; col < array[0].length; col++) {
int pixelColor = array[row][col];
array[row][col] = getNegativeColor(pixelColor);
}
}
return new GImage(array);
}

private int getNegativeColor(int color) {
int alpha = ((color >> 24) & 0xFF);
int negRed = ~((color >> 24) & 0xFF);
int negGreen = ~((color >> 16) & 0xFF);
int negBlue = ~((color >> 8) & 0xFF);
return (alpha << 24) | (negRed << 16) | (negGreen << 8) | negBlue;
}

Thanks
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
There may be other problems, but the bit-shifts used for negRed, negGreen and negBlue are incorrect. The shifts should be 16, 8, and 0, respectively, instead of 24, 16, and 8.
Sophie Bell
Greenhorn

Joined: Oct 23, 2011
Posts: 11
I can't believe I made such a silly mistake...
Thanks, Tim
Although, the program still doesn't work
In fact, it seems this particular mistake had nothing to do with the result
Here's the latest version:

private GImage makeColorNegative(GImage image) {
int[][]array = image.getPixelArray();
for(int row = 0; row < array.length; row++) {
for(int col = 0; col < array[0].length; col++) {
int pixelColor = array[row][col];
array[row][col] = getNegativeColor(pixelColor);
}
}
return new GImage(array);
}

private int getNegativeColor(int color) {
int alpha = ((color >> 24) & 0xFF);
int negRed = ~((color >> 16) & 0xFF);
int negGreen = ~((color >> 8) & 0xFF);
int negBlue = ~(color & 0xFF);
return (alpha << 24) | (negRed << 16) | (negGreen << 8) | negBlue;
}
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Oh, right: the ~ operator will switch *all* 32 bits, and not just the least-significant 8 bits that you're looking at for a particular color. So instead of "~ XYZ", try "255 - XYZ".
Sophie Bell
Greenhorn

Joined: Oct 23, 2011
Posts: 11
Thanks, Tim! It worked
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Or move the ~ within the parentheses:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sophie Bell
Greenhorn

Joined: Oct 23, 2011
Posts: 11

Thanks, Rob
I love this forum! You are great
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

We most definitely are!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: negative color