Getting pixel color from display in allegro5 - colors

I've been trying to check if pixel color is green and I've seen this:
Checking pixel color Allegro 5 C++,
but I don't have bitmap in my code.
I was searching in allegro5 documentation and it seems like there's nothing which can help me.
Is there any solution to convert display to bitmap? Or to get pixel color from display? Anyone knows how to do it? I have position of my pixel and nothing more.

Use al_get_backbuffer to get the display as a bitmap, then use al_get_pixel to retrieve a pixel.

I tried the following and i got some results
ALLEGRO_BITMAP *bitmap;
bitmap = al_get_backbuffer(display);
ALLEGRO_COLOR color = al_get_pixel(bitmap,x,y);
bool isBlack = (color.r ==0 && color.g == 0 && color.b ==0);
if(!isBlack){
alive=0;
explode() or something
}

Related

What does this Imagedraw code line for pixel color setting mean?

I am trying to color a pixel of an image (Pillow ImageDraw) according to an integer property,say m, of that pixel at ix,iy coordinates. I came across this line of code in one plotting example and this gives beautiful results.
bitmap = Image.new("RGB", (WIDTH,HEIGHT), "white")
pix=bitmap.load()
....obtain m value for the pixel ix,iy
pix[ix,iy] = (m>>21)+(m<<10)+m*8
I have searched documentation to see what this means, but couldn't find any. I think the inequality sign has something to do with shifting bits. Can someone expalin? Thanks

Metal Shading Language Texture Will Not Clear

In my scene I have 2 objects, a Water Well and a Quad. The Well has 2 textures, a baseColorTexture (index 0) and a normalMapTexture (index 1). The quad has no textures applied to it. When Rendering the scene I get something that looks like this.
THE QUAD IS USING THE WELLS IMAGE. Now when looking at the debugger I find that there is no image bound to either index 0 or 1 for the quad like the picture shows below.
My Shaders use the following when using a texture. if(!is_null_texture(texture) { ... }
Can anyone please give me an idea as to what may be occuring?
You can see all the code here:
github.com/twohyjr/Metal-Game-Engine-Tutorial/blob/master/….
but it looks like this.
float4 color = material.color; if(!is_null_texture(baseColorMap)) { color = baseColorMap.sample(sampler2d, texCoord); }

How to use a color buffer in OpenGL ES 2

I am a bit confused on how to draw color using a color buffer. I found a similar question here and made my shader the same as shown in the post's accepted answer. I then used the code:
mColorHandle = GLES20.glGetAttribLocation(Shader, "vColor");
GLES20.glEnableVertexAttribArray(mColorHandle);
ByteBuffer cb = ByteBuffer.allocateDirect(color.length * BYTES_PER_FLOAT);
cb.order(ByteOrder.nativeOrder());
colorBuffer = cb.asFloatBuffer();
colorBuffer.put(color);
colorBuffer.position(0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, cbo);
GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, 0, colorBuffer.capacity(), colorBuffer);
GLES20.glVertexAttribPointer(mColorHandle, 4,
GLES20.GL_FLOAT, false,
0, 0);
in attempt to draw the color.
The shape displayed the color I was trying to draw but it faded out the color across the shape like this:
If someone could tell me what's going wrong and how I could get the shape to be all the same color, I would appreciate it.
Thanks to Rabbid76 for helping me find the mistake.
Instead of 4 elements total in the color array, there needs to be 16, an RGBA value for each vertex. (4 elements of the array are used to make one RGBA value.)

Setting color variable

I need to compare colors. I want to set a color into a variable and then compare that it a value obtained using getPixel.
However, the following does not work. It seems like ImageJ does not know that the value in basecolor is a color.
basecolor = 0xFFFFFF;
rightpixel = getPixel(x, y);
if (rightpixel == basecolor) count++;
Your problem is in getPixel which does not yield a color written in hex.
I present to you your best friend on ImageJ : the built-in macro functions code
https://imagej.nih.gov/ij/developer/macro/functions.html
which documents built-in pixel functions such as getPixel().
For getPixel(), it is stated "Note that pixels in RGB images contain red, green and blue components that need to be extracted using shifting and masking. See the Color Picker Tool macro for an example that shows how to do this. ", and the Color Picker Tool macro tells us how to go from color "bits" to RGB.
So if you wish to compare colors, you do :
basecolor=newArray(0,0,0);
rightpixel = getPixel(x,y);
//from the Color Picker Tool macro
//converts what getPixel returns into RGB (values red, green and blue)
if (bitDepth==24) {
red = (v>>16)&0xff; // extract red byte (bits 23-17)
green = (v>>8)&0xff; // extract green byte (bits 15-8)
blue = v&0xff; // extract blue byte (bits 7-0)
}
//compare the color with your color
if(red==basecolor[0] && green==basecolor[1] && blue==basecolor[2]){
print("Same Color");
count++;
}
//you can also work with hex by converting the rgb to hex and then
//comparing the strings like you did

Convert Colored Image to Gray Scale In Codenameone

Can anyone tell me how to go about converting a RGB Image object to Gray Scale? I know there is a lot of information on how to do this in Java already, but I just wanted to get an answer specific to Codenameone so others can benefit.
I am trying to implement image binarization using Otsu’s algorithm
You can use Image.getRGB() then modify the array as explained in this answer:
Convert Image to Grayscale with array matrix RGB java
Notice that the answer above is a bit over simplistic as it doesn't take into account the correct weight per color channel for proper grayscale effect but this depends on your nitpicking levels.
Then use this version of createImage with the resulting array.
For anyone looking for a simplified way (not using matrices) of doing what Shai is hinting, here is some sample code
int[] rgb = image.getRGB();
for(int k = 0;k<rgb.length;k++)
{
if(rgb[k]!=0)
{
int r = rgb[k]/256/256;
rgb[k]=rgb[k]-r*0x10000;
int g = rgb[k]/256;
rgb[k]=rgb[k]-g*0x100;
int b = rgb[k];
int intensity = (int)Math.round(((r+g+b)/(256.0*3.0))*256);
rgb[k] = intensity+(intensity*256)+intensity*(256*256);
}
}
Image grayImage = Image.createImage(rgb,image.getWidth(),image.getHeight());

Resources