ImageIO.write jpg JPEG writer/encoder "tint" problems

Discussion in 'Java' started by Patrick, Jul 15, 2004.

  1. Patrick

    Patrick Guest

    Hello all!
    I am using a BufferedImage object to build an image from scratch.
    I want it to be a grayscale image with only 8bits of color. I have
    the color information as a byte, saved in variable name byte4. I have
    written the following code:

    First, thePanorama is defined as:
    new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Then I loop through like follows:

    int colorInt = 0;
    for (int y=0; y < height; y++) {
    double currentTheta = dsConstants.DEG_TO_RAD(minAzimuthAngle);
    for (int x=0; x<width; x++) {
    ThreeDPoints[x+y*width] = findRtpiAt(currentTheta,
    currentTheta + azimuthStep, currentPhi -
    elevationStep,
    currentPhi);

    //i.reflect has the color information in its fourth byte:
    //(int is four bytes)
    colorInt = ThreeDPoints[x+y*width].i.reflect;
    byte byte1 = (byte)(0xff & (colorInt >> 24)); //is ALWAYS 0
    byte byte2 = (byte)(0xff & (colorInt >> 16)); //is ALWAYS 0
    byte byte3 = (byte) (0xff & (colorInt >> 8)); //is ALWAYS 0
    byte byte4 = (byte) (0xff & colorInt); //byte4 has the "color"

    //R,G,B should all be the same for grayscale, right?
    //A is alpha channel (opacity), I get a black JPEG if it is 0
    byte rcolor = byte4;
    byte gcolor = byte4;
    byte bcolor = byte4;
    byte acolor = (byte)255;

    colorInt = (acolor & 0xff) << 24 |
    (rcolor & 0xff) << 16 |
    (gcolor & 0xff) << 8 |
    (bcolor & 0xff);

    thePanorama.setRGB(x, y, colorInt);
    currentTheta += azimuthStep;
    }
    currentPhi -= elevationStep;
    //System.out.println("Wrote pixel row (" + y + ")");
    }


    The image is not modified and eventually passed into:

    ImageIO.write(thePanorama, "JPEG", new
    java.io.File("images\\test.jpg"));

    The image is written out okay. I get an image with data. My problem
    is that it is not grayscale, it is sort of "blue-green" scale. It has
    a blue-green tint to it. I know the image is alright because I can
    output it to BMP and it comes out okay. Is JPEG somehow forced into
    being 24bit color? If so, what functions can I use to write the color
    information to the BufferedImage? Do I need to use a different type
    of BufferedImage? Any and all comments are welcome, and I would also
    really appreciate it if responses could be sent directly to me, as
    well as to the newsgroup.

    TIA

    -Patrick
     
    Patrick, Jul 15, 2004
    #1
    1. Advertising

  2. Patrick

    Liz Guest

    "Patrick" <> wrote in message
    news:...
    > Hello all!
    > I am using a BufferedImage object to build an image from scratch.
    > I want it to be a grayscale image with only 8bits of color. I have
    > the color information as a byte, saved in variable name byte4. I have
    > written the following code:
    >
    > First, thePanorama is defined as:
    > new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    >
    > Then I loop through like follows:
    >
    > int colorInt = 0;
    > for (int y=0; y < height; y++) {
    > double currentTheta = dsConstants.DEG_TO_RAD(minAzimuthAngle);
    > for (int x=0; x<width; x++) {
    > ThreeDPoints[x+y*width] = findRtpiAt(currentTheta,
    > currentTheta + azimuthStep, currentPhi -
    > elevationStep,
    > currentPhi);
    >
    > //i.reflect has the color information in its fourth byte:
    > //(int is four bytes)
    > colorInt = ThreeDPoints[x+y*width].i.reflect;
    > byte byte1 = (byte)(0xff & (colorInt >> 24)); //is ALWAYS 0
    > byte byte2 = (byte)(0xff & (colorInt >> 16)); //is ALWAYS 0
    > byte byte3 = (byte) (0xff & (colorInt >> 8)); //is ALWAYS 0
    > byte byte4 = (byte) (0xff & colorInt); //byte4 has the "color"
    >
    > //R,G,B should all be the same for grayscale, right?
    > //A is alpha channel (opacity), I get a black JPEG if it is 0
    > byte rcolor = byte4;
    > byte gcolor = byte4;
    > byte bcolor = byte4;
    > byte acolor = (byte)255;
    >
    > colorInt = (acolor & 0xff) << 24 |
    > (rcolor & 0xff) << 16 |
    > (gcolor & 0xff) << 8 |
    > (bcolor & 0xff);
    >
    > thePanorama.setRGB(x, y, colorInt);
    > currentTheta += azimuthStep;
    > }
    > currentPhi -= elevationStep;
    > //System.out.println("Wrote pixel row (" + y + ")");
    > }
    >
    >
    > The image is not modified and eventually passed into:
    >
    > ImageIO.write(thePanorama, "JPEG", new
    > java.io.File("images\\test.jpg"));
    >
    > The image is written out okay. I get an image with data. My problem
    > is that it is not grayscale, it is sort of "blue-green" scale. It has
    > a blue-green tint to it. I know the image is alright because I can
    > output it to BMP and it comes out okay. Is JPEG somehow forced into
    > being 24bit color? If so, what functions can I use to write the color
    > information to the BufferedImage? Do I need to use a different type
    > of BufferedImage? Any and all comments are welcome, and I would also
    > really appreciate it if responses could be sent directly to me, as
    > well as to the newsgroup.
    >
    > TIA
    >
    > -Patrick


    If you set the red, blue, and green colors to the same value it will be
    gray.
     
    Liz, Jul 16, 2004
    #2
    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. F C
    Replies:
    2
    Views:
    5,190
  2. Davidski
    Replies:
    0
    Views:
    3,921
    Davidski
    Nov 5, 2004
  3. lovaspillando
    Replies:
    0
    Views:
    1,039
    lovaspillando
    Aug 26, 2007
  4. Ivan Alameda Carballo
    Replies:
    0
    Views:
    501
    Ivan Alameda Carballo
    Aug 26, 2007
  5. Dan Moskowitz

    Fastest way to tint an image with PIL

    Dan Moskowitz, Nov 6, 2008, in forum: Python
    Replies:
    2
    Views:
    674
    Marc 'BlackJack' Rintsch
    Nov 7, 2008
Loading...

Share This Page