Solved

Cast int[] array to byte[] array?

Posted on 2009-05-14
8
1,465 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reverse digits of a number using for loop 5 52
type mismatch (Object[] to double[] 4 33
restrict decimal places for double datatype 10 22
Java class and jar 3 25
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

828 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