gpt4 book ai didi

delphi - Jpeg 到 Bmp 的转换需要不合理的时间

转载 作者:行者123 更新时间:2023-12-02 09:02:25 32 4
gpt4 key购买 nike

我有这个函数,需要 4.2 秒才能将 jpg 转换为 bmp。为什么需要这么长时间?我可以做得更快吗?
IrfanView 只需一小部分时间即可加载并转换文件。

我认为大部分时间都花在 JPG.LoadFromFile 上。但当我测量时间时,我惊讶地发现它大部分时间都花在 BMP.Assing(JPG) 上。

function ConvertJPG2BMP(CONST FileName: string): TBitmap;
VAR JPG: TJpegImage;
begin
Result:= NIL;
JPG:= TJpegImage.Create;
TRY
JPG.LoadFromFile(FileName);
if (JPG.Width > 0) AND (JPG.Width < 32768)
AND (JPG.Height> 0) AND (JPG.Height < 32768) then
begin
Result:= TBitmap.Create;
TRY
Result.HandleType:= bmDIB;

// Fuji_FinePix_F550.JPG [3200x1800] [1.44MB]
Result.Assign(JPG); <--- 4 seconds!!
EXCEPT
FreeAndNil(Result);
END;
end;
FINALLY
FreeAndNil(JPG);
end;
end;

最佳答案

由于我想测试一次稍微旧的功能,所以现在是一个很好的机会。

The sources used are here

下面的代码已对这些进行了一些更改。

对OP的函数ConvertJPG2BMP()进行了一些改编的源代码(2512:ms)

enter image description here

function ConvertJPG2BMP(CONST FileName: string): TBitmap;
VAR
JPG: TJpegImage;
begin
Result:= NIL;
JPG:= TJpegImage.Create;
TRY
JPG.LoadFromFile(FileName);
if (JPG.Width > 0) AND (JPG.Width < 32768)
AND (JPG.Height> 0) AND (JPG.Height < 32768) then
begin
Result:= TBitmap.Create;
TRY
Result.PixelFormat := pf24bit;
Result.Width := JPG.Width;
Result.Height := JPG.Height;
Result.HandleType:= bmDIB;
// 2018-10-17 14.04.23.jpg [2560x1920] [1.66MB]
Result.Assign(JPG);
Result.SaveToFile('F:\ProgramFiles\Embarcadero\dtx\Projects\Bmp-DIB\JPG2BMP.bmp');
EXCEPT
FreeAndNil(Result);
END;
end;
FINALLY
FreeAndNil(JPG);
end;
end;
<小时/>

TWICImage 的来源使用情况(296:毫秒)

Vcl.Graphics? 中还有一个名为 TWICImage 的类,它处理 Microsoft Imaging Component 支持的图像

包括 BMP、GIF、ICO、JPEG、PNG、TIF 和 Windows Media Photo

<小时/>

enter image description here

procedure LoadImageFromStream(Stream: TStream; Image: TImage);
var
wic: TWICImage;
Bitmap: TBitmap;
begin
Stream.Position := 0;
wic := TWICImage.Create;
try
wic.LoadFromStream(Stream);
Image.Picture.Assign(wic);
Bitmap := TBitmap.Create;
try
Bitmap.PixelFormat := pf24bit;
Bitmap.Width := Image.Picture.Width;
Bitmap.Height := Image.Picture.Height;
Bitmap.Canvas.Draw(0, 0, Image.Picture.Graphic);
Bitmap.SaveToFile('F:\ProgramFiles\Embarcadero\dtx\Projects\Bmp-DIB\TWICImage.bmp');
finally
Bitmap.Free;
end;
finally
wic.Free;
end;
end;

procedure RenderImage(const Filename: string);
var
fs: TFileStream;
begin
fs := TFileStream.Create(Filename, fmOpenRead);
try
LoadImageFromStream(fs, Form1.Image1);
finally
fs.Free;
end;
end;

获取所有测试例程的TickCount。

procedure TForm1.Button1Click(Sender: TObject);
var
MyDIB : TBitmap;
loadStr : string;
XStart,Xend : LongWord;
begin
loadStr := 'F:\ProgramFiles\Embarcadero\dtx\Projects\Bmp-DIB\2018-10-17 14.04.23.jpg';
XStart := GetTickCount;

if RadioGroup1.ItemIndex = 0 then MyDIB := ConvertJPG2BMP(loadStr);// ConvertJPG2BMP()
if RadioGroup1.ItemIndex = 1 then TestBmp(loadStr);
if RadioGroup1.ItemIndex = 2 then RenderImage(loadStr);// TWICImage
if RadioGroup1.ItemIndex = 3 then GetOleGraphic(loadStr);

Xend := GetTickCount;
Label1.Caption := IntToStr(xEnd-XStart) + ' : MS' ;

end;

仅通过函数 GetOleGraphic() 生成的图像与文件大小相同,生成的文件较小且分辨率较差?

here the source used for the GetOleGraphic()

enter image description here

关于delphi - Jpeg 到 Bmp 的转换需要不合理的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53382075/

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