have to use different image types, depending on the OS. how?

Discussion in 'Java' started by linuxadmin@yandex.ru, Jun 20, 2007.

  1. Guest

    hello!


    i load JPEG images into BufferedImage objects and draw them using
    Graphics2D.drawImage(...)

    to be able to use LookupOp (for gamma and brightness adjustments) i
    have to convert them to one of the following image types:
    TYPE_INT_RGB or TYPE_INT_BGR.
    the JPEG-native type TYPE_INT_3BGR doesn't work.

    [ note: the conversion is done by creating an empty BufferedImage with
    same size but different image type, and drawing the loaded image into
    the newly created image: ]

    BufferedImage img = ImageIO.read(new File(fileName));

    BufferedImage temp;
    temp = new BufferedImage(img.getWidth(), img.getHeight(),
    BufferedImage.TYPE_INT_RGB // <-problem
    );

    temp.getGraphics().drawImage(img, 0, 0, null);
    img = temp;


    now the problem:
    the images are drawn correctly, when using
    TYPE_INT_BGR under linux but: TYPE_INT_RGB under windows.
    so, when the 'wrong' type is used, the colors look ugly.

    i tried to get to know the system-native image type by letting
    GraphicConfiguration create a BufferedImage:

    GraphicsEnvironment ge = GraphicsEnvironment
    .getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();
    BufferedImage bestImage = gc.createCompatibleImage(1, 1);

    but: on both systems bestImage.getType() equals TYPE_INT_RGB, so under
    linux the colors are wrong.

    i think that it is wrong to assume, that the linux platform always
    implies TYPE_INT_BGR and the windows platform always implies
    TYPE_INT_RGB.

    so how can i get the real system-native image type?


    thanks in advice!
     
    , Jun 20, 2007
    #1
    1. Advertising

  2. wrote:
    > hello!
    >
    >
    > i load JPEG images into BufferedImage objects and draw them using
    > Graphics2D.drawImage(...)
    >
    > to be able to use LookupOp (for gamma and brightness adjustments) i
    > have to convert them to one of the following image types:
    > TYPE_INT_RGB or TYPE_INT_BGR.
    > the JPEG-native type TYPE_INT_3BGR doesn't work.
    >
    > [ note: the conversion is done by creating an empty BufferedImage with
    > same size but different image type, and drawing the loaded image into
    > the newly created image: ]
    >
    > BufferedImage img = ImageIO.read(new File(fileName));
    >
    > BufferedImage temp;
    > temp = new BufferedImage(img.getWidth(), img.getHeight(),
    > BufferedImage.TYPE_INT_RGB // <-problem
    > );
    >
    > temp.getGraphics().drawImage(img, 0, 0, null);
    > img = temp;
    >
    >
    > now the problem:
    > the images are drawn correctly, when using
    > TYPE_INT_BGR under linux but: TYPE_INT_RGB under windows.
    > so, when the 'wrong' type is used, the colors look ugly.
    >
    > i tried to get to know the system-native image type by letting
    > GraphicConfiguration create a BufferedImage:
    >
    > GraphicsEnvironment ge = GraphicsEnvironment
    > .getLocalGraphicsEnvironment();
    > GraphicsDevice gd = ge.getDefaultScreenDevice();
    > GraphicsConfiguration gc = gd.getDefaultConfiguration();
    > BufferedImage bestImage = gc.createCompatibleImage(1, 1);
    >
    > but: on both systems bestImage.getType() equals TYPE_INT_RGB, so under
    > linux the colors are wrong.
    >
    > i think that it is wrong to assume, that the linux platform always
    > implies TYPE_INT_BGR and the windows platform always implies
    > TYPE_INT_RGB.
    >
    > so how can i get the real system-native image type?
    >
    >
    > thanks in advice!
    >


    Try the method below to convert your image to the type you want it to
    be. This method converts to a compatible type but you can make it any
    type you want.

    // method to convert an image read from a file to a compatible image
    BufferedImage convertImage(BufferedImage image) {
    GraphicsEnvironment ge =
    GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] devices = ge.getScreenDevices();
    GraphicsConfiguration gc =
    devices[Math.min(devices.length-1,0)].getDefaultConfiguration();

    BufferedImage compatible gc.createCompatibleImage(image.getWidth(),
    image.getHeight());

    if (compatible.getType() == image.getType())
    return image;

    ColorConvertOp op = new ColorConvertOp(
    image.getColorModel().getColorSpace(),
    compatible.getColorModel().getColorSpace(), null);
    op.filter(image, compatible);

    return compatible;
    }

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jun 21, 2007
    #2
    1. Advertising

  3. Guest

    thank you very much, i'll try it out!
     
    , Jun 21, 2007
    #3
    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. C Gillespie
    Replies:
    0
    Views:
    303
    C Gillespie
    May 24, 2005
  2. namo
    Replies:
    2
    Views:
    317
  3. Kit
    Replies:
    0
    Views:
    345
  4. Replies:
    0
    Views:
    131
  5. Replies:
    6
    Views:
    260
    Rainer Weikusat
    Nov 8, 2013
Loading...

Share This Page