1 /* babl - dynamically extendable universal pixel conversion library.
2 * Copyright (C) 2005-2008, Øyvind Kolås.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see
16 * <https://www.gnu.org/licenses/>.
26 #define BABL_INSIDE_BABL_H
27 #include <babl/babl-macros.h>
28 #include <babl/babl-types.h>
29 #include <babl/babl-version.h>
30 #undef BABL_INSIDE_BABL_H
36 * Initializes the babl library.
38 void babl_init (void);
43 * Deinitializes the babl library and frees any resources used when
44 * matched with the number of calls to babl_init().
46 void babl_exit (void);
51 * Returns the babl object representing the data type given by @name
52 * such as for example "u8", "u16" or "float".
54 const Babl
* babl_type (const char *name
);
59 * Returns the babl object representing the @horizontal and @vertical
60 * sampling such as for example 2, 2 for the chroma components in
63 const Babl
* babl_sampling (int horizontal
,
70 * Returns the babl object representing the color component given by
71 * @name such as for example "R", "cyan" or "CIE L".
73 const Babl
* babl_component (const char *name
);
78 * Returns the babl object representing the color model given by @name
79 * such as for example "RGB", "CMYK" or "CIE Lab".
81 const Babl
* babl_model (const char *name
);
84 * babl_model_with_space:
86 * The models for formats also have a space in babl, try to avoid code
87 * needing to use this.
90 babl_model_with_space (const char *name
, const Babl
*space
);
96 * Returns the babl object representing the specific RGB matrix color
97 * working space referred to by name. Babl knows of:
98 * sRGB, Rec2020, Adobish, Apple and ProPhoto
101 const Babl
* babl_space (const char *name
);
104 BABL_ICC_INTENT_PERCEPTUAL
= 0,
105 BABL_ICC_INTENT_RELATIVE_COLORIMETRIC
= 1,
106 BABL_ICC_INTENT_SATURATION
= 2,
107 BABL_ICC_INTENT_ABSOLUTE_COLORIMETRIC
= 3,
108 BABL_ICC_INTENT_PERFORMANCE
= 32
109 // black-point compensation toggle will be added if/when support exist in babl
113 * babl_space_from_icc:
114 * @icc_data: pointer to icc profile in memory
115 * @icc_length: length of icc profile in bytes
116 * @intent: the intent from the ICC profile to use.
117 * @error: (out): pointer to a string where decoding errors can be stored,
118 * if an error occurs, NULL is returned and an error message
119 * is provided in error.
121 * Create a babl space from an in memory ICC profile, the profile does no
122 * longer need to be loaded for the space to work, multiple calls with the same
123 * icc profile and same intent will result in the same babl space.
125 * On a profile that doesn't contain A2B0 and B2A0 CLUTs perceptual and
126 * relative-colorimetric intents are treated the same.
128 * If a BablSpace cannot be created from the profile NULL is returned and a
129 * static string is set on the const char *value pointed at with &value
130 * containing a message describing why the provided data does not yield a babl
133 const Babl
*babl_space_from_icc (const char *icc_data
,
135 BablIccIntent intent
,
137 /* babl_space_get_gamma:
138 * @space: a babl space
140 * Returns the gamma of the TRCs of the space, iff they are all equal
141 * and a simple gamma number, otherwise 0.0 is returned.
144 babl_space_get_gamma (const Babl
*space
);
147 const Babl
*babl_icc_make_space (const char *icc_data
,
149 BablIccIntent intent
,
155 * @icc_data: pointer to in-memory icc profile
156 * @icc_length: length of icc profile in bytes
157 * @key: the key we want to quey, see below for some supported values
158 * @language: 2 char code for language to extract or NULL
159 * @country: 2 char country code or NULL
161 * Returns: (transfer full) (nullable): %NULL if key not found or a newly
162 * allocated utf8 string of the key when found, free with free() when done.
163 * Supported keys: "description", "copyright", "manufacturer", "device",
164 * "profile-class", "color-space" and "pcs".
167 char *babl_icc_get_key (const char *icc_data
,
170 const char *language
,
171 const char *country
);
177 * Returns the babl object representing the color format given by
178 * @name such as for example "RGB u8", "CMYK float" or "CIE Lab u16",
179 * creates a format using the sRGB space, to also specify the color space
180 * and TRCs for a format, see babl_format_with_space.
182 const Babl
* babl_format (const char *encoding
);
185 * babl_format_with_space:
187 * Returns the babl object representing the color format given by
188 * @name such as for example "RGB u8", "R'G'B'A float", "Y float" with
189 * a specific RGB working space used as the space, the resulting format
190 * has -space suffixed to it, unless the space requested is sRGB then
191 * the unsuffixed version is used. If a format is passed in as space
192 * the space of the format is used.
194 const Babl
* babl_format_with_space (const char *encoding
, const Babl
*space
);
197 * babl_format_exists:
199 * Returns 1 if the provided format name is known by babl or 0 if it is
200 * not. Can also be used to verify that specific extension formats are
201 * available (though this can also be inferred from the version of babl).
203 int babl_format_exists (const char *name
);
206 * babl_format_get_space:
208 * Retrieve the RGB color space used for a pixel format.
210 const Babl
* babl_format_get_space (const Babl
*format
);
216 * Create a babl fish capable of converting from source_format to
217 * destination_format, source and destination can be either strings
218 * with the names of the formats or Babl-format objects.
220 const Babl
* babl_fish (const void *source_format
,
221 const void *destination_format
);
227 * Create a faster than normal fish with specified performance (and thus
228 * corresponding precision tradeoff), values tolerance can hold: NULL and
229 * "default", means do same as babl_fish(), other values understood in
230 * increasing order of speed gain are:
231 * "exact" "precise" "fast" "glitch"
233 * Fast fishes should be cached, since they are not internally kept track
234 * of/made into singletons by babl and many creations of fast fishes will
235 * otherwise be a leak.
238 const Babl
* babl_fast_fish (const void *source_format
,
239 const void *destination_format
,
240 const char *performance
);
245 * Process n pixels from source to destination using babl_fish,
246 * returns number of pixels converted.
248 long babl_process (const Babl
*babl_fish
,
254 long babl_process_rows (const Babl
*babl_fish
,
266 * Returns a string describing a Babl object.
268 const char * babl_get_name (const Babl
*babl
);
271 * babl_format_has_alpha:
273 * Returns whether the @format has an alpha channel.
275 int babl_format_has_alpha (const Babl
*format
);
278 * babl_format_get_bytes_per_pixel:
280 * Returns the bytes per pixel for a babl color format.
282 int babl_format_get_bytes_per_pixel (const Babl
*format
);
285 * babl_format_get_model:
287 * Return the model used for constructing the format.
289 const Babl
* babl_format_get_model (const Babl
*format
);
295 * @BABL_MODEL_FLAG_ALPHA: the model encodes alpha.
296 * @BABL_MODEL_FLAG_ASSOCIATED: the alpha is associated alpha.
297 * @BABL_MODEL_FLAG_INVERTED: the components are inverted (used for getting the additive complement space of CMYK).
298 * @BABL_MODEL_FLAG_LINEAR: the data has no TRC, i.e. is linear
299 * @BABL_MODEL_FLAG_NONLINEAR: the data has a TRC - the TRC from the configured space
300 * @BABL_MODEL_FLAG_PERCEPTUAL: the data has a TRC - a perceptual TRC where 50% gray is 0.5
301 * @BABL_MODEL_FLAG_GRAY: this is a gray component model
302 * @BABL_MODEL_FLAG_RGB: this is an RGB based component model, the space associated is expected to contain an RGB matrix profile.
303 * @BABL_MODEL_FLAG_CIE: this model is part of the CIE family of spaces
304 * @BABL_MODEL_FLAG_CMYK: the encodings described are CMYK encodings, the space associated is expected to contain an CMYK ICC profile.
309 BABL_MODEL_FLAG_ALPHA
= 1<<1,
310 BABL_MODEL_FLAG_ASSOCIATED
= 1<<2,
311 BABL_MODEL_FLAG_INVERTED
= 1<<3,
313 BABL_MODEL_FLAG_LINEAR
= 1<<10,
314 BABL_MODEL_FLAG_NONLINEAR
= 1<<11,
315 BABL_MODEL_FLAG_PERCEPTUAL
= 1<<12,
317 BABL_MODEL_FLAG_GRAY
= 1<<20,
318 BABL_MODEL_FLAG_RGB
= 1<<21,
319 /* BABL_MODEL_FLAG_SPECTRAL = 1<<22, NYI */
320 BABL_MODEL_FLAG_CIE
= 1<<23,
321 BABL_MODEL_FLAG_CMYK
= 1<<24,
322 /* BABL_MODEL_FLAG_LUZ = 1<<25, NYI */
325 // XXX : should warn when used
326 #define BABL_MODEL_FLAG_PREMULTIPLIED BABL_MODEL_FLAG_ASSOCIATED
328 /* linear, nonlinear and perceptual could occupy two bits with a decidated 0,
329 * but we do not have a lack of bits in this bit pattern so leave it be.
332 BablModelFlag
babl_get_model_flags (const Babl
*model
);
335 * babl_format_get_n_components:
337 * Returns the number of components for the given @format.
339 int babl_format_get_n_components (const Babl
*format
);
342 * babl_format_get_type:
344 * Returns the type in the given @format for the given
347 const Babl
* babl_format_get_type (const Babl
*format
,
348 int component_index
);
354 * Defines a new data type in babl. A data type that babl can have in
355 * its buffers requires conversions to and from "double" to be
356 * registered before passing sanity.
358 * babl_type_new (const char *name,
360 * ["min_val", double min_val,]
361 * ["max_val", double max_val,]
364 const Babl
* babl_type_new (void *first_arg
,
365 ...) BABL_ARG_NULL_TERMINATED
;
368 * babl_component_new:
370 * Defines a new color component with babl.
372 * babl_component_new (const char *name,
375 const Babl
* babl_component_new (void *first_arg
,
376 ...) BABL_ARG_NULL_TERMINATED
;
381 * Defines a new color model in babl. If no name is provided a name is
382 * generated by concatenating the name of all the involved components.
384 * babl_model_new (["name", const char *name,]
385 * BablComponent *component1,
386 * [BablComponent *componentN, ...]
389 const Babl
* babl_model_new (void *first_arg
,
390 ...) BABL_ARG_NULL_TERMINATED
;
395 * Defines a new pixel format in babl. Provided BablType and|or
396 * BablSampling is valid for the following components as well. If no
397 * name is provided a (long) descriptive name is used.
399 * babl_format_new (["name", const char *name,]
402 * [BablSampling, *sampling,]
403 * BablComponent *component1,
405 * [BablSampling *sampling,]
406 * BablComponent *componentN,
411 const Babl
* babl_format_new (const void *first_arg
,
412 ...) BABL_ARG_NULL_TERMINATED
;
417 * Defines a new pixel format in babl. With the specified data storage
418 * type and the given number of components. At the moment behavior of
419 * conversions are only well defined to other babl_format_n derived formats
420 * with the same number of components.
423 babl_format_n (const Babl
*type
,
427 * babl_format_is_format_n:
429 * Returns whether the @format is a format_n type.
431 int babl_format_is_format_n (const Babl
*format
);
434 * babl_conversion_new:
436 * Defines a new conversion between either two formats, two models or
439 * babl_conversion_new (<BablFormat *source, BablFormat *destination|
440 * BablModel *source, BablModel *destination|
441 * BablType *source, BablType *destination>,
442 * <"linear"|"planar">, <BablFuncLinear | BablFuncPlanar> conv_func,
445 const Babl
* babl_conversion_new (const void *first_arg
,
446 ...) BABL_ARG_NULL_TERMINATED
;
449 * babl_conversion_get_source_space:
451 * Returns the RGB space defined for the source of conversion.
453 const Babl
*babl_conversion_get_source_space (const Babl
*conversion
);
456 * babl_conversion_get_destination_space:
458 * Returns the RGB space defined for the destination of conversion.
460 const Babl
*babl_conversion_get_destination_space (const Babl
*conversion
);
465 * create a new palette based format, name is optional pass in NULL to get
466 * an anonymous format. If you pass in with_alpha the format also gets
467 * an 8bit alpha channel. Returns the BablModel of the color model. If
468 * you pass in the same name the previous formats will be provided
471 const Babl
*babl_new_palette (const char *name
,
472 const Babl
**format_u8
,
473 const Babl
**format_u8_with_alpha
);
476 * babl_new_palette_with_space:
478 * create a new palette based format, name is optional pass in NULL to get
479 * an anonymous format. If you pass in with_alpha the format also gets
480 * an 8bit alpha channel. Returns the BablModel of the color model. If
481 * you pass in the same name the previous formats will be provided
484 const Babl
*babl_new_palette_with_space (const char *name
,
486 const Babl
**format_u8
,
487 const Babl
**format_u8_with_alpha
);
490 * babl_format_is_palette:
492 * check whether a format is a palette backed format.
494 int babl_format_is_palette (const Babl
*format
);
497 * babl_palette_set_palette:
499 * @format: The pixel format
500 * @data: (array) (element-type guint8): The pixel data
501 * @count: The number of pixels in @data
503 * Assign a palette to a palette format, the data is a single span of pixels
504 * representing the colors of the palette.
506 void babl_palette_set_palette (const Babl
*babl
,
512 * babl_palette_reset:
514 * reset a palette to initial state, frees up some caches that optimize
517 void babl_palette_reset (const Babl
*babl
);
521 * babl_set_user_data: (skip)
523 * associate a data pointer with a format/model, this data can be accessed and
524 * used from the conversion functions, encoding color profiles, palettes or
525 * similar with the data, perhaps this should be made internal API, not
526 * accesible at all from
528 void babl_set_user_data (const Babl
*babl
, void *data
);
531 * babl_get_user_data: (skip)
533 * Get data set with babl_set_user_data
535 void * babl_get_user_data (const Babl
*babl
);
538 BABL_SPACE_FLAG_NONE
= 0,
539 BABL_SPACE_FLAG_EQUALIZE
= 1
543 * babl_space_from_chromaticities:
544 * @name: (nullable): The name for the color space
545 * @wx: The X-coordinate of the color space's white point
546 * @wy: The Y-coordinate of the color space's white point
547 * @rx: The X-coordinate of the red primary
548 * @ry: The Y-coordinate of the red primary
549 * @gx: The X-coordinate of the green primary
550 * @gy: The Y-coordinate of the green primary
551 * @bx: The X-coordinate of the blue primary
552 * @by: The Y-coordinate of the blue primary
553 * @trc_red: The red component of the TRC.
554 * @trc_green: (nullable): The green component of the TRC (can be %NULL if it's
555 * the same as @trc_red).
556 * @trc_blue: (nullable): The blue component of the TRC (can be %NULL if it's
557 * the same as @trc_red).
558 * @flags: The #BablSpaceFlags
560 * Creates a new babl-space/ RGB matrix color space definition with the
561 * specified CIE xy(Y) values for white point: wx, wy and primary
562 * chromaticities: rx,ry,gx,gy,bx,by and TRCs to be used. After registering a
563 * new babl-space it can be used with babl_space() passing its name;
565 * Internally this does the math to derive the RGBXYZ matrix as used in an ICC
568 const Babl
* babl_space_from_chromaticities (const char *name
,
569 double wx
, double wy
,
570 double rx
, double ry
,
571 double gx
, double gy
,
572 double bx
, double by
,
574 const Babl
*trc_green
,
575 const Babl
*trc_blue
,
576 BablSpaceFlags flags
);
582 * Creates a Babl TRC for a specific gamma value, it will be given
583 * a name that is a short string representation of the value.
585 const Babl
* babl_trc_gamma (double gamma
);
590 * Look up a TRC by name, "sRGB" and "linear" are recognized
591 * strings in a stock babl configuration.
593 const Babl
* babl_trc (const char *name
);
596 * babl_space_with_trc:
598 * Creates a variant of an existing space with different trc.
600 const Babl
*babl_space_with_trc (const Babl
*space
, const Babl
*trc
);
604 * @space: A #Babl instance
605 * @xw: (out) (optional): The X-coordinate of the color space's white point
606 * @yw: (out) (optional): The Y-coordinate of the color space's white point
607 * @xr: (out) (optional): The X-coordinate of the red primary
608 * @yr: (out) (optional): The Y-coordinate of the red primary
609 * @xg: (out) (optional): The X-coordinate of the blue primary
610 * @yg: (out) (optional): The Y-coordinate of the green primary
611 * @xb: (out) (optional): The X-coordinate of the blue primary
612 * @yb: (out) (optional): The Y-coordinate of the blue primary
613 * @red_trc: (out) (optional): The red component of the TRC.
614 * @green_trc: (out) (optional): The green component of the TRC (can be %NULL
615 * if it's the same as @red_trc).
616 * @blue_trc: (out) (optional): The blue component of the TRC (can be %NULL if
617 * it's the same as @red_trc).
619 * query the chromaticities of white point and primaries as well as trcs
620 * used for r g a nd b, all arguments are optional (can be %NULL).
622 void babl_space_get (const Babl
*space
,
623 double *xw
, double *yw
,
624 double *xr
, double *yr
,
625 double *xg
, double *yg
,
626 double *xb
, double *yb
,
627 const Babl
**red_trc
,
628 const Babl
**green_trc
,
629 const Babl
**blue_trc
);
632 * babl_space_get_rgb_luminance:
633 * @space: a BablSpace
634 * @red_luminance: (out) (optional): Location for the red luminance factor.
635 * @green_luminance: (out) (optional): Location for the green luminance factor.
636 * @blue_luminance: (out) (optional): Location for the blue luminance factor.
638 * Retrieve the relevant RGB luminance constants for a babl space.
640 * Note: these luminance coefficients should only ever be used on linear data.
641 * If your input @space is non-linear, you should convert your pixel values to
642 * the linearized variant of @space before making any computation with these
643 * coefficients. See #83.
646 babl_space_get_rgb_luminance (const Babl
*space
,
647 double *red_luminance
,
648 double *green_luminance
,
649 double *blue_luminance
);
654 * Returns: 0 if the name of the model in babl does not correspond to the
655 * provided model name.
657 int babl_model_is (const Babl
*babl
, const char *model_name
);
659 #define babl_model_is(babl,model) (babl&&(babl)==babl_model_with_space(model,babl))
663 * babl_space_get_icc:
665 * @length: (out) (optional): Length of the profile in bytes.
667 * Return pointer to ICC profile for space note that this is
668 * the ICC profile for R'G'B', though in formats only supporting linear
669 * like EXR GEGL chooses to load this lienar data as RGB and use the sRGB
672 * Returns: (transfer none) (array length=length) (element-type guint8): pointer to ICC profile data.
674 const char *babl_space_get_icc (const Babl
*babl
, int *length
);
677 * babl_space_from_rgbxyz_matrix:
678 * @name: (nullable): The name for the color space
679 * @wx: The X-coordinate of the color space's white point
680 * @wy: The Y-coordinate of the color space's white point
681 * @wz: The Z-coordinate of the color space's white point
682 * @rx: The X-coordinate of the red primary
683 * @ry: The Y-coordinate of the red primary
684 * @rz: The Z-coordinate of the red primary
685 * @gx: The X-coordinate of the green primary
686 * @gy: The Y-coordinate of the green primary
687 * @gz: The Z-coordinate of the green primary
688 * @bx: The X-coordinate of the blue primary
689 * @by: The Y-coordinate of the blue primary
690 * @bz: The Z-coordinate of the blue primary
691 * @trc_red: The red component of the TRC.
692 * @trc_green: (nullable): The green component of the TRC (can be %NULL if it's
693 * the same as @trc_red).
694 * @trc_blue: (nullable): The blue component of the TRC (can be %NULL if it's
695 * the same as @trc_red).
697 * Creates a new RGB matrix color space definition using a precomputed D50
698 * adapted 3x3 matrix and associated CIE XYZ whitepoint, as possibly read from
702 babl_space_from_rgbxyz_matrix (const char *name
,
703 double wx
, double wy
, double wz
,
704 double rx
, double gx
, double bx
,
705 double ry
, double gy
, double by
,
706 double rz
, double gz
, double bz
,
708 const Babl
*trc_green
,
709 const Babl
*trc_blue
);
712 * babl_format_get_encoding:
714 * Returns the components and data type, without space suffix.
716 const char * babl_format_get_encoding (const Babl
*babl
);
722 * Returns: 1 if @space is RGB, 0 otherwise.
724 int babl_space_is_rgb (const Babl
*space
);
727 * babl_space_is_cmyk:
730 * Returns: 1 if @space is CMYK, 0 otherwise.
732 int babl_space_is_cmyk (const Babl
*space
);
735 * babl_space_is_gray:
738 * Returns: 1 if @space is grayscale, 0 otherwise.
740 int babl_space_is_gray (const Babl
*space
);
742 typedef void (*BablFishProcess
) (const Babl
*babl
, const char *src
, char *dst
, long n
, void *data
);
744 * babl_fish_get_process: (skip)
746 * get the dispatch function of a fish, this allows faster use of a fish
747 * in a loop than the more indirect method of babl_process, this also avoids
748 * base-level instrumentation.
750 BablFishProcess
babl_fish_get_process (const Babl
*babl
);
756 * Do a babl fish garbage collection cycle, should only be called
757 * from the main thread with no concurrent babl processing in other
758 * threads in paralell.
765 /* values below this are stored associated with this value, it should also be
766 * used as a generic alpha zero epsilon in GEGL to keep the threshold effects
767 * on one known value.
769 #define BABL_ALPHA_FLOOR (1/65536.0)
770 #define BABL_ALPHA_FLOOR_F (1/65536.0f)
780 * International Color Consortium is a registered trademarks of the.
781 * International Color Consortium.
782 * Apple is a trademark or registered trademark of Apple Inc in many countries.
783 * Adobish is meant to concisely convey resemblence/compatibility with Adobe
784 * RGB- without actualy being it, Adobe is a trademark or registered trademark
785 * of Adobe Systems Incorporated in many countires.