# Babl

babl is pixel encoding and color space conversion engine in C.

It allows converting between different methods of storing pixels known as pixel formats that have with different bitdepths and other data representations, color models, color spaces and component permutations.

A vocabulary to formulate new pixel formats from existing primitives is provided as well as the framework to add new color models, spaces and data types.

## Features

- Dynamically extendable with new formats, color models, components and datatypes, and SIMD code paths.
- Reference 64bit floating point conversions for datatypes and color models, with 32bit floating point reference speed-ups in some places.
- Runtime profiling/validating and code-path optimizing with persistence of profiling data across runs, with caching of results.
- Can load Color Spaces from ICC v2 and v4 profiles containing RGB matrix + TRC and with lcms2 dependency also CMYK profiles.
- Uses Unified Alpha conversions for conversions between separate and associate alpha, avoiding loss of color fidelity due to asymptotic behavior when dividing by 0.0 in floating point.
- Portable self contained C code working on win32, linux, bsds and mac on 32bit and 64bit systems.
- Stable, small API, with singleton objects returned.

The pixel data storage in GIMP uses GEGL's GeglBuffer which internally stores tiles and provides an API for retrieving and storing pixel data with implicit conversions using babl formats.

## Download

The latest versioned development version of babl can be found in https://download.gimp.org/pub/babl/.

Babl uses git. The main repository is hosted by GNOME. It can be browsed online and cloned with:

    git clone https://gitlab.gnome.org/GNOME/babl.git/

## NEWS

The following is a list of the major changes that have gone into each babl release. If there are significant improvements to babl when a GEGL release is done a babl release is most often put out just prior to the GEGL release.

2019-08-22 babl-0.1.74
  Build fixes, improved host cpu detection, OSX fixes, clang warning squelches. 2019-08-22 babl-0.1.72
Added handling for grayscale ICC profiles and gobject introspection support. Optimizations for many format conversions for u8, u16 and half formats. And fixed a crash for NANs in float to u8 conversions.
2019-08-02 babl-0.1.70
  Build fixes making the meson build work with more architectures.
2019-07-25 babl-0.1.68
  Added Yu'v' (CIE 1976 UCS) color model. Fixed a severe non-initialised memory issue - that kept us from using fast paths for many babl spaces loaded from profile. More fully symmetric conversions between associated and separate alpha. Added more converters, both SSE2 and non-SSE2 for matrix-conversion between different RGB spaces. Improvements to website/documentation; babl is now using the terms associated alpha and separate alpha, all of: nonpremultiplied-, premultiplied- and nonassociated- alpha are now considered deprecated API. HCY luminance is now relative to the RGB space used - no change for sRGB related pixel formats. AVX2 acceleration for some linear to float conversions.
2019-06-12 babl-0.1.66
  Added API call, babl_space_get_rgb_luminance, Make most SIMD code-paths optional again on i686.
2019-05-25 babl-0.1.64
  Support for Hygon Dhyana, and Y~aA formats for symmetric completeness. Code consistency, gitlab CI, autotools and meson build improvements.
2019-02-01 babl-0.1.62
  Continuous integration with gitlab. Initial CMYK spaces with lcms2 based ICC support, much room for optimization. Improved custom space support for palette formats. scRGB space, works like sRGB but always with linear TRCs. Model introspection API permitting low overhead checks whether a format/model is RGB, CMYK type of alpha and similar.
For more detailed changes see git log.

## Usage

Most users of babl do not know they are using babl and it is GIMP itself which uses babl, this is documentation for such uses - and others that might want to use babl for pixel format or color space conversion in other software.

When using BablFishes to do your conversions, you request a fish to convert between two formats, and an optimal fish to babls capability is provided that you can use to do your conversions. Babl also provides the capability to describe new formats based on a vocabulary of user registered color models and data types.

Babl provides a base vocabulary in BablBase and some extensions that are thought to be generally useful.

When performing further extensions to the vocabulary of babl, the internal consistency is governed by reference conversions that operate on double (64 bit floating point values). Color Spaces can be created from chromaticity coordinates or ICC profiles. The reference color space - to maintain fast and bit-accurate conversions on with sRGB is similar to scRGB using 64bit floating point.

To speed up operations, fast path conversions are used. The registered shortcut might also be used by babl as an intermediate conversion when constructing BablFishes for other conversions.

Babl extensions are shared objects. If you have already developed some fast conversion functions, wrapping them as babl extensions should not take much time and will speed up babl for other users as well.

    babl_process (babl_fish (source_format, destination_format),
                  source_buffer, destination_buffer,
                  pixel_count);

The processing operation that babl performs is copying including conversions if needed between linear buffers containing the same count of pixels, with different pixel formats.

    int width = 123, height = 581, pixel_count = width * height;

    const Babl *srgb             = babl_format ("R'G'B' u8");
    const Babl *lab              = babl_format ("CIE Lab float");
    const Babl *srgb_to_lab_fish = babl_fish (srgb, lab);

    float         *lab_buffer;
    unsigned char *srgb_buffer;

    babl_init ();

    srgb_buffer = malloc (pixel_count * babl_format_get_bytes_per_pixel (srgb));
    lab_buffer  = malloc (pixel_count * 3 * sizeof (float));

    ...... load data into srgb_buffer .......

    babl_process (srgb_to_lab_fish, srgb_buffer, lab_buffer, pixel_count);

    ...... do operation in lab space ........

    babl_process (babl_fish(lab, srgb),
                  lab_buffer, srgb_buffer, pixel_count);

    /* the data has now been transformed back to srgb data */

