1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCIEDE2000.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*=========================================================================
The MIT License (MIT)
Copyright (c) 2015 Greg Fiumara
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
=========================================================================*/
#include "vtkCIEDE2000.h"
#include <algorithm> // std::min, std::max
#include <array>
#include <deque>
#include <limits>
#include <set>
#include <utility> // std::pair, std::make_pair
#include <vtkMath.h>
namespace CIEDE2000
{
VTK_ABI_NAMESPACE_BEGIN
//------------------------------------------------------------------------------
static const int COLORSPACE_SIZE_X = 17;
static const int COLORSPACE_SIZE_Y = 17;
static const int COLORSPACE_SIZE_Z = 17;
static const int NEIGHBORHOOD_SIZE_X = 1;
static const int NEIGHBORHOOD_SIZE_Y = 1;
static const int NEIGHBORHOOD_SIZE_Z = 1;
typedef int PositionComponent;
typedef std::array<PositionComponent, 3> Position;
typedef double Distance;
//------------------------------------------------------------------------------
inline static void getPosition(const double rgb[3], Position& pos)
{
pos[0] = static_cast<PositionComponent>(rgb[0] * (COLORSPACE_SIZE_X - 1) + 0.5);
pos[1] = static_cast<PositionComponent>(rgb[1] * (COLORSPACE_SIZE_Y - 1) + 0.5);
pos[2] = static_cast<PositionComponent>(rgb[2] * (COLORSPACE_SIZE_Z - 1) + 0.5);
}
//------------------------------------------------------------------------------
inline static void getRGBColor(const Position& pos, double rgb[3])
{
rgb[0] = pos[0] / static_cast<double>(COLORSPACE_SIZE_X - 1);
rgb[1] = pos[1] / static_cast<double>(COLORSPACE_SIZE_Y - 1);
rgb[2] = pos[2] / static_cast<double>(COLORSPACE_SIZE_Z - 1);
}
//------------------------------------------------------------------------------
void MapColor(double rgb[3])
{
Position pos;
getPosition(rgb, pos);
getRGBColor(pos, rgb);
}
//------------------------------------------------------------------------------
inline static void getLabColor(const Position& pos, double _lab[3])
{
double rgb[3];
getRGBColor(pos, rgb);
vtkMath::RGBToLab(rgb, _lab);
}
//------------------------------------------------------------------------------
inline static int getIndex(const Position& pos)
{
return pos[0] + COLORSPACE_SIZE_X * (pos[1] + COLORSPACE_SIZE_Y * pos[2]);
}
//------------------------------------------------------------------------------
double GetCIEDeltaE2000(const double lab1[3], const double lab2[3])
{
// The three constants used in the CIEDE2000 measure
static const double k_L = 1.0;
static const double k_C = 1.0;
static const double k_H = 1.0;
// Calculate and return Delta E
double C1 = std::sqrt((lab1[1] * lab1[1]) + (lab1[2] * lab1[2]));
double C2 = std::sqrt((lab2[1] * lab2[1]) + (lab2[2] * lab2[2]));
double barC = 0.5 * (C1 + C2);
double G =
0.5 * (1.0 - std::sqrt(std::pow(barC, 7.0) / (std::pow(barC, 7.0) + std::pow(25.0, 7.0))));
double a1Prime = (1.0 + G) * lab1[1];
double a2Prime = (1.0 + G) * lab2[1];
double CPrime1 = std::sqrt((a1Prime * a1Prime) + (lab1[2] * lab1[2]));
double CPrime2 = std::sqrt((a2Prime * a2Prime) + (lab2[2] * lab2[2]));
double hPrime1;
if ((lab1[2] == 0.0) && (a1Prime == 0.0))
{
hPrime1 = 0.0;
}
else
{
hPrime1 = std::atan2(lab1[2], a1Prime);
if (hPrime1 < 0.0)
{
hPrime1 += 2.0 * vtkMath::Pi();
}
}
double hPrime2;
if ((lab2[2] == 0.0) && (a2Prime == 0.0))
{
hPrime2 = 0.0;
}
else
{
hPrime2 = std::atan2(lab2[2], a2Prime);
if (hPrime2 < 0.0)
{
hPrime2 += 2.0 * vtkMath::Pi();
}
}
double deltaLPrime = lab2[0] - lab1[0];
double deltaCPrime = CPrime2 - CPrime1;
double CPrimeProduct = CPrime1 * CPrime2;
double deltahPrime;
if (CPrimeProduct == 0.0)
{
deltahPrime = 0.0;
}
else
{
deltahPrime = hPrime2 - hPrime1;
if (deltahPrime < -vtkMath::Pi())
{
deltahPrime += 2.0 * vtkMath::Pi();
}
else if (deltahPrime > vtkMath::Pi())
{
deltahPrime -= 2.0 * vtkMath::Pi();
}
}
double deltaHPrime = 2.0 * std::sqrt(CPrimeProduct) * std::sin(0.5 * deltahPrime);
double barLPrime = 0.5 * (lab1[0] + lab2[0]);
double barCPrime = 0.5 * (CPrime1 + CPrime2);
double hPrimeSum = hPrime1 + hPrime2;
double barhPrime;
if (CPrime1 * CPrime2 == 0.0)
{
barhPrime = hPrimeSum;
}
else
{
if (std::fabs(hPrime1 - hPrime2) <= vtkMath::Pi())
{
barhPrime = 0.5 * hPrimeSum;
}
else
{
if (hPrimeSum < 2.0 * vtkMath::Pi())
{
barhPrime = 0.5 * (hPrimeSum + 2.0 * vtkMath::Pi());
}
else
{
barhPrime = 0.5 * (hPrimeSum - 2.0 * vtkMath::Pi());
}
}
}
double T = 1.0 - 0.17 * std::cos(barhPrime - (vtkMath::Pi() * 30.0 / 180.0)) +
0.24 * std::cos(2.0 * barhPrime) +
0.32 * std::cos(3.0 * barhPrime + (vtkMath::Pi() * 6.0 / 180.0)) -
0.20 * std::cos(4.0 * barhPrime - (vtkMath::Pi() * 63.0 / 180.0));
double deltaTheta = (vtkMath::Pi() * 30.0 / 180.0) *
std::exp(-std::pow(
(barhPrime - (vtkMath::Pi() * 275.0 / 180.0)) / (vtkMath::Pi() * 25.0 / 180.0), 2.0));
double R_C =
2.0 * std::sqrt(std::pow(barCPrime, 7.0) / (std::pow(barCPrime, 7.0) + std::pow(25.0, 7.0)));
double S_L =
1.0 + (0.015 * pow(barLPrime - 50.0, 2.0) / std::sqrt(20.0 + std::pow(barLPrime - 50.0, 2.0)));
double S_C = 1.0 + (0.045 * barCPrime);
double S_H = 1.0 + (0.015 * barCPrime * T);
double R_T = -std::sin(2.0 * deltaTheta) * R_C;
double deltaE = std::sqrt(std::pow(deltaLPrime / (k_L * S_L), 2.0) +
std::pow(deltaCPrime / (k_C * S_C), 2.0) + std::pow(deltaHPrime / (k_H * S_H), 2.0) +
R_T * (deltaCPrime / (k_C * S_C)) * (deltaHPrime / (k_H * S_H)));
return deltaE;
}
//------------------------------------------------------------------------------
double CorrectedDistance(std::vector<Node>& path)
{
double distance = 0.0;
for (std::size_t i = 1; i < path.size(); ++i)
{
double currentLabColor[3];
vtkMath::RGBToLab(path.at(i).rgb, currentLabColor);
double previousLabColor[3];
vtkMath::RGBToLab(path.at(i - 1).rgb, previousLabColor);
distance += GetCIEDeltaE2000(currentLabColor, previousLabColor);
path.at(i).distance = distance;
}
return distance;
}
//------------------------------------------------------------------------------
double GetColorPath(
const double rgb1[3], const double rgb2[3], std::vector<Node>& path, bool forceExactSupportColors)
{
Position pos1, pos2;
getPosition(rgb1, pos1);
getPosition(rgb2, pos2);
// Use Dijkstra's algorithm backwards to calculate the shortest distances from
// the second color
std::deque<Distance> distances(COLORSPACE_SIZE_X * COLORSPACE_SIZE_Y * COLORSPACE_SIZE_Z,
std::numeric_limits<Distance>::infinity());
std::deque<Position> predecessors(COLORSPACE_SIZE_X * COLORSPACE_SIZE_Y * COLORSPACE_SIZE_Z);
// Use a set as the priority queue so we can update an entry in the queue by
// deleting the old entry and re-inserting the new entry.
// The set is sorted first by the distance from the seed node, so that the
// first entry always is the node that can be reached shortest.
std::set<std::pair<Distance, Position>> front;
// Start backwards and use the second color as seed
distances[getIndex(pos2)] = static_cast<Distance>(0);
front.insert(std::make_pair(static_cast<Distance>(0), pos2));
while (!front.empty())
{
Distance currentDist = front.begin()->first;
Position currentPos = front.begin()->second;
front.erase(front.begin());
double currentLabColor[3];
getLabColor(currentPos, currentLabColor);
int minNeighborPosX = std::max(static_cast<int>(currentPos[0]) - NEIGHBORHOOD_SIZE_X, 0);
int minNeighborPosY = std::max(static_cast<int>(currentPos[1]) - NEIGHBORHOOD_SIZE_Y, 0);
int minNeighborPosZ = std::max(static_cast<int>(currentPos[2]) - NEIGHBORHOOD_SIZE_Z, 0);
int maxNeighborPosX =
std::min(static_cast<int>(currentPos[0]) + NEIGHBORHOOD_SIZE_X, COLORSPACE_SIZE_X - 1);
int maxNeighborPosY =
std::min(static_cast<int>(currentPos[1]) + NEIGHBORHOOD_SIZE_Y, COLORSPACE_SIZE_Y - 1);
int maxNeighborPosZ =
std::min(static_cast<int>(currentPos[2]) + NEIGHBORHOOD_SIZE_Z, COLORSPACE_SIZE_Z - 1);
for (int neighborPosZ = minNeighborPosZ; neighborPosZ <= maxNeighborPosZ; ++neighborPosZ)
{
for (int neighborPosY = minNeighborPosY; neighborPosY <= maxNeighborPosY; ++neighborPosY)
{
for (int neighborPosX = minNeighborPosX; neighborPosX <= maxNeighborPosX; ++neighborPosX)
{
Position neighborPos;
neighborPos[0] = neighborPosX;
neighborPos[1] = neighborPosY;
neighborPos[2] = neighborPosZ;
if (neighborPos == currentPos)
{
continue;
}
double neighborLabColor[3];
getLabColor(neighborPos, neighborLabColor);
Distance deltaE =
static_cast<Distance>(GetCIEDeltaE2000(currentLabColor, neighborLabColor));
int neighborIdx = getIndex(neighborPos);
Distance oldNeighborDist = distances[neighborIdx];
Distance newNeighborDist = currentDist + deltaE;
if (newNeighborDist < oldNeighborDist)
{
front.erase(std::make_pair(oldNeighborDist, neighborPos));
distances[neighborIdx] = newNeighborDist;
predecessors[neighborIdx] = currentPos;
front.insert(std::make_pair(newNeighborDist, neighborPos));
}
}
}
}
}
// We started backwards from the second color, so the overall length of the
// path is the distance value at the position of the first color
Distance pathDistance = distances[getIndex(pos1)];
// Start the path from the first color and follow each node's predecessor
// until the second color is reached.
// Since each node was reached shortest from its predecessor, this results in
// a shortest path from the first to the second color.
path.clear();
Position currentPos = pos1;
while (true)
{
int currentIdx = getIndex(currentPos);
Node node;
getRGBColor(currentPos, node.rgb);
// The shortest distance from the first color to the node is the overall
// shortest distance
// from the first to the second color minus the shortest distance from the
// second color to the node.
node.distance = pathDistance - distances[currentIdx];
path.push_back(node);
if (currentPos == pos2)
{
break;
}
currentPos = predecessors[currentIdx];
}
// Force the first and the last node's color to be exact
if (forceExactSupportColors)
{
for (int i = 0; i < 3; ++i)
{
path.front().rgb[i] = rgb1[i];
path.back().rgb[i] = rgb2[i];
}
// Return the corrected overall length of the path. Necessary if forcing the
return CorrectedDistance(path);
}
// Return the overall length of the path
return pathDistance;
}
//------------------------------------------------------------------------------
VTK_ABI_NAMESPACE_END
} // namespace CIEDE2000
|