View Issue Details

IDProjectCategoryView StatusLast Update
0019209FPCRTLpublic2014-11-22 18:10
ReporterFrederickAssigned ToMichael Van Canneyt 
PrioritynormalSeveritymajorReproducibilityalways
Status closedResolutionfixed 
PlatformMacintoshOSOS XOS Version10.6.6
Product VersionProduct Build2.4.3 
Target Version2.6.1Fixed in Version3.0.0 
Summary0019209: TFPReaderPNG can't read some Photoshop 16 bit pngs
DescriptionTFPReaderPNG scrambles some 16 bit pngs created in Photoshop, which display fine in browsers and other applications.


lazarus-0.9.31.29986-20110323-i386-macosx
Steps To ReproduceUse TFPReaderPNG to open attached image. Example Lazarus program attached.
Tagsfcl-image
Fixed in Revision22141
FPCOldBugId
FPCTarget
Attached Files
  • PNG_Test.zip (247,534 bytes)
  • bug19209.pp (1,444 bytes)
    program bug19209; 
    
    uses fpimage,fpcanvas,classes,fpimgcanv,fpreadpng,fpwritepng;
    
    const suffix : array[boolean] of string = ('8','16');
    const canvassuffix : array[boolean] of string = ('nocnv','cnv');
    
    procedure 
    dotest(dowordsize:boolean;havecanvas:boolean);
    var
      cnvs: TFPcustomCanvas;
      image: TFPCustomImage;
      writer: TFPCustomImageWriter;
      reader: TFPCustomImageReader;
    begin
      {
        Create the image size, this can be any size.
        The reader will rescale it to the correct size
      }
      image := TFPMemoryImage.Create (256, 256);
      if havecanvas then
        cnvs := TFPImageCanvas.Create (image);
      writer := TFPWriterPNG.Create;
      reader := TFPReaderPNG.Create;
    
      { Load it, this might take a while }
      image.LoadFromFile ('Fig03_sm_16b.png', reader);
    
      { Draw a circle }
      if havecanvas then
        cnvs.Ellipse (10,10, 90,90);
    
      { Setup PNG Write Config }
      with TFPWriterPNG(writer) do
      begin
        Grayscale := false;
        Indexed := false;
        WordSized := dowordsize;
        UseAlpha := true;
      end;
    
      { Save to file }
        image.SaveToFile ('DrawTest'+suffix[dowordsize]+'-'+canvassuffix[havecanvas]+'.png', writer);
    
      { Clean up! }
      if havecanvas then
        cnvs.Free;
      image.Free;
      writer.Free;
    end;
     
    begin
      writeln('note this program takes a few seconds, don''t panic!');
      dotest(false,true); // original
      dotest(true,false); // 16-bit, no canvas. (only load/save)
    end.
    
    
    bug19209.pp (1,444 bytes)
  • 16bit.patch (1,130 bytes)
    Index: packages/fcl-image/src/fpreadpng.pp
    ===================================================================
    --- packages/fcl-image/src/fpreadpng.pp	(revision 22119)
    +++ packages/fcl-image/src/fpreadpng.pp	(working copy)
    @@ -404,22 +404,21 @@
     var cd : longword;
         r : word;
         b : byte;
    +    tmp : pbytearray;
     begin
       if UsingBitGroup = 0 then
         begin
         Databytes := 0;
         if Header.BitDepth = 16 then
           begin
    -      r := 1;
    -      while (r < ByteWidth) do
    -        begin
    -        b := FCurrentLine^[Dataindex+r];
    -        FCurrentLine^[Dataindex+r] := FCurrentLine^[Dataindex+r-1];
    -        FCurrentLine^[Dataindex+r-1] := b;
    -        inc (r,2);
    -        end;
    -      end;
    -    move (FCurrentLine^[DataIndex], Databytes, bytewidth);
    +       getmem(tmp, bytewidth);
    +       fillchar(tmp^, bytewidth, 0);
    +       for r:=0 to bytewidth-2 do
    +        tmp^[r+1]:=FCurrentLine^[Dataindex+r];
    +       move (tmp^[0], Databytes, bytewidth);
    +       freemem(tmp);
    +      end
    +    else move (FCurrentLine^[DataIndex], Databytes, bytewidth);
         {$IFDEF ENDIAN_BIG}
         Databytes:=swap(Databytes);
         {$ENDIF}
    
    16bit.patch (1,130 bytes)

Relationships

has duplicate 0020361 resolvedJuha Manninen Lazarus Some PNG images don't work with TImage. 
related to 0026538 closedMichael Van Canneyt FPC 16bit pngreader fix 

Activities

2011-04-21 16:18

 

PNG_Test.zip (247,534 bytes)

Marco van de Voort

2011-05-22 17:39

manager   ~0048501

Last edited: 2011-05-22 17:42

I've looked inside the PNG and it has the following chunks

0 IHDR
1 pHYs
2 iCCP
3 gAMA
4 cHRM
5 IDAT
6 IEND

fpreadpng doesn't know 1..4, but pHYs (1) is afaik a rendering hint (wrt non square pixels) only. 2..4 are all color space related.

pHYs: http://www.libpng.org/pub/png/book/chapter11.html#png.ch11.div.8
iCCP..cHRM: http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.iCCP

Note that this doesn't mean this (unknown CIE chunks) is the primary problem. The file also grows despite not copying these chunks, but that could of course be different compression settings.

