To implement color inversion is very easy, we don't need a special form for user interface. We can directly implement the code in the main unit.
- Open our previous contrast enhancement project.
- Double click the main menu component, and add a menu item 'Invert' under Image menu (see figure 1).
- Double click on the menu item, and you'll be directed to the event handler, edit as shown below:
procedure TMainForm.Invert1Click(Sender: TObject);
var
i,j:integer;
ptr:PByteArray;
begin
try
ImageForm:=TImageForm(ActiveMDIChild);
for i:=0 to (ImageForm.Image1.Height-1) do
begin
ptr:=ImageForm.Image1.Picture.Bitmap.ScanLine[i];
for j:=0 to (ImageForm.Image1.Width-1) do
begin
if ImageForm.Image1.Picture.Bitmap.PixelFormat
=pf8bit then ptr[j]:=255-ptr[j];
if ImageForm.Image1.Picture.Bitmap.PixelFormat
=pf24bit then
begin
ptr[3*j]:=255-ptr[3*j];
ptr[3*j+1]:=255-ptr[3*j+1];
ptr[3*j+2]:=255-ptr[3*j+2];
end;
end;
ImageForm.Image1.Refresh;
end;
except
ShowMessage('Cannot complete the operation');
end
end; - Save all files and now you can compile and run the executable. The execution is shown in Figure 2 and Figure 3.
Source Code Download
You can download the source code for Delphi 7 here and for Turbo Delphi Explorer here.

0 comments:
Post a Comment