Solved

Cast int[] array to byte[] array?

Posted on 2009-05-14
8
1,448 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
 

Author Comment

by:DJ_AM_Juicebox
ID: 24386241
blast!
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now