Fast GreyScaling Using Byte Arrays
Category:
Screen/GraphicsType:
SnippetsDifficulty:
IntermediateAuthor: neophile
Version Compatibility: Visual Basic 6, Visual Basic 5
More information:
Here are two functions that use DIBs and byte arrays to quickly greyscale images. Bit24ToGrey24 will greyscale a picture for display purposes and Bit24ToGrey8File will greyscale a picture directly to a 256-color bitmap file.
Here are examples of each:
Picture2.Picture = Bit24ToGrey24(Picture1.Picture)
Bit24ToGrey8File(Picture1.Picture, "C:\grey256.bmp")
This code uses the ITU standard greyscaling alogrithm, but here are all the notorious ones...
International Telecommunications Union standard - recommended
Shade = (0.2125 * Red + 0.7154 * Green + 0.0721 * Blue)
NTSC and PAL
Shade = (0.299 * Red + 0.587 * Green + 0.114 * Blue)
Simple average
Shade = (Red + Green + Blue) / 3
Weighted average - common
Shade = (3 * Red + 4 * Green + 2 * Blue) / 9
Distance of color vector in color cube - not recommended
Shade = Sqr((Red ^ 2 + Green ^ 2 + Blue ^ 2) / 3)
Human eye responsive - not recommended (ignores red & blue)
Shade = Green
Anyway, enjoy ;)
Instructions: Copy the declarations and code below and paste directly into your VB project.
Declarations: