unable to display BufferedImage with RGBA colorspace
Hello,
I have some problems displaying a BufferedImage in a GUI.
The program I'm writing uses a set of rules to generate an integer array representing an image in RGBA. I have something like this:
Values[0] = 1st pixel / R component
Values[1] = 1st pixel / G component
Values[2] = 1st pixel / B component
Values[3] = 1st pixel / A component
Values[4] = 2nd pixel / R component
Values[5] = 2nd pixel / G component
...
I am then creating a BufferedImage like this:
Code java:
this.image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_ARGB);
this.image.setRGB(0, 0, this.width, this.height, Values, 0, this.width);
When verifying if that BufferedImage contains the right values, I found that it does.
But the problem is, I can't display it. I tried painting in on a JPanel or JLabel, creating an ImageIcon out of it... It is as if it was completely transparent although the alpha values are varying.
this is the crucial part of the code:
Code java:
public void step(JLabel view) {
BufferedImage image = this.displayGrid.computeGraphics(this.cellGrid); // Construction of the BufferedImage with random
// values
int[] results = image.getRGB(0, 0, this.width, this.height, null, 0, this.width);
for(int i = 0; i < results.length; i++)
System.out.println(results[i]); // to see the values contained in 'image'
ImageIcon icon = new ImageIcon(image);
view.setIcon(icon); // this doesn't seem to do anything although it should !
}
When i run this code, everytime the function step() is called, The program should display a new BufferedImage in the Jlabel 'view'.
What I actually get is that everything seems to work fine but 'view' stays empty throughout the execution...
Any help is appreciated and sorry if I'm doing something dumb :^)
Thanks in advance
Re: unable to display BufferedImage with RGBA colorspace
Can you post an SSCCE (and please use the code tags)?
Re: unable to display BufferedImage with RGBA colorspace
Just a side note.... Does Red or Alpha go first in an ARGB ordering?
Quote:
int[] results = image.getRGB
Wait, are we dealing with RGB or ARGB?
Quote:
int[] results = image.getRGB(0, 0, this.width, this.height
Does this JComponent have a specified width and height at this time?
I would include a few more sysouts and refer to the docs on these jparts to get a feel of what is going on.
PS please be a kind hearted individual and fix the code with code tags too. See the page you should have read before posting for help.
Re: unable to display BufferedImage with RGBA colorspace
Quote:
Originally Posted by
jps
Just a side note.... Does Red or Alpha go first in an ARGB ordering?
I am not sure, i think it comes last but I could be wrong. The point is that each component in each pixels of my BufferedImage is set randomly to be between 0 and 255, so seeing Nothing when trying to display it is kind of unrealisticly improbable.
Quote:
Originally Posted by
jps
Wait, are we dealing with RGB or ARGB?
Actually getRGB returns an array of integer pixels in the default RGB color mode which happen to be ARGB.
see here
Quote:
Originally Posted by
jps
Does this JComponent have a specified width and height at this time?
Yes it does. My window is 600x600 and the image is 200x200 so there should be plenty of space to fit it inside.
Re: unable to display BufferedImage with RGBA colorspace
Here is my SSCCE. Sorry for not thinking of that sooner.
Code java:
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main {
public static void main(String args[]) {
JFrame window = new JFrame();
JPanel panel = new JPanel();
JLabel label = new JLabel();
int width = 100;
int height = 100;
int[] RGBA = new int[width*height*4];
for(int index = 0; index < RGBA.length; index++) {
RGBA[index] = 50; // I didn't reproduced the randomness involved here
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image.setRGB(0, 0, width, height, RGBA, 0, width);
ImageIcon icon = new ImageIcon(image);
label.setIcon(icon);
window.setSize(600, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(0, 0); // default is 0,0 (top left corner)
window.setResizable(false);
window.setVisible(true);
panel.add(label);
window.getContentPane().add(panel);
}
}
This shows straightforwardly the problem I'm having. The window that this code generates is empty...
Re: unable to display BufferedImage with RGBA colorspace
The BufferedImage has an alpha component, which your RGB values are setting to 0 (transparent). Set the alpha component to an appropriate value, or change the image type to exclude the alpha (for instance BufferedImage.TYPE_INT_RGB). For instance, try setting the RGBA values to
Code :
RGB[index] = (int)(255 << 24) | 50;//bitshift the 255 value into the a component of the int
Re: unable to display BufferedImage with RGBA colorspace
How can the alpha component be set to zero when i do this?
Code java:
for(int index = 0; index < RGBA.length; index++) {
RGBA[index] = 50; // I didn't reproduced the randomness involved here
}
This loop sets every value of the RGBA colorspace to 50, including the alpha, if I'm correct.
Re: unable to display BufferedImage with RGBA colorspace
Quote:
Originally Posted by
Cereno
This loop sets every value of the RGBA colorspace to 50, including the alpha, if I'm correct.
The loop sets every pixel value to alpha = 0, red = 0, green = 0, and blue = 50. You need to manipulate each pixel's four values (presuming an TYPE_INT_ARGB image type) to get the color you wish. The code I posted above bitshifted 255 into the alpha position, you can do something similar for each subsequent position.
Re: unable to display BufferedImage with RGBA colorspace
Quote:
Originally Posted by
copeg
The loop sets every pixel value to alpha = 0, red = 0, green = 0, and blue = 50. You need to manipulate each pixel's four values (presuming an TYPE_INT_ARGB image type) to get the color you wish.
That's exactly what I am doing since my RGBA array looks like this:
Values[0] = 1st pixel / R component
Values[1] = 1st pixel / G component
Values[2] = 1st pixel / B component
Values[3] = 1st pixel / A component
Values[4] = 2nd pixel / R component
Values[5] = 2nd pixel / G component
Since I put 50 in every cell of the array, I am therefore setting an alpha of 50 too.
I understood that it is supposed to work that way since changing the size of my RGBA array gives an ArrayIndexOutOfBoundsException during the call to setRGB().
edit:
I just tried your code with the bitshift and I can confirm that it works, it displays the BufferedImage now. However I'm rather confused... Could you try to explain to me the difference between your line and mine? Thank you anyways.
Re: unable to display BufferedImage with RGBA colorspace
Quote:
Originally Posted by
Cereno
That's exactly what I am doing since my RGBA array looks like this:
That is not what you are doing...the int values are not treated that way. With type INT_ARGB, each int value represents a pixel - an int is 32 bytes, the least significant 8 bits are blue, the most significant are alpha, etc...I recommend reading the API for the setRGB method you are using
Quote:
There are only 8-bits of precision for each color component in the returned data when using this method. With a specified coordinate (x, y) in the this image, the ARGB pixel can be accessed in this way:
pixel = rgbArray[offset + (y-startY)*scansize + (x-startX)];
Edit: a more explicit example
Code :
int a = 255;
int r= 0;
int g= 0;
int b= 255;
int pixel = (a << 24) | (r << 16 ) | (g << 8 ) | b;//bitshift each color (0-255) 8 bit value to the appropriate pixel position
RGBA[index] = pixel;
Re: unable to display BufferedImage with RGBA colorspace
I understand now. I never suspected the rgba values needed to be packed that way.
Thank you so very much for your time. Thanks to you, my code is working now :)
Re: unable to display BufferedImage with RGBA colorspace
Quote:
Originally Posted by
Cereno
I understand now. I never suspected the rgba values needed to be packed that way.
Thank you so very much for your time. Thanks to you, my code is working now :)
You are welcome. Glad to be of help. If all your questions have been answered, please mark this thread as solved (go to the top toolbar 'thread tools')