Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > size of pixel data array

Reply
Thread Tools

size of pixel data array

 
 
jimgardener
Guest
Posts: n/a
 
      11-15-2010
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
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      11-15-2010
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/
 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      11-15-2010
In article
<c3f07e29-a255-4178-bea6->,
jimgardener <> 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>
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      11-15-2010
On 11/15/2010 08:59 AM, John B. Matthews wrote:
> In article
> <c3f07e29-a255-4178-bea6->,
> jimgardener<> 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/
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      11-15-2010
In article <ZMdEo.20322$>,
Knute Johnson <> 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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is Fuji S3000 3.2m/pixel output, or 6 m/pixel interpolated output? Peter H Digital Photography 43 12-04-2003 02:35 PM
Fuji S3000 - 3.2 m/pixel or 6 m/pixel? Peter H Digital Photography 3 11-18-2003 11:17 PM
Re: Pixel size of individual Pixel Robert E. Williams Digital Photography 2 09-16-2003 03:02 PM
Re: Pixel size of individual Pixel Tom Thackrey Digital Photography 2 09-14-2003 04:17 PM
Hot pixel vs. stuck pixel Abrasha Digital Photography 5 09-02-2003 04:49 PM



Advertisments