TruePNG

TruePNG is actually one of the most advanced PNG optimizer available. it does an exhaustive checks for reductions and should be able to transform image with more efficiency than other tools. those transformations are called image reductions.

image reductions

an image reduction is a data transformation that reduces PNG file size losslessly for Web. most of PNG optimizers do reductions, but the more the tool will do, the more it will be able to optimize PNG files. that is the reason why TruePNG can beat some tools, even if they are using strong compression algorithm.

colortype

TruePNG does trials to find the best colortype for a PNG file. at the beginning of this process, it should transform the RGB area that let it to choose the optimal colortype with more precision.

TruePNG cleans RGB, and choose paletted
PNGOUT TruePNG
Old Green Car - optimized by PNGOUT Old Green Car - optimized by TruePNG
34,8 KB 9,66 KB

unlike PNGOUT, TruePNG does some transformations and then chooses the best colortype for this image: paletted mode. that is not a lossy process for a Web usage, because both images will be the same. PNGOUT uses a stronger compression algorithm, but TruePNG gets a best result thanks to this « transformation ».

Colortype reductions

TruePNG does some other trials that are not done automatically (or not done at all) by other tools:

  • TruePNG can test grayscale if image is graylevel
  • it tests tRNS chunk instead of alpha channel if applicable
  • it reduces truecolor+alpha to grayscale+alpha
  • it can choose truecolor, even if image contains less than 256 colors (or less)
  • TruePNG can encode to palette mode even if image is graylevel
TruePNG cleans RGB, and choose grayscale
OptiPNGTruePNG
Microformat logo in graylevel, optimized by optipng Microformat logo in graylevel, optimized by truepng
22,7 KB 6,17 KB

Bitdepth

The bitdepth should match to the colortype, with some exceptions in paletted mode. encoders have the choice to use 8, 4, 2 or 1 bit by pixel. most of the time, the best choice is to select the lower bitdepth, that should decrease the filesize. this feature is included in most of optimizers.

standard encoding in paletted mode, considering the number of colors
Number of colorsBitdepth
17 to 256 colors 8 bits / pixel
5 to 16 colors 4 bits / pixel
3 or 4 colors 2 bits / pixel
1 or 2 colors 1 bit / pixel

most of encoders, including TruePNG, choose the minimal bitdepth possible considering the number of colors in the image. sometimes, it is better to choose the 8 bits / pixels, even for an image that have 16 colors or less. TruePNG can check that, and it reduces filesize that way.

TruePNG checks the bitdepth
4 bits8 bits
Coffee Cup with chocolate - high dithering 4 bits Coffee Cup with chocolate - high dithering 8 bits
6,82 KB 6,39 KB

Palette optimizer

TruePNG is also different from other tool (including OptiPNG) because of its palette optimizer. like most of them, it detects an entry that does not correspond to RGB data — unused, or same entries values that have different coordinates in RGB. that way, it truncates the palette at the number of colors that are used.

TruePNG rewrite the order of colors
pngrewriteTruePNG
Coffee Cup with chocolate - high dithering 4 bits Coffee Cup with chocolate - high dithering 8 bits
21 KB 17 KB

the real good point is the order of entries in palette. it tries to optimize the sorting, sometimes againts logical way, prefering a heavy tRNS chunk — 256 transparent entries where 10 are really useful, but a smaller iDAT, mainly because of the filtering that is used.

TruePNG palette optimization trials

TruePNG does some trials to sort the colors to get the smallest file. It tries to sort the colors by popularity, by colors or by luminance. all those trials are done in memory with fast settings (huffman only for compression, filter 0 and 5) and it picks the smallest output.

TruePNG tries popularity ascending and descending
Popularity ascendingPopularity descending
Brown Colors order Popularity Ascending Brown Colors order Popularity Descending

the first method that TruePNG can use is the ordering by popularity, also used by another programs. it counts the number of time that a color is used in the picture, then move it to the top of palette, and does the same with the next one. this is an effective way to store most of paletted PNG.

TruePNG tries luminance ascending and descending
Luminance ascendingLuminance descending
Brown Colors order Luminance Ascending Brown Colors order Luminance Descending

