Howdy all,

I'm wondering if anyone could provide me with some ASM code to seperate the Red Green and Blue values from a Pixel. I'm using MASM. I've used GetPixel to retrieve the entire pixel value, and now I want the Red, Green and Blue values, so I can manipulate the pixel (for applying different effects to images).

Any help would be great..

Phreak
Posted on 2004-07-08 12:13:41 by ktk
Phreak,

Since you have the full color now in a dword value (from the getpixel() call), each byte of that dword is as follows:

example:
invoke GetPixel, 0, 0

now, EAX contains your pixel colors

= alpha byte
= red byte
= green byte
= blue byte.

From there, you can modify the pixel color and use SetPixel to change it.

Hope this helps.

Relv
Posted on 2004-07-08 13:40:37 by Relvinian
Thanks for the reply Relvinian. I'm a bit new to Assembly, so I'm still figuring things out every 5 minutes. This helps heaps :grin:

As for modifying them, can it be done by just storing each part into another DWORD and modifying that DWORD (can't try it right now...)? How would I do that?

Thanks in advance,

Phreak
Posted on 2004-07-08 13:44:11 by ktk
Hi Phreak,

If you have to use SetPixel (it is very slow) then use SetPixelV, it is the faster of the two functions provided by the GDI.

You can directly modify the image by using a DIB bitmap and altering the bits directly, there are many examples on this board for that.
Posted on 2004-07-08 13:51:20 by donkey
Actually, once you have the color values un EAX you have to mask them out:


; This gets only the Alpha value back in EAX discarding the rest
and eax,0FF000000h
shr eax,24

; Same for Red
and eax,00FF0000h
shr eax,16

; Same for Green
and eax,0000FF00h
shr eax,8

; Same for Blue
and eax,000000FFh

The code posted by Relvinian will not work, that's when you have a pointer to the color data in eax (not the data itself as returned by GetPixel).

EDIT: Elaborated the sample code a bit...
EDIT: Yeah, I always mix them up :grin:
Posted on 2004-07-08 17:04:33 by QvasiModo
Thanks heaps QvasiModo. That worked :alright:

I'm learning heaps each week, reading alot of tutorials. I think its time I invested in an ASM book though (they help me way more than online tutorials).


Thanks again,

Phreak
Posted on 2004-07-08 17:17:14 by ktk

I think its time I invested in an ASM book though


"Assembly Language for Intel-Based Computers" by Kip R. Irvine is what I'm reading right now. IMO it's an excellent book and I've learned a lot from it :alright:
Posted on 2004-07-08 17:50:58 by DaRetard
i've read the 2nd and 4th edition before, and i've personally noticed a lot of grammarical and code typos (especially in the earlier editions), many of which could stumble a beginner (i know it did to me!). But yes, that book is a great place to start :alright:
Posted on 2004-07-08 18:11:30 by Drocon
Well, I got what I wanted to work. Maybe a few of you can check it to see if its fine? I'm pretty new to this, and im dancing I'm so happy. I borrowed a bit of code from Donkey, as his was a nice starting point (I've followed it and understand it, I hope you don't mind me using it).


Solarize proc uses edi esi hDC:DWORD,imageX:DWORD,imageY:DWORD

LOCAL rect:RECT
LOCAL red:DWORD
LOCAL green:DWORD
LOCAL blue:DWORD
LOCAL red_res:DWORD
LOCAL green_res:DWORD
LOCAL blue_res:DWORD

mov edi,imageX
dec edi
mov esi,imageY
dec esi
.REPEAT
.REPEAT
invoke GetPixel,hDC,edi,esi

; This gets only the Alpha value back in EAX discarding the rest
;and eax,0FF000000h
;shr eax,24

mov edx,eax

; RED
and eax,00FF0000h
shr eax,16
mov red,eax
mov red_res,eax


mov eax,edx

; BLUE
and eax,00FF0000h
mov blue,eax
mov blue_res,eax


mov eax,edx

; GREEN
and eax,0000FF00h
mov green,eax
mov green_res,eax


.if (red<128) || (red>255)
mov red,255
mov eax,red
sub eax,red_res
mov red,eax
.endif
.if (green<128) || (green>255)
mov green,255
mov eax,green
sub eax,green_res
mov green,eax
.endif
.if (blue<128) || (blue>255)
mov blue,255
mov eax,blue
sub eax,blue_res
mov blue,eax
.endif

mov ecx,red
add ecx,green
add ecx,blue

;xor eax,eax

mov al,cl
shl eax,8
mov al,cl
shl eax,8
mov al,cl


invoke SetPixelV,hDC,edi,esi,eax
dec esi
.UNTIL SIGN?
mov esi,imageY
dec esi
dec edi
.UNTIL SIGN?
ret
Solarize endp

Phreak
Posted on 2004-07-08 20:16:18 by ktk

Actually, once you have the color values un EAX you have to mask them out:


; This gets only the Alpha value back in EAX discarding the rest
and eax,0FF000000h
shr eax,24

; Same for Red
and eax,00FF0000h
shr eax,16

; Same for Green
and eax,0000FF00h
shr eax,8

; Same for Blue
and eax,000000FFh

The code posted by Relvinian will not work, that's when you have a pointer to the color data in eax (not the data itself as returned by GetPixel).

EDIT: Elaborated the sample code a bit...
EDIT: Yeah, I always mix them up :grin:


Very true. ;_)

I was working on pixel stuff when I read this message and didn't fully qualify what I was typing by specifiy each byte as a maskto get the individual byte value. Glad you did that for me. hehe
Posted on 2004-07-08 23:33:59 by Relvinian
Please don't ... this hurts ... , ???

- poor machine

MATRIX
Posted on 2004-10-09 14:31:35 by >Matrix<
Have anyone seen a video mode used EAX for 16 BITS ?
or a video mode with 32BIT RED OR GREEN OR BLUE VALUE?

these are interesting things

MATRIX
Posted on 2004-10-09 14:35:03 by >Matrix<
for seperating the colors into var's with the color in eax you can also do:

mov blue, AL
mov green, AH
shr eax, 16
mov red, AL
mov alpha, AH
Posted on 2004-10-13 15:07:19 by Scorpie
ok i'm not kidding use my code for extra mips:

http://www.win32asmcommunity.net/phpBB2/viewtopic.php?p=152595#152595
Posted on 2004-11-09 20:58:03 by >Matrix<
Originally posted by ktk:
I'm wondering if anyone could provide me with some ASM code to seperate the Red Green and Blue values from a Pixel


If you're lazy, there exist the api functions GetRValue, GetGValue and GetBValue.
Posted on 2004-11-11 18:54:19 by Poimander