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

P

Patrick

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
 
L

Liz

Patrick said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top