gnuplotPaletteReader.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2018, SICK AG
00003  * Copyright (C) 2018, Ing.-Buero Dr. Michael Lehning
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  *     * Redistributions of source code must retain the above copyright
00010  *       notice, this list of conditions and the following disclaimer.
00011  *     * Redistributions in binary form must reproduce the above copyright
00012  *       notice, this list of conditions and the following disclaimer in the
00013  *       documentation and/or other materials provided with the distribution.
00014  *     * Neither the name of DFKI GmbH nor the names of its
00015  *       contributors may be used to endorse or promote products derived from
00016  *       this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00022  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028  * POSSIBILITY OF SUCH DAMAGE.
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                                 // The code is given in the format '#AABBCC'
00106                                 // We skip the '#' by adding + 1 to startPos
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                 // lattice fence problem
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; // create bit mask
00131                                 valLower = (unsigned char)((rgbSupportPoints[supportPntIdx] & mask) >> bytePosInBits);
00132 
00133                                 val = valLower; // preset as fallback
00134                                 if (frac > frac_epsilon)
00135                                 {
00136                                         if (supportPntIdx < (numInterpolationIntervals - 1)) // paranoid
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); // 254.67
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         // channelIdx: 0: RED, 1: GREEN, 2: BLUE
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


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Tue Jul 9 2019 05:05:34