本文用GDI+实现Photoshop色相/饱和度/明度功能,参照我的其它有关GDI+在 Delphi程序的应用的文章,代码也可供TBitmap使用。

有些人不喜欢,或者不太懂Delphi的BASM代码,所以本文给出纯PAS代码。须说明的是,纯PAS代码效率较低,不适合实际应用。喜欢C/C++的,可以看本人文章《C++实现Photoshop色相/饱和度/明度功能》,除了语言不同,其它都一样。

有关Photoshop饱和度调整原理可参见《GDI+ 在Delphi程序的应用 -- 图像饱和度调整》,明度调整原理可参见《GDI+ 在Delphi程序的应用 -- 仿Photoshop的明度调整》。

下面是一个完整的Delphi程序,Photoshop色相/饱和度/明度功能纯PAS代码包含在其中:

unit main;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, ComCtrls, Gdiplus;
 
type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Hbar: TTrackBar;
    SBar: TTrackBar;
    BBar: TTrackBar;
    HEdit: TEdit;
    SEdit: TEdit;
    BEdit: TEdit;
    Button1: TButton;
    PaintBox1: TPaintBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure PaintBox1Paint(Sender: TObject);
    procedure HBarChange(Sender: TObject);
    procedure SBarChange(Sender: TObject);
    procedure BBarChange(Sender: TObject);
    procedure HEditChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    Source: TGpBitmap;
    Bitmap: TGpBitmap;
    r: TGpRect;
    Lock: Boolean;
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure SwapRGB(var a, b: Integer);
begin
  Inc(a, b);
  b := a - b;
  Dec(a, b);
end;
 
procedure CheckRGB(var Value: Integer);
begin
  if Value < 0 then Value := 0
  else if Value > 255 then Value := 255;
end;
 
procedure AssignRGB(var R, G, B: Byte; intR, intG, intB: Integer);
begin
  R := intR;
  G := intG;
  B := intB;
end;
 
procedure SetBright(var R, G, B: Byte; bValue: Integer);
var
  intR, intG, intB: Integer;
begin
  intR := R;
  intG := G;
  intB := B;
  if bValue > 0 then
  begin
    Inc(intR, (255 - intR) * bValue div 255);
    Inc(intG, (255 - intG) * bValue div 255);
    Inc(intB, (255 - intB) * bValue div 255);
  end
  else if bValue < 0 then
  begin
    Inc(intR, intR * bValue div 255);
    Inc(intG, intG * bValue div 255);
    Inc(intB, intB * bValue div 255);
  end;
  CheckRGB(intR);
  CheckRGB(intG);
  CheckRGB(intB);
  AssignRGB(R, G, B, intR, intG, intB);
end;
 
procedure SetHueAndSaturation(var R, G, B: Byte; hValue, sValue: Integer);
var
  intR, intG, intB: Integer;
  H, S, L, Lum: Integer;
  delta, entire: Integer;
  index, extra: Integer;
begin
  intR := R;
  intG := G;
  intB := B;
 
  if intR < intG then SwapRGB(intR, intG);
  if intR < intB then SwapRGB(intR, intB);
  if intB > intG then SwapRGB(intB, intG);
 
  delta := intR - intB;
  if delta = 0 then Exit;
 
  entire := intR + intB;
  L := entire shr 1;
  if L < 128 then
    S := delta * 255 div entire
  else
    S := delta * 255 div (510 - entire);
  if hValue <> 0 then
  begin
    if intR = R then
      H := (G - B) * 60 div delta
    else if intR = G then
      H := (B - R) * 60 div delta + 120
    else
      H := (R - G) * 60 div delta + 240;
    Inc(H, hValue);
    if H < 0 then
      Inc(H, 360)
    else if H > 360 then
      Dec(H, 360);
    index := H div 60;
    extra := H mod 60;
    if (index and 1) <> 0 then
      extra := 60 - extra;
    extra := (extra * 255 + 30) div 60;
    intG := extra - (extra - 128) * (255 - S) div 255;
    Lum := L - 128;
    if Lum > 0 then
      Inc(intG, (((255 - intG) * Lum + 64) div 128))
    else if Lum < 0 then
      Inc(intG, (intG * Lum div 128));
    CheckRGB(intG);
    case index of
      1: SwapRGB(intR, intG);
      2:
      begin
        SwapRGB(intR, intB);
        SwapRGB(intG, intB);
      end;
      3: SwapRGB(intR, intB);
      4:
      begin
        SwapRGB(intR, intG);
        SwapRGB(intG, intB);
      end;
      5: SwapRGB(intG, intB);
    end;
  end
  else
  begin
    intR := R;
    intG := G;
    intB := B;
  end;
  if sValue <> 0 then
  begin
    if sValue > 0 then
    begin
      if sValue + S >= 255 then sValue := S
      else sValue := 255 - sValue;
      sValue := 65025 div sValue - 255;
    end;
    Inc(intR, ((intR - L) * sValue div 255));
    Inc(intG, ((intG - L) * sValue div 255));
    Inc(intB, ((intB - L) * sValue div 255));
    CheckRGB(intR);
    CheckRGB(intG);
    CheckRGB(intB);
  end;
  AssignRGB(R, G, B, intR, intG, intB);
end;
 
procedure GdipHSBAdjustment(Bmp: TGpBitmap; hValue, sValue, bValue: Integer);
var
  Data: TBitmapData;
  x, y: Integer;
  p: PRGBQuad;
begin
  sValue := sValue * 255 div 100;
  bValue := bValue * 255 div 100;
  Data := Bmp.LockBits(GpRect(0, 0, Bmp.Width, Bmp.Height), [imRead, imWrite], pf32bppARGB);
  try
    p := Data.Scan0;
    for y := 1 to Data.Height do
    begin
      for x := 1 to Data.Width do
      begin
        if (sValue > 0) and (bValue <> 0) then
          SetBright(p^.rgbRed, p^.rgbGreen, p^.rgbBlue, bValue);
        SetHueAndSaturation(p^.rgbRed, p^.rgbGreen, p^.rgbBlue, hValue, sValue);
        if (sValue <= 0) and (bValue <> 0) then
          SetBright(p^.rgbRed, p^.rgbGreen, p^.rgbBlue, bValue);
        Inc(p);
      end;
    end;
  finally
    Bmp.UnlockBits(Data);
  end;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  Source := TGpBitmap.Create('../../GdiplusDemo/media/100_0349.jpg');
  r := GpRect(0, 0, Source.Width, Source.Height);
  Bitmap := Source.Clone(r, pf32bppARGB);
  DoubleBuffered := True;
end;
 
procedure TForm1.FormDestroy(Sender: TObject);
begin
  Bitmap.Free;
  Source.Free;
end;
 
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
  g: TGpGraphics;
begin
  g := TGpGraphics.Create(PaintBox1.Canvas.Handle);
  try
    g.DrawImage(Bitmap, r);
    g.TranslateTransform(0, r.Height);
    g.DrawImage(Source, r);
  finally
    g.Free;
  end;
end;
 
procedure TForm1.HBarChange(Sender: TObject);
begin
  if not Lock then
    HEdit.Text := IntToStr(HBar.Position);
end;
 
procedure TForm1.SBarChange(Sender: TObject);
begin
  if not Lock then
    SEdit.Text := IntToStr(SBar.Position);
end;
 
procedure TForm1.BBarChange(Sender: TObject);
begin
  if not Lock then
    BEdit.Text := IntToStr(BBar.Position);
end;
 
procedure TForm1.HEditChange(Sender: TObject);
begin
  Lock := True;
  if TEdit(Sender).Text = '' then
    TEdit(Sender).Text := '0';
  case TEdit(Sender).Tag of
    0: HEdit.Text := IntToStr(HBar.Position);
    1: HEdit.Text := IntToStr(HBar.Position);
    2: HEdit.Text := IntToStr(HBar.Position);
  end;
  Lock := False;
  Bitmap.Free;
  Bitmap := Source.Clone(r, pf32bppARGB);
  if (HBar.Position <> 0) or (SBar.Position <> 0) or (BBar.Position <> 0) then
    GdipHSBAdjustment(Bitmap, HBar.Position, SBar.Position, BBar.Position);
  PaintBox1.Invalidate;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  HBar.Position := 0;
  SBar.Position := 0;
  BBar.Position := 0;
end;
 
end.

程序运行界面截图:

代码中所用Gdiplus单元下载地址及BUG更正见文章《GDI+ for VCL基础 -- GDI+ 与 VCL》。

建议和指导请来信:maozefa@hotmail.com

注:本文于2009.11.1整理,以前的BASM代码从本文删除,转移到《Delphi图像处理》系列文章中,特此致歉。

GDI+在Delphi程序的应用 Photoshop色相饱和度明度功能的更多相关文章

  1. 一名Delphi程序员的开发习惯

    一名Delphi程序员的开发习惯 有关开发习惯的一些想法,如鲠在喉,不吐不快.究其发贴动机,当然不排除有骗取参与分的可能,但另一方面,也希望能给同行(念Xing)者提供一些 建议,或者参考(希望不是误 ...

  2. Fastreport使用经验(转)在Delphi程序中访问报表对象

    Fastreport使用经验(转) 在Delphi程序中访问报表对象 最基本的方法就是frxReport1.FindObject. 然后把返回的对象强制转换成它的类型,当然,在报表中必须真的有这么个东 ...

  3. Delphi 程序结构

    注:该内容整理自以下链接. http://www.cnblogs.com/hackpig/archive/2010/02/15/1668513.html 概要介绍:Object Pascal语言的结构 ...

  4. delphi 程序全屏显示无标题栏,覆盖整个屏幕

    delphi 程序全屏显示无标题栏,覆盖整个屏幕,这个在做工控机或屏保时有用的,所以记下 procedure TMainFrm.FormCreate(Sender: TObject); begin w ...

  5. 远程控制篇:在DELPHI程序中拨号上网

    用MODEM拨号上网,仍是大多数个人网民选择上网的方式.如果能在我们的应用程序中启动拨号连接(如IE浏览器程序中的自动拨号功能),无疑将会方便我们的软件用户(不用再切换应用程序,运行拨号网络),提高我 ...

  6. 问题-delphi 程序在某电脑中显示???问号 乱码

    问题现象:delphi 程序在某电脑中显示???问号 乱码 问题原因:因为语言的原因.不同的国家可能显示的编码不一样. 问题处理:“控制面板”>“区域和语言选项”>“区域选项”>“标 ...

  7. 让delphi程序不受WINDOWS日期格式的影响

    http://www.cnblogs.com/hnxxcxg/archive/2013/01/30/2882672.html 如果WINDOWS系统的短日期格式为“yyyy/m/d”,执行下面的代码会 ...

  8. 让delphi程序不受WINDOWS日期格式的影响(使用SetLocaleInfo函数和Application.UpdateFormatSettings)

    如果WINDOWS系统的短日期格式为“yyyy/m/d”,执行下面的代码会报错:2013-01-29 00:00:00不是合法的日期procedure TFrmQuerySale.FormShow(S ...

  9. Delphi程序调用C#.Net编译的DLL并打开窗体(详解)

    Delphi程序调用C#.Net编译的DLL并打开窗体(详解)最近用C#.Net写了一个公用模块, 本以为仅提供给.Net程序使用, 但是领导要求把这些功能提供给旧系统使用, 天啦, 几套旧系统全是D ...

随机推荐

  1. rstrip

    ====== rstrip ====== Description Returns a copy of the string with trailing characters removed. Synt ...

  2. 折半枚举——poj3977

    暴力搜索超时,但是折半后两部分状态支持合并的情况,可用折半枚举算法 poj3977 给一个序列a[],从里面找到k个数,使其和的绝对值最小 经典折半枚举法+二分解决,对于前一半数开一个map,map[ ...

  3. 工程师技术(二):postfix基础邮件服务、postfix空客户端邮件服务、搭建mariadb数据库系统、配置一个数据库、使用数据库查询

    一.postfix基础邮件服务 目标: 本例要求在虚拟机server0上配置 postfix 基础服务,具体要求如下: 1> 监听本机的所有接口    2> 将邮件域和邮件服务主机名都改为 ...

  4. stm32 hal库串口通信资料汇集

    串口的发送接收函数:HAL_UART_Transmit();串口轮询模式发送,使用超时管理机制.HAL_UART_Receive();串口轮询模式发送,使用超时管理机制.HAL_UART_Transm ...

  5. HBase与Sqoop集成案例

    HBase与Sqoop集成 案例:将RDBMS中的数据抽取到HBase中 Step1.配置sqoop-env.sh如下: Step2.在Mysql中创建一张数据库library,一张表book CRE ...

  6. 非JAVA客户端与mina使用 PrefixedStringCodecFactory 通讯

    与C++,C#不同,java的写入字节顺序是从高到低(左低到右高) 例如 内存数据:{ 0x67,0x45,0x23,0x01} ,java int值是:0x6745231  而C++是:0x1234 ...

  7. js读取json数据

    { "code": 0, "msg": null, "data": { "pageNum": 1, "page ...

  8. HDU 6667 Roundgod and Milk Tea (思维)

    2019 杭电多校 8 1011 题目链接:HDU 6667 比赛链接:2019 Multi-University Training Contest 8 Problem Description Rou ...

  9. PAT_A1112#Stucked Keyboard

    Source: PAT A1112 Stucked Keyboard (20 分) Description: On a broken keyboard, some of the keys are al ...

  10. 2019河北省大学生程序设计竞赛(重现赛)B 题 -Icebound and Sequence ( 等比数列求和的快速幂取模)

    题目链接:https://ac.nowcoder.com/acm/contest/903/B 题意: 给你 q,n,p,求 q1+q2+...+qn 的和 模 p. 思路:一开始不会做,后面查了下发现 ...