53 #define _CRT_SECURE_NO_WARNINGS
72 bool debugDump =
true;
74 const int MAX_LINE_LEN = (1024);
75 char szLine[MAX_LINE_LEN] = { 0 };
76 std::vector<std::string> paletteData;
77 fin = fopen(palettName.c_str(),
"r");
85 while (fgets(szLine, MAX_LINE_LEN, fin) !=
NULL)
87 paletteData.push_back(szLine);
92 std::string lineStylePrefix =
"# line styles";
93 std::string setStyleLinePrefix =
"set style line";
97 for (
size_t i = 0; i < paletteData.size(); i++)
99 if (!paletteData[i].compare(0, setStyleLinePrefix.size(), setStyleLinePrefix))
101 if (-1 != tableStart)
107 if (!paletteData[i].compare(0, lineStylePrefix.size(), lineStylePrefix))
114 if ((tableStart != -1) && (tableEnd != -1))
116 size_t tableLen = tableEnd - tableStart + 1;
117 std::vector<unsigned long> rgbSupportPoints;
118 if (debugDump) printf(
"Parsing Table-Entries\n");
120 for (
int i = tableStart; i <= tableEnd; i++)
122 size_t startPos = paletteData[i].find_first_of(
"'");
123 size_t endPos = paletteData[i].find_last_of(
"'");
124 if ((startPos != std::string::npos) && (endPos != std::string::npos))
128 std::string rgbValue = paletteData[i].substr(startPos + 2, endPos - startPos - 2);
129 unsigned long hex_value = std::strtoul(rgbValue.c_str(), 0, 16);
130 rgbSupportPoints.push_back(hex_value);
135 int numInterpolationIntervals = tableLen - 1;
136 float colorsPerInterval = 255.0f / (float)numInterpolationIntervals;
137 for (
size_t i = 0; i < 256; i++)
139 unsigned char rgbValues[3];
140 int supportPntIdx = (int)(i / colorsPerInterval);
141 int distanceToSupportPoint = (int)(i - supportPntIdx * colorsPerInterval);
142 double frac = distanceToSupportPoint / (double)colorsPerInterval;
143 if (debugDump) printf(
"%3d: ", (
int)i);
144 for (
size_t j = 0; j < 3; j++)
146 float frac_epsilon = 1E-6
f;
147 unsigned char valLower = 0x00;
148 unsigned char valUppper = 0x00;
150 int bytePosInBits = 8 * (2 - j);
151 unsigned long mask = 0xFF << bytePosInBits;
152 valLower = (
unsigned char)((rgbSupportPoints[supportPntIdx] & mask) >> bytePosInBits);
155 if (frac > frac_epsilon)
157 if (supportPntIdx < (numInterpolationIntervals - 1))
159 valUppper = (
unsigned char)((rgbSupportPoints[supportPntIdx + 1] & mask) >> bytePosInBits);
160 val = (
unsigned char)(valLower + frac * (valUppper - valLower));
168 if (debugDump) printf(
"%02x ", rgbValues[j]);
170 for (
size_t j = 0; j < 3; j++)
173 if (debugDump) printf(
"%8.6lf ", rgbValues[j] / 255.0);
175 if (debugDump) printf(
"\n");
187 assert(channelIdx < 3);
188 return(
rgbTable[greyIdx][channelIdx]);