Code archives/Graphics/Chiaroscuro/greyscale buffer

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

Download source code

Chiaroscuro/greyscale buffer by BlitzSupport
(Posted 23 years ago)
Reading Calvin & Hobbes, I found the strip where Calvin's dad says he sees everything in black and white; the strip is drawn with chiaroscuro shading, where you choose black or white only, depending on a brightness threshold (so it works best on high contrast images). I had to convert to greyscale on the way anyway, so that option is there...

Greyscale:


Chiaroscuro:


It's slow, but it amused me to do the Calvin thing, and it could actually be useful for pre-rendering if you want to offer a real-time greyscale option (load image, greyscale its buffer, ta-da)!

Try pasting the code below at the top of the "samples\mak\dragon\dragon.bb" demo supplied with Blitz3D, adding this line just before Flip:

GreyScale GraphicsBuffer (), GraphicsWidth (), GraphicsHeight (), 0


Change the 0 on the end to 40 for a reasonable chiaroscuro effect!

With the 0 changed to around 40, you can try the "fastest Spectrum 48k on earth" effect if you run it in 320 x 200 @ 16-bit fullscreen if possible. Also, 320 x 200 @ 32-bit is the fastest mode here...
; chiatol parameter -- 0 for greyscale, 1-255 for chiaroscuro effect tolerance threshold...

Function GreyScale (buffer, width, height, chiatol = 0)
	tbuffer = GraphicsBuffer ()
	SetBuffer buffer
		LockBuffer buffer
		
			For x = 0 To width - 1
				For y = 0 To height -1
				
					rgb = ReadPixelFast (x, y)
					
					r = rgb Shr 16 And %11111111
					g = rgb Shr 8 And %11111111
					b = rgb And %11111111
	
					trgb = (r + g + b) / 3

					If Not chiatol
						r = trgb
						g = trgb
						b = trgb
					Else
						If trgb < chiatol
							r = 0
							g = 0
							b = 0
						Else
							r = 255
							g = 255
							b = 255
						EndIf
					EndIf
										
					rgb = ((r Shl 16) + (g Shl 8) + b)

					WritePixelFast x, y, rgb
				
				Next
			Next
		
		UnlockBuffer buffer
	SetBuffer tbuffer
End Function