Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #define _CRT_SECURE_NO_WARNINGS
00033
00034 #include <stdio.h>
00035 #include <string>
00036 #include <vector>
00037 #include <assert.h>
00038 #include <cstdlib>
00039
00040 #include "pcl_converter/gnuplotPaletteReader.h"
00041
00042 GnuPlotPalette::GnuPlotPalette()
00043 {
00044 setErrorStatus(0);
00045 }
00046
00047
00048 int GnuPlotPalette::load(std::string palettName)
00049 {
00050 int errorCode= 0;
00051 bool debugDump = true;
00052 FILE *fin;
00053 const int MAX_LINE_LEN = (1024);
00054 char szLine[MAX_LINE_LEN] = { 0 };
00055 std::vector<std::string> paletteData;
00056 fin = fopen(palettName.c_str(), "r");
00057 setErrorStatus(0);
00058 if (NULL == fin)
00059 {
00060 setErrorStatus(-1);
00061 }
00062 else
00063 {
00064 while (fgets(szLine, MAX_LINE_LEN, fin) != NULL)
00065 {
00066 paletteData.push_back(szLine);
00067 }
00068 fclose(fin);
00069 }
00070
00071 std::string lineStylePrefix = "# line styles";
00072 std::string setStyleLinePrefix = "set style line";
00073
00074 int tableStart = -1;
00075 int tableEnd = -1;
00076 for (size_t i = 0; i < paletteData.size(); i++)
00077 {
00078 if (!paletteData[i].compare(0, setStyleLinePrefix.size(), setStyleLinePrefix))
00079 {
00080 if (-1 != tableStart)
00081 {
00082 tableEnd = i;
00083 }
00084 }
00085
00086 if (!paletteData[i].compare(0, lineStylePrefix.size(), lineStylePrefix))
00087 {
00088 tableStart = i + 1;
00089 }
00090 }
00091
00092
00093 if ((tableStart != -1) && (tableEnd != -1))
00094 {
00095 size_t tableLen = tableEnd - tableStart + 1;
00096 std::vector<unsigned long> rgbSupportPoints;
00097 if (debugDump) printf("Parsing Table-Entries\n");
00098
00099 for (int i = tableStart; i <= tableEnd; i++)
00100 {
00101 size_t startPos = paletteData[i].find_first_of("'");
00102 size_t endPos = paletteData[i].find_last_of("'");
00103 if ((startPos != std::string::npos) && (endPos != std::string::npos))
00104 {
00105
00106
00107 std::string rgbValue = paletteData[i].substr(startPos + 2, endPos - startPos - 2);
00108 unsigned long hex_value = std::strtoul(rgbValue.c_str(), 0, 16);
00109 rgbSupportPoints.push_back(hex_value);
00110 }
00111 }
00112
00113
00114 int numInterpolationIntervals = tableLen - 1;
00115 float colorsPerInterval = 255.0f / (float)numInterpolationIntervals;
00116 for (size_t i = 0; i < 256; i++)
00117 {
00118 unsigned char rgbValues[3];
00119 int supportPntIdx = (int)(i / colorsPerInterval);
00120 int distanceToSupportPoint = (int)(i - supportPntIdx * colorsPerInterval);
00121 double frac = distanceToSupportPoint / (double)colorsPerInterval;
00122 if (debugDump) printf("%3d: ", (int)i);
00123 for (size_t j = 0; j < 3; j++)
00124 {
00125 float frac_epsilon = 1E-6f;
00126 unsigned char valLower = 0x00;
00127 unsigned char valUppper = 0x00;
00128 unsigned char val;
00129 int bytePosInBits = 8 * (2 - j);
00130 unsigned long mask = 0xFF << bytePosInBits;
00131 valLower = (unsigned char)((rgbSupportPoints[supportPntIdx] & mask) >> bytePosInBits);
00132
00133 val = valLower;
00134 if (frac > frac_epsilon)
00135 {
00136 if (supportPntIdx < (numInterpolationIntervals - 1))
00137 {
00138 valUppper = (unsigned char)((rgbSupportPoints[supportPntIdx + 1] & mask) >> bytePosInBits);
00139 val = (unsigned char)(valLower + frac * (valUppper - valLower));
00140 }
00141 }
00142 else
00143 {
00144 val = valLower;
00145 }
00146 rgbValues[j] = val;
00147 if (debugDump) printf("%02x ", rgbValues[j]);
00148 }
00149 for (size_t j = 0; j < 3; j++)
00150 {
00151 rgbTable[i][j] = rgbValues[j];
00152 if (debugDump) printf("%8.6lf ", rgbValues[j] / 255.0);
00153 }
00154 if (debugDump) printf("\n");
00155 }
00156
00157 }
00158
00159 errorCode = getErrorStatus();
00160 return(errorCode);
00161 }
00162
00163 unsigned char GnuPlotPalette::getRbgValue(unsigned char greyIdx, unsigned int channelIdx)
00164 {
00165
00166 assert(channelIdx < 3);
00167 return(rgbTable[greyIdx][channelIdx]);
00168 }
00169
00170 #if 0
00171 int main()
00172 {
00173 GnuPlotPalette pal("C:\\temp\\viridis.pal");
00174 return 0;
00175 }
00176 #endif