Solved

Cast int[] array to byte[] array?

Posted on 2009-05-14
8
1,459 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
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 500 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

778 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