By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Liz

"Patrick" <mc**********@hotmail.com> wrote in message
news:f9**************************@posting.google.c om...
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.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.