Solved

Converting pixel value (byte) to integer value

Posted on 2003-03-30
Medium Priority
694 Views
i have used the pixelgrabber to grab pixels. When i tried to work on the individual pixel value from the integer array i found the values were actually byte values something like  (-7697782). I tried to type cast these byte values to integer. It works for certain values but for certain values i get only negative value (like -180 for -7697782). How do i convert the pixel array values to numbers within the range 0 to 255. or else how do i get an integer RGB value of a pixel within the range 0 to 255.

Someone help me in this regard.
Grattitudes.
0
Question by:itmaniac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 35

Accepted Solution

TimYates earned 252 total points
ID: 8235298
>  found the values were actually byte values something like  (-7697782)

This can't be a byte...it's too big...

What code are you using?  If you are loading the image with a Paletted ColorModel, then you will be getting palette entries back, not RGB colors...

Otherwise, you should have an array of int, then get the colors like:

int alpha = ( color & 0xFF000000 ) >> 24 ;
int red = ( color & 0xFF0000 ) >> 16 ;
int green = ( color & 0xFF00 ) >> 8 ;
int blue = ( color & 0xFF ) ;
0

LVL 30

Assisted Solution

Mayank S earned 248 total points
ID: 8236970
'byte' need not be cast to 'int'. If ever required, the conversion will be done automatically, because 'int' has more range than 'byte' and by default, 'byte' and 'short' are always converted to 'int' in expressions.

byte a = 5 ;
int b ;
b = a ; // valid - 'int' has larger storage and both are compatible types
b = ( int ) a ; // the (cast) is actually not required

Mayank.
0

Expert Comment

ID: 9058389
itmaniac:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

LVL 15

Expert Comment

ID: 9735655
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Delete - No refund

jimmack
EE Cleanup Volunteer
0

LVL 30

Expert Comment

ID: 9738146
I would say: Split between TimYates and mayankeagle - their explanations are enough as per the questioner's requirements.
0

LVL 15

Expert Comment

ID: 9738164
OK.  Thanks Mayank ;-)

Correction posted to cleanup:  Split between TimYates and mayankeagle.
0

LVL 30

Expert Comment

ID: 9738177
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll