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 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 *
19 * All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions are met:
23 *
24 * * Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * * Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * * Neither the name of Osnabrueck University nor the names of its
30 * contributors may be used to endorse or promote products derived from
31 * this software without specific prior written permission.
32 * * Neither the name of SICK AG nor the names of its
33 * contributors may be used to endorse or promote products derived from
34 * this software without specific prior written permission
35 * * Neither the name of Ing.-Buero Dr. Michael Lehning nor the names of its
36 * contributors may be used to endorse or promote products derived from
37 * this software without specific prior written permission
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
43 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
50  *
51  */
52 
53 #define _CRT_SECURE_NO_WARNINGS
54 
55 #include <stdio.h>
56 #include <string>
57 #include <vector>
58 #include <assert.h>
59 #include <cstdlib>
60 
62 
64 {
65  setErrorStatus(0);
66 }
67 
68 
69 int GnuPlotPalette::load(std::string palettName)
70 {
71  int errorCode= 0;
72  bool debugDump = true;
73  FILE *fin;
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");
78  setErrorStatus(0);
79  if (NULL == fin)
80  {
81  setErrorStatus(-1);
82  }
83  else
84  {
85  while (fgets(szLine, MAX_LINE_LEN, fin) != NULL)
86  {
87  paletteData.push_back(szLine);
88  }
89  fclose(fin);
90  }
91 
92  std::string lineStylePrefix = "# line styles";
93  std::string setStyleLinePrefix = "set style line";
94 
95  int tableStart = -1;
96  int tableEnd = -1;
97  for (size_t i = 0; i < paletteData.size(); i++)
98  {
99  if (!paletteData[i].compare(0, setStyleLinePrefix.size(), setStyleLinePrefix))
100  {
101  if (-1 != tableStart)
102  {
103  tableEnd = i;
104  }
105  }
106 
107  if (!paletteData[i].compare(0, lineStylePrefix.size(), lineStylePrefix))
108  {
109  tableStart = i + 1;
110  }
111  }
112 
113 
114  if ((tableStart != -1) && (tableEnd != -1))
115  {
116  size_t tableLen = tableEnd - tableStart + 1;
117  std::vector<unsigned long> rgbSupportPoints;
118  if (debugDump) printf("Parsing Table-Entries\n");
119 
120  for (int i = tableStart; i <= tableEnd; i++)
121  {
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))
125  {
126  // The code is given in the format '#AABBCC'
127  // We skip the '#' by adding + 1 to startPos
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);
131  }
132  }
133 
134  // lattice fence problem
135  int numInterpolationIntervals = tableLen - 1;
136  float colorsPerInterval = 255.0f / (float)numInterpolationIntervals;
137  for (size_t i = 0; i < 256; i++)
138  {
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++)
145  {
146  float frac_epsilon = 1E-6f;
147  unsigned char valLower = 0x00;
148  unsigned char valUppper = 0x00;
149  unsigned char val;
150  int bytePosInBits = 8 * (2 - j);
151  unsigned long mask = 0xFF << bytePosInBits; // create bit mask
152  valLower = (unsigned char)((rgbSupportPoints[supportPntIdx] & mask) >> bytePosInBits);
153 
154  val = valLower; // preset as fallback
155  if (frac > frac_epsilon)
156  {
157  if (supportPntIdx < (numInterpolationIntervals - 1)) // paranoid
158  {
159  valUppper = (unsigned char)((rgbSupportPoints[supportPntIdx + 1] & mask) >> bytePosInBits);
160  val = (unsigned char)(valLower + frac * (valUppper - valLower));
161  }
162  }
163  else
164  {
165  val = valLower;
166  }
167  rgbValues[j] = val;
168  if (debugDump) printf("%02x ", rgbValues[j]);
169  }
170  for (size_t j = 0; j < 3; j++)
171  {
172  rgbTable[i][j] = rgbValues[j];
173  if (debugDump) printf("%8.6lf ", rgbValues[j] / 255.0); // 254.67
174  }
175  if (debugDump) printf("\n");
176  }
177 
178  }
179 
180  errorCode = getErrorStatus();
181  return(errorCode);
182 }
183 
184 unsigned char GnuPlotPalette::getRbgValue(unsigned char greyIdx, unsigned int channelIdx)
185 {
186  // channelIdx: 0: RED, 1: GREEN, 2: BLUE
187  assert(channelIdx < 3);
188  return(rgbTable[greyIdx][channelIdx]);
189 }
190 
191 #if 0
192 int main()
193 {
194  GnuPlotPalette pal("C:\\temp\\viridis.pal");
195  return 0;
196 }
197 #endif
GnuPlotPalette::getErrorStatus
int getErrorStatus(void)
Definition: gnuplotPaletteReader.h:16
NULL
#define NULL
GnuPlotPalette::GnuPlotPalette
GnuPlotPalette()
Definition: gnuplotPaletteReader.cpp:63
GnuPlotPalette
Definition: gnuplotPaletteReader.h:5
GnuPlotPalette::load
int load(std::string palettName)
Definition: gnuplotPaletteReader.cpp:69
f
f
pal
GnuPlotPalette pal
Definition: pcl_converter.cpp:75
GnuPlotPalette::setErrorStatus
void setErrorStatus(int _errorCode)
Definition: gnuplotPaletteReader.h:11
GnuPlotPalette::rgbTable
unsigned char rgbTable[256][3]
Definition: gnuplotPaletteReader.h:22
gnuplotPaletteReader.h
GnuPlotPalette::getRbgValue
unsigned char getRbgValue(unsigned char greyIdx, unsigned int channelIdx)
Definition: gnuplotPaletteReader.cpp:184
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:96


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:08