⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 对图像的处理.主要是对图像像素的处理.希望对大家有帮助1
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    OrigImg: TImage;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    Label5: TLabel;
    Label6: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

procedure NegativeBitmap(OrigBmp, DestBmp: TBitmap);
procedure FastNegativeBitmap(OrigBmp, DestBmp: TBitmap);
procedure InvertBitmap(OrigBmp, DestBmp: TBitmap);

const
  MaxPixelCount = 32768;

type
  PRGBArray = ^TRGBArray;
  TRGBArray = array[0..MaxPixelCount - 1] of TRGBTriple;

implementation


{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  IniTime, ElapsedTime: DWord;
begin
  Label6.Caption := '';

  IniTime := GetTickCount;

  case RadioGroup1.ItemIndex of
    0: NegativeBitmap(OrigImg.Picture.Bitmap, OrigImg.Picture.Bitmap);
    1: FastNegativeBitmap(OrigImg.Picture.Bitmap, OrigImg.Picture.Bitmap);
    2: InvertBitmap(OrigImg.Picture.Bitmap, OrigImg.Picture.Bitmap);
  end;
  ElapsedTime := GetTickCount - IniTime;

  Label6.Caption := Format('%d ms', [ElapsedTime]);
end;

procedure NegativeBitmap(OrigBmp, DestBmp: TBitmap);
var
  i, j, R, G, B: Integer;
  TmpBmp: TBitmap;
begin
    // Create a temporal bitmap. This allows to use the same bitmap
    // as input or output
  TmpBmp := TBitmap.Create;

  try
      // Assign the temporal bitmap the same characteristics as the original
    TmpBmp.Width := OrigBmp.Width;
    TmpBmp.Height := OrigBmp.Height;
    TmpBmp.PixelFormat := OrigBmp.PixelFormat;

      // For each row
    for i := 0 to TmpBmp.Height - 1 do
    begin
        // For each column
      for j := 0 to TmpBmp.Width - 1 do
      begin
//        r := 255 - GetRValue(OrigBmp.Canvas.Pixels[j, i]);
//        g := 255 - GetGValue(OrigBmp.Canvas.Pixels[j, i]);
//        b := 255 - GetBValue(OrigBmp.Canvas.Pixels[j, i]);

        R := not GetRValue(OrigBmp.Canvas.Pixels[j, i]);
        G := not GetGValue(OrigBmp.Canvas.Pixels[j, i]);
        B := not GetBValue(OrigBmp.Canvas.Pixels[j, i]);

        TmpBmp.Canvas.Pixels[j, i] := RGB(R, G, B);
      end;                    // Column
    end;                      // Row

      // Assign the negative bitmap to the destination bitmap
    DestBmp.Assign(TmpBmp);
  finally
      // Destroy temp bitmap
    TmpBmp.Free;
  end;
end;

procedure FastNegativeBitmap(OrigBmp, DestBmp: TBitmap);
var
  i, j: Integer;
  TmpBmp: TBitmap;
  OrigRow, DestRow: PRGBArray;
begin
    // Create a temporal bitmap. This allows to use the same bitmap
    // as input or output
  TmpBmp := TBitmap.Create;

  try
      // Assign the temporal bitmap the same characteristics as the original
    TmpBmp.Width := OrigBmp.Width;
    TmpBmp.Height := OrigBmp.Height;
    OrigBmp.PixelFormat := pf24bit;
    TmpBmp.PixelFormat := OrigBmp.PixelFormat;

      // For each row
    for i := 0 to TmpBmp.Height - 1 do
    begin
        // Sssign current ScanLines
      OrigRow := OrigBmp.ScanLine[i];
      DestRow := TmpBmp.ScanLine[i];

        // For each column
      for j := 0 to TmpBmp.Width - 1 do
      begin
        // Invert red, green, blue values
//        DestRow[j].rgbtRed := 255 - OrigRow[j].rgbtRed;
//        DestRow[j].rgbtGreen := 255 - OrigRow[j].rgbtGreen;
//        DestRow[j].rgbtBlue := 255 - OrigRow[j].rgbtBlue;

        DestRow[j].rgbtRed := not OrigRow[j].rgbtRed;
        DestRow[j].rgbtGreen := not OrigRow[j].rgbtGreen;
        DestRow[j].rgbtBlue := not OrigRow[j].rgbtBlue;
      end;
    end;

      // Assign the negative bitmap to the destination bitmap
    DestBmp.Assign(TmpBmp);
  finally
      // Destroy temp bitmap
    TmpBmp.Free;
  end;
end;

// procedure added by Kerstin Thaler
procedure InvertBitmap(OrigBmp, DestBmp: TBitmap);
begin
  // use of the GDI InvertRect() A>PI is even faster...
  InvertRect(OrigBmp.Canvas.Handle, OrigBmp.Canvas.ClipRect);
  DestBmp.Assign(OrigBmp);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -