size of pixel data array

J

jimgardener

hi,
if I want to create a BufferedImage of certain width and height from
a given array of pixel data,how should I check for the array size?

should it be like data.length == width* height ? or should I check
data.length == 3 * width* height ?

Below is the code snippet where I would like to check the pixel data
array size.

public class ImageCreateUtil {
public BufferedImage createImage (int w , int h ,double[] data){
BufferedImage myImage=new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
myImage.getRaster().setPixels(0, 0, w, h, data);
return myImage;
}

....
}

thanks in advance,
jim
 
K

Knute Johnson

hi,
if I want to create a BufferedImage of certain width and height from
a given array of pixel data,how should I check for the array size?

should it be like data.length == width* height ? or should I check
data.length == 3 * width* height ?

Below is the code snippet where I would like to check the pixel data
array size.

public class ImageCreateUtil {
public BufferedImage createImage (int w , int h ,double[] data){
BufferedImage myImage=new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
myImage.getRaster().setPixels(0, 0, w, h, data);
return myImage;
}

...
}

thanks in advance,
jim

The docs are your friend:

setPixels

public void setPixels(int x,
int y,
int w,
int h,
int[] iArray)

Sets all samples for a rectangle of pixels from an int array
containing one sample per array element. An
ArrayIndexOutOfBoundsException may be thrown if the coordinates are not
in bounds. However, explicit bounds checking is not guaranteed.

Parameters:
x - The X coordinate of the upper left pixel location.
y - The Y coordinate of the upper left pixel location.
w - Width of the pixel rectangle.
h - Height of the pixel rectangle.
iArray - The input int pixel array.
Throws:
NullPointerException - if iArray is null.
ArrayIndexOutOfBoundsException - if the coordinates are not in
bounds, or if iArray is too small to hold the input.


"one sample per array element" so width * height would be correct.
 
J

John B. Matthews

jimgardener said:
if I want to create a BufferedImage of certain width and height from
a given array of pixel data, how should I check for the array size?

should it be like data.length == width* height ? or should I check
data.length == 3 * width* height ?

Below is the code snippet where I would like to check the pixel data
array size.

public class ImageCreateUtil {
public BufferedImage createImage(int w, int h, double[] data) {
BufferedImage myImage=new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
myImage.getRaster().setPixels(0, 0, w, h, data);
return myImage;
}
}

A double[] is merely a sequence of numbers. In order to know what kind
of BufferedImage to create, you have to know _a prori_ how many of those
numbers represent a single-pixel sample, e.g. one for TYPE_BYTE_GRAY,
three for TYPE_INT_RGB, four for TYPE_INT_ARGB, etc.

A BufferedImage is comprised of a ColorModel and a Raster of image data.
A Raster, in turn, encapsulates a DataBuffer and a SampleModel. Your
createImage method specifies TYPE_INT_RGB. As a sanity check, you could
do something like this:

BufferedImage myImage = new BufferedImage(
w, h, BufferedImage.TYPE_INT_RGB);
int n = myImage.getRaster().getSampleModel().getNumBands();
if (data.length != n * w * h) {
throw new IllegalArgumentException(...);
}

As an aside, thank you for looking after your code's line wrap, but
watch the indents, too.
 
K

Knute Johnson

jimgardener said:
if I want to create a BufferedImage of certain width and height from
a given array of pixel data, how should I check for the array size?

should it be like data.length == width* height ? or should I check
data.length == 3 * width* height ?

Below is the code snippet where I would like to check the pixel data
array size.

public class ImageCreateUtil {
public BufferedImage createImage(int w, int h, double[] data) {
BufferedImage myImage=new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
myImage.getRaster().setPixels(0, 0, w, h, data);
return myImage;
}
}

A double[] is merely a sequence of numbers. In order to know what kind
of BufferedImage to create, you have to know _a prori_ how many of those
numbers represent a single-pixel sample, e.g. one for TYPE_BYTE_GRAY,
three for TYPE_INT_RGB, four for TYPE_INT_ARGB, etc.

A BufferedImage is comprised of a ColorModel and a Raster of image data.
A Raster, in turn, encapsulates a DataBuffer and a SampleModel. Your
createImage method specifies TYPE_INT_RGB. As a sanity check, you could
do something like this:

BufferedImage myImage = new BufferedImage(
w, h, BufferedImage.TYPE_INT_RGB);
int n = myImage.getRaster().getSampleModel().getNumBands();
if (data.length != n * w * h) {
throw new IllegalArgumentException(...);
}

As an aside, thank you for looking after your code's line wrap, but
watch the indents, too.

John's right, ignore my post please :).
 
J

John B. Matthews

Knute Johnson said:
John's right, ignore my post please :).

OP: I would argue that Knute is also right on two counts:

1) The docs are your friend, and
2) the setPixel* methods of WritableRaster to which he referred may
offer some insight.

Note how the final parameter of each is a primitive array containing the
samples that comprise a given pixel. In this example [1], the
BufferedImage returned by createImage() happens to be compatible with
TYPE_INT_ARGB, so four bytes are required for each sample.

It might be helpful to elaborate on the source of your double[] in order
to minimize the effort to render it.

[1]<https://sites.google.com/site/drjohnbmatthews/raster>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top