sorting is done according the following calculation 0,299R + 0,587G + 0,114B. colors which have most brightness are moved to the top of the palette. this method is also used by another programs, like PNGOptimizer or OptiPNG. this could be very effective on images which have a lot of gradients.

TruePNG tries colors ascending and descending
Colors ascendingColors descending
Brown Colors order Colors Ascending Brown Colors order Colors Descending

the palette can be sorted using another combination using the following transformation, consisting to collect the first bit of each RGB bytes of an entry in palette, and does the same with following bits to the end. this can be good for pictures that contains gradients.

RGB: 255,230,241
255 == 11111111
230 == 11100110
241 == 11110001
Oct == 111 111 111 101 100 110 110 101

according to the version of TruePNG, it also uses another trials with differents methods, that can be done in RGB, YUV or L*a*b* colorspace, with a different reference color at start.

TruePNG another trials, according to the version
MethodColor spaceColor reference
Nearest RGB, YUV, L*a*b* most popular, most bright or most dark color
Nearest (W) RGB, YUV, L*a*b* most popular, most bright or most dark color
Nearest (N) RGB, YUV, L*a*b* most popular, most bright or most dark color
  • Nearest: each subsequent color in the palette is taken as the most similar to the previous one. it is done with Euclidean distance: sqr(R1-R2) + sqr(G1-G2) + sqr(B1-B2) [+sqr(A1-A2)]
  • Nearest (W): « W » means weight, and is the closest color value multiplied by the popularity of color which is indirectly correlated with the context of the image.
  • Nearest (N): « N » means neighbor, and is the value of the color multiplied by a factor of popularity in the immediate environment.
TruePNG, colors ordering in PNG palette
pngrewrite TruePNG 0.6.2.2
Colors ordering brown pngrewrite Colors ordering brown by TruePNG

Palette and transparency

TruePNG is considering the alpha values in a palette to sort colors. it is usually better to store all transparents colors at the top of the palette, because of unnecessary tRNS values affected for non-transparent colors.

TruePNG rewrite the order of colors
pngrewriteTruePNG
Coffee Cup with chocolate - high dithering 4 bits Coffee Cup with chocolate - high dithering 8 bits
21,1 KB 17,4 KB

but in some cases, it is better to have colors sorted, even if it makes a big tRNS chunk, because the sorting could reduce the iDAT with more efficiency. TruePNG adds combinations for sorting transparency values and selects the smaller output.

TruePNG, colors ordering in PNG palette with transparency
pngrewrite TruePNG 0.6.2.2
Colors ordering brown pngrewrite trns Colors ordering brown by TruePNG trns
  • ignore: that ignores the transparent value, considering only the colors;
  • sort: sort transparency values, considering the colors
  • start: sort all transparency values at the top of the palette, considering the colors.

color transformation

If you use the -a1 switch for paletted PNG, TruePNG will try to add a new test, by changing the RGB value of a fully transparent color to 255,255,255. this should add a new trial that change the ordering, and it will select the smaller output.

TruePNG that changes one fully transparent color to 255,255,255
Original TruePNG -a0 TruePNG -a1
Green Lemon - one tRNS by popularity Green Lemon - one tRNS with a0 Green Lemon - one tRNS with a1
22,4 KB 19,4 KB 18,9 KB
Green colors with transparency by popularity Green colors with transparency by similarity Green colors with transparency by similarity with white

tRNS optimization

this tRNS optimization can also be done for grayscale or true color images. TruePNG can turn from 0,0,0 to 255,255,255 the transparent color specified in the tRNS chunk:

TruePNG changes the tRNS chunk
TruePNG -a0 TruePNG -a1
Bird graylevel with a0 Bird graylevel with a1
11,6 KB 11,5 KB

RGB values that are not displayed

this feature is enabled by default. it detects the occurrency between a pixel and the alpha value: if alpha value is 0, then it converts the RGB data to 0 for all channels. this pixel will not be displayed anyway and it makes it more compressible that way.

