Recent

Author Topic: Loading, resizing and display a JPEG file  (Read 2168 times)

Whqd

  • Newbie
  • Posts: 1
Loading, resizing and display a JPEG file
« on: August 22, 2019, 02:15:20 am »
Hi, I'm trying to load a large JPEG (3776x3850 4.0mb) from a TMemoryStream using this process:

Code: Pascal  [Select][+][-]
  1. var
  2.   W, H: integer;
  3.   Start, Stop: integer;
  4. begin
  5.   if (Length(Files) > 0) and (Index < FileCount) then
  6.   begin
  7.   //start := GetTickCount;
  8.     Files[Index].Position := 0;
  9.     FullBmp := TBGRABitmap.Create(Files[Index]);
  10.   //stop := GetTickCount;
  11.  
  12.    //New Size
  13.    W := PaintBox1.Width;
  14.     H := (FullBmp.Height * W) div FullBmp.Width;
  15.     if H > PaintBox1.Height then
  16.     begin
  17.       H := PaintBox1.Height;
  18.       W := (FullBmp.Width * H) div FullBmp.Height;
  19.     end;
  20.  
  21.    //if is smaller than the paintbox height
  22.     if FullBmp.Height < PaintBox1.Height then
  23.       ResizeBmp.SetSize(FullBmp.Width, FullBmp.Height)
  24.     else
  25.       ResizeBmp.SetSize(W, H);
  26.      
  27.     //Slow
  28.     FullBmp.ResampleFilter := rfBestQuality;
  29.  
  30.     //Better Quality or Fast
  31.     if filter then
  32.     BGRAReplace(ResizeBmp, FullBmp.Resample(ResizeBmp.Width, ResizeBmp.Height,
  33.       rmFineResample) as TBGRABitmap)
  34.     else
  35.     BGRAReplace(ResizeBmp, FullBmp.Resample(ResizeBmp.Width, ResizeBmp.Height,
  36.       rmSimpleStretch) as TBGRABitmap);
  37.  
  38.     ResizeBmp.Assign(FullBmp.Resample(ResizeBmp.Width, ResizeBmp.Height, rmSimpleStretch) as TBGRABitmap);
  39.    
  40.     FullBmp.Free;
  41.    
  42.     PaintBox1.Canvas.Brush.Color := clGray;
  43.     PaintBox1.Canvas.FillRect(PaintBox1.Canvas.ClipRect);
  44.     //Draw in the center
  45.     ResizeBmp.Draw(PaintBox1.Canvas, trunc((PaintBox1.Width - ResizeBmp.Width) / 2),
  46.       trunc((PaintBox1.Height - ResizeBmp.Height) / 2));
  47.     Caption := (IntToStr(stop - start));
  48.   end;
  49. end;  
  50.  
 

It loads without any problem, but is slow at the "FullBmp := TBGRABitmap.Create(Files[Index]);" it takes more than 650ms to load to the stream to the TBRABitmap in a i7-2600 3.4GHz 12GB RAM, loading the image with the resize and displaying take up to 1100ms with the rmSimpleStretch.

There is a way to make this faster? The Draw function handles de paint process instantly once de bitmap is resized and ready.

Thanks in advance and sorry for the ENGRISH ;)

circular

  • Hero Member
  • *****
  • Posts: 4220
    • Personal webpage
Re: Loading, resizing and display a JPEG file
« Reply #1 on: September 05, 2019, 09:53:06 pm »
Hi,

If lower quality is ok, you can specify [loJpegQuick] option for the AOptions parameter.

Alternatively if you want a thumbnail of a certain size you can directly call GetFileThumbnail or GetStreamThumbnail from the BGRAThumbnail unit.
Conscience is the debugger of the mind

sstvmaster

  • Sr. Member
  • ****
  • Posts: 299
Re: Loading, resizing and display a JPEG file
« Reply #2 on: September 05, 2019, 11:23:57 pm »
Hi Whqd,

Even IrfanView needs his time to load the picture. See pic.
greetings Maik

Windows 10,
- Lazarus 2.2.6 (stable) + fpc 3.2.2 (stable)
- Lazarus 2.2.7 (fixes) + fpc 3.3.1 (main/trunk)

 

TinyPortal © 2005-2018