Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   size of pixel data array (http://www.velocityreviews.com/forums/t738063-size-of-pixel-data-array.html)

jimgardener 11-15-2010 12:23 PM

size of pixel data array
 
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

Knute Johnson 11-15-2010 04:26 PM

Re: size of pixel data array
 
On 11/15/2010 04:23 AM, jimgardener wrote:
> 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.

--

Knute Johnson
s/nospam/knute2010/

John B. Matthews 11-15-2010 04:59 PM

Re: size of pixel data array
 
In article
<c3f07e29-a255-4178-bea6-24233bc3aca9@35g2000prt.googlegroups.com>,
jimgardener <jimgardener@gmail.com> wrote:

> 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 B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Knute Johnson 11-15-2010 05:07 PM

Re: size of pixel data array
 
On 11/15/2010 08:59 AM, John B. Matthews wrote:
> In article
> <c3f07e29-a255-4178-bea6-24233bc3aca9@35g2000prt.googlegroups.com>,
> jimgardener<jimgardener@gmail.com> wrote:
>
>> 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 :-).

--

Knute Johnson
s/nospam/knute2010/

John B. Matthews 11-15-2010 09:18 PM

Re: size of pixel data array
 
In article <ZMdEo.20322$AT2.18206@newsfe01.iad>,
Knute Johnson <nospam@knutejohnson.com> wrote:

> 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>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>


All times are GMT. The time now is 09:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.