1 |
|
|
/* |
2 |
|
|
Teem: Tools to process and visualize scientific data and images . |
3 |
|
|
Copyright (C) 2013, 2012, 2011, 2010, 2009 University of Chicago |
4 |
|
|
Copyright (C) 2008, 2007, 2006, 2005 Gordon Kindlmann |
5 |
|
|
Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998 University of Utah |
6 |
|
|
|
7 |
|
|
This library is free software; you can redistribute it and/or |
8 |
|
|
modify it under the terms of the GNU Lesser General Public License |
9 |
|
|
(LGPL) as published by the Free Software Foundation; either |
10 |
|
|
version 2.1 of the License, or (at your option) any later version. |
11 |
|
|
The terms of redistributing and/or modifying this software also |
12 |
|
|
include exceptions to the LGPL that facilitate static linking. |
13 |
|
|
|
14 |
|
|
This library is distributed in the hope that it will be useful, |
15 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 |
|
|
Lesser General Public License for more details. |
18 |
|
|
|
19 |
|
|
You should have received a copy of the GNU Lesser General Public License |
20 |
|
|
along with this library; if not, write to Free Software Foundation, Inc., |
21 |
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
|
25 |
|
|
#include "dye.h" |
26 |
|
|
|
27 |
|
|
const int |
28 |
|
|
dyePresent = 42; |
29 |
|
|
|
30 |
|
|
const char * |
31 |
|
|
dyeBiffKey = "dye"; |
32 |
|
|
|
33 |
|
|
const char * |
34 |
|
|
dyeSpaceToStr[DYE_MAX_SPACE+1] = { |
35 |
|
|
"(unknown)", |
36 |
|
|
"HSV", |
37 |
|
|
"HSL", |
38 |
|
|
"RGB", |
39 |
|
|
"XYZ", |
40 |
|
|
"LAB", |
41 |
|
|
"LUV", |
42 |
|
|
"LCH" |
43 |
|
|
}; |
44 |
|
|
|
45 |
|
|
static const char * |
46 |
|
|
_dyeSpaceDesc[DYE_MAX_SPACE+1] = { |
47 |
|
|
"unknown colorspace", |
48 |
|
|
"single hexcone", |
49 |
|
|
"double hexcone", |
50 |
|
|
"traditional device primaries", |
51 |
|
|
"CIE 1931 XYZ space", |
52 |
|
|
"CIE L*a*b*", |
53 |
|
|
"CIE 1976 L*u*v*", |
54 |
|
|
"polar coord(L*a*b*)" |
55 |
|
|
}; |
56 |
|
|
|
57 |
|
|
static const airEnum |
58 |
|
|
_dyeSpace = { |
59 |
|
|
"colorspace", |
60 |
|
|
DYE_MAX_SPACE, |
61 |
|
|
dyeSpaceToStr, NULL, |
62 |
|
|
_dyeSpaceDesc, |
63 |
|
|
NULL, NULL, |
64 |
|
|
AIR_FALSE |
65 |
|
|
}; |
66 |
|
|
const airEnum *const |
67 |
|
|
dyeSpace = &_dyeSpace; |
68 |
|
|
|
69 |
|
|
/* |
70 |
|
|
** this function predates the dyeSpace airEnum, so we'll keep it. |
71 |
|
|
*/ |
72 |
|
|
int |
73 |
|
|
dyeStrToSpace(char *_str) { |
74 |
|
|
int spc; |
75 |
|
|
char *str; |
76 |
|
|
|
77 |
|
|
spc = dyeSpaceUnknown; |
78 |
|
|
if ( (str = airStrdup(_str)) ) { |
79 |
|
|
airToUpper(str); |
80 |
|
|
for (spc=0; spc<dyeSpaceLast; spc++) { |
81 |
|
|
if (!strcmp(str, dyeSpaceToStr[spc])) { |
82 |
|
|
break; |
83 |
|
|
} |
84 |
|
|
} |
85 |
|
|
if (dyeSpaceLast == spc) { |
86 |
|
|
spc = dyeSpaceUnknown; |
87 |
|
|
} |
88 |
|
|
str = (char *)airFree(str); |
89 |
|
|
} |
90 |
|
|
return spc; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
dyeColor * |
94 |
|
|
dyeColorInit(dyeColor *col) { |
95 |
|
|
|
96 |
|
|
if (col) { |
97 |
|
|
ELL_3V_SET(col->val[0], AIR_NAN, AIR_NAN, AIR_NAN); |
98 |
|
|
ELL_3V_SET(col->val[1], AIR_NAN, AIR_NAN, AIR_NAN); |
99 |
|
|
col->xWhite = col->yWhite = AIR_NAN; |
100 |
|
|
col->spc[0] = dyeSpaceUnknown; |
101 |
|
|
col->spc[1] = dyeSpaceUnknown; |
102 |
|
|
col->ii = 0; |
103 |
|
|
} |
104 |
|
|
return col; |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
dyeColor * |
108 |
|
|
dyeColorSet(dyeColor *col, int space, float v0, float v1, float v2) { |
109 |
|
|
|
110 |
|
|
if (col && DYE_VALID_SPACE(space)) { |
111 |
|
|
col->ii = AIR_CLAMP(0, col->ii, 1); |
112 |
|
|
|
113 |
|
|
/* We switch to the other one if the current one seems to be used, |
114 |
|
|
but we don't switch if new and current colorspaces are the same. |
115 |
|
|
If the other one is being used too, oh well. */ |
116 |
|
|
if (dyeSpaceUnknown != col->spc[col->ii] && |
117 |
|
|
AIR_EXISTS(col->val[col->ii][0]) && |
118 |
|
|
col->spc[col->ii] != space) { |
119 |
|
|
col->ii = 1 - col->ii; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
ELL_3V_SET(col->val[col->ii], v0, v1, v2); |
123 |
|
|
col->spc[col->ii] = space; |
124 |
|
|
} |
125 |
|
|
return col; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
int |
129 |
|
|
dyeColorGet(float *v0P, float *v1P, float *v2P, dyeColor *col) { |
130 |
|
|
int spc; |
131 |
|
|
|
132 |
|
|
spc = dyeSpaceUnknown; |
133 |
|
|
if (v0P && v1P && v2P && col) { |
134 |
|
|
col->ii = AIR_CLAMP(0, col->ii, 1); |
135 |
|
|
spc = col->spc[col->ii]; |
136 |
|
|
ELL_3V_GET(*v0P, *v1P, *v2P, col->val[col->ii]); |
137 |
|
|
} |
138 |
|
|
return spc; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
int |
142 |
|
|
dyeColorGetAs(float *v0P, float *v1P, float *v2P, |
143 |
|
|
dyeColor *colIn, int space) { |
144 |
|
|
dyeColor _col, *col; |
145 |
|
|
|
146 |
|
|
col = &_col; |
147 |
|
|
dyeColorCopy(col, colIn); |
148 |
|
|
/* hope for no error */ |
149 |
|
|
dyeConvert(col, space); |
150 |
|
|
return dyeColorGet(v0P, v1P, v2P, col); |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
dyeColor * |
154 |
|
|
dyeColorNew() { |
155 |
|
|
dyeColor *col; |
156 |
|
|
|
157 |
|
|
col = (dyeColor *)calloc(1, sizeof(dyeColor)); |
158 |
|
|
col = dyeColorInit(col); |
159 |
|
|
return col; |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
dyeColor * |
163 |
|
|
dyeColorCopy(dyeColor *c1, dyeColor *c0) { |
164 |
|
|
|
165 |
|
|
if (c1 && c0) { |
166 |
|
|
memcpy(c1, c0, sizeof(dyeColor)); |
167 |
|
|
} |
168 |
|
|
return c1; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
dyeColor * |
172 |
|
|
dyeColorNix(dyeColor *col) { |
173 |
|
|
|
174 |
|
|
if (col) { |
175 |
|
|
col = (dyeColor *)airFree(col); |
176 |
|
|
} |
177 |
|
|
return NULL; |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
int |
181 |
|
|
dyeColorParse(dyeColor *col, char *_str) { |
182 |
|
|
static const char me[]="dyeColorParse"; |
183 |
|
|
char *str; |
184 |
|
|
char *colon, *valS; |
185 |
|
|
float v0, v1, v2; |
186 |
|
|
int spc; |
187 |
|
|
|
188 |
|
|
if (!(col && _str)) { |
189 |
|
|
biffAddf(DYE, "%s: got NULL pointer", me); |
190 |
|
|
return 1; |
191 |
|
|
} |
192 |
|
|
if (!(str = airStrdup(_str))) { |
193 |
|
|
biffAddf(DYE, "%s: couldn't strdup!", me); |
194 |
|
|
return 1; |
195 |
|
|
} |
196 |
|
|
if (!(colon = strchr(str, ':'))) { |
197 |
|
|
biffAddf(DYE, "%s: given string \"%s\" didn't contain colon", me, str); |
198 |
|
|
return 1; |
199 |
|
|
} |
200 |
|
|
*colon = '\0'; |
201 |
|
|
valS = colon+1; |
202 |
|
|
if (3 != sscanf(valS, "%g,%g,%g", &v0, &v1, &v2)) { |
203 |
|
|
biffAddf(DYE, "%s: couldn't parse three floats from \"%s\"", me, valS); |
204 |
|
|
return 1; |
205 |
|
|
} |
206 |
|
|
spc = dyeStrToSpace(str); |
207 |
|
|
if (dyeSpaceUnknown == spc) { |
208 |
|
|
biffAddf(DYE, "%s: couldn't parse colorspace from \"%s\"", me, str); |
209 |
|
|
return 1; |
210 |
|
|
} |
211 |
|
|
str = (char *)airFree(str); |
212 |
|
|
|
213 |
|
|
dyeColorSet(col, spc, v0, v1, v2); |
214 |
|
|
return 0; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
char * |
218 |
|
|
dyeColorSprintf(char *str, dyeColor *col) { |
219 |
|
|
|
220 |
|
|
if (str && col) { |
221 |
|
|
col->ii = AIR_CLAMP(0, col->ii, 1); |
222 |
|
|
sprintf(str, "%s:%g,%g,%g", dyeSpaceToStr[col->spc[col->ii]], |
223 |
|
|
col->val[col->ii][0], |
224 |
|
|
col->val[col->ii][1], |
225 |
|
|
col->val[col->ii][2]); |
226 |
|
|
} |
227 |
|
|
return str; |
228 |
|
|
} |