Re:Lighten an image for a watermark
Jamie Rorrison <Rorris...@gaelQuality.co.uk> skrev i
diskussionsgruppsmeddelandet:8rc3k8$3...@bornews.borland.com...
Quote
> Hi,
> I need to put a watermark on a printout ( and a preview).
> I am storing the image in a TPicture, which can be a bitmap, gif, jpg etc.
> Because its a watermark I need to gray it or just lighten, and I was
> wondering if any one had some good references on how to do this.
> Thanks
> Jamie
I use the following code to generate a watermark. The procedure first turns
Bitmap into a grayscale image, and then uses the brightness channel to
determine which color to use. Black is considered to the foreground color
and white the background color.
type
PBuffer = ^TBuffer;
TBuffer = array [0..0] of Byte;
procedure FormatBitmap(Bitmap: TBitmap; Foreground, Background: TColor);
type
TSplitColor = packed record
Red: Byte;
Green: Byte;
Blue: Byte;
Reserved: Byte;
end;
var
Bits: PBuffer;
Pitch: Integer;
X, Y: Integer;
Opacity: Single;
R, G, B: Byte;
Fore: TSplitColor absolute Foreground;
Back: TSplitColor absolute Background;
begin
if (Bitmap.Height < 1) or (Bitmap.Width < 1) then
Exit;
Bitmap.PixelFormat := pf24Bit;
Bits := Bitmap.ScanLine[0];
if Bitmap.Height > 1 then
Pitch := Integer(Bitmap.ScanLine[1]) - Integer(Bits)
else
Pitch := 0;
for X := 0 to Bitmap.Width - 1 do
for Y := 0 to Bitmap.Height - 1 do begin
Opacity := (Bits^[Y * Pitch + X * 3 + 2] * 0.30 + Bits^[Y * Pitch + X
* 3 + 1] * 0.59 + Bits^[Y * Pitch + X * 3] * 0.11) / 255;
R := Round(Fore.Red * (1 - Opacity) + Back.Red * Opacity);
G := Round(Fore.Green * (1 - Opacity) + Back.Green * Opacity);
B := Round(Fore.Blue * (1 - Opacity) + Back.Blue * Opacity);
Bits^[Y * Pitch + X * 3 + 2] := R;
Bits^[Y * Pitch + X * 3 + 1] := G;
Bits^[Y * Pitch + X * 3] := B;
end;
end;