# 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.

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.

## 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