brendan-duncan / image

Dart Image Library for opening, manipulating, and saving various different image file formats.
MIT License
1.16k stars 262 forks source link

Question: Get Pixel #46

Closed xaguzman closed 7 years ago

xaguzman commented 7 years ago

I have been unable to compare the colors of the getPixel method successfully to some hex numbers constants I defined.

The source is a standard PNG image without transparency. I have just been getting false results doing the following:

const int RED = 0xff0000

int pixel = image.getPixel(5,5);

if ( (pixel >> 8).toUnsigned(32) == RED){
   print ('RED PIXEL');
}

However, even black dots are printing RED PIXEL in the console with this approach.

Am I doing it the wrong way?

brendan-duncan commented 7 years ago

The reason why you're seeing everything pass is because the alpha channel is 0xff in the pixels you're getting from the image. So, in the case of a black pixel, the the value of pixel would be 0xff000000. In your test, you're shifting pixel right by 8, which would become 0x00ff0000, the same value as RED. So, why are you doing the shift in your test?

xaguzman commented 7 years ago

It's a code port I'm doing, I did try before to do it without the shifting but it would give me pretty far away values from the hex ones I'm trying to compare against...

This is the exact color comparison:

const int RED = 0xff0000;

int pixel = image.getPixel(x, y);
int pix = (pixel >> 8).toUnsigned(32) & 0xffffff;

if (pix == RED){
 ....
}

It's far easier to read it in hex for color comparison, thus why I'm trying to accomplish the masking like this. Do you have any suggestion?

brendan-duncan commented 7 years ago

The channels encoded in pixel are in ABGR order. So the shift is nocking off the red channel and moving alpha into blues spot, blue into greens, and green into reds.

So, the actual color you have stored in RED is blue=0xff, and 0x00 for alpha, green and red. The color red with no transparency would actually be 0xff0000ff. Are you testing if the pixel color is exactly red? In that case, just test with == and no shift. Do you want to check a specific channel of pixel? There are functions in the image library for getRed, getGreen, getBlue and getAlpha, which will extract the specific channel from pixel. If that's what you're trying to do, then the test would be getRed(pixel) == 0xff.

Forgive me if I'm not understanding your problem correctly.

On Mon, Jan 23, 2017, 4:17 PM Xavier Guzman notifications@github.com wrote:

It's a code port I'm doing, I did try before to do it without the shifting but it would give me pretty far away values from the hex ones I'm trying to compare against...

This is the exact color comparison:

const int RED = 0xff0000;

int pixel = image.getPixel(x, y);int pix = (pixel >> 8).toUnsigned(32) & 0xffffff; if (pix == RED){ .... }

It's far easier to read it in hex for color comparison, thus why I'm trying to accomplish the masking like this. Do you have any suggestion?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/brendan-duncan/image/issues/46#issuecomment-274661160, or mute the thread https://github.com/notifications/unsubscribe-auth/ADeS8yGmr3GSwqNBSjkNM513aMbmomlVks5rVUMmgaJpZM4Lrnks .

xaguzman commented 7 years ago

Sweet...somehow i skipped that the format is ABGR :) I assumed it was RGBA so my comparison was all off!

Thanks :)

brendan-duncan commented 7 years ago

Awesome, glad that got worked out.

On Mon, Jan 23, 2017, 7:00 PM Xavier Guzman notifications@github.com wrote:

Sweet...somehow i skipped that the format is ABGR :) I assumed it was RGBA so my comparison was all off!

Thanks :)

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/brendan-duncan/image/issues/46#issuecomment-274688052, or mute the thread https://github.com/notifications/unsubscribe-auth/ADeS89pXykWfNe9WBAFGZpHuxNlXgYq4ks5rVWldgaJpZM4Lrnks .