Recent

Author Topic: Problem to load PNG Image from file  (Read 11876 times)

wyfinger

  • Newbie
  • Posts: 5
Problem to load PNG Image from file
« on: January 03, 2019, 01:22:49 pm »
Code: Pascal  [Select][+][-]
  1. Png := TPortableNetworkGraphic.Create;
  2. Png.LoadFromFile(PngFileName);
  3. //
  4. Error when load image: CRC check failed
  5.  

But it is a valid PNG image.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Problem to load PNG Image from file
« Reply #1 on: January 03, 2019, 02:46:32 pm »
It seems it maddens also the forum's code? Something wrong with it but maybe not terribly worng, so some programs accept it and others don't.

Try opening it in a well-behaved editor and saving it again.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wyfinger

  • Newbie
  • Posts: 5
Re: Problem to load PNG Image from file
« Reply #2 on: January 03, 2019, 03:12:06 pm »
This is a texture map from ikea.com.
My program should convert these images to BMP.
IrfanView open this file correctly.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Problem to load PNG Image from file
« Reply #3 on: January 03, 2019, 04:14:13 pm »
I ever had similar issue for glSlideshow project, which I submitted for Graphics Contest 2017:
http://forum.lazarus.freepascal.org/index.php/topic,35313.msg255275.html#msg255275

Not sure was it a bug in the internal graphics library used by Lazarus or maybe the png file was saved in a non-standard format.

Someone suggested me to use BGRABitmap but I ended with FCL-Image, which is bundled with Lazarus installation:
http://wiki.freepascal.org/fcl-image

For anyone interested, you can grab the source code of glSlideshow 0.95. It uses the OpenGL old-school mode, which is easier to understand for newbies to learn OpenGL:
http://forum.lazarus.freepascal.org/index.php/topic,35313.msg256719.html#msg256719

wyfinger

  • Newbie
  • Posts: 5
Re: Problem to load PNG Image from file
« Reply #4 on: January 03, 2019, 04:37:53 pm »
LazPaint (based on BGRABitmap) can't open this PNG images.
FCL-Image doesn't help either.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Problem to load PNG Image from file
« Reply #5 on: January 03, 2019, 04:47:43 pm »
The image 000000000017.png really is corrupted. Although GIMP can open it without problem but Krita and Eye of MATE (Ubuntu Mate default image viewer) failed to open it.

Below is the screenshot of the error opening the image using Krita 4.15 and Eye of Mate:

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Problem to load PNG Image from file
« Reply #6 on: January 03, 2019, 09:32:10 pm »
If you use the VampyreImaging library you can load that particular png image. Get the most recent version from the author's github: https://github.com/galfar/imaginglib or, if you don't have git, you can also load a zipped snapshot from https://sourceforge.net/p/imaginglib/code/ci/default/tree/. (Or you can try the version distributed by the OnlinePackageManager (pl_vampyreimaging) which maybe is not the latest version but this probably does not affect png reading.) Load the package vampyreimagingpackage in folder Extras/IDEPackages/Lazarus and compile it, no need to install. Load and compile also the package vampyireimagingpackage_ext - this is not absolutely needed for png, but with it you can compile the demo LCLImager in folder Demos/ObjectPascal from which you can learn how to use the library. There is also a large documentation at http://galfar.vevb.net/imaging/doc/imaging.html.

If you don't get around with vampyre post again, I'll probably find a working example which is simpler than the LCLImager.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Problem to load PNG Image from file
« Reply #7 on: January 03, 2019, 09:59:10 pm »
The image 000000000017.png really is corrupted. Although GIMP can open it without problem but Krita and Eye of MATE (Ubuntu Mate default image viewer) failed to open it.

