Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Cast int[] array to byte[] array?

Posted on 2009-05-14
8
Medium Priority
?
1,480 Views
Last Modified: 2012-05-07
Hi,

I'm doing some image work. One image class wants a pixel buffer in the form of an int[] array, another image class wants a pixel buffer in the form of a byte[] array. Is there a way to cast between to the two? I don't want to manually iterate through each pixel, converting, it will be very slow. Example below, thanks:
Image img;
int width = img.getWidth();
int height = img.getHeight();
 
// Pixels are stored in aarrggbb format in a single integer.
int argb[] = new int[width * height];
img.getPixels(argb);
 
 
// Now this other image class wants the pixel buffer as a byte array.
byte argb2[] = new byte[width * height * 4];
ImgOther img2 = new ImgOther(argb2);
 
// But is there any way to do something like:
byte argb2[] = (byte[])argb;
ImgOther img3 = new ImgOther(argb3);

Open in new window

0
Comment
Question by:DJ_AM_Juicebox
[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
  • Learn & ask questions
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24384923
Casting is not possible in non-native languages like Java. You need to create a new array and copy
0
 
LVL 1

Expert Comment

by:mohgupta
ID: 24385588
Type casting is not possible from a data type up in hierarchy to lower data types i.e from integer to byte but the reverse is possible i.e you can convert byte to integer..
There is no other way to directly cast byte array to integer. you need to iterate or use recursion for each value.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 24385795
>>Type casting is not possible from a data type up in hierarchy to lower data types i.e from integer to byte

It is actually (see below) but the question's really nothing to do with the casting of primitives
        int i = 128;
        byte b = (byte)i;

Open in new window

0
Industry Leaders: 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!

 

Author Comment

by:DJ_AM_Juicebox
ID: 24386241
blast!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24386331
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 24391732
typically in image processing you do not need to convert between array types, sounds like a limitation in the classes you are using.

0
 

Author Comment

by:DJ_AM_Juicebox
ID: 24392455
Yeah I am working on blackberry phones using j2me - the j2me Image class only has a method to give you the pixel data as an integer array. The native blackberry image class only has a constructor using a byte array. How annoying. My sadness is unprecedented.
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 24392457
Oh yeah and the reason I wanted to mix the two was because the native BB image class has some rescaling functions built-in, which the j2me version amazingly does not.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

609 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