sRGBz—a minimal sRGB ICC v2 profile
This page is about the size and precision optimizations of sRGBz.icc a 491byte, CC0 licensed, ICC v2, sRGB equivalent profile, useful for ensuring consistent handling of 8bpc images files in environments where ICC v2 profiles are widely supported (like the web).
I've been addding the ability to specify the colorimetric characteristics of the RGB color space used to store colors in the pixels to GEGL and GIMP's pixel conversion library, babl. This information can be provided directly to babl - or babl can extract the information from an ICC profile. Babl also has the ability to write such information back out as an ICC profile.
There are many sRGB ICC profiles out there, resulting from slightly different starting points, and different floating point and fixed point representations used in the ICC profile computation/creation process.
Babl upgrades loaded ICC RGB spaces that are within a small error bound equal to standard RGB spaces to be exactly that RGB space.
After implementing the matching I stumbled across facebook's Tiny sRGB ICC profile from 2012, which encodes the sRGB space in a 524byte ICCv2 profile. Intended as a minimal but precise enough for display of 8bpc JPEGs images and thumbnails. [Some versions of some web browsers apparently treat untagged images as if they were in the display-space, rather than sRGB, probably yielding worst results on wide-gamut displays, there are other ways of more tersely specifying sRGB for a JPG through fields of exif tags - but that is beyond the scope of this article]
The tiny sRGB ICC profile which calls itself "c2" in its own description is considered by babl similar enough to sRGB to be treated as sRGB, having implemented reading and writing of the ICC format I was curious about how it packed the data in the file and decided to take a close look at how the profile is expressed in terms of the ICC file format, and ended up using the ICC writing code and transform matrix preparation code in babl to create sRGBz from first principles improving on it.
Given that one of the bulkier parts of ICCv2 sRGB profiles is the LUT it is very reasonable that the sTRC, gTRC and bTRC data tags are aliases for each other. Otherwise the profile would've been 128 bytes larger..
An ICC profile consists of 128bytes header, 12bytes * number of tags with type/size/offset, followed by the tag data itself with each tag data aligned to 4 bytes. By moving the biggest alignment disruptor the 'desc' tag from the beginning to the end, and reducing its length to 1 we can save 5 bytes. This saving by alignment trick only works for one tag, thus leaving room for 3 bytes in the copyright tag, suited for the string "CC0" to indicate a CC0-1.0 license for it.
The choice of 26 bins is argued to be because this makes the linear segment of the sRGB TRC fit almost exactly the first bins as a nice coincidence; which might be yielding a closer match than the LUT size otherwise provides.
As part of improving my understanding of the LUTs in ICC profiles, I've implemented a brute force solver for the best 16bit unsigned LUTs of various sizes for approximating the sRGB transfer function.
This is a program that first computes an approximate value for all entires of the LUT, then it goes through each entry of the LUT trying all alternative combinations within a small range, keeping the best one. The best one is determined as the value that yields the lowest average/total error when the LUT is used to derive all 256 values relevnt for an 8bpc file. The above procedure is then repeated until the total error no longer decreases decreasing anymore and we've found our optimum.
|26||0.000119||0,203,457,867,1426,2155,3062,4159,5457,6964,8689,10640,12824,15250,17925,20855,24045,27504,31237,35259,39548,44137,49021,54211,59696,65535(This is the tiny sRGB LUT,which also is available under CC0.)|
Each additional entry in the lookup-table consumes two bytes in the ICC profile, there doesn't seem to be a particular large win in precision around 26 - as perhaps could be expected the overall error continues being decreased with an increased LUT-size, I considered using some of the gained bytes for a higher precision LUT, but the trade-off chosen good - and we were able to improve on the precision while staying at 26 entries.
The tiny sRGB ICC profile contains a black-point tag, odd for a working/storage RGB space but useful if this was a monitor profile. The black-point tag takes up 20 + 12 = 32 bytes in the profile.
If the sRGB space were to have a black point tag, it should be 0, 0, 0 to indicate that 0,0,0 is indeed black. Which is equivalent to dropping the tag altogether.
I experimented with moving some of the tags to the unused/reserved 0 padded bytes in the header - but some ICC parsers protest on such packing and I had to give up on that.
ICC profiles generated internally by babl have the ICC 15.16 fixed point precision rounding equalized to provide exact conversions of 255,255,255 to CIE Lab 100 0.0 0.0 as well as a completely neutral gray axis.
Thus the smallest ICCv2 profile possible save for the luxury of a human readable name, we can give the profile the description 'z' one char, giving a file size of 487 bytes 487 bytes, with a human readable description/name it becomes 491 bytes.
Saving more than 32bytes per ICC profile over "c2" from facebook, such an ICC profile might be the best way to explicitly color manage 8bit jpg files. Facebook is currently embedding "c2" in every jpeg served, even thumbnails. Summed up the total savings in storage and transfer adds up - preserving monthly mobile quotas. It adds up, just like the monthly donations on patreon adds up to enabling my persistence in making and sharing things investigating and improving digital media.
update 2017-05-13For someone else falling deep into the rabbiy hole of optimizing sRGB ICC profile size/accuracy, see Making a Minimal sRGB ICC Profile Part 1: Trim the Fat; Abuse the Spec which has a 424 bytes 26 point cure, following blog posts investigate well behaved small rather than tiny ICCv2 profiles.