convert PNG and JPG to sRGB

Cédric Louvrier said on 08/30/2018

[edit: 03/05/2019] pingo is able to handle colors more efficiently than before for PNG and JPG. it could try to keep the profiles (ICC/iCCP) or convert image data to sRGB to match what an user could expect. it picks by itself what could be done to preserve speed, quality and filesize. it is still work in progress atm

PNG

here is a sample with and without iCCP profile

iCCP profile
iCCP profile no iCCP

-sN

if you use a web-lossless profile, pingo should preserve only small iCCP profiles. you can use -strip=1 to keep the iCCP regardless of its size

-sN and iCCP profile
iCCP profile -s0
iCCP chunk iCCP chunk
187.32 KB 186.10 KB
pingo -s0 <targets>

-sN -srgb

you can force the conversion to sRGB if you use -srgb. this should convert the image data without additionnal loss

-sN -srgb and iCCP profile
iCCP profile -s0 -srgb
iCCP chunk no iCCP chunk
187.32 KB 166.05 KB
pingo -s0 -srgb <targets>

lossy

regardless of what lossy transformation you set in pingo, it should convert the image data to sRGB by default

lossy and iCCP profile
iCCP profile -pngcolor
iCCP chunk no iCCP chunk
187.32 KB 124.79 KB
pingo -pngcolor <targets>

or you could keep the iCCP chunk by using -nosrgb

lossy and iCCP profile
iCCP profile -pngcolor -nosrgb
iCCP chunk iCCP chunk
187.32 KB 137.45 KB
pingo -pngcolor -nosrgb <targets>

Photoshop and pingo (PNG)

as a side note, you probably know that Photoshop could do this transformation (Convert to profile > sRGB). however, what is done in pingo should be faster, *should try* to provide more compressible data, and will perform a better image optimization — note that the image data is different from Photoshop conversion

pingo 0.98.3 (G1820 @ 2.7 Ghz - 2 Go RAM - Windows 7 32-bit)
original Photoshop (sRGB converted)
817,56 KB 9.34s — non-optimized output (2094,41 KB)
original pingo -s0 -srgb (sRGB converted)
817,56 KB 1.92s — optimized output (650,89 KB)
pingo -s0 -srgb <targets>

the Photoshop output could be optimized, but not that much. running pingo -s0 — the same optimization level — over it with could save 328.68 KB more. the filesize should be 1765.73 KB and require 4.86s to process: that is because the transformation to sRGB from Photoshop makes data less compressible

JPG

convert the JPG image data to sRGB

the default (or lossy) converts the initial data to what should be rendered. however, unlike what could be done with PNG, this can be performed *only* with additional loss. it will process the ICC segment instructions, convert image data to sRGB and remove the ICC segment

pingo 0.98.3
original JPG converted to sRGB (quality 75)
290,04 KB 241,77 KB
pingo -jpgquality=75 <targets>

instead of converting the image data that way, you could choose to avoid this by using -nojpgsrgb: in this case, it will apply the lossy but keep the ICC segment

pingo 0.98.3
preserve ICC segment in lossy
original optimized
290,04 KB 230,93 KB
pingo -nojpgsrgb -jpgquality=75 <targets>

JPG color type

pingo is also able to convert some JPG color type to sRGB. this can be very useful because some JPG could be rendered differently according to what software/hardware is used, even on Web:

CMYK JPG
original and rendered — OS: Windows
original (rendered by your browser) Chrome 70.0.3536.0 (rendered)
MSPaint (rendered) Firefox 61.0.2 (rendered)

by default, pingo should convert the image data to sRGB. this is done only in lossy mode, but will provide a JPG that will be rendered in the same way by most of viewer/browser. if pingo does not support the input color type, or if you use -nojpgsrgb to disable the conversion, it could try to convert to the RGB color space instead

conversion from CMYK to sRGB
JPG image data conversion from CMYK to sRGB
original — CMYK pingo (default) — sRGB
714,47 KB 70,83 KB
pingo <targets>

PNG to JPG

