gpt4 book ai didi

delphi - PNG图像旋转组件

转载 作者:行者123 更新时间:2023-12-03 15:55:10 25 4
gpt4 key购买 nike

谁能帮我找到一个可以快速旋转 PNG 图像同时保持透明度的组件或 SDK?

最佳答案

PNGImage 组件(Delphi native 组件的基础)的原始作者有一个论坛,他和其他人在其中发布了有关如何使用 PNGImage 组件执行操作的代码片段。

在论坛被关闭之前,我获取了所有代码片段的副本,并将它们放在 CodeGear 代码中心网站上。

大多数(如果不是全部)都适用于原生 PNG 图像,并且确实维护 Alpha channel 。

以下是包中包含的示例的完整列表:

Smooth rotates a PNG object
Resizes a TPNGObject using a smooth algorithm
Slice one PNG into several smaller ones
Saves an image as either a bitmap or a png.
Sample chunk descendant
Read all tEXt-Chunks and write values into a TStrings object
Display a message box with information extracted from the PNG File
Finds and cuts a block from a PNG image
This method converts the png into a jpeg object
This method converts the png into a bmp object
Overlay one PNG over another
This makes the image half transparent
Flips a png image vertically and saves back
Draws a png image over the desktop

这是链接:CodeCentral PNG Methods

编辑

这是 RotatePNG 函数的高度优化版本。从技术上讲,它仍然是逐像素进行的,但它使用了多种技术,例如扫描线、指针数学和图像尺寸存储,以实现相当大的速度提升。

我使用了 2550x3300 像素图像(~5MB)进行测试,并通过使用半粗糙但完全(非)科学的计算(在我的脑海中计算)我得出了以下指标:

旧例程(如上所述):~7秒
新例程(代码如下):~1.5秒

我不能严格相信这一点。代码最初来自EFG's website我决定尝试将其中一个例程转换为旋转 PNG 图像而不是位图。

我确信任何对此类事情了解更多的人都会查看代码并提供一些建议来调整速度。

procedure RotatePNG(
const PNGOriginal:TPNGImage;//input PNG
out PNGRotated:TPNGImage; //output PNG
Const Angle : double);
{
(c) har*GIS L.L.C., 1999
You are free to use this in any way, but please retain this comment block.
Please email questions to jim@har-gis.com .
Doc & Updates: http://www.efg2.com/Lab/ImageProcessing/RotateScanline.htm
and http://www.efg2.com/Lab/Library/Delphi/Graphics/JimHargis_RotateBitMap.zip
}
{
Modified by R.J.Mills, 2012 -
- Use pointer arithmetic instead of type sub-scripting for faster pixels.
- Converted to rotate PNG images June 2012.
}

Type
TRGBTripleArray = array [0..32767] of TRGBTriple; //allow integer subscript
pRGBTripleArray = ^TRGBTripleArray;

VAR
wPng : TPngImage;
theta:Double; // rotn angle in radians counterclockwise in windows
cosTheta : Single; {in windows}
sinTheta : Single;
i : INTEGER;
iOriginal : INTEGER;
iPrime : INTEGER;
j : INTEGER;
jOriginal : INTEGER;
jPrime : INTEGER;
NewWidth,NewHeight:INTEGER;
nBytes: Integer;//no. bytes per pixelformat
Oht,Owi,Rht,Rwi: Integer;//Original and Rotated subscripts to bottom/right
RowSizeRot : integer;
RowSizeOrg : integer;
AlphaSizeRot : integer;
AlphaSizeOrg : integer;
RowStartPtr : Pointer;
AlphaStartPtr : Pointer;

RowRotatedT: pRGBtripleArray; //3 bytes
AlphaRowT: pByteArray; //1 byte
AlphaRotatedT : pByteArray; //1 byte

TransparentT: TRGBTriple;

{=======================================}
function Mod360( const angle:Double ):Double;
begin
result := frac( angle/360 )*360;
if result < 0 then
result := result+360;
end;
{=======================================}

begin
Theta := -(2*pi* Mod360(angle))/360;
sinTheta := SIN( theta );
cosTheta := COS( theta );

