Color Inversion Processing in Delphi

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.
  1. Open our previous contrast enhancement project.
  2. Double click the main menu component, and add a menu item 'Invert' under Image menu (see figure 1).
  3. 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;
  4. Save all files and now you can compile and run the executable. The execution is shown in Figure 2 and Figure 3.

Figure 1. Invert Menu


Figure 2. Color Inversion of Monochrome Image


Figure 3. Color Inversion on True Color Picture

Source Code Download
You can download the source code for Delphi 7 here and for Turbo Delphi Explorer here.

0 comments: