uses
wincrt,Unit6,Unit5,Unit4,Unit3,Unit2,FileUtil,intfgraphics, windows, SysUtils,
Forms, Controls, Graphics, LResources,FPimage, GraphUtil,
Process, ExtCtrls, Dialogs, StdCtrls, LCLProc, ActnList, LCLIntf, LCLType,
Interfaces, Buttons, ExtDlgs, Menus, ComCtrls, strutils,
Classes, GraphType,BGRABitmap, BGRABitmapTypes, EpikTimer;
procedure TForm1.Button15Click(Sender: TObject);
var tempstr:string;
chosencolor,newcolor:TColor;
x,y,lum,rednum,greennum,bluenum,code:integer;
tempred,tempgreen,tempblue,templum,val1,val2,val3:extended;
num1,num2,num3,num4,num5,num6,num7,U,V:extended;
bgra1:TBGRABitmap;
c:TBGRAPixel;
mybitmap,mybitmap2:TBitmap;
inf1,inf2:TLazIntfImage;
ImgFormatDescription: TRawImageDescription;
begin
form4go := false;
form4.button1.Enabled := true;
form4.Button1.visible := true;
form4.button1.caption := 'GO';
form4.paintbox1.color := clNone;
form4.statictext1.caption := ' This tool applies tinting with a chosen color to pictures and videos, including grayscale and colored images. Press GO to choose a tinting color or close this message to cancel.';
form4.show;
application.processmessages;
backgroundcolor := '';
image1.transparent := false;
while form4.visible = true do
begin
form4.bringtofront;
application.processmessages;
end;
form4.Button1.visible := false;
form4.button1.caption := 'Button1';
form4.button1.enabled := false;
if (form4go = false) then
begin
form3.show;
if backgroundcolor = '' then
form3.statictext1.caption := 'Process cancelled by user.';
delay(1600);
application.processmessages;
form3.hide;
exit;
end;
if colordialog1.execute = false then
exit;
form2.show; //Please Wait message
application.processmessages;
bgra1 := TBGRABitmap.create;
mybitmap := TBitmap.create;
mybitmap.width := image1.width;
mybitmap.height := image1.height;
mybitmap2 := TBitmap.create;
mybitmap2.width := image1.width;
mybitmap2.height := image1.height;
mybitmap.assign(image1.picture.bitmap);
bgra1.assign(mybitmap);
chosencolor := colordialog1.color;
tintcolor := chosencolor;
for y := 0 to (image1.height - 1) do
for x := 0 to (image1.width - 1) do
begin
c := bgra1.GetPixel(x,y); //Get each pixel
newcolor := BGRAToColor(c);
GetRGBColorNumbers(newcolor,rednum,greennum,bluenum); //Get the red, green and blue numbers.
//Get industry standard percentages
val1 := StrToFloat('0.299');
val2 := StrToFloat('0.587');
val3 := StrToFloat('0.114');
num1 := StrToFloat('0.439');
num2 := StrToFloat('0.368');
num3 := StrToFloat('0.071');
num4 := StrToFloat('0.148');
num5 := StrToFloat('0.291');
num6 := StrToFloat('0.439');
templum := (val1 * rednum) + (val2 * greennum) + (val3 * bluenum);
if templum < 0 then
templum := 1
else
if templum > 255 then
templum := 255;
lum := round(templum); //Lum = the luminance of each pixel.
GetRGBColorNumbers(chosencolor,rednum,greennum,bluenum);
V := (num1 * rednum) - (num2 * greennum) - (num3 * bluenum) + 128;
U := -(num4 * rednum) - (num5 * greennum) + (num6 * bluenum) + 128;
num1 := StrToFloat('1.164');
num2 := StrToFloat('2.018');
num3 := StrToFloat('1.164');
num4 := StrToFloat('0.813');
num5 := StrToFloat('0.391');
num6 := StrToFloat('1.164');
num7 := StrToFloat('1.596');
tempblue := (num1 * (lum - 16)) + (num2 * (U - 128));
tempgreen := (num3 * (lum - 16)) - (num4 * (V - 128)) - (num5 * (U - 128));
tempred := (num6 * (lum - 16)) + (num7 * (V - 128));
//Ensure that no number is less than zero or larger than 255.
if tempred < 0 then
tempred := 0
else
if tempred > 255 then
tempred := 255;
if tempgreen < 0 then
tempgreen := 0
else
if tempgreen > 255 then
tempgreen := 255;
if tempblue < 0 then
tempblue := 0
else
if tempblue > 255 then
tempblue := 255;
rednum := round(tempred);
greennum := round(tempgreen);
bluenum := round(tempblue);
newcolor := RGBToColor(rednum, greennum, bluenum);
bgra1.SetPixel(x,y,newcolor);
end;
mybitmap.assign(bgra1);
//The TLazIntfImage stuff ensures that the bitmap is 24 bit.
inf2:=TLazIntfImage.Create(mybitmap.Width,mybitmap.Height);
inf1:= mybitmap.CreateIntfImage;
try
ImgFormatDescription.Init_BPP24_B8G8R8_BIO_TTB(mybitmap.Width,mybitmap.Height);
inf2.DataDescription:=ImgFormatDescription;
inf2.CopyPixels(inf1);
mybitmap2.PixelFormat:=pf24bit;
mybitmap2.LoadFromIntfImage(inf2);
mybitmap2.PixelFormat:=pf24bit;
finally
inf1.free;
inf2.Free;
end;
image1.picture.bitmap.assign(mybitmap2);
tentativetinted := true; //Global variable
tinted := true; //Global variable
mybitmap.free;
mybitmap2.free;
bgra1.free;
form2.hide;
end;