画像を四つの鏡へ変換するサンプルコード
画像を四つの鏡へ変換するサンプルです。ソースコードは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.
[四つの鏡の関数]
//------------------------------------------------------------------------------
// ■関数名 EffectMirror
// ■用途 画像を←→回転させる
// ■引数
// hBMP ...加工するビットマップのハンドル
// ■戻り値 ビットマップのハンドル
// -----------------------------------------------------------------------------
function EffectMirror(hBMP :HBitmap):HBitmap;
var
Row,Col : integer;
SrcRow,DestRow : pRGBArray;
SrcBitmap,DestBitmap : TBitmap;
begin
SrcBitmap :=TBitmap.Create;
DestBitmap :=TBitmap.Create;
SrcBitmap.Handle :=hBMP;
Set24bit(SrcBitmap,DestBitmap);
Try
for Row:=DestBitmap.Height-1 Downto 0 do
begin
SrcRow:=SrcBitmap.ScanLine[Row];
DestRow:=DestBitmap.ScanLine[Row];
for Col:=0 to DestBitmap.Width-1 do
begin
DestRow[Col].rgbtRed :=SrcRow[SrcBitmap.Width-Col-1].rgbtRed;
DestRow[Col].rgbtGreen:=SrcRow[SrcBitmap.Width-Col-1].rgbtGreen;
DestRow[Col].rgbtBlue :=SrcRow[SrcBitmap.Width-Col-1].rgbtBlue;
end;
end;
Result := DestBitmap.ReleaseHandle;
except
Result := SrcBitmap.ReleaseHandle;
end;
SrcBitmap.Free;
DestBitmap.Free;
end;
//-----------------------------------------------------------------------------
// ■関数名 EffectFlip
// ■用途 画像を↑↓回転させる
// ■引数
// hBMP ...加工するビットマップのハンドル
// ■戻り値 ビットマップのハンドル
// -----------------------------------------------------------------------------
function EffectFlip(hBMP :HBitmap):HBitmap;
var
Row,Col : Integer;
SrcRow,DestRow : pRGBArray;
SrcBitmap,DestBitmap : TBitmap;
begin
SrcBitmap :=TBitmap.Create;
DestBitmap :=TBitmap.Create;
SrcBitmap.Handle :=hBMP;
Set24bit(SrcBitmap,DestBitmap);
try
for Row:=DestBitmap.Height-1 Downto 0 do
begin
SrcRow:=SrcBitmap.ScanLine[DestBitmap.Height-Row-1];
DestRow:=DestBitmap.ScanLine[Row];
for Col:=0 to DestBitmap.Width-1 do
begin
DestRow[Col].rgbtBlue :=SrcRow[Col].rgbtBlue;
DestRow[Col].rgbtGreen:=SrcRow[Col].rgbtGreen;
DestRow[Col].rgbtRed :=SrcRow[Col].rgbtRed;
end;
end;
Result := DestBitmap.ReleaseHandle;
except
Result := SrcBitmap.ReleaseHandle;
end;
SrcBitmap.Free;
DestBitmap.Free;
end;
//------------------------------------------------------------------------------
//■関数名 EffectTrumpEx
//■用途 トランプ横反転
//■引数 hBMP ...転送元のTBitmapのハンドル
//■戻り値 ビットマップのハンドル
//------------------------------------------------------------------------------
function EffectTrumpEx(hBMP :HBitmap):HBitmap;
var
divs : Integer;
SrcBitmap,DestBitmap,Mirror : TBitmap;
begin
SrcBitmap :=TBitmap.Create;
DestBitmap :=TBitmap.Create;
Mirror :=TBitmap.Create;
SrcBitmap.handle :=hBMP;
SrcBitmap.PixelFormat:=pf24bit;
DestBitmap.PixelFormat:=pf24bit;
DestBitmap.Assign(SrcBitmap);
Mirror.Assign(SrcBitmap);
Mirror.Handle:=EffectMirror(Mirror.ReleaseHandle) ;
Mirror.Handle:=EffectFlip(Mirror.ReleaseHandle) ;
if (DestBitmap.Width mod 2) =0 then
divs:=DestBitmap.Width div 2
else
divs:=(DestBitmap.Width div 2)+1;
BitBlt(DestBitmap.canvas.handle,divs,0,DestBitmap.Width,DestBitmap.Height,
Mirror.canvas.Handle,divs,0,SRCCOPY);
Result :=DestBitmap.ReleaseHandle;
SrcBitmap.Free ;
DestBitmap.Free;
Mirror.Free;
end;
//------------------------------------------------------------------------------
//■関数名 EffectTrump
//■用途 トランプ反転
//■引数 hBMP ...転送元のTBitmapのハンドル
//■戻り値 ビットマップのハンドル
//------------------------------------------------------------------------------
function EffectTrump(hBMP :HBitmap):HBitmap;
var
divs : Integer;
SrcBitmap,DestBitmap,Mirror : TBitmap;
begin
SrcBitmap :=TBitmap.Create;
DestBitmap :=TBitmap.Create;
Mirror :=TBitmap.Create;
SrcBitmap.handle :=hBMP;
SrcBitmap.PixelFormat:=pf24bit;
DestBitmap.PixelFormat:=pf24bit;
DestBitmap.Assign(SrcBitmap);
Mirror.Assign(SrcBitmap);
Mirror.Handle:=EffectFlip(Mirror.ReleaseHandle) ;
if (DestBitmap.Height mod 2) =0 then
divs:=DestBitmap.Height div 2
else
divs:=(DestBitmap.Height div 2)+1;
BitBlt(DestBitmap.canvas.handle,0,divs,DestBitmap.Width,DestBitmap.Height,
Mirror.canvas.Handle,0,divs,SRCCOPY);
Result :=DestBitmap.ReleaseHandle;
SrcBitmap.Free ;
DestBitmap.Free;
Mirror.Free;
end;
//-----------------------------------------------------------------------------
//■関数名 EffectFourMirror
//■用途 四つの鏡
//■引数 hBMP ...転送元のTBitmapのハンドル
//■戻り値 ビットマップのハンドル
//-----------------------------------------------------------------------------
function EffectFourMirror(hBMP :HBitmap):HBitmap;
var
SrcBitmap,DestBitmap: TBitmap;
begin
SrcBitmap :=TBitmap.Create;
DestBitmap :=TBitmap.Create;
SrcBitmap.handle :=hBMP;
Set24bit(SrcBitmap,DestBitmap);
DestBitmap.Assign(SrcBitmap);
DestBitmap.Handle:=EffectTrump(DestBitmap.ReleaseHandle);
DestBitmap.Handle:=EffectTrumpEx(DestBitmap.ReleaseHandle);
Result :=DestBitmap.ReleaseHandle;
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:00231
プチモンテ ※この記事を書いた人
![]() | |
![]() | 💻 ITスキル・経験 サーバー構築からWebアプリケーション開発。IoTをはじめとする電子工作、ロボット、人工知能やスマホ/OSアプリまで分野問わず経験。 画像処理/音声処理/アニメーション、3Dゲーム、会計ソフト、PDF作成/編集、逆アセンブラ、EXE/DLLファイルの書き換えなどのアプリを公開。詳しくは自己紹介へ |
| 🎵 音楽制作 BGMは楽器(音源)さえあれば、何でも制作可能。歌モノは主にロック、バラード、ポップスを制作。歌詞は抒情詩、抒情的な楽曲が多い。楽曲制作は🔰2023年12月中旬 ~ | |









