ホーム > カテゴリ > 画像処理工学 >

画像をネガポシ反転するサンプルコード

画像をネガポシ反転するサンプルです。ソースコードはDelphi5で作成しましたがその他の言語でも流用できるかと思います。

画像処理の結果

ネガポシ反転の処理を行うと下図のようになります。

ソースコード

[EffectBass.pas]

//Bass Unit
unit EffectBass;

interface
uses
 Windows,SysUtils, Classes, Graphics;

type
 //24bitアクセス用ポインタ
 pRGBarray  = ^TRGBarray;
 TRGBarray  = array[0..0] of TRGBTriple; //None リテラル

type
 //24bitアクセス用ダブルポインタ
 PPBits = ^TPBits;
 TPBits = array[0..0] of pRGBarray; //None リテラル

//汎用プロシージャ
procedure Set24bit(Src,Dest :TBitmap);
function  Set255(Value : integer) : BYTE;

implementation


/////////
procedure Set24bit(Src,Dest :Tbitmap);
begin
  Src.PixelFormat :=pf24bit;
  Dest.PixelFormat:=pf24bit;
  Dest.Width:=Src.Width;
  Dest.Height:=Src.Height;
end;

/////////
function Set255(Value:Integer):Byte;
begin
 if Value>=255 then
  Result:=255
 else if Value<=0 then
 Result:=0
 else Result:=Value;
end;
end.

[ネガポシ反転の関数]

//------------------------------------------------------------------------------
// ■関数名    EffectNOTMask
// ■用途      ネガポシ反転
// ■引数
//             hBMP  ...転送元のTBitmapのハンドル
//■戻り値
//             ビットマップのハンドル
// -----------------------------------------------------------------------------
function EffectNOTMask(hBMP: HBitmap):HBitmap;
var
  Row,Col          : Integer;
  SrcRow,DestRow   : pRGBArray;
  _Height,_Width   : integer;
  SrcBitmap,DestBitmap : TBitmap;
begin
 SrcBitmap   :=TBitmap.Create;
 DestBitmap  :=TBitmap.Create;
 SrcBitmap.Handle  :=hBMP;
 Set24bit(SrcBitmap,DestBitmap);
 _Height := SrcBitmap.height-1;
 _Width  := SrcBitmap.width-1;

try
   For Row := 0 To _Height   do
   begin
       SrcRow:=SrcBitmap.ScanLine[Row];
       DestRow:=DestBitmap.ScanLine[Row];
        For Col := 0 To _Width do
        begin
             DestRow[Col].rgbtBlue := not SrcRow[Col].rgbtBlue ;
             DestRow[Col].rgbtGreen:= not SrcRow[Col].rgbtGreen;
             DestRow[Col].rgbtRed  := not SrcRow[Col].rgbtRed ;
       end;
    end;
  Result :=DestBitmap.ReleaseHandle;
except
   Result :=SrcBitmap.ReleaseHandle;
end;
  SrcBitmap.Free ;
  DestBitmap.Free;
end;

[関数の呼び出し]

  procedure TForm1.Button1Click(Sender: TObject);
  begin
    Image1.Picture.bitmap.Handle:= 関数名(Image1.Picture.Bitmap.ReleaseHandle);
  end;




関連記事



公開日:2015年02月18日 最終更新日:2015年02月19日
記事NO:00235