Get RGB components from ColorModel - java

I want to extract the R,G and B values of the pixels of an image. I do it in two ways.
File img_file = new File("../foo.png");
BufferedImage img = ImageIO.read(img_file);
1st method(which works fine):
img.getRaster().getPixel(i, j, rgb);
2nd method(which throws new IllegalArgumentException("More than one component per pixel"))
red = img.getColorModel().getRed(img.getRGB(i, j));
What is the reason for this behaviour?

Normally when I want to extract RGB from a BufferedImage I do something like this:
File img_file = new File("../foo.png");
BufferedImage img = ImageIO.read(img_file);
Color color = new Color(img.getRGB(i,j));
int red = color.getRed();

Based on the JavaDocs
An IllegalArgumentException is thrown if pixel values for this
ColorModel are not conveniently representable as a single int
It would suggest that the underlying color model is representable by a single int value
You may also want to take a look at this answer for some more details
Typically, you would simply take the int packed pixel from the image and use Color to generate a Color representation and then extract the values from there...
First, get the int packed value of the pixel at x/y...
int pixel = img.getRGB(i, j);
Use this to construct a Color object...
Color color = new Color(pixel, true); // True if you care about the alpha value...
Extract the R, G, B values...
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
Now you could simply do some bit maths, but this is simpler and is more readable - IMHO

Related

How to set color in graphics in C#

How to set color in graphics in c#?
SetColor method not working.
How do I convert the following Java code to C#?
private Graphics g1;
g1.setColor(Color.getHSBColor(h, 0.8f, b));
Color col = Color.getHSBColor(h, 0.8f, b);
int red = col.getRed();
int green = col.getGreen();
int blue = col.getBlue();
The drawing model in C# is slightly different. Instead of setting resources, like color, on a graphic object, you create resources, such as Pen, or Brush that have properties on them, like Color. You then use those objects to draw on an image.
Graphics g = Graphics.FromImage(someBitMap); //create a graphics object for an existing BitMap
Color c = new Color(255,0,0); //a "red" color
Pen p = new Pen(c); //create a Pen using the Red color from earlier
p.Width = 5; //Pen is 5 pixels wide
g.DrawLine(p, 0,0,100,100); //draw a diagonal line
//Get rbg values.
int r = c.R;
int g = c.G;
int b = c.B;
//It is VERY important to call `.Dispose()` on GDI objects. They contain unmanaged system resources that can, and will, leak if you don't.
g.Dispose()
p.Dispose()
It looks like you might be trying to convert a color from the HSB color-space to RGB. Unfortunately, that isn't natively supported in .NET. The following blog post has some code that can do that for you: https://blogs.msdn.microsoft.com/cjacks/2006/04/12/converting-from-hsb-to-rgb-in-net/

Java, image is changing to wrong rgb values

i wrote a java code to change all the red values of a black and white image to 255, so the output would be a red image.
But its not red, instead it outputs a brighter image.
What did I do wrong?
File bwgFile = new File("X:/Java/Documents/NetBeansProjects/colour/input/bwg.png");
BufferedImage bwgImage = ImageIO.read(bwgFile);
int width=bwgImage.getWidth();
int height=bwgImage.getHeight();
for(int w=0; w<width; w++){
for(int h=0; h<height; h++){
int pixel = bwgImage.getRGB(w,h);
Color bwg = new Color(pixel);
int c=bwg.getRed();
Color red = new Color(255,c,c);
int cpixel = red.getRGB();
bwgImage.setRGB(w,h,cpixel);
}
}
ImageIO.write(bwgImage, "png", new File("X:/Java/Documents/NetBeansProjects/colour/output/c.png"));
input
output
EDIT:
I have found out what the problem was, apparently when the input is a greyscale image it will try to make the output a greyscale image as well thus making it darker when blue and green colors get removed and brighter when red gets added. not using a grayscale image as input fixed it.
If I understand what you're trying to do, you're trying to create a greyscale image, except that it is "redscale", using only shades of red. Therefore, you need to compute the greyscale constant of each pixel.
From wikipedia (Greyscale), the luminance of a pixel Y = 0.2126R + 0.7152G + 0.0722B. So, try this
int pixel = bwgImage.getRGB(w,h);
Color bwg = new Color(pixel);
float c = (0.2126f * bwg.getRed() + 0.7152f * bwg.getGreen() + 0.0722f * bwg.getBlue());
int cc = (int)Math.round(c);
Color red = new Color(cc, 0, 0);
int cpixel = red.getRGB();
bwgImage.setRGB(w,h,cpixel);
Alternatively, you can simply retain the red component and set green and blue to 0. This will leave you with just the "redness" of each pixel.
int pixel = bwgImage.getRGB(w,h);
Color bwg = new Color(pixel);
int c=bwg.getRed();
Color red = new Color(c,0,0);
int cpixel = red.getRGB();
bwgImage.setRGB(w,h,cpixel);
NOTE: This solution above only works on images that are not using IndexColorModel. You can check the color model using BufferedImage's getColorModel(). For IndexColorModel, setRGB() does not work directly and instead picks a color in the index closest to the set color, as per HaraldK's comment. To achieve the desired result for images using IndexColorModel, you can create a new BufferedImage with TYPE_INT_ARGB:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Then, write the calculated pixel colors to this new image and save the new image instead.

Java Color conversion

