Recent

Author Topic: Problem with TLazWriterTiff  (Read 2940 times)

Martin Lowry

  • New Member
  • *
  • Posts: 17
Problem with TLazWriterTiff
« on: July 17, 2018, 01:47:08 pm »
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

  • Hero Member
  • *****
  • Posts: 11853
Re: Problem with TLazWriterTiff
« Reply #1 on: July 17, 2018, 03:13:17 pm »
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][+][-]
  1. procedure TFPWriterTiff.AddImage(Img: TFPCustomImage);  // from "fpwritetiff" in "fpc/packages/fcl-image/src"
  2. begin
  3.   ...
  4.     if IFD.DocumentName<>'' then
  5.       AddEntryString(269,IFD.DocumentName);
  6.     if IFD.ImageDescription<>'' then
  7.       AddEntryString(270,IFD.ImageDescription);
  8.     if IFD.Make_ScannerManufacturer<>'' then
  9.       AddEntryString(271,IFD.Make_ScannerManufacturer);
  10.     if IFD.Model_Scanner<>'' then
  11.       AddEntryString(272,IFD.Model_Scanner);
  12.     if IFD.Software<>'' then
  13.       AddEntryString(305,IFD.Software);
  14.     if IFD.DateAndTime<>'' then
  15.       AddEntryString(306,IFD.DateAndTime);
  16.     if IFD.Artist<>'' then
  17.       AddEntryString(315,IFD.Artist);
  18.     if IFD.HostComputer<>'' then
  19.       AddEntryString(316,IFD.HostComputer);
  20.   ...
 

Could you comment the lines between "// add additional elements" and "SaveToStream" in the intfgraphics unit and check if the error goes away.
« Last Edit: July 17, 2018, 03:27:58 pm by wp »

Martin Lowry

  • New Member
  • *
  • Posts: 17
Re: Problem with TLazWriterTiff
« Reply #2 on: July 17, 2018, 04:19:39 pm »
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

  • Hero Member
  • *****
  • Posts: 11853
Re: Problem with TLazWriterTiff
« Reply #3 on: July 17, 2018, 07:18:59 pm »
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][+][-]
  1. procedure TLazWriterTiff.InternalWrite(Stream: TStream; Img: TFPCustomImage);
  2. begin
  3.   AddImage(Img);
  4.   SaveToStream(Stream);
  5. 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][+][-]
  1. procedure TFPWriterTiff.AddImage(Img: TFPCustomImage);
  2.   ...
  3.     AlphaBits:=StrToIntDef(Img.Extra[TiffAlphaBits],0); //8);
  4.   ...

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.

 

TinyPortal © 2005-2018