RGBカラー交換のサンプルコード
画像に「RGBカラー交換」の処理をするサンプルです。ソースコードはDelphi5で作成しましたがその他の言語でも流用できるかと思います。
画像処理の結果
RGBカラー交換をすると下図のようになります。(RGB→RBG)
ソースコード
[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.
[RGBカラー交換の関数]
//-----------------------------------------------------------------------------
//■関数 EffectRGBColor_Exchange
//■用途 RGBカラー交換
//■引数 hBMP ... 転送元のビットマップのハンドル
// Value ...
// 1 : Red <-> Green のみ変換
// 2 : Red <-> Blue のみ変換
// 3 : Blue <-> Green のみ変換
// 4 : R->G B->R G->B
// 5 : R->B B->G G->R
//■戻り値
// 新しいビットマップのハンドル
//-----------------------------------------------------------------------------
function EffectRGBColor_Exchange(hBMP:HBitmap;Value:integer):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:=0 to DestBitmap.Height-1 do
begin
SrcRow := SrcBitmap.ScanLine[Row];
DestRow := DestBitmap.ScanLine[Row];
for Col:=0 to DestBitmap.Width-1 do
begin
WITH DestRow[Col] do
CASE Value of
1:begin
rgbtBlue := SrcRow[Col].rgbtBlue;
rgbtGreen := SrcRow[Col].rgbtRed;
rgbtRed := SrcRow[Col].rgbtGreen;
end;
2:begin
rgbtBlue := SrcRow[Col].rgbtRed;
rgbtGreen := SrcRow[Col].rgbtGreen;
rgbtRed := SrcRow[Col].rgbtBlue;
end;
3:begin
rgbtBlue := SrcRow[Col].rgbtGreen;
rgbtGreen := SrcRow[Col].rgbtBlue;
rgbtRed := SrcRow[Col].rgbtRed;
end;
4:begin
rgbtBlue := SrcRow[Col].rgbtRed;
rgbtGreen := SrcRow[Col].rgbtBlue;
rgbtRed := SrcRow[Col].rgbtGreen;
end;
5:begin
rgbtBlue := SrcRow[Col].rgbtGreen;
rgbtGreen := SrcRow[Col].rgbtred;
rgbtRed := SrcRow[Col].rgbtBlue;
end;
end;
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 :=
EffectRGBColor_Exchange(Image1.Picture.Bitmap.ReleaseHandle,3);
end;
スポンサーリンク
関連記事
| 前の記事: | RGBカラー調整のサンプルコード |
| 次の記事: | HSVカラー調整のサンプルコード |
公開日:2015年02月24日
記事NO:00297
プチモンテ ※この記事を書いた人
![]() | |
![]() | 💻 ITスキル・経験 サーバー構築からWebアプリケーション開発。IoTをはじめとする電子工作、ロボット、人工知能やスマホ/OSアプリまで分野問わず経験。 画像処理/音声処理/アニメーション、3Dゲーム、会計ソフト、PDF作成/編集、逆アセンブラ、EXE/DLLファイルの書き換えなどのアプリを公開。詳しくは自己紹介へ |
| 🎵 音楽制作 BGMは楽器(音源)さえあれば、何でも制作可能。歌モノは主にロック、バラード、ポップスを制作。歌詞は抒情詩、抒情的な楽曲が多い。楽曲制作は🔰2023年12月中旬 ~ | |









