Code archives/Graphics/Grayscale

This code has been declared by its author to be Public Domain code.

Download source code

Grayscale by pexe2005
Turn your graphics to grayscale mode.

This is my first script using readpixel and writepixel. Because of this, its very slow.

If somebody have a tip to make it faster, tell me please.

Enjoy. =D
;Grayscale
;By pexe

;WARNING: This is my first script using readpixel and writepixel, and its VERY SLOW!

;Function grayscale(Buffer, X Starting Point, Y Starting Point, Width, Height)

Function grayscale(GS_buffer,GS_vx,GS_vy,GS_w,GS_h)
LockBuffer(GS_buffer)
For GS_y = GS_vy To GS_vy+GS_h-1
For GS_x = GS_vx To GS_vx+GS_w-1
GS_pix = ReadPixelFast(GS_x,GS_y,GS_buffer)

GS_r% = (GS_pix Shr 16) And $ff ;\
GS_g% = (GS_pix Shr 8) And $ff  ;  Transform values
GS_b% = GS_pix And $ff          ;/

GS_v% = 0.3*GS_r+0.59*GS_g+0.11*GS_b

GS_pix=(GS_v Or (GS_v Shl 8) Or (GS_v Shl 16) Or ($ff000000)) ;Put values back
WritePixelFast GS_x,GS_y,GS_pix,GS_buffer

Next
Next
UnlockBuffer(GS_buffer)
End Function

Comments

Byteemoz2005
There are two errors in the For-loops:
It should be
"For GS_y = GS_vy To GS_vy+GS_h - 1" and
"For GS_x = GS_vx To GS_vx+GS_w - 1".
Otherwise this function will attempt to read and write outside the specified rectangle.

For example if you want to grayscale the whole buffer in 800 x 600 graphics mode with

grayscale GraphicsBuffer(), 0, 0, 800, 600

GS_y will go from 0 to 600 and GS_x from 0 to 800, but it should go to 599 and 799 respectively.


Bertrand2005
Another error, every greyscale algorithm don't work like that. I mean, by additioning tree value and divide it by 3.
Usualy, the best way it's to multiply each color component by a specific value: red*0.3(red <0-1>), green*0.59(green <0-1>) and blue*0.11(blue <0-1>).
You can check this values on any website in relation with computer graphic or any 2D softwares(Shake, Digital Fusion)


pexe2005
Living and Learning..

Thanks for the tips.. i will re-make this script soon.. (and trying to make something more fast)


pexe2011
BTW: I've fixed the code


Code Archives Forum