TruePNG transforms RGB data to 0,0,0
Original TruePNG -a0
Bear Logo - RGB data to 0,0,0
44,7 KB 41,4 KB

image transformations

when the alpha value is 0, the pixel is not displayed whatever the RGB values. if you use -a1, TruePNG should transform an image by modifying those RGB values to make it more compressible. instead of just turning the value to 0,0,0, it does the following transformations, and pick up the smallest result.

Image transformations done by TruePNG -a1
RGB to 0,0,0 RGB to 255,255,255 RGB data to Up
RGB data have turned to 0,0,0 RGB data have turned to 255,255,255 RGB data have been extended to the top
RGB data to Right RGB data to Down RGB data to Left
RGB data have been extended to the right RGB data have been extended to the down RGB data have been extended to the left

each image is tested with -zc6 -zm8 -zs0,1 -f0,1,2,5 as internal settings trials, that could explain why TruePNG could be slow on large images. the -a1 option will also try to optimize the file keeping the RGB data as they are. if it is better without modifications, TruePNG should use it for the image:

TruePNG -a1 does not change RGB data
Original -a0 (modified) -a1 (not modified)
RGB keep - original RGB keep - 000 RGB keep - keep
36,3 KB 11,0 KB 9,22 KB

Lossless filtering

TruePNG can use heuristic filtering with the -f5 switch. it uses methods that are differents from the one defined in PNG specification. the most complete job is done by -fe that tries filter combinations without using one or more specific filter for the line. that's seems not optimal, but it could be better for overall image compression.

truepng -fe image.png

if you care about time, the generic profile -o1 (or -f0,5) should be faster, because it tries only without filtering, and with an adaptative heuristic filter. it should be nice enought for most kind of images.

Lossy optimization

the lossy optimization is most the time the best way to optimize an image for web. TruePNG can do lossy by quantization, that consists to reduce the number of colors, with or without conversion to palette mode. in a more recent approch, it can do a lossy averaging filter that denoises the image to make it more compressible.

Lossy averaging filter

TruePNG significantly reduces PNG size by doing a lossy averaging filter, that consists to modify data for a specific filter. this method works well with the average filter, so perhaps you could try to use with it. it gives nice speed - size ratio, specially for truecolor images.

TruePNG 0.6.2.2
original TruePNG
Colored lab 3 Colored lab - truepng
90,6 KB 58,5 KB
truepng -f3 -l image.png

it is good for images that can not be converted to palette mode because of quality loss. at this time, this feature is still experimental and a better implementation is on the way: combination of image transformation and lossy filter can be used to get an even smaller result.

Color quantization

the color quantization consists to reduce the number of colors in an image. you should consider to use the lossy averaging filter instead specially with big images, because color quantization can be very slow on some samples. Also, it should give a lower quality - size ratio.

Color Quantizer
original (31896 colors) Color Quantizer (1024 colors)
Colored lab 1 Colored lab - color quantizer 1024
90,6 KB 36,2 KB
truepng -f0 -cq c=1024 image.png

the -cq switch active the quantization, and user have to specify a value for c= which represents the approximative amount of colors there will be in the image. if you use a value below 257, the PNG could be converted to paletted mode.

Conversion to palette mode

TruePNG can use color quantization to convert an image to paletted mode that will significantly reduce the filesize. at this time, TruePNG includes an old implementation of color quantization, it is preferable to use Color Quantizer instead.

TruePNG does color quantization
Original Color quantization
Hand rotate movement Hand rotate movement - quantized
17,3 KB 8,95 KB
truepng -cq image.png

you can use c= to specify the number of colors, and d= switch if you want to active the dithering. note that dithering is optionnal, and can help to keep a better visual perception if there are lot of colors in the image.

Interlacing

interlacing is a method of encoding that displays the image partially according to received data. PNG uses the Adam7 algorithm to display with interlacing. if you care about file size, you should always disable interlacing: most of the time, it makes files bigger.

Interlacing
With interlacing Without interlace
Hand rotate movement - interlacing image Hand rotate movement - quantized, no interlace
11,5 KB 8,95 KB
truepng -i0 image.png

