Mega Code Archive

 
Categories / Delphi / Graphic
 

Simple TBitmap blur algorithm

Title: Simple TBitmap blur algorithm Question: How to blur a TBitmap. Answer: The algorithm is very simple. The value of every pixel in the bitmap is replaced by the sum of the pixels information around the taken pixel divided by 8 (because there are 8 pixels around one pixel). To avoid problems we skip the pixels on the first and the last line and on the first and the last column. function blur(source:TBitmap):TBitmap; var x,y:integer; tline, mline, bline:PByteArray; begin for y := 1 to source.Height - 2 do begin tline := source.ScanLine[y-1]; mline := source.ScanLine[y]; bline := source.ScanLine[y+1]; for x := 1 to source.Width - 2 do begin mline^[x*3] := (mline^[x*3 - 3] + mline^[x*3 + 3] + tline^[x*3 - 3] + tline^[x*3 + 3] + tline^[x*3] + mline^[x*3 - 3] + mline^[x*3 + 3] + mline^[x*3]) div 8; mline^[x*3+1] := (mline^[x*3 - 2] + mline^[x*3 + 4] + tline^[x*3 - 2] + tline^[x*3 + 4] + tline^[x*3+1] + mline^[x*3 - 2] + mline^[x*3 + 4] + mline^[x*3+1]) div 8; mline^[x*3+2] := (mline^[x*3 - 1] + mline^[x*3 + 5] + tline^[x*3 - 1] + tline^[x*3 + 5] + tline^[x*3+2] + mline^[x*3 - 1] + mline^[x*3 + 5] + mline^[x*3+2]) div 8; end; end; result := source; end; Usage example: image1.picture.bitmap := blur(image1.picture.bitmap); where image1 is a TPicture object.