Monday, 26 November 2012

New colour package

libvips master has a new colour package. This is the set of operations that vips uses to move images between colour spaces and calculate various colour metrics.

This rewrite is part of the ongoing vips8 effort: a complete rewrite of libvips using GObject as a base. With the colour package done, we're about 70% of the way there, hopefully. The new package will be in the upcoming libvips 7.32, due some time in January.

Implicit conversions

This new package improves over the old one in quite a few ways. The big one is that vips now knows what colour space an image is in, and can select the best sequence of conversions to perform for you. For example:
$ vips colourspace k2.jpg x.v xyz
will make x.v, the JPEG file converted to XYZ colour space. vips doesn't need to be told that it has to use the vips_sRGB2XYZ() converter to do this, it'll pick it for you.

Later you can do something like:
$ vips colourspace x.v x2.v cmc
And it'll do the best sequence of conversions (XYZ to Lab, Lab to LCh, LCh to CMC(1:1)) for you.

The explicit conversion operations are still there if you need them. You can still do:
$ vips sRGB2XYZ k2.jpg x.v
This new implicit conversion system is used everywhere in vips. If an operation (perhaps JPEG save, for example) needs an RGB image, it'll convert whatever you give it to RGB before starting work.

Better sRGB support

Next, the operations for converting sRGB to XYZ and back have been completely rewritten. They now implement the sRGB standard exactly (the vips7 ones were missing a small feature), they are much more accurate, and they are faster.

For example:
$ vips colourspace k2.jpg x.v xyz
$ vips copy x.v k2.png
vips copy knows that png images can only be written in RGB, so it'll automatically convert from XYZ to sRGB for you.
$ vips subtract k2.jpg x2.png x3.v
$ vips abs x3.v x4.v
$ vips max x4.v
0.000000
So the maximum absolute difference doing sRGB to XYZ and back is zero.

Speed is good too, it's about twice as quick as lcms2:
$ time vips colourspace wtc.jpg x.v xyz
real    0m11.332s
user    0m3.256s
sys     0m1.048s
$ time vips icc_import wtc.jpg x.v \
    --input-profile ~/vips/share/nip2/data/sRGB.icm
real    0m11.548s
user    0m7.796s
sys     0m0.980s
It supports 16 bit as well:
$ vips XYZ2sRGB x.v x2.v --depth 16
[update: scRGB support has just been added too. This is a floating-point format with linear light and the sRGB primaries]

Minor features

All the colour operations now just process the first three bands. This means you can take an image with an alpha channel, such as an RGBA image from a PNG load, and perform colour operations on it without having to remove and reattach the alpha channel.

The colour difference metrics now do implicit conversions. Previously operations were named (awkwardly) as something like im_dE00_from_lab(), meaning you had to remember to convert the source images to CIELAB before calling the operation. Now they convert for you, for example:
$ vips dE00 a.v b.v diff.v
will calculate the colour difference between corresponding pixels in a.v and b.v using CIELAB dE 2000, doing any necessary conversions first.

Just like the other vips8 operations, the colour difference operations also work for image of varying sizes and band formats.

1 comment:

  1. Correction of a small typo: "...png images can only be written in sRGB..."

    ReplyDelete