PDA

View Full Version : Mask Bitmap



Ser
08-29-2011, 08:46 PM
Hi,
I would like to create a mask with Delphi,

Here is a tutorial with photoshop

http://villavu.com/forum/showthread.php?t=6129

said first grayscale
Then contrast to 100
and brightness to 55

follows the code


procedure AdjustContrastAndBrightness(ABitmap: TBitmap; AContrast, ABrightness: integer);
var
Map: array[0..255] of byte;
Stride, i, W, H, x, y: integer;
PS, P: Pbyte;

function Clamp(V: integer): integer;
begin
if V < 0 then Result := 0
else if V>255 then Result := 255
else Result := V;
end;

begin
for i := 0 to 255 do
Map[i] := Clamp(round((i - 127) * AContrast / 100) + 127 + ABrightness);
ABitmap.PixelFormat := pf24bit;
W := ABitmap.Width;
H := ABitmap.Height;
if H = 0 then exit;
Stride := 0;
if H>= 2 then
Stride := integer(ABitmap.ScanLine[1]) - integer(ABitmap.ScanLine[0]);
PS := ABitmap.ScanLine[0];
for y := 0 to H - 1 do
begin
P := PS;
for x := 0 to W * 3 - 1 do
begin
P^ := Map[P^];
inc(P);
end;
inc(PS, Stride);
end;
end;

function ConvertBitmapToGrayscale(const Bitmap: TBitmap): TBitmap;
var
i, j: Integer;
Grayshade, Red, Green, Blue: Byte;
PixelColor: Longint;
begin
with Bitmap do
for i := 0 to Width - 1 do
for j := 0 to Height - 1 do
begin
PixelColor := ColorToRGB(Canvas.Pixels[i, j]);
Red := PixelColor;
Green := PixelColor shr 8;
Blue := PixelColor shr 16;
Grayshade := Round(0.3 * Red + 0.6 * Green + 0.1 * Blue);
Canvas.Pixels[i, j] := RGB(Grayshade, Grayshade, Grayshade);
end;
Result := Bitmap;
end;

procedure CreateMask;
var
tBmp: TBitmap;
tmpDC: HDC;
Size: TRect;
begin
tBmp := TBitmap.Create;
tmpDC := GetWindowDC(GetDesktopWindow);
GetWindowRect(GetDesktopWindow, Size);
tBmp.Width := Size.Right - Size.Left;
tBmp.Height := Size.Bottom - Size.Top;
BitBlt(tBmp.Canvas.Handle, 0, 0, tBmp.Width, tBmp.Height, tmpDC, 0, 0, SRCCOPY);
tBmp.PixelFormat := pf32bit;
DeleteDC(tmpDC);
tBmp.SaveToFile('c:\dir\rgb.bmp');
tBmp := ConvertBitmapToGrayscale(tBmp);
tBmp.SaveToFile('c:\dir\gray.bmp');
AdjustContrastAndBrightness(tBmp, 100, 55);
tBmp.SaveToFile('c:\dir\mask.bmp');
tBmp.Free;
end;


I do not get the desired effect
some help?