Java2D: not possible to set 32bpp ints?

Discussion in 'Java' started by Timo Nentwig, Apr 29, 2004.

  1. Timo Nentwig

    Timo Nentwig Guest

    Hi!

    It seems that it's not possible to use 32bpp ARGB encoded integers in
    Java2D, at least I didn't find such a method in the WriteableRaster API.

    Is there same different way to do so?

    Timo
     
    Timo Nentwig, Apr 29, 2004
    #1
    1. Advertising

  2. Timo Nentwig wrote:
    > It seems that it's not possible to use 32bpp ARGB encoded integers in
    > Java2D, at least I didn't find such a method in the WriteableRaster API.


    This might do (I didn't try):

    Using integers:

    Raster.createPackedRaster(
    DataBuffer.TYPE_INT, // data type
    w, h, // image width, height
    4, // bands
    8, // bits per band
    null); // use default (0,0) location
    Using bytes:

    Raster.createInterleavedRaster(
    DataBuffer.TYPE_BYTE, // data type
    w, h, // image width, height
    w * 4, // scan line stride
    4, // pixel stride
    new int[]{3, 2, 1, 0}, // band offset
    null); // use default (0, 0) location

    /Thomas
     
    Thomas Weidenfeller, Apr 30, 2004
    #2
    1. Advertising

  3. Timo Nentwig

    Timo Nentwig Guest

    Thomas Weidenfeller wrote:
    > Timo Nentwig wrote:
    >
    >> It seems that it's not possible to use 32bpp ARGB encoded integers in
    >> Java2D, at least I didn't find such a method in the WriteableRaster API.

    >
    >
    > This might do (I didn't try):
    >
    > Using integers:
    >
    > Raster.createPackedRaster(
    > DataBuffer.TYPE_INT, // data type
    > w, h, // image width, height
    > 4, // bands
    > 8, // bits per band
    > null); // use default (0,0) location


    What I meant is that I want to encode all the channels in a single int:

    int pixel = a << 24 | r << 16 | g << 8 | b;
    raster.setPixel(x,y,pixel);


    Java2D is incredibly slow :-( At least 5 times slower than a old-style
    MemoryImageSource solution.
     
    Timo Nentwig, Apr 30, 2004
    #3
  4. Timo Nentwig wrote:
    > What I meant is that I want to encode all the channels in a single int:


    That's why I suggested createPackedRaster. If I didn't mix up things, my
    example would pack 4 bands (A, R, G, B) with 8 bits per band into
    integers. Which should give you, if my math didn't fail me 4 * 8 = 32
    bits per pixel, which just happen to fit nicely in one Java 32 bit integer.

    /Thomas
     
    Thomas Weidenfeller, Apr 30, 2004
    #4
  5. Timo Nentwig

    Timo Nentwig Guest

    Thomas Weidenfeller wrote:
    > Timo Nentwig wrote:
    >
    >> What I meant is that I want to encode all the channels in a single int:

    >
    > That's why I suggested createPackedRaster. If I didn't mix up things, my
    > example would pack 4 bands (A, R, G, B) with 8 bits per band into
    > integers. Which should give you, if my math didn't fail me 4 * 8 = 32
    > bits per pixel, which just happen to fit nicely in one Java 32 bit integer.


    And how do I set that one integer? There's no Raster.setPixel() that
    accepts a single integer.
     
    Timo Nentwig, May 1, 2004
    #5
  6. Timo Nentwig wrote:
    > And how do I set that one integer? There's no Raster.setPixel() that
    > accepts a single integer.


    If nothing else helps, drill down to the DataBuffer and insert the data
    there.

    /Thomas
     
    Thomas Weidenfeller, May 4, 2004
    #6
  7. Timo Nentwig

    Guest

    Timo Nentwig <> wrote in message news:<c70bo0$gp03o$-berlin.de>...

    > And how do I set that one integer? There's no Raster.setPixel() that
    > accepts a single integer.


    Raster is general and doesn't know directly about any specific format.
    getDataBuffer() still is generic.

    If you know that the pixels are stored in ints, then you can cast the
    DataBuffer that comes out of Raster's getDataBuffer() to
    DataBufferInt.

    DataBufferInt has a getData(), that returns int[], at which point you
    can access the pixels directly.

    How about starting with a BufferedImage, which wraps all the
    colorspace/format issues all in one package:
    BufferedImage image = new BufferedImage(640, 480,
    BufferedImage.TYPE_INT_ARGB);
    int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
    for (int y = 0; y < 480; y++)
    for (int x = 0; x < 640; x++)
    pixels[y * 640 + x] = x * y;
    ....
    I'm not sure, but the compiler has some nifty optimization features,
    where something like this may actually not be any faster:
    for (int y = 0; y < 480; y++)
    {
    int offset = y * 640;
    for (int x = 0; x < 640; x++)
    pixels[offset + x] = x * y;
    }

    On an exciting side note, Java's bytecode to machine code translator
    is efficient enough that it can compete and sometimes outperform
    optimized C++ code! So raster-hacking in Java can be far more
    exciting as many people with presumptions about Java are willing to
    admit. Discover for yourself.

    Also, Java2D is one of *THE* cleanest, if not *THE* cleanest graphics
    library ever written. You get a lot of flexibility, performance,
    extendability in an amazing package. By the looks of it, it's better
    than GDI+, and most definitely way better than GNU land's GTK/GTK++.
     
    , May 8, 2004
    #7
    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. Marco Schmidt

    Re: Using Component based on Java2D

    Marco Schmidt, Jul 1, 2003, in forum: Java
    Replies:
    0
    Views:
    786
    Marco Schmidt
    Jul 1, 2003
  2. Replies:
    1
    Views:
    2,367
  3. Replies:
    3
    Views:
    568
    Mark P
    Apr 3, 2005
  4. Replies:
    1
    Views:
    1,126
    Andrey Kuznetsov
    Jun 20, 2006
  5. Skybuck Flying

    ints ints ints and ints

    Skybuck Flying, Jul 8, 2004, in forum: C Programming
    Replies:
    24
    Views:
    844
    Jack Klein
    Jul 10, 2004
Loading...

Share This Page