Problem with TLazWriterTiff lazarus / 31-LCL ---------------------------------------------------------------------- Martin Lowry · 2018-07-17 11:47 Hi All, This issue must have been reported before but I can't find a solution.  I think I've discovered a bug in this component which occurs when trying to overwrite an image previously written by TLazWriterTiff. Here's the relevant code: (intfgraphics.pas 54524 2017-04-05 08:34:48Z) with comments. procedure TLazWriterTiff.InternalWrite(Stream: TStream; Img: TFPCustomImage); var   S: String; begin   AddImage(Img);                                     // Adds all entries from the existing Img and sorts the Tag entry list   //add additional elements   S := Img.Extra[LazTiffHostComputer];     // Add more entries - now out of sequence   if S <> '' then AddEntryString(316, S);   S := Img.Extra[LazTiffMake];   if S <> '' then AddEntryString(271, S);   S := Img.Extra[LazTiffModel];   if S <> '' then AddEntryString(272, S);   S := Img.Extra[LazTiffSoftware];   if S <> '' then AddEntryString(305, S);   SaveToStream(Stream);                          // Save the image WITHOUT re-sorting the Tag entries                                                                // Next time a TSavePictureDialog is executed and the file selected TLazReaderTiff complains                                                                // of out of sequence tags. end; I'm using Lazarus 1.8.0 but couldn't see any patches in the merged change list for 1.8.2 or 1.8.4.  Hopefully somebody can recommend a workaround or get a bug reported. Cheers ---------------------------------------------------------------------- wp · 2018-07-17 13:13 I cannot reproduce the issue. What exactly are you doing? Could you post a little demo showing the issue (including tiff image). In my opinion, these "additional elements" should not be there at all - they already have been added in the line before by calling "AddImage": 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 TFPWriterTiff.AddImage(Img: TFPCustomImage);  // from "fpwritetiff" in "fpc/packages/fcl-image/src"begin  ...    if IFD.DocumentName<>'' then      AddEntryString(269,IFD.DocumentName);    if IFD.ImageDescription<>'' then      AddEntryString(270,IFD.ImageDescription);    if IFD.Make_ScannerManufacturer<>'' then      AddEntryString(271,IFD.Make_ScannerManufacturer);    if IFD.Model_Scanner<>'' then      AddEntryString(272,IFD.Model_Scanner);    if IFD.Software<>'' then    AddEntryString(305,IFD.Software);    if IFD.DateAndTime<>'' then    AddEntryString(306,IFD.DateAndTime);    if IFD.Artist<>'' then      AddEntryString(315,IFD.Artist);    if IFD.HostComputer<>'' then      AddEntryString(316,IFD.HostComputer);  ...   Could you comment the lines between "// add additional elements" and "SaveToStream" in the intfgraphics unit and check if the error goes away. ---------------------------------------------------------------------- Martin Lowry · 2018-07-17 14:19 Hi wp, It seems the additional Tag entries are there to overwrite the ones in the existing image.  I've checked the Tag order on images written by TLazWriterTiff using exiftool and they are out of sequence.  To demonstrate that I've created a tiny demo for you (attached). Run the demo in the IDE in debug mode, load the attached JPEG file with File->Open and save the image with File->Save As... accepting the tiff file extension.  Now try and save the image again by overwriting the same file. Bingo, TFPReaderTiff complains. Cheers, ---------------------------------------------------------------------- wp · 2018-07-17 17:18 Now I can reproduce the bug, but it is fixed when you replace the following method in IntfGraphics and recompile the IDE ("Tools" > "Build Lazarus with profile..."): 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 TLazWriterTiff.InternalWrite(Stream: TStream; Img: TFPCustomImage);begin  AddImage(Img);  SaveToStream(Stream);end; Without the duplicated tags, the TIF image loads without crashing. But you will not be happy with it - it is displayed incorrectly. If, in FpWriteTiff, procedure TFPWriterTIFF.AddImage, the default value for "AlphaBits" is set to 0 instead of 8 the image is displayed correctly: 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 TFPWriterTiff.AddImage(Img: TFPCustomImage);  ...    AlphaBits:=StrToIntDef(Img.Extra[TiffAlphaBits],0); //8);  ... But the previously written images are displayed correctly by the Windows Explorer. Therefore, I think this is not the real reason of the display issue.