If the existing pixel formats are not sufficient for your conversion needs, new ones can be created on the fly. The constructor needs, new ones can be created on the fly. The constructor will provide the prior created one if duplicates are registered.

    const Babl *format = babl_format_new (babl_model ("R'G'B'"),
                                          babl_type ("u16"),
                                          babl_component ("B'"),
                                          babl_component ("G'"),
                                          babl_component ("R'"),
                                          NULL);

## Environment

Through the environment variable `BABL_TOLERANCE` you can control a speed/performance trade off that by default is set very low (0.000001) values in the range 0.01-0.1 can provide reasonable preview performance by allowing lower numerical accuracy

.

`BABL_PATH` contains the path of the directory, containing the .so extensions to babl.

## Extending

For samples of how the current internal API specification of data types, color models, and conversions look in the `extensions/` directory. The tables in this HTML file is directly generated based on the data registered by BablCore (double and RGBA), BablBase (core datatypes, and RGB models), extensions (CIE Lab, naive CMYK, various shortcut conversions).

## Directory Overview

    babl-dist-root
     │
     ├──babl       the babl core
     │   └──base   reference implementations for RGB and Grayscale Color Models,
     │             8bit 16bit, and 32bit and 64bit floating point.
     ├──extensions CIE-Lab color model as well as a naive-CMYK color model.
     │             also contains a random cribbage of old conversion optimized
     │             code from gggl. Finding more exsisting conversions in third
     │             part libraries (hermes, lcms?, liboil?) could improve the
     │             speed of babl.
     ├──tools      various commandline tools used for verifying accuracy of extensions.
     ├──tests      tests used to keep babl sane during development.
     └──docs       Documentation/webpage for babl (the document you are reading
                   originated there.

## TODO

These are among desired or expected changes to babl, that are missing.

- extensions for many SIMD variants for many archiectures.
- Spectral pixel formats - with configurable spectral mapping to tristimulus with configurable illuminant + observer
- Spectral substrate + ink/paint job configuration as a pixel format, combined with above to achieve soft proofing, and stochastic sparse LUT for separation.
- Support for datatypes that are not a multiple of 8bit.

## Copyright

Babl is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

### Authors

Øyvind Kolås pippin at gimp.org
  Original author.
Sven Neumann sven at gimp.org
  Build sanity and optimizations.
Michael Natterer mitch at gimp.org
  Build sanity.
Kevin Cozens kcozens at cvs.gnome.org
  Build sanity.
Tim Mooney
  Portability fixes.
Michael Schumacher schumaml at cvs.gnome.org
  win32 support for dynamic extensions.
  Portability fixes.
Jan Heller jheller at svn.gnome.org
  Optimizations, refactoring and documentation.
Mukund Sivaraman muks at mukund.org
  Sparse fixes and sanity.
dmacks at netspace.org
  Build sanity
Sam Hocevar
  Build sanity.
Zhang Junbo
  Frequency domain color model.
Martin Nordholts
  Optimizations and API overhaul.
Gary V. Vaughan
  Multiplatform build support.
Stanislav Brabec
  Portability patch.
Hubert Figuiere
  C++ friendliness patch.
Danny Robson
  Code cleanups.
Fryderyk Dziarmagowski freetz at gmx.net
  Code cleanups.
Daniel Paredes García danipga at gmail.com
  Initial work to make babl threadsafe
Rupert Weber gimp at leguanease.org
  Documentation and other improvements
Jehan jehan at girinstud.io
  win32 portabilitiy
Sven Claussner
  Update DOAP file
Alexander Larsson
  Math optimizations
Nils Philippsen
  code cleanups
Simon Budig
  cleanups
Micheal Muré
  portability fixes
Edward E
  win32 platform adaptations
Maxime Nicco

HSV color model extension

Teo Mazars
  Color spaces/models in extensions
Daniel Sabo
  Dead code elimination, general cleanups,
Michael Henning
  Conversion fixes
Elle Stone
  CIE xyY color space
  Verification and improvements to accuracy of color space conversions.
Thomas Manni
  CIE related fixups
Roman Lebedev
  Stability/crasher fixes
Jon Nordby
  Portability, Stability and more
Massimo Valentini
  stability fixes
Ell
  fast paths
Guiu Rocafort
  sanity crash fix
Tobias Stoeckmann
  bug fixes for leaks and crashers
Debarshi Ray
  SSE2 version of CIE code paths
Félix Piédallu
  Meson build; original port, and improvements.
Yaakov Selkowitz
  Cygwin patch
Étienne Bersac
  build/packaging
Nuno Ferreira
  coding style fixes
John Marshall
  meson build improvements