gnuplotPaletteReader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018, SICK AG
3  * Copyright (C) 2018, Ing.-Buero Dr. Michael Lehning
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of DFKI GmbH nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 
32 #define _CRT_SECURE_NO_WARNINGS
33 
34 #include <stdio.h>
35 #include <string>
36 #include <vector>
37 #include <assert.h>
38 #include <cstdlib>
39 
41 
43 {
44  setErrorStatus(0);
45 }
46 
47 
48 int GnuPlotPalette::load(std::string palettName)
49 {
50  int errorCode= 0;
51  bool debugDump = true;
52  FILE *fin;
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");
57  setErrorStatus(0);
58  if (NULL == fin)
59  {
60  setErrorStatus(-1);
61  }
62  else
63  {
64  while (fgets(szLine, MAX_LINE_LEN, fin) != NULL)
65  {
66  paletteData.push_back(szLine);
67  }
68  fclose(fin);
69  }
70 
71  std::string lineStylePrefix = "# line styles";
72  std::string setStyleLinePrefix = "set style line";
73 
74  int tableStart = -1;
75  int tableEnd = -1;
76  for (size_t i = 0; i < paletteData.size(); i++)
77  {
78  if (!paletteData[i].compare(0, setStyleLinePrefix.size(), setStyleLinePrefix))
79  {
80  if (-1 != tableStart)
81  {
82  tableEnd = i;
83  }
84  }
85 
86  if (!paletteData[i].compare(0, lineStylePrefix.size(), lineStylePrefix))
87  {
88  tableStart = i + 1;
89  }
90  }
91 
92 
93  if ((tableStart != -1) && (tableEnd != -1))
94  {
95  size_t tableLen = tableEnd - tableStart + 1;
96  std::vector<unsigned long> rgbSupportPoints;
97  if (debugDump) printf("Parsing Table-Entries\n");
98 
99  for (int i = tableStart; i <= tableEnd; i++)
100  {
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))
104  {
105  // The code is given in the format '#AABBCC'
106  // We skip the '#' by adding + 1 to startPos
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);
110  }
111  }
112 
113  // lattice fence problem
114  int numInterpolationIntervals = tableLen - 1;
115  float colorsPerInterval = 255.0f / (float)numInterpolationIntervals;
116  for (size_t i = 0; i < 256; i++)
117  {
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++)
124  {
125  float frac_epsilon = 1E-6f;
126  unsigned char valLower = 0x00;
127  unsigned char valUppper = 0x00;
128  unsigned char val;
129  int bytePosInBits = 8 * (2 - j);
130  unsigned long mask = 0xFF << bytePosInBits; // create bit mask
131  valLower = (unsigned char)((rgbSupportPoints[supportPntIdx] & mask) >> bytePosInBits);
132 
133  val = valLower; // preset as fallback
134  if (frac > frac_epsilon)
135  {
136  if (supportPntIdx < (numInterpolationIntervals - 1)) // paranoid
137  {
138  valUppper = (unsigned char)((rgbSupportPoints[supportPntIdx + 1] & mask) >> bytePosInBits);
139  val = (unsigned char)(valLower + frac * (valUppper - valLower));
140  }
141  }
142  else
143  {
144  val = valLower;
145  }
146  rgbValues[j] = val;
147  if (debugDump) printf("%02x ", rgbValues[j]);
148  }
149  for (size_t j = 0; j < 3; j++)
150  {
151  rgbTable[i][j] = rgbValues[j];
152  if (debugDump) printf("%8.6lf ", rgbValues[j] / 255.0); // 254.67
153  }
154  if (debugDump) printf("\n");
155  }
156 
157  }
158 
159  errorCode = getErrorStatus();
160  return(errorCode);
161 }
162 
163 unsigned char GnuPlotPalette::getRbgValue(unsigned char greyIdx, unsigned int channelIdx)
164 {
165  // channelIdx: 0: RED, 1: GREEN, 2: BLUE
166  assert(channelIdx < 3);
167  return(rgbTable[greyIdx][channelIdx]);
168 }
169 
170 #if 0
171 int main()
172 {
173  GnuPlotPalette pal("C:\\temp\\viridis.pal");
174  return 0;
175 }
176 #endif
GnuPlotPalette::getErrorStatus
int getErrorStatus(void)
Definition: gnuplotPaletteReader.h:15
GnuPlotPalette::GnuPlotPalette
GnuPlotPalette()
Definition: gnuplotPaletteReader.cpp:42
GnuPlotPalette
Definition: gnuplotPaletteReader.h:4
GnuPlotPalette::load
int load(std::string palettName)
Definition: gnuplotPaletteReader.cpp:48
f
f
pal
GnuPlotPalette pal
Definition: pcl_converter.cpp:60
GnuPlotPalette::setErrorStatus
void setErrorStatus(int _errorCode)
Definition: gnuplotPaletteReader.h:10
GnuPlotPalette::rgbTable
unsigned char rgbTable[256][3]
Definition: gnuplotPaletteReader.h:21
gnuplotPaletteReader.h
assert.h
GnuPlotPalette::getRbgValue
unsigned char getRbgValue(unsigned char greyIdx, unsigned int channelIdx)
Definition: gnuplotPaletteReader.cpp:163
main
int main(int argc, char **argv)
Startup routine - if called with no argmuments we assume debug session. Set scanner name variable by ...
Definition: sick_generic_caller.cpp:141


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Thu Sep 8 2022 02:30:19