pingo can convert PNG to JPG for a while, but 0.98.3 is now able to process this conversion to sRGB. it uses the iCCP chunk from the PNG but it does not write the ICC segment in JPG: image data is converted to what should be expected. on the precedent example:

pingo 0.98.3
original pingo (-jpgconvert)
817,56 KB 218,34 KB
pingo -jpgconvert <targets>

Javi said on 08/31/2018

Hi Cédric,

It's great great to have the option to convert the color profiles to sRGB!

If I understand it correctly this means that the param -srgb forces the conversion to sRGB even with the -lossless param, am I right?

BTW, the grayscale images with ICC are converted to the sGray profile?

Cédric Louvrier said on 09/01/2018

[edit: 03/05/2019]

-srgb forces the conversion to sRGB even with the -lossless param, am I right?

no, because the -srgb converts the image data — it is lossy

the grayscale images with ICC are converted to the sGray profile?

yes, grayscale should be supported

Cédric Louvrier said on 09/06/2018

i reviewed how this is done in 0.98.14 to make it more simple while giving maximum control to the user:

option what it does conditions
-srgb force conversion to sRGB overwrite *
-nosrgb disable conversion to sRGB for PNG and JPG -
-nopngsrgb disable conversion to sRGB for PNG only -
-nojpgsrgb disable conversion to sRGB for JPG only -
option what it does conditions
-srgb=0 disable conversion to sRGB -
-srgb=1 (default) enable conversion to sRGB in lossy modes overwrite only if smaller
-srgb=2 enable conversion to sRGB in all modes overwrite only if smaller
-srgb=3 force conversion to sRGB overwrite *

* only if file could need to be converted. if it is sRGB already and could not be optimized further, it will not overwrite the file

Pet01 said on 09/10/2018

Pingo crashes with this image or can't optimize (skip?). According to this (https://encode.ru/threads/1214-defluff-a-deflate-huffman-optimizer?p=35955&viewfull=1#post35955) the Photoshop ICC profile is corrupted...

Truecolor.png

Cédric Louvrier said on 09/10/2018

the Photoshop ICC profile is corrupted

no, the tRNS chunk is. should be fixed in 0.98.17, thanks for report

Cédric Louvrier said on 01/24/2019

gAMA + cHRM + iCCP chunks in PNG

original — (gAMA + cHRM + iCCP) optimized — all chunks removed, no sRGB conversion
58,9 KB 38,3 KB
optimized — iCCP only, no sRGB conversion optimized — all chunks removed, sRGB conversion
38,6 KB 47,8 KB
pingo -s0 iccp3-org.png
pingo -s0 -srgb iccp3-org.png
optimized — all chunks removed, sRGB conversion + loss optimized — sRGB conversion to JPG (no ICC)
30 KB 26,8 KB
pingo -pngfilter iccp3-org.png
pingo -jpgconvert -jpgquality=93 iccp3-org.png

Javi said on 02/23/2019

Hi,

Pingo outputs bad command if you use -grayscale and -srgb params. But it works OK if you use -grayscale and -nosrgb (of course, in this case it will not convert the image to the sGray profile).

I know that -grayscale converts the image to sGray automatically so it's not necessary to include -srgb, but I think it should accept it anyway (just to not complicate the code if you use Pingo in a script, for example).

BTW, the -strip=3 param removes the ICC but, wouldn't be a good idea to keep the ICC (if it's present and it's different of sRGB/sGray) when using -nosrgb?

Cédric Louvrier said on 02/25/2019

-grayscale and -srgb

this still need review atm

-strip=3 param removes the ICC

see -strip=N

Javi said on 02/25/2019

Hi,

I've seen another problem with -grayscale.

If you try to optimize a grayscale JPEG (8 bits) and include the -grayscale param it does not work, the optimization is almost 0%, for example:

pingo -s9 -jpgquality=50 -grayscale grayimage.jpg    (saved 0.08%)

But without the -grayscale param it works perfectly:

pingo -s9 -jpgquality=50 grayimage.jpg    (saved 53.62%)

Cédric Louvrier said on 02/25/2019

yes, i have already changed this in pingo 0.99 beta (but i did not test it). thanks for report

comment this