32 #define _CRT_SECURE_NO_WARNINGS
51 bool debugDump =
true;
53 const int MAX_LINE_LEN = (1024);
54 char szLine[MAX_LINE_LEN] = { 0 };
55 std::vector<std::string> paletteData;
56 fin = fopen(palettName.c_str(),
"r");
64 while (fgets(szLine, MAX_LINE_LEN, fin) != NULL)
66 paletteData.push_back(szLine);
71 std::string lineStylePrefix =
"# line styles";
72 std::string setStyleLinePrefix =
"set style line";
76 for (
size_t i = 0; i < paletteData.size(); i++)
78 if (!paletteData[i].compare(0, setStyleLinePrefix.size(), setStyleLinePrefix))
86 if (!paletteData[i].compare(0, lineStylePrefix.size(), lineStylePrefix))
93 if ((tableStart != -1) && (tableEnd != -1))
95 size_t tableLen = tableEnd - tableStart + 1;
96 std::vector<unsigned long> rgbSupportPoints;
97 if (debugDump) printf(
"Parsing Table-Entries\n");
99 for (
int i = tableStart; i <= tableEnd; i++)
101 size_t startPos = paletteData[i].find_first_of(
"'");
102 size_t endPos = paletteData[i].find_last_of(
"'");
103 if ((startPos != std::string::npos) && (endPos != std::string::npos))
107 std::string rgbValue = paletteData[i].substr(startPos + 2, endPos - startPos - 2);
108 unsigned long hex_value = std::strtoul(rgbValue.c_str(), 0, 16);
109 rgbSupportPoints.push_back(hex_value);
114 int numInterpolationIntervals = tableLen - 1;
115 float colorsPerInterval = 255.0f / (float)numInterpolationIntervals;
116 for (
size_t i = 0; i < 256; i++)
118 unsigned char rgbValues[3];
119 int supportPntIdx = (int)(i / colorsPerInterval);
120 int distanceToSupportPoint = (int)(i - supportPntIdx * colorsPerInterval);
121 double frac = distanceToSupportPoint / (double)colorsPerInterval;
122 if (debugDump) printf(
"%3d: ", (
int)i);
123 for (
size_t j = 0; j < 3; j++)
125 float frac_epsilon = 1E-6
f;
126 unsigned char valLower = 0x00;
127 unsigned char valUppper = 0x00;
129 int bytePosInBits = 8 * (2 - j);
130 unsigned long mask = 0xFF << bytePosInBits;
131 valLower = (
unsigned char)((rgbSupportPoints[supportPntIdx] & mask) >> bytePosInBits);
134 if (frac > frac_epsilon)
136 if (supportPntIdx < (numInterpolationIntervals - 1))
138 valUppper = (
unsigned char)((rgbSupportPoints[supportPntIdx + 1] & mask) >> bytePosInBits);
139 val = (
unsigned char)(valLower + frac * (valUppper - valLower));
147 if (debugDump) printf(
"%02x ", rgbValues[j]);
149 for (
size_t j = 0; j < 3; j++)
152 if (debugDump) printf(
"%8.6lf ", rgbValues[j] / 255.0);
154 if (debugDump) printf(
"\n");
166 assert(channelIdx < 3);
167 return(
rgbTable[greyIdx][channelIdx]);