Converting pixel value (byte) to integer value

Posted on 2003-03-30
Medium Priority
Last Modified: 2008-03-10
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.
Question by:itmaniac
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
  • Learn & ask questions
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 ) ;
LVL 30

Assisted Solution

by:Mayank S
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


Expert Comment

ID: 9058389
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

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

Please leave any comments here within the next seven days.


EE Cleanup Volunteer
LVL 30

Expert Comment

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

Expert Comment

ID: 9738164
OK.  Thanks Mayank ;-)

Correction posted to cleanup:  Split between TimYates and mayankeagle.
LVL 30

Expert Comment

by:Mayank S
ID: 9738177
Please go ahead.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question