the -i0 switch should disable the interlacing. it makes file bigger, specially with those that are using a filter to store the data. if you need smaller files to display them faster, you sould consider optimization, quantization or lossy averaging filter instead of using interlacing.

Gamma integration

in most of cases, it is better to disable any chunk that correct the way the image is displayed, specially for Web. gAMA chunk is not an exception, and the -g0 should delete it. However, TruePNG let you to merge the way the image is displayed instead of using gAMA chunk.

Gamma integration
With gAMA chunk Gamma integrated
Colors palette with high gamma Colors palette with high gamma integrated
9,08 KB 7,08 KB
truepng -g1 image.png -out test.png

be warned, it is a no back process, so you should make a backup of your file (or use -out switch). gAMA chunk can be added or deleted, but in this case, it changes RGB values in the new file.

Chunk deletion

for a web usage, PNG chunks are more often a problem instead of a solution, because some of them can modify the way the image is displayed: some devices can use them, some can not. TruePNG deletes all unnecessary chunks with the -md remove all instruction:

truepng -md remove all image.png

TruePNG options

TruePNG detailed options
OptionsEffect
/f# PNG delta filters 0=None, 1=Sub, 2=Up, 3=Average, 4=Paeth, 5=Mixed
Example:
truepng -f0,5 image.png
/fe PNG delta filters combination to get the smallest output size. That overrides /f#
/i# 0 deletes the interlacing, 1 should active or keep existing interlacing
/g#
  • 0 deletes the gAMA chunk
  • 1 deletes the gAMA chunk and it applies values in RGBA data
  • 2 keep the gAMA chunk
/a#
  • 0 will turn RGB values to 0,0,0
  • 1 will make images transformations
/na Disable the RGB data optimization for fully transparent pixels
/nc Disable most of optimization, including color type or bitdepth changes
/np TruePNG will not change palette ordering
/nz TruePNG will not compress the file. It works with /md switch for chunks deletion.
/tz TruePNG compresses tEXt chunk
/md This switch can delete some chunks:
  • color: gAMA,cHRM,sRGB,iCCP
  • text: iTXt,tEXt,zTXt
  • misc: bKGD,pHYs,sBIT,sPLT,hIST,tIME
  • all: deletes all non critical chunks
Example:
truepng -md remove all image.png
/cq Enable color quantization to reduce the number of colors:
  • c=<number of colors> from 2 to 4096 (default will convert to paletted mode)
  • d=<dithering amount> from 0 to 100 (default disabled)
Example:
truepng -cq c=200 d=75 image.png
/l Enable the lossy filtering (use with -f):
  • q=<quality> from 0 to 255 (default is 4)
  • m=<method> from 0 to 1 (default is 0)
Example:
truepng -f3 -l -q=5 -m=1 image.png
/zc Zlib compression level, from 1 to 9
/zm Zlib memory level, from 1 to 9
/zs Zlib compression strategy from 0 to 3
/zw Zlib window size from 0 to 7 (0-7 corresponds to 256, 512, 1024, 2048, 4096, 8192, 16384 and 32768)
/quiet Quiet mode, will not display informations
/force Always write the file, even if it's bigger
/out Specify where you want to write the file:
Example:
truepng image.png -out image-optimized.png
/y Overwrites the file if it exists
/t Preserves the time and date
/o0 /nz /md remove all - Remove all non-critical chunks without compression
/o1 /zc9 /zm8-9 /zs0-3 /f0,5 - Default optimization
/o2 /zc9 /zm1-9 /zs0-3 /f0-5
/o3 /zc9 /zm1-9 /zs0-3 /fe
/o4 /zc9 /zm1-9 /zs0-3 /fe /a1 /i0 /md remove all
/info Displays PNG file informations

TruePNG also works if you drag and drop files on the TruePNG.exe file. it will use the /o1 profile to optimize in this case. if you plan to combine it with other tools, TruePNG should be the at the beginning of process because of those reductions. an example of a combination, if you want to optimize your image for Web:

:: TruePNG optimization
truepng -o1 -g0 -i0 -md remove all image.png
:: libdeflate compression
advdef -z -2 image.png