size of pixel data array

Discussion in 'Java' started by jimgardener, Nov 15, 2010.

  1. jimgardener

    jimgardener Guest

    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
    jimgardener, Nov 15, 2010
    #1
    1. Advertising

  2. 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/
    Knute Johnson, Nov 15, 2010
    #2
    1. Advertising

  3. In article
    <>,
    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>
    John B. Matthews, Nov 15, 2010
    #3
  4. On 11/15/2010 08:59 AM, John B. Matthews wrote:
    > In article
    > <>,
    > 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/
    Knute Johnson, Nov 15, 2010
    #4
  5. 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>
    John B. Matthews, Nov 15, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anitha Namachivayam via JavaKB.com

    Pixel array

    Anitha Namachivayam via JavaKB.com, Apr 13, 2005, in forum: Java
    Replies:
    0
    Views:
    594
    Anitha Namachivayam via JavaKB.com
    Apr 13, 2005
  2. D i s h a n

    Pixel array into Image

    D i s h a n, Jan 8, 2007, in forum: Java
    Replies:
    5
    Views:
    1,869
    Knute Johnson
    Jan 9, 2007
  3. Jason B

    Pixel Array => Bitmap File

    Jason B, Feb 27, 2007, in forum: Python
    Replies:
    3
    Views:
    697
    Jason B
    Feb 27, 2007
  4. André Wagner

    Fastest pixel by pixel operation

    André Wagner, Mar 17, 2008, in forum: Java
    Replies:
    12
    Views:
    1,028
    Larry A Barowski
    Mar 19, 2008
  5. RMZ
    Replies:
    2
    Views:
    181
    David Mark
    Aug 2, 2007
Loading...

Share This Page