Lazarus

Programming => Graphics => Graphics and Multimedia => BGRABitmap and LazPaint => Topic started by: bills on February 17, 2019, 09:46:31 am

Title: How to load a large picture?
Post by: bills on February 17, 2019, 09:46:31 am
When i load a large picture(its size is 40.6MB, 20482*27271pix),the program show message "TBGRAWinBitmap.ReallocBitmap:Windows error 87."
The code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   bmp: TBGRABitmap;
  4. begin
  5.   bmp := TBGRABitmap.Create('big.png');
  6.   memo1.Append('ok');
  7.  
  8. end;  

How can i load a large picture? Thanks.

codeTyphon 6.6,64bit;
FPC version:3.3.1;
bgraBitmap 6.6.2;
windows 8.1 64bit;
memory:8G;

Title: Re: How to load a large picture?
Post by: lucamar on February 17, 2019, 01:04:31 pm
I don't know about the error but take into account that the bitmap is not 40.6 MiB but 2130,75 MiB (20482*27271*4), more than 2 GiB. Of course, if your program is 64 bit and you have 8GiB physical RAM that's not so much ...
Title: Re: How to load a large picture?
Post by: LemonParty on February 17, 2019, 01:18:15 pm
It is not always possible to get a solid chunk of memory (2 Gb) on the system with 8 Gb.
You can probably do what you want with 16 Gb of memory.
Or you need some special component to handle with such a huge images.
Title: Re: How to load a large picture?
Post by: Cyrax on February 17, 2019, 02:26:18 pm
You probably need to research on memory mapped files so you can process huge files easily.

https://docs.microsoft.com/en-gb/windows/desktop/Memory/file-mapping
Title: Re: How to load a large picture?
Post by: lucamar on February 17, 2019, 02:38:38 pm
You probably need to research on memory mapped files so you can process huge files easily.

https://docs.microsoft.com/en-gb/windows/desktop/Memory/file-mapping

The problem is not really one of huge files: after all the file is only a meagre ~40 MiB. The problem is that, once decoded, it uses more than 2GiB of RAM--although that isn't so much either, by today's standards.
Title: Re: How to load a large picture?
Post by: circular on February 18, 2019, 06:06:46 pm
Bill, do you need 32bits per pixel? Otherwise you could try another container for the image.
Title: Re: How to load a large picture?
Post by: bills on February 20, 2019, 08:28:15 am
circular´╝îi dont need 32bits per pixel. i only care about the resolution of the picture. photoshop can open this picture,but my program cant. Iif there is a control which can load this big resolution picture?
Title: Re: How to load a large picture?
Post by: PascalDragon on February 20, 2019, 09:30:45 am
It is not always possible to get a solid chunk of memory (2 Gb) on the system with 8 Gb.
That by itself should not be a problem as every 64-bit process on a 64-bit OS has an address space with an address length of 2^48 Bit (addresses are size extended to 64 bit, so there is a "gap" in the middle of the virtual address space). So when an application allocates 2 GB the OS will try to use the physical RAM and the swap space to satisfy this whereby the linear area in virtual address space might be rather fragmented in the physical RAM and swap space. Only if there isn't enough physical memory plus swap space the allocation will fail.
Title: Re: How to load a large picture?
Post by: circular on February 21, 2019, 11:19:36 pm
circular´╝îi dont need 32bits per pixel. i only care about the resolution of the picture. photoshop can open this picture,but my program cant. Iif there is a control which can load this big resolution picture?
Are you saying you don't care at all about the content of the image? If that's the case you can use TBGRAReadPng from BGRABitmap and call GetQuickInfo function on the stream.
Title: Re: How to load a large picture?
Post by: wp on February 22, 2019, 12:15:13 am
i only care about the resolution of the picture.
You can also use the unit fpsimages.pas of fpspreadsheet and call the following function "GetImageInfo" which reads only the file headers

Code: Pascal  [Select][+][-]
  1. var
  2.   itPNG: TsImageType;
  3.   itJPEG: TsImageType;
  4.   itTIFF: TsImageType;
  5.   itBMP: TsImageType;
  6.   itGIF: TsImageType;
  7.   itWMF: TsImageType;
  8.   itEMF: TsImageType;
  9.   itPCX: TsImageType;
  10.  
  11. function GetImageInfo(AStream: TStream; out AWidth, AHeight: DWord;
  12.   out dpiX, dpiY: Double; AImageType: TsImageType = itUnknown): TsImageType;

The attached version of the unit is self-contained and you can use it independently of fpspreadsheet.

Example of usage:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   stream: TFileStream;
  4.   imgType: TsImageType;
  5.   w, h: DWord;
  6.   dpix, dpiy: double;
  7. begin
  8.   stream := TFileStream.Create('C:\lazarus\images\splash_source\cheetah.jpg', fmOpenRead);
  9.   try
  10.     imgType := GetImageInfo(stream, w, h, dpix, dpiy);
  11.     if imgType <> itUnknown then
  12.       Label1.Caption := Format('Width = %d, Height = %d, xdpi = %.0f, ydpi = %.0f', [w, h, dpix, dpiy])
  13.     else
  14.       Label1.Caption := 'Unknown format';
  15.   finally
  16.     stream.Free;
  17.   end;
  18. end;
Title: Re: How to load a large picture?
Post by: bills on March 02, 2019, 11:37:50 am
Thank you,circular and wp,your advice can get the width and height of the picture.
Do you know how to load the big picture into a control,such as a panel?
Title: Re: How to load a large picture?
Post by: wp on March 02, 2019, 11:51:08 am
But then you are doing what you wanted to avoid, namely load the entire file.

Does the file contain a small thumbnail preview image in addition to the main image? You can extract that by using the fpexif library: https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fpexif/
Title: Re: How to load a large picture?
Post by: circular on March 02, 2019, 01:49:10 pm
What is the bit depth of the image?
Title: Re: How to load a large picture?
Post by: bills on March 13, 2019, 02:18:17 pm
THe png image is 24 bit depth.
Perhaps the main problem is my memory is too small. I tested several controls, they all collapse.
TinyPortal © 2005-2018