#1 2020-02-17 15:15:27

domek
Member
Registered: 2019-04-09
Posts: 8

Making it usuable

So JpegDec is fast but can't handle:
- progressive JPEGs
- CMYK JPEGs
- Grayscale JPEGs

In many cases JpegDec is as fast as SynGdiPlus.TJpegImage but I noticed it's faster than SynGdiPlus on bigger images.
So I glued both things together. I used thread-safe version of JpegDec from:
http://www.marktg.com/jpegdec/

Remeber to put somewhere in your code this line:

Gdip.RegisterPictures;

Uses libraries:

JpegDec, SynGdiPlus
function LoadJPEG(Filename: String; out Bmp: TBitmap): Boolean;
var F: TFileStream;
    Mem: TMemoryStream;
    Bpp: Integer;
    Jpg: SynGdiPlus.TJpegImage;
begin
  Result := False;
  Mem := TMemoryStream.Create;

  try
    F := TFileStream.Create(Filename, fmOpenRead or fmShareExclusive);
    Mem.SetSize(F.Size);
    Mem.CopyFrom(F, F.Size);
  finally
    F.Free;
  end;

  if Mem.Size < 1 then begin
    Mem.Free;
    Exit;
  end;

  Bmp := JpegDecode(Mem.Memory, Mem.Size);
  if Bmp <> nil then begin
    Result := True;
    Mem.Free;
    Exit;
  end;

  Mem.Position := 0;
  Jpg := SynGdiPlus.TJpegImage.Create;
  try
    Jpg.LoadFromStream(Mem);
    Bmp := Jpg.ToBitmap;
  finally
    Jpg.Free;
    Result := True;    
  end;

  Mem.Free;
end;

Last edited by domek (2020-02-17 15:25:13)

Offline

#2 2020-02-17 15:53:44

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,649
Website

Re: Making it usuable

Note: current http://synopse.info/files/jpegdec.zip is thread-safe since 2017.

Offline

Board footer

Powered by FluxBB