NewWidth := ABS( ROUND( PNGOriginal.Height*sinTheta) ) + ABS( ROUND( PNGOriginal.Width*cosTheta ) );
NewHeight := ABS( ROUND( PNGOriginal.Width*sinTheta ) ) + ABS( ROUND( PNGOriginal.Height*cosTheta) );

if ( ABS(theta)*MAX( PNGOriginal.width,PNGOriginal.height ) ) > 1 then
begin//non-zero rotation

wPng := TPngImage.createblank(PNGOriginal.Header.ColorType, 8, NewWidth, NewHeight);
try

//local constants for loop, each was hit at least width*height times 1/8/00
Rwi := NewWidth - 1; //right column index
Rht := NewHeight - 1;//bottom row index
Owi := PNGOriginal.Width - 1; //transp color column index
Oht := PNGOriginal.Height - 1; //transp color row index

RowRotatedT := wPng.Scanline[ Rht ] ;

RowStartPtr := PNGOriginal.Scanline[ 0 ];
RowSizeRot := Integer(wPng.ScanLine[1])-Integer(wPng.ScanLine[0]);
RowSizeOrg := Integer(PNGOriginal.ScanLine[1])-Integer(PNGOriginal.ScanLine[0]);

TransparentT := pRGBtripleArray( PNGOriginal.Scanline[ Oht ] )[0];

if PNGOriginal.Header.ColorType in [COLOR_RGBALPHA] then
begin
AlphaRowT := PNGOriginal.AlphaScanline[ Oht ];
AlphaStartPtr := PNGOriginal.AlphaScanline[ 0 ];
AlphaRotatedT := wPng.AlphaScanline[ Rht ];
AlphaSizeRot := Integer(wPng.AlphaScanline[1])-Integer(wPng.AlphaScanline[0]);
AlphaSizeOrg := Integer(PNGOriginal.AlphaScanline[1])-Integer(PNGOriginal.AlphaScanline[0]);
end
else
begin
AlphaRowT := nil;
AlphaStartPtr := nil;
AlphaRotatedT := nil;
AlphaSizeRot := 0;
AlphaSizeOrg := 0;
end;

for j := Rht downto 0 DO //1/8/00
begin //for j
jPrime := 2*j - NewHeight + 1 ;
for i := Rwi downto 0 DO //1/8/00
begin //for i
iPrime := 2*i - NewWidth + 1;
iOriginal := ( ROUND( iPrime*CosTheta - jPrime*sinTheta ) + Owi ) div 2;

if ( iOriginal >= 0 ) and ( iOriginal <= Owi ) then
begin //inside
jOriginal := ( ROUND( iPrime*sinTheta + jPrime*cosTheta ) + Oht ) div 2 ;
if ( jOriginal >= 0 ) and ( jOriginal <= Oht ) then
begin //1/8/00
RowRotatedT[i] := pRGBTripleArray(Integer(RowStartPtr) + (jOriginal * RowSizeOrg))[iOriginal];
if assigned(AlphaRotatedT) then
AlphaRotatedT[i] := pByteArray(Integer(AlphaStartPtr) + (jOriginal * AlphaSizeOrg))[iOriginal];
end
else
begin //set Transparent
if Assigned(AlphaRotatedT) then
AlphaRotatedT[i] := 0;
RowRotatedT[i] := TransparentT;
end;
end //inside
else
begin //Set Transpaarent;
if Assigned(AlphaRotatedT) then
AlphaRotatedT[i] := 0;
RowRotatedT[i] := TransparentT;
end;
end; //for i
Dec(Integer(RowRotatedT), RowSizeRot) ;
if assigned(AlphaRotatedT) then
Dec(Integer(AlphaRotatedT), AlphaSizeRot) ;
end;//for j
PNGRotated.Assign(wPng);
finally
wPng.Free;
end;
end //non-zero rotation
else
begin //Zero rotation
if PNGRotated <> PNGOriginal then
PNGRotated.Assign(PNGOriginal);
end;
end; {RotatePNG}

关于delphi - PNG图像旋转组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8338394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com