【问题标题】:Delphi - Implementation of "sepia" routine for an arbitrary colorDelphi - 任意颜色的“棕褐色”例程的实现
【发布时间】:2012-11-08 23:57:43
【问题描述】:

我见过将图像转换为棕褐色调版本的奇怪例程,例如:

function bmptosepia(const bmp: TBitmap; depth: Integer): Boolean;
var
color,color2:longint;
r,g,b,rr,gg:byte;
h,w:integer;
begin
  for h := 0 to bmp.height do
  begin
    for w := 0 to bmp.width do
    begin
//first convert the bitmap to greyscale
    color:=colortorgb(bmp.Canvas.pixels[w,h]);
    r:=getrvalue(color);
    g:=getgvalue(color);
    b:=getbvalue(color);
    color2:=(r+g+b) div 3;
    bmp.canvas.Pixels[w,h]:=RGB(color2,color2,color2);
//then convert it to sepia
    color:=colortorgb(bmp.Canvas.pixels[w,h]);
    r:=getrvalue(color);
    g:=getgvalue(color);
    b:=getbvalue(color);
    rr:=r+(depth*2);
    gg:=g+depth;
    if rr <= ((depth*2)-1) then
    rr:=255;
    if gg <= (depth-1) then
    gg:=255;
    bmp.canvas.Pixels[w,h]:=RGB(rr,gg,b);
    end;
  end;
end;

(来自here)但我需要一些可以为任意颜色执行此操作的东西 - 即它将拍摄一张图像,大概形成它的灰度版本,然后将新颜色应用于图像。这是我遇到的最后一点问题 - 即用感兴趣的颜色替换灰色阴影。

所以我需要

procedure BmpToOneColor (const bmp      : TBitmap ;
                               depth    : Integer ; 
                               NewColor : TColor) ;

(我不知道为什么原件被写成布尔函数)。

【问题讨论】:

  • 另外,使用ScanLine 代替Pixels。当您使用更多像素时,后者的效率非常低。

标签: delphi colors image-manipulation delphi-2006 colorize


【解决方案1】:

您的基本算法只是为灰度值的每个颜色通道添加固定偏移量。我们可以概括这一点,以便NewColor 参数确定每个通道的偏移量。请注意,depth 变得多余,您可以完全忽略它。

rbase:=getrvalue(NewColor);
gbase:=getgvalue(NewColor);
bbase:=getbvalue(NewColor);
base:=min(rbase,min(gbase,bbase));
rdepth:=rbase-base;
gdepth:=gbase-base;
bdepth:=bbase-base;

rr:=r+rdepth;
gg:=g+gdepth;
bb:=b+bdepth;
if rr < rdepth then
    rr:=255;
if gg < gdepth then
    gg:=255;
if bb < bdepth then
    bb:=255;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2021-03-28
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多