原创 ···DELPHI 图片图形图像处理 PS置换滤镜效果,亮度/对比度,纹理贴图效果

DELPHI 图片图形图像处理 PS置换滤镜效果,亮度/对比度,纹理贴图

以前没弄过DELPHI,最近学了1个多星期时间,无聊时候吧以前用C#写的部分图像处理代码转换成DELPHI,竟然发现D处理图片的速度比C#快了20倍··················D处理图片0.3s,C#处理同一图片6s多 ,差距真大·····················我写的算法调用GDI函数,无汇编便于理解,觉的好的顶一个,顶的多就发布下一篇图像处理,转帖的话请写明转帖和原作者 胡睿 QQ 235483710 EMAIL: jennievictor@163.com

欢迎大家交流技术。D没学多久,有些地方没写好的别见怪,有优化的大家优化吧,把成果共享下。

 

 

www.googler.cc/software.html 下载

 

http://www.googler.cc/ps.html 可以购买该软件完整源码

 

  1. //==============================================================================
  2. // 图像处理程序 , 纹理与背景融合算法  未优化版
  3. // 作者 : 胡睿   QQ :235483710, Email :jennievictor@163.com
  4. // 创建日期 : 2008/9/2
  5. // 承接手机程序开发,windows程序开发,图形图像处理开发,数据库开发等外包业务
  6. // 主打 .net, SqlServer 2K , Oracle , J2me , WM
  7. //==============================================================================
  8. unit Main;
  9. interface
  10. uses
  11.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  12.   Dialogs, ExtCtrls, StdCtrls, Buttons, ComCtrls, jpeg, StrUtils, FmtBcd ;
  13. type
  14.   TForm1 = class(TForm)
  15.     BitBtn1: TBitBtn;
  16.     BitBtn2: TBitBtn;
  17.     OpenDialog1: TOpenDialog;
  18.     OpenDialog2: TOpenDialog;
  19.     GroupBox1: TGroupBox;
  20.     Image1: TImage;
  21.     GroupBox2: TGroupBox;
  22.     Image2: TImage;
  23.     BitBtn3: TBitBtn;
  24.     TrackBar1: TTrackBar;
  25.     Button2: TButton;
  26.     Label7: TLabel;
  27.     Label8: TLabel;
  28.     Label1: TLabel;
  29.     TrackBar2: TTrackBar;
  30.     TrackBar3: TTrackBar;
  31.     Label2: TLabel;
  32.     Button1: TButton;
  33.     Label3: TLabel;
  34.     TrackBar4: TTrackBar;
  35.     Label4: TLabel;
  36.     TrackBar5: TTrackBar;
  37.     TrackBar6: TTrackBar;
  38.     Label5: TLabel;
  39.     Label6: TLabel;
  40.     Button3: TButton;
  41.     Label9: TLabel;
  42.     procedure FormCreate(Sender: TObject);
  43.     procedure BitBtn3Click(Sender: TObject);
  44.     procedure BitBtn1Click(Sender: TObject);
  45.     procedure BitBtn2Click(Sender: TObject);
  46.     procedure TrackBar1Change(Sender: TObject);
  47.     procedure TrackBar2Change(Sender: TObject);
  48.     procedure TrackBar3Change(Sender: TObject);
  49.     procedure Button2Click(Sender: TObject);
  50.     // 处理移动图像
  51.     // 参数定义   bmpBackGround1 背景图片
  52.     //            bmpTexture1 纹理图片
  53.     //            moveX 纹理开始区域在背景图中X坐标
  54.     //            moveY 纹理开始区域在背景图中Y坐标
  55.     // 返回  Bitmap移动后的图片
  56.     function createMoveImg( bmpBackGround1 : TBitmap ; moveX : Integer ; moveY : Integer) : TBitmap;
  57.     // 处理混合图像
  58.     // 参数定义   bmpBackGround1 背景图片
  59.     //            bmpTexture1 纹理图片
  60.     //            moveX 纹理开始区域在背景图中X坐标
  61.     //            moveY 纹理开始区域在背景图中Y坐标
  62.     // 返回  Bitmap混合效果特效图片
  63.     function createMixImg( bmpBackGround1 : TBitmap ; bmpTexture1 : TBitmap ; moveX : Integer ; moveY : Integer ) : TBitmap ;
  64.     // 处理图像置换
  65.     // 参数定义   bmpBackGround1 背景图片
  66.     //            bmpTexture1 纹理图片
  67.     //            moveX 纹理开始区域在背景图中X坐标
  68.     //            moveY 纹理开始区域在背景图中Y坐标
  69.     // 返回  Bitmap混合置换特效图片
  70.     function replacementEffect( bmpBackGround1 : TBitmap ; bmpTexture1 : TBitmap ; moveX : Integer ; moveY : Integer ) : TBitmap ;
  71.     //调节背景图亮度对比度
  72.     //参数定义    brightness 亮度
  73.     //            contrast 对比度
  74.     function BrightnessAndContrast( brightness : Integer ; contrast : Integer ) : TBitmap ;
  75.     procedure Button1Click(Sender: TObject);
  76.     procedure TrackBar4Change(Sender: TObject);
  77.     procedure TrackBar5Change(Sender: TObject);
  78.     procedure TrackBar6Change(Sender: TObject);
  79.     procedure Button3Click(Sender: TObject);
  80.   private
  81.     { Private declarations }
  82.   { procedure BlendBmp(Bmp : TBitmap; BlendValue: Byte);//透明度处理 }
  83.   public
  84.     { Public declarations }
  85.   end;
  86. var
  87.   Form1 : TForm1 ;
  88.   bmpBackGround : TBitmap ; //背景位图
  89.   bmpTexture : TBitmap ; //纹理位图
  90.   isLoadBackGround : Boolean ; //是否已经加载背景图
  91.   isLoadTexture : Boolean ; //是否已经加载纹理图
  92.   alphaValue : Integer ; //alpha数值
  93. implementation
  94. {$R *.dfm}
  95. //==============================================================================
  96. // 图像处理程序 , 纹理与背景融合算法  未优化版
  97. // 作者 : 胡睿   QQ :235483710, Email :jennievictor@163.com
  98. // 创建日期 : 2008/9/2
  99. // 承接手机程序开发,windows程序开发,图形图像处理开发,数据库开发等外包业务
  100. // 主打 .net, SqlServer 2K , Oracle , J2me , WM
  101. //==============================================================================
  102. //==============================================================================
  103. // 窗体初始化
  104. //==============================================================================
  105. procedure TForm1.FormCreate(Sender: TObject);
  106. begin
  107.   //窗体初始化位置
  108.   OpenDialog1.Title := '请选择图片文件,Demo仅支持bmp' ;
  109.   OpenDialog1.InitialDir := ExtractFilePath(Application.ExeName) ;
  110.   OpenDialog2.Title := '请选择图片文件,Demo仅支持bmp' ;
  111.   OpenDialog2.InitialDir := ExtractFilePath(Application.ExeName) ;
  112.   Form1.Top := -20 ;
  113.   Form1.Left := 11 ;
  114.   //初始化位图
  115.   bmpBackGround := TBitmap.Create ; //背景位图
  116.   bmpTexture := TBitmap.Create ; //纹理位图
  117.   self.DoubleBuffered := true ; //打开双缓冲
  118.                                              
  119. end;
  120. //==============================================================================
  121. // 混合溶解,产生特效图片
  122. //==============================================================================
  123. procedure TForm1.BitBtn3Click(Sender: TObject);
  124. begin
  125.     GroupBox1.Caption := '特效图片' ;
  126.     if isLoadTexture = True and isLoadBackGround = True then
  127.       begin
  128.       
  129.          Image1.Picture := nil ; //释放图片
  130.          Image1.Canvas.Draw( 0 , 0 , createMixImg( bmpBackGround ,bmpTexture , TrackBar2.Position , TrackBar3.Position ) );
  131.          Image1.Canvas.TextOut( 60,60,'DEMO 胡睿制作 QQ : 235483710 ' );
  132.       end
  133.     else
  134.       begin
  135.          ShowMessage( '请先载入纹理图片及背景图片' ) ;
  136.       end;
  137. end;
  138. //==============================================================================
  139. //打开背景文件,选择框加载图片
  140. //==============================================================================
  141. procedure TForm1.BitBtn1Click(Sender: TObject);
  142. begin
  143.     //打开文件选择框加载图片
  144.     OpenDialog1.Execute ;
  145.     //判断选择的背景图片是否格式正确
  146.     if RightStr( OpenDialog1.FileName , 4 ) = '.bmp' then
  147.     begin
  148.       Image1.Picture.LoadFromFile( OpenDialog1.FileName ) ;
  149.       //将jpeg图转化为位图
  150.       //jpegImg := TJPEGImage.Create ;
  151.       //jpegImg.LoadFromFile( OpenDialog1.FileName ) ;
  152.       //bmpBackGround.Assign( jpegImg ) ;
  153.       bmpBackGround.Assign(Image1.Picture) ;
  154.       isLoadBackGround := True ; 
  155.       
  156.       //设置trackbar2,trackbar3最大值
  157.       trackBar2.Max := bmpBackGround.Width ;
  158.       TrackBar3.Max := bmpBackGround.Height ;
  159.     end;
  160. end;
  161. //==============================================================================
  162. //打开纹理文件,选择框加载图片
  163. //==============================================================================
  164. procedure TForm1.BitBtn2Click(Sender: TObject);
  165. begin
  166.     //打开文件选择框加载图片
  167.     OpenDialog2.Execute;
  168.     //判断选择的纹理图片是否格式正确
  169.     if RightStr( OpenDialog2.FileName , 4 ) = '.bmp' then
  170.     begin
  171.     
  172.       Image2.Picture.LoadFromFile( OpenDialog2.FileName ) ;
  173.       
  174.       //将jpeg图转化为位图
  175.       //jpegImg := TJPEGImage.Create ;
  176.       //jpegImg.LoadFromFile( OpenDialog2.FileName ) ;
  177.       //bmpTexture.Assign( jpegImg ) ;
  178.       bmpTexture.Assign(Image2.Picture.Graphic) ;
  179.       isLoadTexture := True ;
  180.     end;
  181. end;
  182. //==============================================================================
  183. // trackbar1显示透明度
  184. //==============================================================================
  185. procedure TForm1.TrackBar1Change(Sender: TObject);
  186. begin
  187.     alphaValue := TrackBar1.Position ;
  188.     Label1.Caption := 'Alpha : ' + IntToStr( alphaValue ) ;
  189. end;
  190. //==============================================================================
  191. // trackbar2显示移动到背景图X位置
  192. //==============================================================================
  193. procedure TForm1.TrackBar2Change(Sender: TObject);
  194. var
  195.   X : Integer ; //X数值
  196.   bmpMove : TBitmap ;
  197.   rectMove : TRect ;
  198. begin 
  199.     X :=  trackbar2.Position ;
  200.     Label7.Caption := 'X : ' + IntToStr( X ) ;
  201.              
  202.     bmpMove := TBitmap.Create ;
  203.     bmpMove.Assign( bmpBackGround );
  204.     //产生移动虚线框
  205.     rectMove := Rect(X , trackbar3.Position , X + bmpTexture.Width , trackbar3.Position + bmpTexture.Height ) ;
  206.     bmpMove.Canvas.DrawFocusRect( rectMove );
  207.     Image1.Canvas.Draw(0,0, bmpMove );
  208.     Image1.Refresh;
  209. end;
  210. //==============================================================================
  211. // trackbar3显示移动到背景图Y位置
  212. //==============================================================================
  213. procedure TForm1.TrackBar3Change(Sender: TObject);
  214. var
  215.   Y : Integer ; //Y数值
  216.   bmpMove : TBitmap ;
  217.   rectMove : TRect ;
  218. begin
  219.     Y :=  trackbar3.Position ;
  220.     Label8.Caption := 'Y : ' + IntToStr( Y ) ;
  221.     bmpMove := TBitmap.Create ;
  222.     bmpMove.Assign( bmpBackGround );
  223.     //产生移动虚线框
  224.     rectMove := Rect( trackbar2.Position , Y , trackbar2.Position + bmpTexture.Width , Y + bmpTexture.Height ) ;
  225.     bmpMove.Canvas.DrawFocusRect( rectMove );
  226.     Image1.Canvas.Draw(0,0, bmpMove );
  227.     Image1.Refresh;
  228. end;
  229. //==============================================================================
  230. // 移动纹理图至背景图指定位置
  231. //==============================================================================
  232. procedure TForm1.Button2Click(Sender: TObject);
  233. begin
  234.   bmpBackGround := BrightnessAndContrast( trackbar5.Position  , trackbar6.Position ) ;
  235.   if isLoadTexture = True and isLoadBackGround = True then
  236.       begin
  237.           GroupBox1.Caption := '已移动到背景图片' ;
  238.           Image1.Picture := nil ; //释放图片
  239.           Image1.Canvas.Draw( 0 , 0 , createMoveImg( bmpBackGround , TrackBar2.Position , TrackBar3.Position ) );
  240.       end
  241.   else
  242.       begin
  243.         ShowMessage( '请先载入纹理图片及背景图片' ) ;
  244.         
  245.       end ;
  246. end;
  247. //==============================================================================
  248. // 处理移动图像
  249. // 参数定义 bmpBackGround1 背景图片
  250. //          bmpTexture1 纹理图片
  251. //          moveX 纹理开始区域在背景图中X坐标
  252. //          moveY 纹理开始区域在背景图中Y坐标
  253. // 返回  Bitmap混合效果特效图片
  254. //==============================================================================
  255. function TForm1.createMoveImg( bmpBackGround1 : TBitmap ; moveX : Integer ; moveY : Integer) : TBitmap;
  256. var
  257.   i : Integer ;
  258.   j : Integer ;
  259.   pixColorTexture : TColor ;
  260.   cr,cg,cb:dword;
  261.   bmpResult : TBitmap ; //结果图片
  262. begin
  263.    bmpResult := TBitmap.Create ;
  264.    bmpResult.Assign( bmpBackGround1 ) ;
  265.    for i := 0 to  bmpTexture.Width - 1  do
  266.     for j := 0 to  bmpTexture.Height - 1 do
  267.       begin
  268.           pixColorTexture := bmpTexture.Canvas.Pixels[ i , j ];
  269.           cr := GetRValue(pixColorTexture) ;
  270.           cg := GetGValue(pixColorTexture) ;
  271.           cb := GetBValue(pixColorTexture) ;
  272.           bmpResult.Canvas.Pixels[ i + moveX , j + moveY ] := RGB( cr , cg , cb ) ;
  273.       end ;
  274.     Result := bmpResult ;
  275. end;
  276. //==============================================================================
  277. // 处理混合图像
  278. // 参数定义   bmpBackGround1 背景图片
  279. //            bmpTexture1 纹理图片
  280. //            moveX 纹理开始区域在背景图中X坐标
  281. //            moveY 纹理开始区域在背景图中Y坐标
  282. // 返回  Bitmap混合效果特效图片
  283. //==============================================================================
  284. function TForm1.createMixImg( bmpBackGround1 : TBitmap ; bmpTexture1 : TBitmap ; moveX : Integer ; moveY : Integer ) : TBitmap ;
  285. var
  286.   pixColorBG : TColor ; //背景图像素RGB值
  287.   pixColorTexture : TColor ; //纹理图像素RGB值
  288.   bmpResult : TBitmap ; //结果图片
  289.   i : Integer ;
  290.   j : Integer ;
  291.   cr,cg,cb:dword;
  292.   R,G,B : dword ;
  293. begin
  294.     bmpResult := TBitmap.Create ;
  295.     bmpResult.Assign( bmpBackGround1 ) ;
  296.    //处理纹理alpha透明   ( RGB_Texture * alpha + RGB_Background * (256 - alpha) ) >>8
  297.     For i:=0 to  bmpTexture.Width - 1  do
  298.         For j:=0 to bmpTexture.Height - 1 do
  299.         begin
  300.           pixColorTexture := bmpTexture1.Canvas.Pixels[ i , j ];
  301.           pixColorBG :=  bmpBackGround1.Canvas.Pixels[ i + moveX , j + moveY ];
  302.           R:=( GetRValue(pixColorTexture) * alphaValue + GetRValue(pixColorBG) * (256-alphaValue) ) shr 8;
  303.           G:=( GetGValue(pixColorTexture) * alphaValue + GetGValue(pixColorBG) * (256-alphaValue) ) shr 8;
  304.           B:=( GetBValue(pixColorTexture) * alphaValue + GetBValue(pixColorBG) * (256-alphaValue) ) shr 8;
  305.           bmpResult.Canvas.Pixels[ i + moveX , j + moveY ] := RGB( R , G , B ) ;
  306.         end;
  307.    //混合算法公式  RGB_Texture * RGB_Background / 255
  308.    for i := 0 to  bmpTexture.Width - 1  do
  309.     for j := 0 to bmpTexture.Height  - 1 do
  310.       begin
  311.           pixColorTexture := bmpTexture1.Canvas.Pixels[ i , j ];
  312.           pixColorBG :=  bmpBackGround1.Canvas.Pixels[ i + moveX , j + moveY ];
  313.           cr :=  ( GetRValue(pixColorBG) * GetRValue(pixColorTexture)  ) div 255 ;
  314.           cg :=  ( GetGValue(pixColorBG) * GetGValue(pixColorTexture)  ) div 255 ;
  315.           cb :=  ( GetBValue(pixColorBG) * GetBValue(pixColorTexture)  ) div 255 ;
  316.           bmpResult.Canvas.Pixels[ i + moveX , j + moveY ] := RGB( cr , cg , cb ) ;
  317.           
  318.       end ;
  319.     Result := bmpResult ;
  320.     
  321. end;
  322. //==============================================================================
  323. // 处理图像置换
  324. // 参数定义   bmpBackGround1 背景图片
  325. //            bmpTexture1 纹理图片
  326. //            moveX 纹理开始区域在背景图中X坐标
  327. //            moveY 纹理开始区域在背景图中Y坐标
  328. // 返回  Bitmap混合置换特效图片
  329. //==============================================================================
  330. function TForm1.replacementEffect( bmpBackGround1 : TBitmap ; bmpTexture1 : TBitmap ; moveX : Integer ; moveY : Integer ) : TBitmap ;
  331. var
  332.   pixColorBG : array of array of  TColor ; //背景图像素RGB值
  333.   pixColorTexture : array of array of  TColor ; //纹理图像素RGB值
  334.   pixColorBGBak : array of array of  TColor ; //背景图备份像素RGB值
  335.   pixColorResult : array of array of  TColor ; //混合结果图
  336.   bmpBackgroundBak : TBitmap ; //背景图备份
  337.   bmpNewTexture : TBitmap ; //扭曲后新纹理图
  338.   i : Integer ;
  339.   j : Integer ;
  340.   bmpWidth : Integer ;
  341.   bmpHeight : Integer ;
  342.   replaceFactor : Integer ; //置换因子值
  343.   cr, cg, cb : array of array of DWORD; //背景像素颜色分量
  344.   cgray : array of array of DWORD ; //背景图的灰阶图颜色分量
  345.   R, G, B : array of array of DWORD ; //纹理像素颜色分量
  346.   crResult, cgResult, cbResult : array of array of DWORD ; //结果图片像素颜色分量
  347. begin
  348.    SetLength( R ,  bmpTexture1.Width + 128 , bmpTexture1.Height + 128 ) ;
  349.    SetLength( G ,  bmpTexture1.Width + 128 , bmpTexture1.Height + 128 ) ;
  350.    SetLength( B ,  bmpTexture1.Width + 128 , bmpTexture1.Height + 128  ) ;
  351.    SetLength( cr ,  bmpBackGround1.Width , bmpBackGround1.Height ) ;
  352.    SetLength( cg ,  bmpBackGround1.Width , bmpBackGround1.Height ) ;
  353.    SetLength( cb ,  bmpBackGround1.Width , bmpBackGround1.Height ) ;
  354.    SetLength( crResult , bmpTexture1.Width + 128 , bmpTexture1.Height + 128 ) ;
  355.    SetLength( cgResult , bmpTexture1.Width + 128 , bmpTexture1.Height + 128 ) ;
  356.    SetLength( cbResult , bmpTexture1.Width + 128 , bmpTexture1.Height + 128 ) ;
  357.    SetLength( pixColorBG , bmpBackGround1.Width , bmpBackGround1.Height ) ;
  358.    SetLength( pixColorTexture , bmpTexture1.Width + 128 , bmpTexture1.Height + 128 ) ;
  359.    SetLength( pixColorBGBak , bmpBackGround1.Width , bmpBackGround1.Height ) ; //纹理图的灰阶图
  360.    SetLength( cgray , bmpBackGround1.Width , bmpBackGround1.Height ) ; //纹理图的灰阶图颜色分量
  361.    bmpNewTexture := TBitmap.Create ;
  362.    bmpNewTexture.Assign( bmpTexture ) ;
  363.    bmpBackgroundBak := TBitmap.Create ;
  364.    bmpBackgroundBak.Assign( bmpBackGround1 ) ;
  365.    replaceFactor := TrackBar4.Position  ; //置换因子,调整扭曲度用
  366.    //纹理区域内背景灰阶图生成
  367.    for i := 1 to bmpTexture.Width  do
  368.     for j := 1 to bmpTexture.Height  do
  369.       begin
  370.           pixColorBG[i][j] :=  bmpBackGround1.Canvas.Pixels[ i + moveX , j + moveY ] ;
  371.           //获得图像灰阶图
  372.           cr[i][j] := GetRValue( pixColorBG[i][j] ) ;  //背景图红色分量
  373.           cg[i][j] := GetGValue( pixColorBG[i][j] ) ;  //背景图绿色分量
  374.           cb[i][j] := GetBValue( pixColorBG[i][j] ) ;  //背景图蓝色分量
  375.           cgray[i][j] := ( cr[i][j] +  cg[i][j] +  cb[i][j] ) div 3 ;  //获得纹理区域在背景图的灰阶图各个像素的颜色分量 算数平均求灰度
  376.           //显示灰度图
  377.           //bmpBackgroundBak.Canvas.Pixels[ i + moveX , j + moveY ] := RGB( cgray[i][j] , cgray[i][j] , cgray[i][j] ) ; //生成背景灰度图
  378.       end ;
  379.    //纹理图颜色分量
  380.    for i := 1 to bmpTexture1.Width    do
  381.     for j := 1 to bmpTexture1.Height   do
  382.       begin
  383.           pixColorTexture[i][j] := bmpTexture1.Canvas.Pixels[ i , j ] ;
  384.           R[i][j] := GetRValue( pixColorTexture[i][j] ) ;  //纹理图红色分量
  385.           G[i][j] := GetGValue( pixColorTexture[i][j] ) ;  //纹理图绿色分量
  386.           B[i][j] := GetBValue( pixColorTexture[i][j] ) ;  //纹理图蓝色分量
  387.       end ;
  388.    //置换特效图生成  效果图区域 (bmpTexture.Width ) * (bmpTexture.Height )
  389.    for i := 1 to  bmpTexture.Width   do
  390.       for j := 1 to bmpTexture.Height  do
  391.         begin
  392.             //***********************************算法说明 **********************************************
  393.             // 左点和上点像素判断情况  , 默认情况下选择做点和上点像素作为判断基准。
  394.             // 如果 目标像素左边一像素的灰度值 > 128 ,则将目标像素向上移动 (目标像素点灰阶值 - 128) * 置换因子 的距离
  395.             // 否则 目标像素左边一像素的灰度值 < 128 ,则将目标像素向下移动 (128 - 目标像素点灰阶值) * 置换因子 的距离
  396.             //*****************************************************************************************
  397.             if (cgray[i - 1][j] > 128and ( j - ( cgray[i][j] - 128 ) * replaceFactor div 100  >= 1 ) then
  398.                begin
  399.                     crResult[i][j - ( cgray[i][j] - 128 ) * replaceFactor div 100 ] := R[i][j] ;
  400.                     cgResult[i][j - ( cgray[i][j] - 128 ) * replaceFactor div 100 ] := G[i][j] ;
  401.                     cbResult[i][j - ( cgray[i][j] - 128 ) * replaceFactor div 100 ] := B[i][j] ;
  402.                     crResult[i][j] := R[i][j] ;
  403.                     cgResult[i][j] := G[i][j] ;
  404.                     cbResult[i][j] := B[i][j] ;
  405.                end
  406.             else if (cgray[i - 1][j] < 128and ( j + ( 128 - cgray[i][j] ) * replaceFactor div 100 <= bmpTexture.Height  )  then
  407.                begin
  408.                     crResult[i][ j + ( 128 - cgray[i][j] ) * replaceFactor div 100 ] := R[i][j] ;
  409.                     cgResult[i][ j + ( 128 - cgray[i][j] ) * replaceFactor div 100 ] := G[i][j] ;
  410.                     cbResult[i][ j + ( 128 - cgray[i][j] ) * replaceFactor div 100 ] := B[i][j] ;
  411.                     crResult[i][j] := R[i][j] ;
  412.                     cgResult[i][j] := G[i][j] ;
  413.                     cbResult[i][j] := B[i][j] ;
  414.                end ;
  415.             //*************************************************************************************************
  416.             // 如果 目标像素上边一像素的灰度值 > 128 ,则将目标像素向左移动 (目标像素点灰阶值 - 128) * 置换比例 的距离
  417.             // 否则 目标像素上边一像素的灰度值 < 128 ,则将目标像素向右移动 (128 - 目标像素点灰阶值) * 置换比例 的距离
  418.             //*************************************************************************************************
  419.             if ( cgray[i][j - 1] > 128 ) and ( i - ( cgray[i][j] - 128 ) * replaceFactor div 100 >= 1 ) then
  420.                begin
  421.                     crResult[i - ( cgray[i][j] - 128 ) * replaceFactor div 100 ][j] := R[i][j] ;
  422.                     cgResult[i - ( cgray[i][j] - 128 ) * replaceFactor div 100 ][j] := G[i][j] ;
  423.                     cbResult[i - ( cgray[i][j] - 128 ) * replaceFactor div 100 ][j] := B[i][j] ;
  424.                     crResult[i][j] := R[i][j] ;
  425.                     cgResult[i][j] := G[i][j] ;
  426.                     cbResult[i][j] := B[i][j] ;
  427.                end
  428.             else if ( cgray[i][j - 1] < 128 ) and ( i + ( 128 - cgray[i][j] ) * replaceFactor div 100 < bmpTexture.Width  ) then
  429.                begin
  430.                     crResult[i + ( 128 - cgray[i][j] ) * replaceFactor div 100 ][j] := R[i][j] ;
  431.                     cgResult[i + ( 128 - cgray[i][j] ) * replaceFactor div 100 ][j] := G[i][j] ;
  432.                     cbResult[i + ( 128 - cgray[i][j] ) * replaceFactor div 100 ][j] := B[i][j] ;
  433.                     crResult[i][j] := R[i][j] ;
  434.                     cgResult[i][j] := G[i][j] ;
  435.                     cbResult[i][j] := B[i][j] ;
  436.                end ;
  437.             //*************************************************************************************************
  438.             // 右点和下点像素判断情况
  439.             // 如果 目标像素右边一像素的灰度值 > 128 ,则将目标像素向上移动 (目标像素点灰阶值 - 128) * 置换因子 的距离
  440.             // 否则 目标像素右边一像素的灰度值 < 128 ,则将目标像素向下移动 (128 - 目标像素点灰阶值) * 置换因子 的距离
  441.             // 如果 目标像素下边一像素的灰度值 > 128 ,则将目标像素向左移动 (点上的灰阶值 - 128) * 置换比例 的距离
  442.             // 否则 目标像素下边一像素的灰度值 < 128 ,则将目标像素向右移动 (128 - 目标像素点灰阶值) * 置换比例 的距离
  443.             //*************************************************************************************************
  444.             //*************************************************************************************************
  445.             // 如果目标像素点上下左右像素灰度都为128 则不发生移动
  446.             //*************************************************************************************************
  447.             if ( cgray[i - 1][j] = 128 ) and ( cgray[i + 1][j] = 128 ) and ( cgray[i][j - 1] = 128 ) and ( cgray[i][j + 1] = 128 ) then
  448.                begin
  449.                     crResult[i][j] := R[i][j] ;
  450.                     cgResult[i][j] := G[i][j] ;
  451.                     cbResult[i][j] := B[i][j] ;
  452.                end ;
  453.       end;
  454.     //像素绘制图形
  455.     for i := 1 to   bmpTexture.Width   do
  456.       for j := 1 to   bmpTexture.Height   do
  457.         begin
  458.            //消除黑点
  459.            if crResult[i][j] <> clBlack then
  460.              bmpNewTexture.Canvas.Pixels[ i , j ] := RGB( crResult[i][j] , cgResult[i][j] , cbResult[i][j] ) ;
  461.         end ;
  462.   //混合图形纹理效果
  463.   Result := createMixImg( bmpBackGround1 ,bmpNewTexture,TrackBar2.Position,TrackBar3.Position ) ;
  464. end;
  465. //==============================================================================
  466. //调节背景图亮度对比度
  467. //参数定义    brightness 亮度
  468. //            contrast 对比度
  469. //==============================================================================
  470. function TForm1.BrightnessAndContrast( brightness : Integer ; contrast : Integer ) : TBitmap ;
  471. var
  472.   pixColorBG : TColor ; //背景图像素颜色
  473.   bmpBGBak : TBitmap ; //背景图备份
  474.   cr, cg, cb : double//背景像素颜色分量
  475.   cgray : DWORD ; //背景图的灰阶图颜色分量
  476.   R, G, B : double ;
  477.   ir, ig, ib : Integer ;
  478.   i, j : Integer ;
  479. begin
  480.   bmpBGBak := TBitmap.Create ;
  481.   bmpBGBak.Assign( bmpBackGround );
  482.   //算法公式
  483.   for i := 1 to bmpBackGround.Width  do
  484.     for j := 1 to bmpBackGround.Height  do
  485.       begin
  486.          pixColorBG :=  bmpBackGround.Canvas.Pixels[i , j] ;
  487.          //获得图像RGB分量
  488.          cr := GetRValue( pixColorBG ) ;
  489.          cg := GetGValue( pixColorBG ) ;
  490.          cb := GetBValue( pixColorBG ) ; 
  491.          {
  492.          //黑白2色图, contrast = 0 时 图片只有黑白2色
  493.          cgray := ( cr + cg + cb ) div 3 ;
  494.          if cgray > 128 then
  495.             begin
  496.                //加亮
  497.                 R := cr * contrast div 100 + brightness ;
  498.                 G := cg * contrast div 100 + brightness ;
  499.                 B := cb * contrast div 100 + brightness ;
  500.             end
  501.          else if cgray = 128 then
  502.             begin
  503.                //不变
  504.                R := cr ;
  505.                G := cg ;
  506.                B := cb ;
  507.             end
  508.          else
  509.             begin
  510.                //减暗
  511.                R := cr * contrast div 100 - brightness ;
  512.                G := cg * contrast div 100 - brightness ;
  513.                B := cb * contrast div 100 - brightness ;
  514.             end ;}
  515.          //背景图RGB分量  调整亮度
  516.          {
  517.          R := cr * contrast div 100 + brightness ;
  518.          G := cg * contrast div 100 + brightness ;
  519.          B := cb * contrast div 100 + brightness ;
  520.          }
  521.          //对比度调整  调节后像素点颜色 = ((RGB / 255.0 - 0.5) * contrast + 0.5) * 255 + brightness;
  522.          //处理颜色越界
  523.          R := ( ( cr / 255 - 0.5 ) * contrast + 0.5 ) * 255 + brightness ;
  524.          G := ( ( cg / 255 - 0.5 ) * contrast + 0.5 ) * 255 + brightness ;
  525.          B := ( ( cb / 255 - 0.5 ) * contrast + 0.5 ) * 255 + brightness ;
  526.          //if pixColorBG > 255 then pixColorBG := 255 else if pixColorBG < 0 then pixColorBG :=0 ;
  527.          if R > 255 then R := 255 else if R < 0 then R :=0 ;
  528.          if G > 255 then G := 255 else if G < 0 then G :=0 ;
  529.          if B > 255 then B := 255 else if B < 0 then B :=0 ;
  530.          bmpBGBak.Canvas.Pixels[i,j] := RGB( Round(R), Round(G), Round(B) ) ;
  531.       end ;
  532.       Image1.Picture.Assign( bmpBGBak ) ;
  533.       Result :=  bmpBGBak ;
  534. end;
  535. //==============================================================================
  536. // 置换按钮
  537. //==============================================================================
  538. procedure TForm1.Button1Click(Sender: TObject);
  539. begin
  540.    if isLoadTexture = True and isLoadBackGround = True then
  541.       begin
  542.           GroupBox1.Caption := '置换特效图片' ;
  543.           Image1.Picture := nil ; //释放图片
  544.           Image1.Canvas.Draw( 0 , 0 , replacementEffect( BrightnessAndContrast( trackbar5.Position  , trackbar6.Position ) ,bmpTexture , TrackBar2.Position , TrackBar3.Position ) );
  545.       end
  546.   else
  547.       begin
  548.           ShowMessage( '请先载入纹理图片及背景图片' ) ;
  549.       end ;
  550. end;
  551. //==============================================================================
  552. // 置换因子调整
  553. //==============================================================================
  554. procedure TForm1.TrackBar4Change(Sender: TObject);
  555. begin
  556.     Label4.Caption := 'Replace Factor : ' + IntToStr( TrackBar4.Position );
  557. end;
  558. //==============================================================================
  559. // 亮度因子调整
  560. //==============================================================================
  561. procedure TForm1.TrackBar5Change(Sender: TObject);
  562. begin
  563.     //BrightnessAndContrast( 0 , 105 ) ;
  564.     label5.Caption := 'Brightness :' + InttoStr( trackbar5.Position ) ;
  565. end;
  566. //==============================================================================
  567. // 对比度因子调整按钮
  568. //==============================================================================
  569. procedure TForm1.TrackBar6Change(Sender: TObject);
  570. begin
  571.     //BrightnessAndContrast( 0 , 105 ) ;
  572.     label6.Caption := 'Contrast :' + InttoStr( trackbar6.Position ) ;
  573. end;
  574. //==============================================================================
  575. // 确认亮度对比度按钮
  576. //==============================================================================
  577. procedure TForm1.Button3Click(Sender: TObject);
  578. begin
  579.    BrightnessAndContrast( trackbar5.Position  , trackbar6.Position ) ;
  580. end;
  581. end.

效果图

1.原背景

 

 

 

 

2

 

 

 

2。原纹理

 

 

 

 

 

 

 

3.合成效果1

 

 

 

 

 

 

4.合成效果2

 

 

 

界面代码:

  1. object Form1: TForm1
  2.   Left = 168
  3.   Top = 177
  4.   Width = 1000
  5.   Height = 721
  6.   Caption = #22270#20687#22788#29702','#32993#30591#21046#20316'DEMO'#65292'QQ'#65306'235483710'
  7.   Color = clBtnFace
  8.   Font.Charset = DEFAULT_CHARSET
  9.   Font.Color = clWindowText
  10.   Font.Height = -11
  11.   Font.Name = 'MS Sans Serif'
  12.   Font.Style = []
  13.   OldCreateOrder = False
  14.   Position = poMainFormCenter
  15.   OnCreate = FormCreate
  16.   PixelsPerInch = 96
  17.   TextHeight = 13
  18.   object Label7: TLabel
  19.     Left = 672
  20.     Top = 600
  21.     Width = 7
  22.     Height = 13
  23.     Caption = 'X'
  24.   end
  25.   object Label8: TLabel
  26.     Left = 672
  27.     Top = 624
  28.     Width = 7
  29.     Height = 13
  30.     Caption = 'Y'
  31.   end
  32.   object Label1: TLabel
  33.     Left = 632
  34.     Top = 552
  35.     Width = 27
  36.     Height = 13
  37.     Caption = 'Alpha'
  38.   end
  39.   object Label2: TLabel
  40.     Left = 480
  41.     Top = 656
  42.     Width = 33
  43.     Height = 13
  44.     Caption = '<<<-----'
  45.   end
  46.   object Label3: TLabel
  47.     Left = 480
  48.     Top = 624
  49.     Width = 33
  50.     Height = 13
  51.     Caption = '<<<-----'
  52.   end
  53.   object Label4: TLabel
  54.     Left = 721
  55.     Top = 672
  56.     Width = 73
  57.     Height = 13
  58.     Caption = 'Replace Factor'
  59.   end
  60.   object Label5: TLabel
  61.     Left = 16
  62.     Top = 616
  63.     Width = 49
  64.     Height = 13
  65.     Caption = 'Brightness'
  66.   end
  67.   object Label6: TLabel
  68.     Left = 16
  69.     Top = 648
  70.     Width = 39
  71.     Height = 13
  72.     Caption = 'Contrast'
  73.   end
  74.   object Label9: TLabel
  75.     Left = 224
  76.     Top = 624
  77.     Width = 32
  78.     Height = 13
  79.     Caption = 'Label9'
  80.   end
  81.   object BitBtn1: TBitBtn
  82.     Left = 16
  83.     Top = 16
  84.     Width = 89
  85.     Height = 25
  86.     Caption = #23548#20837#32972#26223#22270#29255
  87.     TabOrder = 0
  88.     OnClick = BitBtn1Click
  89.   end
  90.   object BitBtn2: TBitBtn
  91.     Left = 520
  92.     Top = 16
  93.     Width = 89
  94.     Height = 25
  95.     Caption = #23548#20837#32441#29702#22270#29255
  96.     TabOrder = 1
  97.     OnClick = BitBtn2Click
  98.   end
  99.   object GroupBox1: TGroupBox
  100.     Left = 16
  101.     Top = 48
  102.     Width = 457
  103.     Height = 553
  104.     Caption = #32972#26223#22270#29255
  105.     TabOrder = 2
  106.     object Image1: TImage
  107.       Left = 8
  108.       Top = 16
  109.       Width = 441
  110.       Height = 513
  111.       AutoSize = True
  112.       Center = True
  113.     end
  114.   end
  115.   object GroupBox2: TGroupBox
  116.     Left = 520
  117.     Top = 48
  118.     Width = 457
  119.     Height = 489
  120.     Caption = #32441#29702#22270#29255
  121.     TabOrder = 3
  122.     object Image2: TImage
  123.       Left = 8
  124.       Top = 16
  125.       Width = 441
  126.       Height = 449
  127.       Center = True
  128.     end
  129.   end
  130.   object BitBtn3: TBitBtn
  131.     Left = 520
  132.     Top = 616
  133.     Width = 129
  134.     Height = 28
  135.     Caption = #28151#21512#28342#35299
  136.     TabOrder = 4
  137.     OnClick = BitBtn3Click
  138.   end
  139.   object TrackBar1: TTrackBar
  140.     Left = 712
  141.     Top = 552
  142.     Width = 257
  143.     Height = 33
  144.     Ctl3D = True
  145.     Max = 255
  146.     ParentCtl3D = False
  147.     Frequency = 55
  148.     Position = 125
  149.     TabOrder = 5
  150.     OnChange = TrackBar1Change
  151.   end
  152.   object Button2: TButton
  153.     Left = 520
  154.     Top = 584
  155.     Width = 129
  156.     Height = 28
  157.     Caption = #31227#21160#21040#32972#26223#22352#26631
  158.     TabOrder = 6
  159.     OnClick = Button2Click
  160.   end
  161.   object TrackBar2: TTrackBar
  162.     Left = 712
  163.     Top = 584
  164.     Width = 257
  165.     Height = 33
  166.     Frequency = 10
  167.     TabOrder = 7
  168.     OnChange = TrackBar2Change
  169.   end
  170.   object TrackBar3: TTrackBar
  171.     Left = 712
  172.     Top = 616
  173.     Width = 257
  174.     Height = 33
  175.     Frequency = 10
  176.     TabOrder = 8
  177.     OnChange = TrackBar3Change
  178.   end
  179.   object Button1: TButton
  180.     Left = 520
  181.     Top = 656
  182.     Width = 129
  183.     Height = 25
  184.     Caption = #32622#25442#29305#25928
  185.     TabOrder = 9
  186.     OnClick = Button1Click
  187.   end
  188.   object TrackBar4: TTrackBar
  189.     Left = 712
  190.     Top = 640
  191.     Width = 257
  192.     Height = 33
  193.     Max = 100
  194.     Min = 1
  195.     Frequency = 20
  196.     Position = 1
  197.     TabOrder = 10
  198.     OnChange = TrackBar4Change
  199.   end
  200.   object TrackBar5: TTrackBar
  201.     Left = 96
  202.     Top = 616
  203.     Width = 233
  204.     Height = 33
  205.     Max = 100
  206.     Frequency = 55
  207.     TabOrder = 11
  208.     OnChange = TrackBar5Change
  209.   end
  210.   object TrackBar6: TTrackBar
  211.     Left = 96
  212.     Top = 640
  213.     Width = 233
  214.     Height = 33
  215.     Max = 100
  216.     Min = -100
  217.     Frequency = 55
  218.     Position = 1
  219.     TabOrder = 12
  220.     OnChange = TrackBar6Change
  221.   end
  222.   object Button3: TButton
  223.     Left = 336
  224.     Top = 616
  225.     Width = 113
  226.     Height = 25
  227.     Caption = #30830#35748#20142#24230#23545#27604#24230
  228.     TabOrder = 13
  229.     OnClick = Button3Click
  230.   end
  231.   object OpenDialog1: TOpenDialog
  232.     Filter = 'bmp'#25991#20214'|*.bmp'
  233.     Left = 104
  234.     Top = 16
  235.   end
  236.   object OpenDialog2: TOpenDialog
  237.     Filter = 'bmp'#25991#20214'|*.bmp'
  238.     Left = 608
  239.     Top = 16
  240.   end
  241. end

 

 

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值