首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

DelphiX 實戰:TDIB.DoEmboss 浮雕/壓紋效果

     編輯:Delphi

TDIB.DoEmboss 以固定 3x3 卷積核產生浮雕效果,模擬光源從左上方照射的立體質感。無參數、一鍵套用,本文提供最小範例。

效果預覽

DoEmboss 效果

核心程式碼

unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DIB, StdCtrls;
type
  TForm1 = class(TForm)
    DXPaintBox1: TDXPaintBox;
    Button1: TButton; // 載入圖片
    Button2: TButton; // 浮雕
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;
var Form1: TForm1;
implementation {$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
const ImgPath1 = 'C:\Temp\DX.jpg';
begin
  DXPaintBox1.DIB.LoadFromFile(ImgPath1);
  DXPaintBox1.Width := DXPaintBox1.DIB.Width;
  DXPaintBox1.Height := DXPaintBox1.DIB.Height;
  DXPaintBox1.Repaint;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  // DoEmboss  // 無參數,固定左上光源浮雕核
  DXPaintBox1.DIB.DoEmboss;
  DXPaintBox1.Repaint;
end;
end.

內建卷積核(近似)


-2 -1  0
-1  1  1
 0  1  2

等效於 Draw3x3Matrix(dib, msEmboss),但無需指定矩陣。

視覺效果

  • 邊緣轉為高光(亮)與陰影(暗)
  • 平坦區域變為中性灰(RGB≈128)
  • 產生立體浮雕感,類似石雕、金屬壓紋
  • 色彩資訊大幅減少,主要保留亮度邊緣

應用場景

  • **UI 按鈕/面板**:立體邊框、按下狀態
  • **材質預處理**:法線貼圖生成前置
  • **藝術風格化**:石雕、浮雕、錢幣壓紋
  • **邊緣增強**:OCR/識別前強化輪廓
  • **地圖地形**:高程圖轉視覺立體感

進階:可控光源方向浮雕


procedure EmbossAngle(DIB: TDIB; AngleDeg: Real; Height: Single = 1.0);
var Kernel: array[0..2, 0..2] of Integer; cosA, sinA: Double; dx, dy: Integer;
begin
  cosA := Cos(AngleDeg * Pi / 180); sinA := Sin(AngleDeg * Pi / 180);
  for dy := -1 to 1 do
    for dx := -1 to 1 do begin
      var proj := Round((dx * cosA + dy * sinA) * Height * 2);
      Kernel[dy+1, dx+1] := proj;
    end;
  Kernel[1, 1] := 1; // 中心權重
  DIB.Draw3x3Matrix(DIB, Kernel);
end;
// 用法:EmbossAngle(DIB, 45, 1.5); // 右上光源、高度 1.5x

與 Draw3x3Matrix 差異

特性DoEmbossDraw3x3Matrix(msEmboss)
參數無需指定矩陣常數
靈活性固定左上光源可用 msHardEmboss 等變體
程式碼量最少略多
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved