| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
|
| 13 |
#include <sparse/general.h> |
| 14 |
#include <sparse/QuadTree.h> |
| 15 |
#include <edgepaint/lab.h> |
| 16 |
#include <math.h> |
| 17 |
#include <stdio.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
#include <sparse/color_palette.h> |
| 21 |
#include <edgepaint/lab_gamut.h> |
| 22 |
#include <util/alloc.h> |
| 23 |
#include <util/prisize_t.h> |
| 24 |
|
| 25 |
color_rgb color_rgb_init(double r, double g, double b){ |
| 26 |
color_rgb rgb; |
| 27 |
rgb.r = r; rgb.g = g; rgb.b = b; |
| 28 |
return rgb; |
| 29 |
} |
| 30 |
|
| 31 |
color_xyz color_xyz_init(double x, double y, double z){ |
| 32 |
color_xyz xyz; |
| 33 |
xyz.x = x; xyz.y = y; xyz.z = z; |
| 34 |
return xyz; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
color_lab color_lab_init(double l, double a, double b){ |
| 39 |
color_lab lab; |
| 40 |
lab.l = l; lab.a = a; lab.b = b; |
| 41 |
return lab; |
| 42 |
} |
| 43 |
|
| 44 |
double XYZEpsilon = 216./24389.; |
| 45 |
double XYZKappa = 24389./27.; |
| 46 |
|
| 47 |
static double PivotXYZ(double n){ |
| 48 |
if (n > XYZEpsilon) return pow(n, 1/3.); |
| 49 |
return (XYZKappa*n + 16)/116; |
| 50 |
} |
| 51 |
|
| 52 |
static double PivotRgb(double n){ |
| 53 |
if (n > 0.04045) return 100*pow((n + 0.055)/1.055, 2.4); |
| 54 |
return 100*n/12.92; |
| 55 |
} |
| 56 |
|
| 57 |
color_xyz RGB2XYZ(color_rgb color){ |
| 58 |
double r = PivotRgb(color.r/255.0); |
| 59 |
double g = PivotRgb(color.g/255.0); |
| 60 |
double b = PivotRgb(color.b/255.0); |
| 61 |
return color_xyz_init(r*0.4124 + g*0.3576 + b*0.1805, r*0.2126 + g*0.7152 + b*0.0722, r*0.0193 + g*0.1192 + b*0.9505); |
| 62 |
} |
| 63 |
|
| 64 |
color_lab RGB2LAB(color_rgb color){ |
| 65 |
color_xyz white = color_xyz_init(95.047, 100.000, 108.883); |
| 66 |
color_xyz xyz = RGB2XYZ(color); |
| 67 |
double x = PivotXYZ(xyz.x/white.x); |
| 68 |
double y = PivotXYZ(xyz.y/white.y); |
| 69 |
double z = PivotXYZ(xyz.z/white.z); |
| 70 |
double L = MAX(0, 116*y - 16); |
| 71 |
double A = 500*(x - y); |
| 72 |
double B = 200*(y - z); |
| 73 |
return color_lab_init(L, A, B); |
| 74 |
} |
| 75 |
|
| 76 |
void LAB2RGB_real_01(double *color){ |
| 77 |
/* convert an array[3] of LAB colors to RGB between 0 to 1, in place */ |
| 78 |
const color_lab lab = {.l = color[0], .a = color[1], .b = color[2]}; |
| 79 |
const color_rgb rgb = LAB2RGB(lab); |
| 80 |
color[0] = rgb.r/255; |
| 81 |
color[1] = rgb.g/255; |
| 82 |
color[2] = rgb.b/255; |
| 83 |
} |
| 84 |
color_rgb LAB2RGB(color_lab color){ |
| 85 |
double y = (color.l + 16.0)/116.0; |
| 86 |
double x = color.a/500.0 + y; |
| 87 |
double z = y - color.b/200.0; |
| 88 |
color_xyz white = color_xyz_init(95.047, 100.000, 108.883), xyz; |
| 89 |
double t1, t2, t3; |
| 90 |
if(pow(x, 3.) > XYZEpsilon){ |
| 91 |
t1 = pow(x, 3.); |
| 92 |
} else { |
| 93 |
t1 = (x - 16.0/116.0)/7.787; |
| 94 |
} |
| 95 |
if (color.l > (XYZKappa*XYZEpsilon)){ |
| 96 |
t2 = pow(((color.l + 16.0)/116.0), 3.); |
| 97 |
} else { |
| 98 |
t2 = color.l/XYZKappa; |
| 99 |
} |
| 100 |
if (pow(z, 3.) > XYZEpsilon){ |
| 101 |
t3 = pow(z, 3.); |
| 102 |
} else { |
| 103 |
t3 = (z - 16.0/116.0)/7.787; |
| 104 |
} |
| 105 |
xyz = color_xyz_init(white.x*t1, white.y*t2, white.z*t3); |
| 106 |
return XYZ2RGB(xyz); |
| 107 |
} |
| 108 |
|
| 109 |
color_rgb XYZ2RGB(color_xyz color){ |
| 110 |
double x = color.x/100.0; |
| 111 |
double y = color.y/100.0; |
| 112 |
double z = color.z/100.0; |
| 113 |
double r = x*3.2406 + y*(-1.5372) + z*(-0.4986); |
| 114 |
double g = x*(-0.9689) + y*1.8758 + z*0.0415; |
| 115 |
double b = x*0.0557 + y*(-0.2040) + z*1.0570; |
| 116 |
if (r > 0.0031308){ |
| 117 |
r = 1.055*pow(r, 1/2.4) - 0.055; |
| 118 |
} else { |
| 119 |
r = 12.92*r; |
| 120 |
} |
| 121 |
if (g > 0.0031308) { |
| 122 |
g = 1.055*pow(g, 1/2.4) - 0.055; |
| 123 |
} else { |
| 124 |
g = 12.92*g; |
| 125 |
} |
| 126 |
if (b > 0.0031308){ |
| 127 |
b = 1.055*pow(b, 1/2.4) - 0.055; |
| 128 |
} else { |
| 129 |
b = 12.92*b; |
| 130 |
} |
| 131 |
r = MAX(0, r); |
| 132 |
r = MIN(255, r*255); |
| 133 |
g = MAX(0, g); |
| 134 |
g = MIN(255, g*255); |
| 135 |
b = MAX(0, b); |
| 136 |
b = MIN(255, b*255); |
| 137 |
|
| 138 |
return color_rgb_init(r, g, b); |
| 139 |
} |
| 140 |
|
| 141 |
double *lab_gamut(const int *lightness, int *n) { |
| 142 |
/* give a list of n points in the file defining the LAB color gamut. |
| 143 |
*/ |
| 144 |
double *xx, *x; |
| 145 |
|
| 146 |
int l1 = lightness[0]; |
| 147 |
int l2 = lightness[1]; |
| 148 |
|
| 149 |
if (l1 < 0) l1 = 0; |
| 150 |
if (l2 > 100) l2 = 100; |
| 151 |
if (l1 > l2) l1 = l2; |
| 152 |
|
| 153 |
if (Verbose) |
| 154 |
fprintf(stderr,"LAB color lightness range = %d,%d\n", l1, l2); |
| 155 |
|
| 156 |
if (Verbose) |
| 157 |
fprintf(stderr,"size of lab gamut = %" PRISIZE_T "\n", lab_gamut_data_size); |
| 158 |
|
| 159 |
// each L* value can be paired with 256 a* values and 256 b* values, so |
| 160 |
// compute the maximum number of doubles we will need to span the space |
| 161 |
size_t m = ((size_t)l2 - (size_t)l1 + 1) * 256 * 256 * 3; |
| 162 |
|
| 163 |
x = gv_calloc(m, sizeof(double)); |
| 164 |
xx = x; |
| 165 |
*n = 0; |
| 166 |
for (size_t i = 0; i < lab_gamut_data_size; i += 4){ |
| 167 |
if (lab_gamut_data[i] >= l1 && lab_gamut_data[i] <= l2){ |
| 168 |
int b_lower = lab_gamut_data[i + 2]; |
| 169 |
int b_upper = lab_gamut_data[i + 3]; |
| 170 |
for (int b = b_lower; b <= b_upper; ++b) { |
| 171 |
xx[0] = lab_gamut_data[i]; |
| 172 |
xx[1] = lab_gamut_data[i+1]; |
| 173 |
xx[2] = b; |
| 174 |
xx += 3; |
| 175 |
(*n)++; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return x; |
| 181 |
} |
| 182 |
|
| 183 |
QuadTree lab_gamut_quadtree(const int *lightness, |
| 184 |
int max_qtree_level) { |
| 185 |
/* read the color gamut points list in the form "x y z\n ..." and store in the octtree */ |
| 186 |
int n; |
| 187 |
double *x = lab_gamut(lightness, &n); |
| 188 |
QuadTree qt; |
| 189 |
int dim = 3; |
| 190 |
|
| 191 |
if (!x) return NULL; |
| 192 |
qt = QuadTree_new_from_point_list(dim, n, max_qtree_level, x); |
| 193 |
|
| 194 |
|
| 195 |
free(x); |
| 196 |
return qt; |
| 197 |
} |
| 198 |
|
| 199 |
static double lab_dist(color_lab x, color_lab y){ |
| 200 |
return sqrt((x.l-y.l)*(x.l-y.l) +(x.a-y.a)*(x.a-y.a) +(x.b-y.b)*(x.b-y.b)); |
| 201 |
} |
| 202 |
|
| 203 |
static void lab_interpolate(color_lab lab1, color_lab lab2, double t, double *colors){ |
| 204 |
colors[0] = lab1.l + t*(lab2.l - lab1.l); |
| 205 |
colors[1] = lab1.a + t*(lab2.a - lab1.a); |
| 206 |
colors[2] = lab1.b + t*(lab2.b - lab1.b); |
| 207 |
} |
| 208 |
|
| 209 |
double *color_blend_rgb2lab(const char *color_list, const int maxpoints) { |
| 210 |
/* give a color list of the form "#ff0000,#00ff00,...", get a list of around maxpoints |
| 211 |
colors in an array colors0 of size [maxpoints*3] of the form {{l,a,b},...}. |
| 212 |
color_list: either "#ff0000,#00ff00,...", or "pastel" |
| 213 |
*/ |
| 214 |
|
| 215 |
int nc = 1, r, g, b, i, ii, jj, cdim = 3; |
| 216 |
color_rgb rgb; |
| 217 |
double step, dist_current; |
| 218 |
const char *cp = color_palettes_get(color_list); |
| 219 |
if (cp){ |
| 220 |
color_list = cp; |
| 221 |
} |
| 222 |
|
| 223 |
if (maxpoints <= 0) return NULL; |
| 224 |
|
| 225 |
const char *cl = color_list; |
| 226 |
while ((cl=strchr(cl, ',')) != NULL){ |
| 227 |
cl++; nc++; |
| 228 |
} |
| 229 |
color_lab *lab = gv_calloc(MAX(nc, 1), sizeof(color_lab)); |
| 230 |
|
| 231 |
cl = color_list - 1; |
| 232 |
nc = 0; |
| 233 |
do { |
| 234 |
cl++; |
| 235 |
if (sscanf(cl,"#%02X%02X%02X", &r, &g, &b) != 3) break; |
| 236 |
rgb.r = r; rgb.g = g; rgb.b = b; |
| 237 |
lab[nc++] = RGB2LAB(rgb); |
| 238 |
} while ((cl=strchr(cl, ',')) != NULL); |
| 239 |
|
| 240 |
double *dists = gv_calloc(MAX(1, nc), sizeof(double)); |
| 241 |
dists[0] = 0; |
| 242 |
for (i = 0; i < nc - 1; i++){ |
| 243 |
dists[i+1] = lab_dist(lab[i], lab[i+1]); |
| 244 |
} |
| 245 |
/* dists[i] is now the summed color distance from the 0-th color to the i-th color */ |
| 246 |
for (i = 0; i < nc - 1; i++){ |
| 247 |
dists[i+1] += dists[i]; |
| 248 |
} |
| 249 |
if (Verbose) |
| 250 |
fprintf(stderr,"sum = %f\n", dists[nc-1]); |
| 251 |
|
| 252 |
double *colors = gv_calloc(maxpoints * cdim, sizeof(double)); |
| 253 |
if (maxpoints == 1){ |
| 254 |
colors[0] = lab[0].l; |
| 255 |
colors[1] = lab[0].a; |
| 256 |
colors[2] = lab[0].b; |
| 257 |
} else { |
| 258 |
step = dists[nc-1]/(maxpoints - 1); |
| 259 |
ii = 0; jj = 0; dist_current = 0; |
| 260 |
while (dists[jj] < dists[ii] + step) jj++; |
| 261 |
|
| 262 |
double *colors_ptr = colors; |
| 263 |
for (i = 0; i < maxpoints; i++){ |
| 264 |
lab_interpolate(lab[ii], lab[jj], (dist_current - dists[ii]) / |
| 265 |
MAX(0.001, (dists[jj] - dists[ii])), colors_ptr); |
| 266 |
dist_current += step; |
| 267 |
colors_ptr += cdim; |
| 268 |
if (dist_current > dists[jj]) ii = jj; |
| 269 |
while (jj < nc -1 && dists[jj] < dists[ii] + step) jj++; |
| 270 |
} |
| 271 |
} |
| 272 |
free(dists); |
| 273 |
free(lab); |
| 274 |
return colors; |
| 275 |
} |