Recent

Author Topic: How to load a large picture?  (Read 1919 times)

bills

  • New Member
  • *
  • Posts: 38
How to load a large picture?
« 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;


lucamar

  • Hero Member
  • *****
  • Posts: 2130
Re: How to load a large picture?
« Reply #1 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 ...
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

LemonParty

  • New Member
  • *
  • Posts: 28
Re: How to load a large picture?
« Reply #2 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.

Cyrax

  • Hero Member
  • *****
  • Posts: 758
Re: How to load a large picture?
« Reply #3 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

lucamar

  • Hero Member
  • *****
  • Posts: 2130
Re: How to load a large picture?
« Reply #4 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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

circular

  • Hero Member
  • *****
  • Posts: 3048
    • Personal webpage
Re: How to load a large picture?
« Reply #5 on: February 18, 2019, 06:06:46 pm »
Bill, do you need 32bits per pixel? Otherwise you could try another container for the image.
Conscience is the debugger of the mind

bills

  • New Member
  • *
  • Posts: 38
Re: How to load a large picture?
« Reply #6 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?

PascalDragon

  • Hero Member
  • *****
  • Posts: 712
  • Compiler Developer
Re: How to load a large picture?
« Reply #7 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.

circular

  • Hero Member
  • *****
  • Posts: 3048
    • Personal webpage
Re: How to load a large picture?
« Reply #8 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.
Conscience is the debugger of the mind

wp

  • Hero Member
  • *****
  • Posts: 6462
Re: How to load a large picture?
« Reply #9 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;
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

bills

  • New Member
  • *
  • Posts: 38
Re: How to load a large picture?
« Reply #10 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?

wp

  • Hero Member
  • *****
  • Posts: 6462
Re: How to load a large picture?
« Reply #11 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/
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

circular

  • Hero Member
  • *****
  • Posts: 3048
    • Personal webpage
Re: How to load a large picture?
« Reply #12 on: March 02, 2019, 01:49:10 pm »
What is the bit depth of the image?
Conscience is the debugger of the mind

bills

  • New Member
  • *
  • Posts: 38
Re: How to load a large picture?
« Reply #13 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.