Problem to load PNG Image from file lazarus / 39-Graphics ---------------------------------------------------------------------- wyfinger · 2019-01-03 12:22 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Png := TPortableNetworkGraphic.Create;Png.LoadFromFile(PngFileName);//Error when load image: CRC check failed  But it is a valid PNG image. ---------------------------------------------------------------------- lucamar · 2019-01-03 13:46 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. ---------------------------------------------------------------------- wyfinger · 2019-01-03 14:12 This is a texture map from ikea.com. My program should convert these images to BMP. IrfanView open this file correctly. ---------------------------------------------------------------------- Handoko · 2019-01-03 15:14 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.ht ml#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.ht ml#msg256719 ---------------------------------------------------------------------- wyfinger · 2019-01-03 15:37 LazPaint (based on BGRABitmap) can't open this PNG images. FCL-Image doesn't help either. ---------------------------------------------------------------------- Handoko · 2019-01-03 15:47 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 · 2019-01-03 20:32 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 · 2019-01-03 20:59 Quote from: Handoko on January 03, 2019, 04:47:43 pmThe 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. ---------------------------------------------------------------------- wp · 2019-01-03 23:53 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  ImagingTypes, Imaging; procedure ConvertImageFormat(InFile, OutFile: String);var  img: TImageData;begin  InitImage(img);  LoadImageFromFile(InFile, img);  SaveImageToFile(OutFile, img);  FreeImage(img);end; Call it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  ConvertImageFormat('000000000017.png', '000000000017.bmp');end; BTW: if you give the destination file also a png extension then the written png file will be readable by the LCL. ---------------------------------------------------------------------- wyfinger · 2019-01-04 17:14 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i data:image/ png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAIAAAD/gAIDAAAABGdBTUEAAYa gMeiWXwAAADRJREFUeJztwQENAAAAwqD3T20ON6AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAH4MdZQAAahQ8jkAAAAASUVORK5C/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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$DEFINE CheckCRC}               //Enables CRC checking// ...{Loads the chunk from a stream}function TChunk.LoadFromStream(Stream: TStream; const ChunkName: TChunkName;  Size: Integer): Boolean;var  CheckCRC: Cardinal;  {$IFDEF CheckCRC}RightCRC: Cardinal;{$ENDIF}begin//...  {Check if crc readed is valid}  {$IFDEF CheckCRC}    RightCRC := update_crc($ffffffff, @ChunkName[0], 4);    RightCRC := update_crc(RightCRC, fData, Size) xor $ffffffff;    Result := RightCRC = CheckCrc;     {Handle CRC error}    if not Result then    begin    {In case it coult not load chunk}      Owner.RaiseError(EPngInvalidCRC, EPngInvalidCRCText);      exit;    end  {$ELSE}Result := TRUE; {$ENDIF}//...// and in other methods  I propose to add a similar definition in the FCL (FPReadPNG.pp). ---------------------------------------------------------------------- wp · 2019-01-04 17:25 Quote from: wyfinger on January 04, 2019, 06:14:16 pmPNG Delphi library code contains this: [...]You're doing something here which is absolutely forbidden: NEVER POST DELPHI CODE! ---------------------------------------------------------------------- Handoko · 2019-01-04 18:08 Delphi is not open sourced. Posting Delphi code here may cause intellectual property infringement, and that is bad. 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 · 2019-01-04 18:08 Quote from: wp on January 04, 2019, 06:25:44 pmQuote from: wyfinger on January 04, 2019, 06:14:16 pmPNG 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 ---------------------------------------------------------------------- dsiders · 2019-01-04 18:12 Quote from: lucamar on January 04, 2019, 07:08:58 pmIt'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 } ---------------------------------------------------------------------- lucamar · 2019-01-04 18:20 Quote from: dsiders on January 04, 2019, 07:12:47 pmQuote from: lucamar on January 04, 2019, 07:08:58 pmIt'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. Quote2. 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. ---------------------------------------------------------------------- dsiders · 2019-01-04 18:51 Quote from: lucamar on January 04, 2019, 07:20:26 pmThe "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. Quote2. 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. Well #2 is restrictive only if Mr. Daud refuses to maintain his code. Otherwise, I have no issues with it. Not using a "vanilla" license does not make it any less free or any less open source. License purists can take over from here. ---------------------------------------------------------------------- HeavyUser · 2019-01-05 07:19 Quote from: lucamar on January 04, 2019, 07:20:26 pmQuote from: dsiders on January 04, 2019, 07:12:47 pmQuote from: lucamar on January 04, 2019, 07:08:58 pmIt'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. Quote2. 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. the original component, which the embarcadero (sorry I do not remember their new name) bought, was an open source component that had only one restriction, you were not allowed to share copies of the code with out permission from the author. Since there were no copies shared only a few lines of code to make a point this, falls under fair use and no license is violated. Lets not  spin things out of proportion here. If there is any doubt contact the author or embarcadero to get the only definitive answer. ---------------------------------------------------------------------- wp · 2019-01-05 11:08 Quote from: wyfinger on January 04, 2019, 06:14:16 pmI solved my problem.How? Quote from: wyfinger on January 04, 2019, 06:14:16 pmAnd I decided to look deeper. Original image is base64 encoded string, like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i data:image/ png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAIAAAD/gAIDAAAABGdBTUEAAYa gMeiWXwAAADRJREFUeJztwQENAAAAwqD3T20ON6AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAH4MdZQAAahQ8jkAAAAASUVORK5C/4I= I don't understand what you want to say... In the meantime, I investigated myself. There is a program PNGAnalyzer (http://wolfgangfellger.de/de/product,4) which also reports the CRC issue and is able to fix it - afterwards the file is readable by standard Lazarus code. I temporarily bypassed the crc check in the fpc png reader (fpreadpng.pp), and this made the original file being readable immediately. So, should fpreadpng be extended to bypass the crc check? (I think now that it is this what you wanted to say (in an non-understandable way) by citing Delphi code). I don't think we should. The crc check is the sanity check of the file. If it fails there is some chance that it has been modified for example to inject buffer overflow etc. I uploaded the file to VirusTotal, it seems to be clean. But it could have been an attack. In my opinion, all the programs which had opened the file without any notice have a security risk regarding manipulated png files. ---------------------------------------------------------------------- wyfinger · 2019-01-05 12:00 Yes, I suggested adding the conditional compilation directive {$DEFINE CheckCRC} so that you can easily disable CRC checking. It is strange that, according to the specification, it is required to check the CRC of the blocks, but in fact a large number of applications do not.