Re: Problems with BufferedImage and ColorModel
From: Knute Johnson (nospam_at_frazmtn.com)
Date: 06/21/04
- Next message: Roedy Green: "Re: Davadoc and methods"
- Previous message: Andrew Thompson: "Re: Davadoc and methods"
- In reply to: Inertia_sublimation: "Problems with BufferedImage and ColorModel"
- Next in thread: Andrew Thompson: "Re: Problems with BufferedImage and ColorModel"
- Reply: Andrew Thompson: "Re: Problems with BufferedImage and ColorModel"
- Reply: Inertia_sublimation: "Re: Problems with BufferedImage and ColorModel"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 21 Jun 2004 16:16:40 GMT
Inertia_sublimation wrote:
> Hi everyone!
>
> I've been experimenting with BufferedImage and am attempting to extract the
> individual R, G, B, and Alpha values from the color of each pixel in an image.
> This seems simple and straight-forward, but somehow I got slightly confused. I
> managed to figure out how to extract some large int value that supposedly
> represents the combined color using BufferedImage.getColor(x, y), however, once
> I pass that value to the getRed() method of the ColorModel I get from
> BufferedImage.getColorModel(), it throws an IllegalArgumentException.
>
> I tryed using the ColorModel from ColorModel.getRGBDefault(), but now, that
> gives me bogus values from its getRed/getBlue/getGreen/getAlpha methods for the
> int-representations of the pixels. The 3x3 image attached is the one I'm
> debugging with, the value this method gives me is:
> "<0, 0, 0, 255><255, 255, 255, 255><255, 255, 255, 255><0, 0, 0, 0><0, 0, 0,
> 0><0, 0, 0, 0><0, 0, 0, 0><0, 0, 0, 0><0, 0, 0, 0>"
>
> It seems to get the first row of pixels right, but anything more then that is
> totally bogus.
>
> Anyone know my problem? Here's a snippet of the code I'm using:
>
> public class MyClass {
> public String getImageColors(BufferedImage image) {
> /* Get the default ColorModel, which lets us convert the int pixels
> * into 4 integers: red, green, blue and alpha. */
> ColorModel pixelConverter = ColorModel.getRGBdefault();
>
> /* Get the pixels in the entire image, these are large ints that have
> * the RGB and Alpha values all together. */
> int[] pixels = new int[image.getWidth() * image.getHeight()];
> for(int i = 0; i < image.getWidth(); i++) {
> for(int j = 0; j < image.getHeight(); j++) {
> pixels[i] = image.getRGB(i, j);
> }
> }
>
> // Construct the containing StringBuffer.
> StringBuffer bigBuff = new StringBuffer(pixels.length * 20);
>
> // This will help us construct a string in this form: "<RRR, GGG, BBB,
> AAA>"
> StringBuffer quatBuff = new StringBuffer(20);
>
> int sendCount = 0;
> for(int i = 0; i < pixels.length; i++) {
> quatBuff.append('<');
> quatBuff.append(pixelConverter.getRed(pixels[i]));
> quatBuff.append(',').append(' ');
> quatBuff.append(pixelConverter.getGreen(pixels[i]));
> quatBuff.append(',').append(' ');
> quatBuff.append(pixelConverter.getBlue(pixels[i]));
> quatBuff.append(',').append(' ');
> quatBuff.append(pixelConverter.getAlpha(pixels[i]));
> quatBuff.append('>');
>
> bigBuff.append(quatBuff);
>
> // Too bad there isnt a StringBuffer.clear() method. :-(
> quatBuff = new StringBuffer(20);
> }
> return bigBuff.toString();
> }
> }
>
>
>
It always helps to give complete code examples that compile. Your
problem however appears to be not understanding how the RGB information
is coded into the int value returned from getRGB(). Pixels are encoded
into 32 bits of an int. The least significant 8 bites represent the
blue value and the most significant bits (of the 32) represent the alpha
value.
int alpha = pixel >> 24;
int red = pixel >> 16 & 0xff;
int green = pixel >> 8 & 0xff;
int blue = pixel & 0xff;
The default ColorModel is not necessarily the ColorModel of your image.
If you want to use the methods from ColorModel you need to get the
specific ColorModel from your image. Here is a quote from the docs;
"ColorModel objects used with images for which pixel values are not
conveniently representable as a single int throw an
IllegalArgumentException when methods taking a single int pixel argument
are called. Subclasses of ColorModel must specify the conditions under
which this occurs. This does not occur with DirectColorModel or
IndexColorModel objects."
That probably explains why you are getting the exception from those
methods. Just get the pixel values from the BufferedImage using one of
the getRGB()s and use the code above to extract an int value for each of
the colors.
-- knute... email s/nospam/knute/
- Next message: Roedy Green: "Re: Davadoc and methods"
- Previous message: Andrew Thompson: "Re: Davadoc and methods"
- In reply to: Inertia_sublimation: "Problems with BufferedImage and ColorModel"
- Next in thread: Andrew Thompson: "Re: Problems with BufferedImage and ColorModel"
- Reply: Andrew Thompson: "Re: Problems with BufferedImage and ColorModel"
- Reply: Inertia_sublimation: "Re: Problems with BufferedImage and ColorModel"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|