nk 1.0.0
C++ Numerical Computing Runtime
Loading...
Searching...
No Matches
color.hpp
Go to the documentation of this file.
1// toolboxes/image/include/nk/image/color/color.hpp
2//
3// Colour space conversions and colormaps.
4
5#pragma once
6
7#include <memory_resource>
8#include <nk/value/value.hpp>
9
10#include <string>
11#include <vector>
12
13namespace nk::image {
14
17
18
33
44void imsplit(const Value &I, std::vector<Value> &planes,
45 std::pmr::memory_resource *mr = nullptr);
46
47// ── RGB ↔ HSV / YCbCr / NTSC / XYZ / Lab ─────────────────────────────
48
58Value rgb2hsv(const Value &x, std::pmr::memory_resource *mr = nullptr);
59
64Value hsv2rgb(const Value &x, std::pmr::memory_resource *mr = nullptr);
65
71Value rgb2ycbcr(const Value &x, std::pmr::memory_resource *mr = nullptr);
72
76Value ycbcr2rgb(const Value &x, std::pmr::memory_resource *mr = nullptr);
77
86Value rgb2ntsc(const Value &x, std::pmr::memory_resource *mr = nullptr);
87
91Value ntsc2rgb(const Value &x, std::pmr::memory_resource *mr = nullptr);
92
101Value rgb2xyz(const Value &x, std::pmr::memory_resource *mr = nullptr);
102
106Value xyz2rgb(const Value &x, std::pmr::memory_resource *mr = nullptr);
107
118Value rgb2lab(const Value &x, std::pmr::memory_resource *mr = nullptr);
119
123Value lab2rgb(const Value &x, std::pmr::memory_resource *mr = nullptr);
124
133Value rgb2lightness(const Value &RGB, std::pmr::memory_resource *mr = nullptr);
134
155std::pair<Value, Value>
156cmunique_xm(const Value &X, const Value &MAP,
157 std::pmr::memory_resource *mr = nullptr);
158std::pair<Value, Value>
159cmunique_rgb(const Value &RGB,
160 std::pmr::memory_resource *mr = nullptr);
161std::pair<Value, Value>
162cmunique_i(const Value &I,
163 std::pmr::memory_resource *mr = nullptr);
164
177std::pair<Value, Value>
178rgb2ind_inmap(const Value &RGB, const Value &cmap, std::pmr::memory_resource *mr = nullptr);
179
183Value xyz2lab(const Value &x, std::pmr::memory_resource *mr = nullptr);
184
188Value lab2xyz(const Value &x, std::pmr::memory_resource *mr = nullptr);
189
190// ── Lab class conversions ───────────────────────────────────────────
191//
192// **Scale conventions:**
193// - DOUBLE / SINGLE: `L* ∈ [0, 100]`, `a*/b* ∈ [-128, 127]`.
194// - UINT8: `L* ∈ [0, 255]`, `a*/b* ∈ [0, 255]` with offset 128.
195// - UINT16: `L* ∈ [0, 65280]`, `a*/b* ∈ [0, 65280]` with offset 32768.
196
201Value lab2double(const Value &lab, std::pmr::memory_resource *mr = nullptr);
202
206Value lab2single(const Value &lab, std::pmr::memory_resource *mr = nullptr);
207
211Value lab2uint8(const Value &lab, std::pmr::memory_resource *mr = nullptr);
212
216Value lab2uint16(const Value &lab, std::pmr::memory_resource *mr = nullptr);
217
218// ── Colormap synthesis ──────────────────────────────────────────────
219
232Value colorgradient(const Value &C, const Value &w, int n,
233 std::pmr::memory_resource *mr = nullptr);
234
246Value wavelength2rgb(const Value &wavelength, const std::string &out_class,
247 double gamma, std::pmr::memory_resource *mr = nullptr);
248
261Value colorangle(const Value &rgb1, const Value &rgb2,
262 std::pmr::memory_resource *mr = nullptr);
263
264// ── Standard colormaps ───────────────────────────────────────
265//
266// All follow the convention:
267// - `n` defaults to 256 in adapters (we don't track figure state).
268// - `n == 1` → palette's "extreme" row.
269// - `n <= 0` → `0 × 3` empty.
270// - Output is DOUBLE `N × 3`.
271
276Value gray_cmap(int n, std::pmr::memory_resource *mr = nullptr);
277
282Value hot_cmap(int n, std::pmr::memory_resource *mr = nullptr);
283
288Value cool_cmap(int n, std::pmr::memory_resource *mr = nullptr);
289
294Value spring_cmap(int n, std::pmr::memory_resource *mr = nullptr);
295
300Value summer_cmap(int n, std::pmr::memory_resource *mr = nullptr);
301
306Value autumn_cmap(int n, std::pmr::memory_resource *mr = nullptr);
307
312Value winter_cmap(int n, std::pmr::memory_resource *mr = nullptr);
313
319Value copper_cmap(int n, std::pmr::memory_resource *mr = nullptr);
320
326Value pink_cmap(int n, std::pmr::memory_resource *mr = nullptr);
327
332Value hsv_cmap(int n, std::pmr::memory_resource *mr = nullptr);
333
338Value flag_cmap(int n, std::pmr::memory_resource *mr = nullptr);
339
344Value prism_cmap(int n, std::pmr::memory_resource *mr = nullptr);
345
350Value lines_cmap(int n, std::pmr::memory_resource *mr = nullptr);
351
357Value bone_cmap(int n, std::pmr::memory_resource *mr = nullptr);
358
362Value white_cmap(int n, std::pmr::memory_resource *mr = nullptr);
363
364// ── Gamma helpers / XYZ class conversion / brighten / contrast ──────
365
377Value rgb2lin(const Value &A, std::pmr::memory_resource *mr = nullptr);
378
389Value lin2rgb(const Value &A, std::pmr::memory_resource *mr = nullptr);
390
400Value xyz2double(const Value &xyz, std::pmr::memory_resource *mr = nullptr);
401
411Value xyz2uint16(const Value &xyz, std::pmr::memory_resource *mr = nullptr);
412
423Value brighten(const Value &map, double beta,
424 std::pmr::memory_resource *mr = nullptr);
425
436Value contrast(const Value &x, int m, std::pmr::memory_resource *mr = nullptr);
437
438// ── Colour difference / whitepoints / utility ───────────────────────
439
465Value rgbwide2ycbcr(const Value &RGB, int bits_per_sample,
466 std::pmr::memory_resource *mr = nullptr);
467
496Value ycbcr2rgbwide(const Value &YCBCR, int bits_per_sample,
497 std::pmr::memory_resource *mr = nullptr);
498
535Value imcolordiff(const Value &I1, const Value &I2,
536 const std::string &standard, bool is_input_lab,
537 double kL, double kC, double kH, double K1, double K2,
538 std::pmr::memory_resource *mr = nullptr);
539
552Value deltaE(const Value &I1, const Value &I2, bool isInputLab,
553 std::pmr::memory_resource *mr = nullptr);
554
565Value whitepoint(const std::string &illuminant,
566 std::pmr::memory_resource *mr = nullptr);
567
576Value cmap2gray(const Value &cmap, std::pmr::memory_resource *mr = nullptr);
577
578// ── White-balance illumination estimation ─────────────────────────────
579
596Value illumwhite(const Value &A, double P, const Value &mask,
597 std::pmr::memory_resource *mr = nullptr);
598
623Value illumpca(const Value &A, double P, const Value &mask,
624 std::pmr::memory_resource *mr = nullptr);
625
643Value illumgray(const Value &A, const std::vector<double> &P,
644 const Value &mask, std::pmr::memory_resource *mr = nullptr);
645
663Value label2rgb(const Value &L, const Value &cmap, const Value &background,
664 std::pmr::memory_resource *mr = nullptr);
665
714Value imfuse(const Value &A, const Value &B,
715 const std::string &method,
716 const std::string &scaling,
717 const Value &channels,
718 std::pmr::memory_resource *mr = nullptr);
719
748Value tonemap(const Value &HDR,
749 double lremap_lo, double lremap_hi,
750 double saturation,
751 int ntilesR, int ntilesC,
752 std::pmr::memory_resource *mr = nullptr);
753
811Value labeloverlay(const Value &A, const Value &L,
812 const Value &colormap,
813 const std::string &color_assignment,
814 const Value &included_labels,
815 double transparency,
816 std::pmr::memory_resource *mr = nullptr);
817
873Value chromadapt(const Value &A, const Value &illuminant,
874 const std::string &method,
875 const std::string &color_space,
876 std::pmr::memory_resource *mr = nullptr);
877
917Value rgbwide2xyz(const Value &RGB, int bits_per_sample,
918 const std::string &color_space,
919 const std::string &linearization,
920 std::pmr::memory_resource *mr = nullptr);
921
935Value xyz2rgbwide(const Value &XYZ, int bits_per_sample,
936 const std::string &color_space,
937 const std::string &linearization,
938 std::pmr::memory_resource *mr = nullptr);
939
969Value demosaic(const Value &I, const std::string &sensorAlignment,
970 int bitsPerSample = 0,
971 std::pmr::memory_resource *mr = nullptr);
972
984Value raw2planar(const Value &cfa, std::pmr::memory_resource *mr = nullptr);
985
996Value planar2raw(const Value &I, std::pmr::memory_resource *mr = nullptr);
997
998
1000} // namespace nk::image
Core MATLAB-compatible multidimensional array and scalar value type.
Definition value.hpp:61
Value chromadapt(const Value &A, const Value &illuminant, const std::string &method, const std::string &color_space, std::pmr::memory_resource *mr=nullptr)
Chromatic adaptation for white-balance correction (B = chromadapt(A, illuminant, ....
Value ycbcr2rgbwide(const Value &YCBCR, int bits_per_sample, std::pmr::memory_resource *mr=nullptr)
BT.2020 / BT.2100 wide-gamut YCbCr → RGB (rgb = ycbcr2rgbwide(YCBCR, bps)).
Value rgbwide2ycbcr(const Value &RGB, int bits_per_sample, std::pmr::memory_resource *mr=nullptr)
BT.2020 / BT.2100 wide-gamut RGB → YCbCr (ycbcr = rgbwide2ycbcr(RGB, bps)).
Value contrast(const Value &x, int m, std::pmr::memory_resource *mr=nullptr)
Histogram-equalising gray colormap (cmap = contrast(x, m)).
Value lab2rgb(const Value &x, std::pmr::memory_resource *mr=nullptr)
CIE L*a*b* → sRGB (inverse of rgb2lab).
Value lab2uint8(const Value &lab, std::pmr::memory_resource *mr=nullptr)
Cast Lab image to UINT8 (labu = lab2uint8(lab)).
Value cool_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Cyan → magenta colormap (map = cool(n)).
Value bone_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Grayscale-with-blue-tint colormap (map = bone(n)).
Value illumpca(const Value &A, double P, const Value &mask, std::pmr::memory_resource *mr=nullptr)
PCA-based illuminant estimate (illum = illumpca(A [, P] [, 'Mask', M])).
Value lab2single(const Value &lab, std::pmr::memory_resource *mr=nullptr)
Cast Lab image to SINGLE (labs = lab2single(lab)).
Value gray_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Grayscale colormap (map = gray(n)).
Value pink_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Pastel-pink colormap (map = pink(n)).
Value rgb2lin(const Value &A, std::pmr::memory_resource *mr=nullptr)
sRGB → linear RGB gamma inverse (B = rgb2lin(A)).
Value lab2double(const Value &lab, std::pmr::memory_resource *mr=nullptr)
Cast Lab image to DOUBLE (labd = lab2double(lab)).
Value xyz2rgb(const Value &x, std::pmr::memory_resource *mr=nullptr)
CIE XYZ → sRGB (inverse of rgb2xyz).
Value whitepoint(const std::string &illuminant, std::pmr::memory_resource *mr=nullptr)
CIE reference-illuminant tristimulus (wp = whitepoint(illuminant)).
Value copper_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Copper-tinted colormap (map = copper(n)).
Value colorgradient(const Value &C, const Value &w, int n, std::pmr::memory_resource *mr=nullptr)
Smooth colormap through K anchor colours (M = colorgradient(C, w, n)).
Value white_cmap(int n, std::pmr::memory_resource *mr=nullptr)
All-ones colormap (map = white(n)).
Value rgb2hsv(const Value &x, std::pmr::memory_resource *mr=nullptr)
RGB → HSV (hsv = rgb2hsv(rgb)).
Value planar2raw(const Value &I, std::pmr::memory_resource *mr=nullptr)
Re-interleave 4 sensor planes into a full Bayer CFA mosaic (cfa = planar2raw(I)).
Value brighten(const Value &map, double beta, std::pmr::memory_resource *mr=nullptr)
Gamma-adjust a colormap (rmap = brighten(map, beta)).
Value autumn_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Red → yellow colormap (map = autumn(n)).
Value labeloverlay(const Value &A, const Value &L, const Value &colormap, const std::string &color_assignment, const Value &included_labels, double transparency, std::pmr::memory_resource *mr=nullptr)
Overlay a label / mask / categorical-index image on a 2-D base (B = labeloverlay(A,...
Value prism_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Cyclic 6-row rainbow palette (map = prism(n)).
Value label2rgb(const Value &L, const Value &cmap, const Value &background, std::pmr::memory_resource *mr=nullptr)
Colourise a labelled image (RGB = label2rgb(L, cmap, background)).
Value xyz2uint16(const Value &xyz, std::pmr::memory_resource *mr=nullptr)
XYZ → UINT16 ICC encoding (xyzu16 = xyz2uint16(xyz)).
Value rgb2xyz(const Value &x, std::pmr::memory_resource *mr=nullptr)
sRGB → CIE XYZ (D65) (xyz = rgb2xyz(rgb)).
Value xyz2lab(const Value &x, std::pmr::memory_resource *mr=nullptr)
CIE XYZ → CIE L*a*b* (D50 ICC reference white).
Value summer_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Green → yellow colormap (map = summer(n)).
Value cmap2gray(const Value &cmap, std::pmr::memory_resource *mr=nullptr)
RGB colormap → grayscale colormap (gmap = cmap2gray(cmap)).
Value lin2rgb(const Value &A, std::pmr::memory_resource *mr=nullptr)
Linear RGB → sRGB gamma forward (B = lin2rgb(A)).
Value hsv_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Hue-rotation colormap (map = hsv(n)).
Value tonemap(const Value &HDR, double lremap_lo, double lremap_hi, double saturation, int ntilesR, int ntilesC, std::pmr::memory_resource *mr=nullptr)
Render high-dynamic-range image for low-dynamic-range display (RGB = tonemap(HDR, ....
Value colorangle(const Value &rgb1, const Value &rgb2, std::pmr::memory_resource *mr=nullptr)
Angle between two RGB colours (theta = colorangle(rgb1, rgb2)).
Value lines_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Axes-color-order palette (map = lines(n)).
Value imcolordiff(const Value &I1, const Value &I2, const std::string &standard, bool is_input_lab, double kL, double kC, double kH, double K1, double K2, std::pmr::memory_resource *mr=nullptr)
CIE94 / CIEDE2000 colour difference (dE = imcolordiff(I1, I2, ...)).
Value rgb2lightness(const Value &RGB, std::pmr::memory_resource *mr=nullptr)
Lightness L* (L = rgb2lightness(RGB)).
Value spring_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Magenta → yellow colormap (map = spring(n)).
Value ntsc2rgb(const Value &x, std::pmr::memory_resource *mr=nullptr)
NTSC YIQ → RGB (inverse of rgb2ntsc).
Value flag_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Cyclic red/white/blue/black colormap (map = flag(n)).
Value xyz2double(const Value &xyz, std::pmr::memory_resource *mr=nullptr)
XYZ → DOUBLE (xyzd = xyz2double(xyz)).
Value raw2planar(const Value &cfa, std::pmr::memory_resource *mr=nullptr)
Deinterleave a Bayer CFA mosaic into its four sensor planes (P = raw2planar(cfa)).
Value ycbcr2rgb(const Value &x, std::pmr::memory_resource *mr=nullptr)
YCbCr → RGB (inverse of rgb2ycbcr).
Value rgbwide2xyz(const Value &RGB, int bits_per_sample, const std::string &color_space, const std::string &linearization, std::pmr::memory_resource *mr=nullptr)
Narrow-range wide-gamut RGB → CIE 1931 XYZ (XYZ = rgbwide2xyz(RGB, BPS, ...)).
Value hot_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Black-red-yellow-white colormap (map = hot(n)).
Value imfuse(const Value &A, const Value &B, const std::string &method, const std::string &scaling, const Value &channels, std::pmr::memory_resource *mr=nullptr)
Composite two images for visual comparison (C = imfuse(A, B, method, ...)).
Value illumgray(const Value &A, const std::vector< double > &P, const Value &mask, std::pmr::memory_resource *mr=nullptr)
Grey-World illuminant estimate (illum = illumgray(A [, P] [, 'Mask', M])).
Value lab2xyz(const Value &x, std::pmr::memory_resource *mr=nullptr)
CIE L*a*b* → CIE XYZ (inverse of xyz2lab).
void imsplit(const Value &I, std::vector< Value > &planes, std::pmr::memory_resource *mr=nullptr)
Split a multi-plane image into per-plane Values (imsplit(I, planes)).
Value rgb2lab(const Value &x, std::pmr::memory_resource *mr=nullptr)
sRGB → CIE L*a*b* (D65) (lab = rgb2lab(rgb)).
std::pair< Value, Value > rgb2ind_inmap(const Value &RGB, const Value &cmap, std::pmr::memory_resource *mr=nullptr)
RGB → indexed image with a fixed colormap ([ind, cmap] = rgb2ind(RGB, cmap)).
Value illumwhite(const Value &A, double P, const Value &mask, std::pmr::memory_resource *mr=nullptr)
White-Patch illuminant estimate (illum = illumwhite(A [, P] [, 'Mask', M])).
Value demosaic(const Value &I, const std::string &sensorAlignment, int bitsPerSample=0, std::pmr::memory_resource *mr=nullptr)
Convert a Bayer-encoded mosaic to a truecolor RGB image (RGB = demosaic(I, sensorAlignment,...
Value lab2uint16(const Value &lab, std::pmr::memory_resource *mr=nullptr)
Cast Lab image to UINT16 (labu = lab2uint16(lab)).
Value winter_cmap(int n, std::pmr::memory_resource *mr=nullptr)
Blue → cyan-ish colormap (map = winter(n)).
Value rgb2ycbcr(const Value &x, std::pmr::memory_resource *mr=nullptr)
RGB → YCbCr (BT.601) (ycbcr = rgb2ycbcr(rgb)).
Value rgb2ntsc(const Value &x, std::pmr::memory_resource *mr=nullptr)
RGB → NTSC YIQ (yiq = rgb2ntsc(rgb)).
Value wavelength2rgb(const Value &wavelength, const std::string &out_class, double gamma, std::pmr::memory_resource *mr=nullptr)
Wavelength → RGB (Bruton 1996) (RGB = wavelength2rgb(wavelength, class, gamma)).
Value hsv2rgb(const Value &x, std::pmr::memory_resource *mr=nullptr)
HSV → RGB (inverse of rgb2hsv).
Value deltaE(const Value &I1, const Value &I2, bool isInputLab, std::pmr::memory_resource *mr=nullptr)
CIE76 colour difference (delE = deltaE(I1, I2, isInputLab)).
std::pair< Value, Value > cmunique_xm(const Value &X, const Value &MAP, std::pmr::memory_resource *mr=nullptr)
Eliminate duplicate colors in colormap; convert grayscale or RGB to indexed ([Y, newmap] = cmunique(....
Value xyz2rgbwide(const Value &XYZ, int bits_per_sample, const std::string &color_space, const std::string &linearization, std::pmr::memory_resource *mr=nullptr)
CIE 1931 XYZ → narrow-range wide-gamut RGB (RGB = xyz2rgbwide(XYZ, BPS, ...)).