(Vista's) Paint won't open these files (input or output), but irfanview and the (vista) fotoviewer do display them

I also attach my reduced test program.

2011-05-22 17:39

 

bug19209.pp (1,444 bytes)
program bug19209; 

uses fpimage,fpcanvas,classes,fpimgcanv,fpreadpng,fpwritepng;

const suffix : array[boolean] of string = ('8','16');
const canvassuffix : array[boolean] of string = ('nocnv','cnv');

procedure 
dotest(dowordsize:boolean;havecanvas:boolean);
var
  cnvs: TFPcustomCanvas;
  image: TFPCustomImage;
  writer: TFPCustomImageWriter;
  reader: TFPCustomImageReader;
begin
  {
    Create the image size, this can be any size.
    The reader will rescale it to the correct size
  }
  image := TFPMemoryImage.Create (256, 256);
  if havecanvas then
    cnvs := TFPImageCanvas.Create (image);
  writer := TFPWriterPNG.Create;
  reader := TFPReaderPNG.Create;

  { Load it, this might take a while }
  image.LoadFromFile ('Fig03_sm_16b.png', reader);

  { Draw a circle }
  if havecanvas then
    cnvs.Ellipse (10,10, 90,90);

  { Setup PNG Write Config }
  with TFPWriterPNG(writer) do
  begin
    Grayscale := false;
    Indexed := false;
    WordSized := dowordsize;
    UseAlpha := true;
  end;

  { Save to file }
    image.SaveToFile ('DrawTest'+suffix[dowordsize]+'-'+canvassuffix[havecanvas]+'.png', writer);

  { Clean up! }
  if havecanvas then
    cnvs.Free;
  image.Free;
  writer.Free;
end;
 
begin
  writeln('note this program takes a few seconds, don''t panic!');
  dotest(false,true); // original
  dotest(true,false); // 16-bit, no canvas. (only load/save)
end.

bug19209.pp (1,444 bytes)

2012-08-18 20:23

 

16bit.patch (1,130 bytes)
Index: packages/fcl-image/src/fpreadpng.pp
===================================================================
--- packages/fcl-image/src/fpreadpng.pp	(revision 22119)
+++ packages/fcl-image/src/fpreadpng.pp	(working copy)
@@ -404,22 +404,21 @@
 var cd : longword;
     r : word;
     b : byte;
+    tmp : pbytearray;
 begin
   if UsingBitGroup = 0 then
     begin
     Databytes := 0;
     if Header.BitDepth = 16 then
       begin
-      r := 1;
-      while (r < ByteWidth) do
-        begin
-        b := FCurrentLine^[Dataindex+r];
-        FCurrentLine^[Dataindex+r] := FCurrentLine^[Dataindex+r-1];
-        FCurrentLine^[Dataindex+r-1] := b;
-        inc (r,2);
-        end;
-      end;
-    move (FCurrentLine^[DataIndex], Databytes, bytewidth);
+       getmem(tmp, bytewidth);
+       fillchar(tmp^, bytewidth, 0);
+       for r:=0 to bytewidth-2 do
+        tmp^[r+1]:=FCurrentLine^[Dataindex+r];
+       move (tmp^[0], Databytes, bytewidth);
+       freemem(tmp);
+      end
+    else move (FCurrentLine^[DataIndex], Databytes, bytewidth);
     {$IFDEF ENDIAN_BIG}
     Databytes:=swap(Databytes);
     {$ENDIF}
16bit.patch (1,130 bytes)

ocean

2012-08-18 20:24

reporter   ~0061704

Attached patch fixes 16bit loading here Win32, don't ask why, it's like magic :) Other bug had some different bytewidth pngs attached, they work too

Michael Van Canneyt

2012-08-20 00:25

administrator   ~0061738

Applied the patch. Thank you very much.

Frederick

2012-09-24 14:26

reporter   ~0062599

Thanks!

Issue History

Date Modified Username Field Change
2011-04-21 16:18 Frederick New Issue
2011-04-21 16:18 Frederick File Added: PNG_Test.zip
2011-05-02 21:30 Michael Van Canneyt Status new => assigned
2011-05-02 21:30 Michael Van Canneyt Assigned To => Michael Van Canneyt
2011-05-22 17:39 Marco van de Voort Note Added: 0048501
2011-05-22 17:39 Marco van de Voort File Added: bug19209.pp
2011-05-22 17:40 Marco van de Voort Note Edited: 0048501
2011-05-22 17:42 Marco van de Voort Note Edited: 0048501
2011-12-29 16:01 Marco van de Voort Relationship added has duplicate 0020361
2012-06-02 00:17 Marco van de Voort Tag Attached: fcl-image
2012-08-18 20:23 ocean File Added: 16bit.patch
2012-08-18 20:24 ocean Note Added: 0061704
2012-08-20 00:25 Michael Van Canneyt Fixed in Revision => 22141
2012-08-20 00:25 Michael Van Canneyt Status assigned => resolved
2012-08-20 00:25 Michael Van Canneyt Fixed in Version => 2.7.1
2012-08-20 00:25 Michael Van Canneyt Resolution open => fixed
2012-08-20 00:25 Michael Van Canneyt Note Added: 0061738
2012-08-20 00:25 Michael Van Canneyt Target Version => 2.6.1
2012-09-24 14:26 Frederick Status resolved => closed
2012-09-24 14:26 Frederick Note Added: 0062599
2014-11-22 18:10 Marco van de Voort Relationship added related to 0026538