📄 unit1.pas
字号:
{ *********************************************************************** }
{ }
{ Delphi Demos - PNGImage Demo }
{ }
{ 李洲 (Jeff Lee) @ Sturd Group }
{ Website : http://islet8.delphibbs.com }
{ Email : islet8@21cn.com islet8@shareware.com.cn }
{ }
{ This program is free software; you can redistribute it and/or modify }
{ it under the terms of the GNU General Public License as published by }
{ the Free Software Foundation. }
{ }
{ *********************************************************************** }
(*******************************************************
Aug 10th, 2004 Version 1.0
------------------------------------------------------
Internal version, just for experiment
*******************************************************)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Buttons, pngimage;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Image1: TImage;
Image2: TImage;
Button3: TButton;
procedure FormPaint(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
MaxPixelCount = 32768;
type
PRGBArray = ^TRGBArray;
TRGBArray = array [0..MaxPixelCount - 1] of TRGBTriple;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormPaint(Sender: TObject);
var
Png: TPngObject;
Rect: TRect;
begin
Png := TPngObject.Create;
Png.LoadFromFile('1.png');
Rect.Left := 0;
Rect.Top := 0;
Rect.Right := Rect.Left + Png.Width;
Rect.Bottom := Rect.Top + Png.Height;
Png.Draw(Canvas, Rect);
Png.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Png: TPngObject;
Rect: TRect;
begin
Png := TPngObject.Create;
Png.LoadFromFile('2.png');
Image1.Picture.Assign(Png);
Rect.Left := 0;
Rect.Top := 0;
Rect.Right := Rect.Left + Png.Width;
Rect.Bottom := Rect.Top + Png.Height;
Png.Draw(Image2.Canvas, Rect);
Image2.Refresh;
Png.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i, j: Integer;
R, G, B, RGBTemp: Cardinal;
t1, t2: Cardinal;
Png: TPngObject;
Rect: TRect;
begin
Png := TPngObject.Create;
Png.LoadFromFile('2.png');
t1 := GetTickCount;
for i := 0 to Png.Width - 1 do
begin
for j := 0 to Png.Height - 1 do
begin
RGBTemp := Png.Pixels[i, j];
R := GetRValue(RGBTemp);
G := GetGValue(RGBTemp);
B := GetBValue(RGBTemp);
{ 计算公式:目标色R/G/B值 = 255 - (255 - 灰色R/G/B值) * (255 - 指定色R/G/B值) / (255 - 最深色R/G/B值) }
Png.Pixels[i, j] := RGB(255 - (255 - R) * (255 - 153) div 255, // 按公式计算当前像素的 R 的值
255 - (255 - G) * (255 - 0) div 255, // 计算 G 值
255 - (255 - B) * (255 - 0) div 255); // 计算 B 值
end;
end;
Rect.Left := 448;
Rect.Top := 152;
Rect.Right := Rect.Left + Png.Width;
Rect.Bottom := Rect.Top + Png.Height;
Png.Draw(Canvas, Rect);
t2 := GetTickCount - t1;
ShowMessage(IntToStr(t2));
Png.Free;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
i, j: Integer;
t1, t2: Cardinal;
Row: PRGBArray;
Png: TPngObject;
Rect: TRect;
begin
Png := TPngObject.Create;
Png.LoadFromFile('2.png');
t1 := GetTickCount;
for i := 0 to Png.Height - 1 do
begin
Row := Png.Scanline[i];
for j := 0 to Png.Width - 1 do
begin
Row[j].rgbtRed := 255 - (255 - Row[j].rgbtRed) * (255 - 153) div 255;
Row[j].rgbtGreen := 255 - (255 - Row[j].rgbtGreen) * (255 - 0) div 255;
Row[j].rgbtBlue := 255 - (255 - Row[j].rgbtBlue) * (255 - 0) div 255;
end;
end;
Rect.Left := 448;
Rect.Top := 152;
Rect.Right := Rect.Left + Png.Width;
Rect.Bottom := Rect.Top + Png.Height;
Png.Draw(Canvas, Rect);
t2 := GetTickCount - t1;
ShowMessage(IntToStr(t2));
Png.Free;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -