Recent

Author Topic: TFPWriterPNG compression ratio problem  (Read 1013 times)

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 349
TFPWriterPNG compression ratio problem
« on: September 23, 2021, 01:52:40 pm »
Hi,

I use TFPWriterPNG to save a PNG image from TFPCustomImage with max compression.

Even with clMax compression a resulting PNG in 1.3x larger than the same PNG image saved in Photoshop or another editor.

Yes, I know that TFPCustomImage works at 64-bit per pixel, and I set TFPWriterPNG.WordSized:=False to convert it to 32-bit pixel format.

My project doesn't use LCL and I can use only pure FPC code.

FPC 3.2.0 / Lazarus 2.0.10

Code: Pascal  [Select][+][-]
  1. program PngSaveTest;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$ifdef fpc}{$mode Delphi}{$endif}
  6.  
  7. uses
  8.   Classes, FPImage, FPWritePNG, FPReadPNG, ZStream;
  9.  
  10. procedure SavePNG;
  11. type TReadKlas = class of TFPCustomImageReader;
  12. var
  13.   image: TFPCustomImage;
  14.   Ireader: TFPCustomImageReader;
  15.   IWriter: TFPWriterPNG;
  16.   Reader : TReadKlas;
  17. begin
  18.   Image := TFPMemoryImage.Create(8, 8);
  19.   reader:=TFPReaderPNG;
  20.   IReader:=Reader.Create;
  21.   Image.LoadFromFile('Src.png',IReader);
  22.   iReader.Free;
  23.  
  24.   IWriter := TFPWriterPNG.Create;
  25.   IWriter.UseAlpha:=False;
  26.   IWriter.WordSized:=False;
  27.   IWRiter.CompressionLevel:=clMax;
  28.   Image.SaveToFile('Dst.png',IWriter);
  29.   IWriter.Free;
  30.  
  31.   image.Free;
  32. end;
  33.  
  34. begin
  35.   SavePng;
  36. end.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9706
  • FPC developer.
Re: TFPWriterPNG compression ratio problem
« Reply #1 on: September 23, 2021, 02:15:58 pm »
png doesn't just have Z lib compression, but also configurable filters before hand.  Some editors that have no time constraints on save try multiple and then save the smallest.

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 349
Re: TFPWriterPNG compression ratio problem
« Reply #2 on: September 24, 2021, 02:46:46 pm »
Thanks, I've understood.

 

TinyPortal © 2005-2018