Solved

Cast int[] array to byte[] array?

Posted on 2009-05-14
8
1,451 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
create a gui in perl 3 70
Error with Java/Cache JDBC Classpath 2 30
Java SE 8u111  Lot of stuff broke 11 54
expectj telnet failing 5 23
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

912 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

20 Experts available now in Live!

Get 1:1 Help Now