您现在的位置:首页 >> 图形媒体 >> 图形媒体 >> 内容

一个将图像的某个区域之外的区域变暗的函数

时间:2011/9/3 15:03:36 点击:

  核心提示:过程名: DarkBmp 作者: 不得闲 日期: 2009.02.04 参数: bmp: TBitmap;var DarkRect: TRect;const DarkValue: Integer;co...
过程名:    DarkBmp
  作者:      不得闲
  日期:      2009.02.04
  参数:      bmp: TBitmap;var DarkRect: TRect;const DarkValue: Integer;const HighValue: Integer = 0
             DarkRect指定不变暗的区域
             DarkValue: 指定暗化的数量值
             HighValue: 指定矩形区域增亮化的数量值
  功能:     将图像的某个区域之外的区域变暗的函数
  返回值:    无

procedure DarkBmp(bmp: TBitmap;var DarkRect: TRect;const DarkValue: Integer;const HighValue: Integer = 0);
var
  i,j : integer;
  pB : PByteArray;
  BmpFormatXs: Integer;
  w,h:Integer;
begin
  i := Integer(bmp.PixelFormat);
  if i < 4 then
    i := 4
  else if i = 4 then
    inc(i)
  else if i > 7 then
    i := 7;  
  BmpFormatXs := i - 3;

  w:= DarkRect.Right - DarkRect.Left;
  h:= DarkRect.Bottom - DarkRect.Top;
 
  if DarkRect.Right > bmp.Width then
  begin
    DarkRect.Left:=bmp.Width - w;
    DarkRect.Right:=bmp.Width;
  end;
  if (DarkRect.Bottom > bmp.Height) then
  begin
    DarkRect.Top:= bmp.Height - h;
    DarkRect.Bottom:=bmp.Height;
  end;
  if DarkRect.Left <0 then
  begin
    DarkRect.Left:=0;
    DarkRect.Right:=w;
  end;
  if DarkRect.Top <0 then
  begin
    DarkRect.Top:=0;
    DarkRect.Bottom:=h;
  end;
  for i := 0 to DarkRect.Top - 1 do
  begin
    pb:=bmp.ScanLine[i];
    for j:=0 to BmpFormatXs*bmp.Width - 1 do
      if pb[j]<DarkValue then
        pb[j]:=0
      else dec(pb[j],DarkValue);
  end;

  for i := DarkRect.Top to bmp.Height - 1 do
  begin
    pb:=bmp.ScanLine[i];
    for j:=0 to BmpFormatXs*DarkRect.Left - 1 do
      if pb[j]<DarkValue then
        pb[j]:=0
      else dec(pb[j],DarkValue);
  end;

  for i := DarkRect.Bottom to bmp.Height - 1 do
  begin
    pb:=bmp.ScanLine[i];
    for j:= BmpFormatXs*DarkRect.Left to BmpFormatXs*bmp.Width - 1 do
      if pb[j]<DarkValue then
        pb[j]:=0
      else dec(pb[j],DarkValue);
  end;

  for i := DarkRect.Top to DarkRect.Bottom - 1 do
  begin
    pb:=bmp.ScanLine[i];
    for j:= BmpFormatXs*DarkRect.Right to BmpFormatXs*bmp.Width - 1 do
      if pb[j]<DarkValue then
        pb[j]:=0
      else dec(pb[j],DarkValue)
  end;

  if HighValue <> 0 then
    for i := DarkRect.Top to DarkRect.Bottom - 1 do
    begin
      pb:=bmp.ScanLine[i];
      for j:= BmpFormatXs*DarkRect.Left to BmpFormatXs*DarkRect.Right - 1 do
        if pb[j]>(255-DarkValue) then
          pb[j]:=255
        else inc(pb[j],HighValue);
    end;
end;

 

使用方法:

procedure TForm1.Button1Click(Sender: TObject);
var
  bmp: TBitmap;
  r: TRect;
begin
  bmp := TBitmap.Create;
  bmp.Assign(Image1.Picture.Bitmap);
  r := Rect(0,0,50,50);
  DarkBmp(bmp,r,40,10);
  Image2.Picture.Bitmap := bmp;
end;

作者:不得闲 日期: 20 来源:原创
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号