DJ_AM_Juicebox
asked on
Cast int[] array to byte[] array?
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:
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);
Casting is not possible in non-native languages like Java. You need to create a new array and copy
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.
There is no other way to directly cast byte array to integer. you need to iterate or use recursion for each value.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
blast!
:-)
typically in image processing you do not need to convert between array types, sounds like a limitation in the classes you are using.
ASKER
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.
ASKER
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.