Yep. In Gnome land both Eye of Gnome and Shotwell fail to load the image, while GIMP copes with it w/out (apparent) problems. It's posible that GIMP (and probably other software) simply shows the image as decoded, disregarding the CRC error as a "minor" corruption.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Problem to load PNG Image from file
« Reply #8 on: January 04, 2019, 12:53:56 am »
Here is code which converts image formats by using the VampyreImaging library (you must add the vampyreimagingpackage to the requirements of your project). As noted above, it is able to load the "corrupted" (which I don't believe) png image:

Code: Pascal  [Select][+][-]
  1. uses
  2.   ImagingTypes, Imaging;
  3.  
  4. procedure ConvertImageFormat(InFile, OutFile: String);
  5. var
  6.   img: TImageData;
  7. begin
  8.   InitImage(img);
  9.   LoadImageFromFile(InFile, img);
  10.   SaveImageToFile(OutFile, img);
  11.   FreeImage(img);
  12. end;

Call it like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   ConvertImageFormat('000000000017.png', '000000000017.bmp');
  4. end;

BTW: if you give the destination file also a png extension then the written png file will be readable by the LCL.
« Last Edit: January 04, 2019, 01:00:33 am by wp »

wyfinger

  • Newbie
  • Posts: 5
Re: Problem to load PNG Image from file
« Reply #9 on: January 04, 2019, 06:14:16 pm »
Thank you very much. I solved my problem.

And I decided to look deeper.

Original image is base64 encoded string, like this:
Code: Pascal  [Select][+][-]
  1. data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAIAAAD/gAIDAAAABGdBTUEAAYagMeiWXwAAADRJREFUeJztwQENAAAAwqD3T20ON6AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH4MdZQAAahQ8jkAAAAASUVORK5C/4I=

This image shown normally at Chrome (71.0.3578.10), Vivaldi (2.2.1388.34), InternetExplorer (11.0.9600).
But, Firefox (I test at 62.0 and 64.0) can't display it.

This image is a simple black square. And it can be open at:
- Adobe Photoshop 13.1.2;
- IrfanView 4.44;
- Inkscape 0.92;
- Paint.Net 4.0.17;
- MS Paint (Win7);
- MS Office 2016;
- LibreOffice (6.0.2.1);
and others.

Error at opened in:
- Lazarus;
- Delphi 7 (PNG Delphi by Gustavo Daud 1.545);
- Delphi 10.1;
- Firefox;
and others.

PNG Delphi library code contains this:
Code: Pascal  [Select][+][-]
  1. {$DEFINE CheckCRC}               //Enables CRC checking
  2. // ...
  3. {Loads the chunk from a stream}
  4. function TChunk.LoadFromStream(Stream: TStream; const ChunkName: TChunkName;
  5.   Size: Integer): Boolean;
  6. var
  7.   CheckCRC: Cardinal;
  8.   {$IFDEF CheckCRC}RightCRC: Cardinal;{$ENDIF}
  9. begin
  10. //...
  11.   {Check if crc readed is valid}
  12.   {$IFDEF CheckCRC}
  13.     RightCRC := update_crc($ffffffff, @ChunkName[0], 4);
  14.     RightCRC := update_crc(RightCRC, fData, Size) xor $ffffffff;
  15.     Result := RightCRC = CheckCrc;
  16.  
  17.     {Handle CRC error}
  18.     if not Result then
  19.     begin
  20.       {In case it coult not load chunk}
  21.       Owner.RaiseError(EPngInvalidCRC, EPngInvalidCRCText);
  22.       exit;
  23.     end
  24.   {$ELSE}Result := TRUE; {$ENDIF}
  25. //...
  26. // and in other methods
  27.  

I propose to add a similar definition in the FCL (FPReadPNG.pp).

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Problem to load PNG Image from file
« Reply #10 on: January 04, 2019, 06:25:44 pm »
PNG Delphi library code contains this: [...]
You're doing something here which is absolutely forbidden: NEVER POST DELPHI CODE!

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Problem to load PNG Image from file
« Reply #11 on: January 04, 2019, 07:08:30 pm »
Delphi is not open sourced. Posting Delphi code here may cause intellectual property infringement, and that is bad. >:D

Luckily Portable Network Graphics Delphi by Gustavo Daud is open sourced. The latest version is 1.564 (31 July 2006) and can be found here:
https://github.com/JackTrapper/pngdelphi/blob/master/PNGImage.pas

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Problem to load PNG Image from file
« Reply #12 on: January 04, 2019, 07:08:58 pm »
PNG Delphi library code contains this: [...]
You're doing something here which is absolutely forbidden: NEVER POST DELPHI CODE!

It's not from Embarcadero; it's Gustavo Daud's (mostly free/open) source code
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: Problem to load PNG Image from file
« Reply #13 on: January 04, 2019, 07:12:47 pm »
It's not from Embarcadero; it's Gustavo Daud's (mostly free/open) source code

What's not open source about it? According to the source:

Code: [Select]
{This is a full, open sourced implementation of png in Delphi }
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Problem to load PNG Image from file
« Reply #14 on: January 04, 2019, 07:20:26 pm »
It's not from Embarcadero; it's Gustavo Daud's (mostly free/open) source code

What's not open source about it? According to the source:

Code: [Select]
{This is a full, open sourced implementation of png in Delphi }

The "mostly" applied to the "free" part (but now I think about it also to the "open") and applies because the license is not a vanilla free/open source one and contains some ad-hoc restrictions, like p.e.
Quote
2. Modified files may not be distributed. [...]
4. Commercial visual graphics libraries are not allowed to use this component WITHOUT AUTHOR PRIOR AGREEMENT.
5. Credit for the author is required somewhere [...]
6. Source code may be changed if it's not redistributed.

"Free/Open Source" is about more than just letting other see the source.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018