I am having a problem using a method from com.esri.arcgis.display.IColor. The method is IColor.getRGB() which returns an int.
IColor symbolColor = symbol.getColor();
int colorInt = symbolColor.getRGB();
//TODO: get RGB values from colorInt
From the documentation:
RGB returns the Long (ASCII) number calculated from the Red, Green and Blue color attributes.
I need to get the individual RGB values (standard 0-255) from this int somehow but I have been unable to find almost any information on converting from an ASCII number to RGB values.
Have solved by converting the int to hex, and then decoding the hex string using Color.decode() to obtain a Color. Using Color instance we can grab the individual RGB values.
IColor symbolColor = symbol.getColor();
int colorInt = symbolColor.getRGB();
String hexColor = Integer.toHexString(colorInt);
Color color = Color.decode("#"+hexColor);
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
int alpha = color.getAlpha();`
It is possible to convert these values by transferring their RGB integer.
int rgb = symbol.getColor().getRGB();
Color color = new Color(rgb);
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
int alpha = color.getAlpha();
You might need to further investigate wether that rgb variable also contains the alpha values, in which case you want to use new Color(rgb, true) instead.

Get just one pixel value without allocating whole array of pixels

I just need to check three pixel values on the whole image (Image instance). I'd really like to do this without allocating an array of pixels.
Is that possible? Something like BufferedImage's getRGB()?
Yes. You are on the right track with the getRGB method, as it would return a single int with three values (RGB). To convert the single int to three ints, you can do two things:
1. Use the Color class's built-in constructor:
int rgb = img.getGraphics().getRGB(0,0);//Get color of pixel 0,0
Color c = new Color(rgb); //c now contains the r, g, and b values.
2. Build the decoder yourself:
int rgb = img.getGraphics().getRGB(0,0) //Get color of pixel 0,0
int r = rgb >> 24;
int g = 0 >> 16;
int b = 0 >> 8;
Both methods will return a r, g, and b value, which can be used.

What type of array required in WritableRaster method setPixels()?

I don't understand how WritableRaster class of Java works. I tried looking at the documentation but don't understand how it takes values from an array of pixels. Plus, I am not sure what the array of pixels consists.
Here I explain.
What I want to do is : Shamir's Secret Sharing on images. For that I need to fetch an image in BuferedImage image. I take a secret image. Create shares by running a 'function' on each pixel of the image. (basically changing the pixel values by something)
Snippet:
int w = image.getWidth();
int h = image.getHeight();
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
int pixel = image.getRGB(j, i);
int red = (pixel >> 16) & 0xFF;
int green = (pixel >> 8) & 0xFF;
int blue = (pixel) & 0xFF;
pixels[j][i] = share1(red, green, blue);
// Now taking those rgb values. I change them using some function and return an int value. Something like this:
public int share1 (r, g, b)
{
a1 = rand.nextInt(primeNumber);
total1 = r+g+b+a1;
new_pixel = total1 % primeNumber;
return new_pixel;
}
// This 2d array pixels has all the new color values, right? But now I want to build an image using this new values. So what I did is.
First converted this pixels array to a list.
Now this list has pixel values of the new image. But to build an image using RasterObj.setPixels() method, I need an array with RGB values [I MIGHT BE WRONG HERE!]
So I take individual values of a list and find rgb values and put it consecutively in a new 1D array
pixelvector..something like this (r1,g1,b1,r2,g2,b2,r3,g3,b3...)
Size of the list is wh because it contains single pixel value of each pixel.
BUT, Size of the new array pixelvector will become wh*3 since it contains r,g,b values of each pixel..
Then to form image I do this: Snippet
BufferedImage image_share1 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
WritableRaster rast = (WritableRaster) image_share1.getData();
rast.setPixels(0, 0, w, h, pixelvector);
image_share1.setData(rast);
ImageIO.write(image_share1,"JPG",new File("share1.jpg"));
If I put an array with just single pixel values in setPixels() method, it does not return from that function! But if I put an array with separate r,g,b values, it returns from the function. But doing the same thing for share1 , share 2 etc.. I am getting nothing but shades of blue. So, I am not even sure I will be able to reconstruct the image..
PS - This might look like a very foolish code I know. But I had just one day to do this and learn about images in Java. So I am doing the best I can.
Thanks..
A Raster (like WriteableRaster and its subclasses) consists of a SampleModel and a DataBuffer. The SampleModel describes the sample layout (is it pixel packed, pixel interleaved, band interleaved? how many bands? etc...) and dimensions, while the DataBuffer describes the actual storage (are the samples bytes, short, ints, signed or unsigned? single array or array per band? etc...).
For BufferedImage.TYPE_INT_RGB the samples will be pixel packed (all 3 R, G and B samples packed into a single int for each pixel), and data/transfer type DataBuffer.TYPE_INT.
Sorry for not answering your question regarding WritableRaster.setPixels(...) directly, but I don't think it's the method you are looking for (in most cases, it's not). :-)
For your goal, I think what you should do is something like:
// Pixels in TYPE_INT_RGB format
// (ie. 0xFFrrggbb, where rr is two bytes red, gg two bytes green etc)
int[] pixelvector = new int[w * h];
BufferedImage image_share1 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
WritableRaster rast = image_share1.getRaster(); // Faster! No copy, and live updated
rast.setDataElements(0, 0, w, h, pixelvector);
// No need to call setData, as we modified image_share1 via it's raster
ImageIO.write(image_share1,"JPG",new File("share1.jpg"));
I'm assuming the rest of your code for modifying each pixel value is correct. :-)
But just a tip: You'll make it easier for yourself (and faster due to less conversion) if you use a 1D array instead of a 2D array. I.e.:
int[] pixels = new int[w * h]; // instead of int[][] pixels = new int[w][h];
// ...
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
// ...
pixels[y * w + x] = share1(red, green, blue); // instead of pixels[x][y];
}
}

Categories