OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimNBandLutDataObject.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2005 Garrett Potts
3 //
4 // License: See top level LICENSE.txt file.
5 //
6 // Author: Garrett Potts
7 //
8 //*******************************************************************
9 // $Id: ossimNBandLutDataObject.cpp 19732 2011-06-06 22:24:54Z dburken $
10 
17 
18 #include <iostream>
19 #include <sstream>
20 
21 RTTI_DEF1(ossimNBandLutDataObject, "ossimNBandLutDataObject", ossimObject);
22 
24  const ossimNBandLutDataObject& lut)
25 {
26  if(lut.theLut)
27  {
29  << lut.theNumberOfEntries << std::endl;
31  << lut.theNumberOfBands << std::endl;
32  out << "null_pixel_index: " << lut.theNullPixelIndex << std::endl;
33  for(ossim_uint32 idx = 0; idx < lut.theNumberOfEntries; ++idx)
34  {
35  const ossimNBandLutDataObject::LUT_ENTRY_TYPE *bandPtr =lut[idx];
36  ossim_uint32 bandIdx = 0;
37  out << ossimKeywordNames::ENTRY_KW << idx << ": ";
38  for(bandIdx = 0; bandIdx < lut.theNumberOfBands; ++bandIdx)
39  {
40  out << bandPtr[bandIdx] << " ";
41  }
42  if(idx < (lut.theNumberOfEntries-1))
43  {
44  out << std::endl;
45  }
46  }
47  }
48  return out;
49 }
50 
52  ossim_uint32 numberOfBands,
53  ossimScalarType bandScalarType,
54  ossim_int32 nullPixelIndex)
55  :theLut(0),
56  theNumberOfEntries(0),
57  theNumberOfBands(0),
58  theBandScalarType(bandScalarType),
59  theNullPixelIndex(nullPixelIndex)
60 {
61  create(numberOfEntries, numberOfBands);
62 }
63 
65  :theLut(0),
66  theNumberOfEntries(lut.theNumberOfEntries),
67  theNumberOfBands(lut.theNumberOfBands),
68  theBandScalarType(lut.theBandScalarType),
69  theNullPixelIndex(lut.theNullPixelIndex),
70  m_entryLabels(lut.m_entryLabels)
71 {
72  *this = lut;
73 }
74 
76 {
77  if(theLut)
78  {
79  delete [] theLut;
80  theLut = 0;
81  }
83  theNumberOfBands = 0;
84  m_entryLabels.clear();
85 }
86 
88  ossim_uint32 numberOfBands)
89 {
90  if(theLut)
91  {
92  delete [] theLut;
93  theLut = 0;
94  }
95  if(numberOfEntries&&numberOfBands)
96  {
97  theLut = new ossimNBandLutDataObject::LUT_ENTRY_TYPE[numberOfEntries*numberOfBands];
98  theNumberOfEntries = numberOfEntries;
99  theNumberOfBands = numberOfBands;
100  }
101  else
102  {
103  theNumberOfEntries = 0;
104  theNumberOfBands = 0;
105  }
106 }
107 
110 {
111  return findIndex(bandValues, theNumberOfBands);
112 }
113 
116 {
117  ossim_uint32 result = 0;
118 
119  if ( (theNumberOfEntries > 0) && (size <= theNumberOfBands) )
120  {
122  ossim_uint32 idx = 0;
123  ossim_uint32 bandIdx = 0;
125 
126  for(idx = 0; idx < theNumberOfEntries; ++idx,lutPtr+=theNumberOfBands)
127  {
128  ossim_float64 sumSquare = 0.0;
129 
130  for(bandIdx = 0; bandIdx < size; ++bandIdx)
131  {
132  ossim_int64 delta = lutPtr[bandIdx] - bandValues[bandIdx];
133  sumSquare += (delta*delta);
134  }
135  if((ossimNBandLutDataObject::LUT_ENTRY_TYPE)sumSquare == 0)
136  {
137  return idx;
138  }
139  else if( sumSquare < distance)
140  {
141  result = idx;
142  distance = sumSquare;
143  }
144  }
145  }
146 
147  return result;
148 }
149 
151 {
152  if(theLut)
153  {
155  }
156 }
157 
161 {
162  minValue = 0;
163  maxValue = 0;
164  ossim_uint32 idx = 0;
165  LUT_ENTRY_TYPE *bandPtr = theLut+band;
166  if((band < theNumberOfBands)&&
167  (theNumberOfEntries > 0))
168  {
169  minValue = theLut[band];
170  maxValue = theLut[band];
171 
172  for(idx = 0; idx < theNumberOfEntries; ++idx,bandPtr+=theNumberOfBands)
173  {
174  if((ossim_int32)idx != theNullPixelIndex)
175  {
176  if(bandPtr[band] < minValue)
177  {
178  minValue = bandPtr[band];
179  }
180  if(bandPtr[band] > maxValue)
181  {
182  maxValue = bandPtr[band];
183  }
184  }
185  }
186  }
187 }
188 
190 {
191  ossim_int32 result = -1;
192  if ( (theNumberOfBands == 4) && (theNumberOfEntries > 0) )
193  {
194  ossim_uint32 idx = 0;
195  LUT_ENTRY_TYPE* bandPtr = theLut+3; // Index to the first alpha channel.
196  for ( idx = 0; idx < theNumberOfEntries; ++idx, bandPtr+=theNumberOfBands )
197  {
198  if ( *bandPtr == 0 )
199  {
200  result = *bandPtr;
201  break;
202  }
203  }
204  }
205  return result;
206 }
207 
209 {
211  {
212  delete [] theLut;
213  theLut = 0;
214  }
215 
219  lut.theNumberOfBands);
220 
221  if(theLut)
222  {
224  }
225 
226  return *this;
227 }
228 
230 {
232  {
233  return false;
234  }
235 
236  if(!theLut && !lut.theLut) return true;
237  if(theNullPixelIndex != lut.theNullPixelIndex) return false;
238  if(theBandScalarType != lut.theBandScalarType) return false;
239 
240  if(theLut&&lut.theLut)
241  {
243  }
244  return false;
245 }
246 
247 bool ossimNBandLutDataObject::saveState(ossimKeywordlist& kwl, const char* prefix)const
248 {
249  kwl.add(prefix,
251  getClassName(),
252  true);
253  kwl.add(prefix,
256  true);
257  kwl.add(prefix,
260  true);
261  kwl.add(prefix,
264  true);
265  kwl.add(prefix,
268  true);
269 
271  for(ossim_uint32 idx = 0; idx < theNumberOfEntries; ++idx, lutPtr+=theNumberOfBands)
272  {
274  newPrefix += ossimString::toString(idx);
275  std::ostringstream ostr;
276  ossim_uint32 bandIdx = 0;
277  for(bandIdx = 0; bandIdx < theNumberOfBands; ++bandIdx)
278  {
279  ostr << lutPtr[bandIdx]
280  << " ";
281  }
282  kwl.add(prefix,
283  newPrefix,
284  ostr.str().c_str(),
285  true);
286  }
287 
288  return true;
289 }
290 
292 {
293  ossimKeywordlist kwl;
294  kwl.addFile(lutFile);
295 
296  return loadState(kwl);
297 }
298 
299 bool ossimNBandLutDataObject::loadState(const ossimKeywordlist& kwl, const char* prefix)
300 {
301  const char* nullPixelIndex = kwl.find(prefix, ossimKeywordNames::NULL_VALUE_KW);
302  const char* lutFile = kwl.find(prefix, ossimKeywordNames::FILENAME_KW);
303  ossimKeywordlist fileLut;
304  const ossimKeywordlist* tempKwl = &kwl;
305  ossimString tempPrefix = prefix;
306 
307  // check to see if we should open an external file
308  // if so point the fileLut to the one that we use
309  if(lutFile)
310  {
311  ossimFilename filename(lutFile);
312  if(filename.exists())
313  {
314  fileLut.addFile(filename.c_str());
315  tempKwl = &fileLut;
316  tempPrefix = "";
317  }
318  }
319 
320  if(nullPixelIndex)
321  {
322  theNullPixelIndex = ossimString(nullPixelIndex).toInt32();
323  }
324  else
325  {
326  theNullPixelIndex = -1;
327  }
328  int scalar = ossimScalarTypeLut::instance()->getEntryNumber(kwl, prefix);
329 
330  if (scalar != ossimLookUpTable::NOT_FOUND)
331  {
332  theBandScalarType = static_cast<ossimScalarType>(scalar);
333  }
334  const char* numberOfBands = tempKwl->find(tempPrefix, ossimKeywordNames::NUMBER_BANDS_KW);
335  const char* numberOfEntries = tempKwl->find(tempPrefix, ossimKeywordNames::NUMBER_ENTRIES_KW);
336 
337  if (numberOfBands && numberOfEntries)
338  {
339  create(ossimString(numberOfEntries).toUInt32(),
340  ossimString(numberOfBands).toUInt32());
341  }
342 
343  clearLut();
345 
346  if(lutPtr)
347  {
348  ossim_uint32 entryIdx;
349  ossim_uint32 bandIdx;
350  for(entryIdx = 0; entryIdx < theNumberOfEntries; ++entryIdx)
351  {
352  ossimString newPrefix = "entry";
353  newPrefix += ossimString::toString(entryIdx);
354  ossimString v = tempKwl->find(tempPrefix, newPrefix.c_str());
355  v = v.trim();
356  if(v != "")
357  {
358  std::istringstream istr(v);
359  ossimString lutValue;
360  for(bandIdx = 0; bandIdx < theNumberOfBands; ++bandIdx)
361  {
362  if(!istr.fail())
363  {
364  istr >> lutValue;
365  lutPtr[bandIdx] = lutValue.toInt32();
366  }
367  }
368  }
369  lutPtr += theNumberOfBands;
370  }
371  }
372 
373  return true;
374 }
375 
377 {
378  std::map<ossim_uint32, std::vector<ossimString> >::iterator it = m_entryLabels.find(band);
379  if (it != m_entryLabels.end())
380  {
381  return it->second;
382  }
383  return std::vector<ossimString>();
384 }
385 
386 void ossimNBandLutDataObject::setEntryLables(ossim_uint32 band, std::vector<ossimString> entryLabels)
387 {
388  m_entryLabels[band] = entryLabels;
389 }
std::ostream & operator<<(std::ostream &out, const ossimNBandLutDataObject &lut)
std::vector< ossimString > getEntryLabels(ossim_uint32 band)
std::basic_ostringstream< char > ostringstream
Class for char output memory streams.
Definition: ossimIosFwd.h:35
Represents serializable keyword/value map.
bool addFile(const char *file)
const char * find(const char *key) const
void setEntryLables(ossim_uint32 band, std::vector< ossimString > entryLabels)
static const char * NULL_VALUE_KW
static ossimString toString(bool aValue)
Numeric to string methods.
static const char * NUMBER_ENTRIES_KW
virtual ossimString getClassName() const
Definition: ossimObject.cpp:64
void getMinMax(ossim_uint32 band, LUT_ENTRY_TYPE &minValue, LUT_ENTRY_TYPE &maxValue)
Will return the min max value for a given band.
ossimNBandLutDataObject(ossim_uint32 numberOfEntries=0, ossim_uint32 numberOfBands=0, ossimScalarType bandScalarType=OSSIM_UINT8, ossim_int32 nullPixelIndex=-1)
std::map< ossim_uint32, std::vector< ossimString > > m_entryLabels
bool operator==(const ossimNBandLutDataObject &lut) const
Determines if the state of this lut is identical to the passed in lut.
static const char * TYPE_KW
virtual ossim_int32 getEntryNumber(const char *entry_string, bool case_insensitive=true) const
ossim_int32 toInt32() const
void clearLut()
Will do a simple memset to &#39;\0&#39; all.
double ossim_float64
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
static ossimScalarTypeLut * instance()
Returns the static instance of an ossimScalarTypeLut object.
yy_size_t size
bool exists() const
unsigned int ossim_uint32
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
bool open(const ossimFilename &lutFile)
Reads in an external lut file.
static const char * NUMBER_BANDS_KW
#define DBL_EPSILON
ossimScalarType
RTTI_DEF1(ossimNBandLutDataObject, "ossimNBandLutDataObject", ossimObject)
const ossimNBandLutDataObject & operator=(const ossimNBandLutDataObject &lut)
does a deep copy from the passed in lut to the object.
static const char * ENTRY_KW
void create(ossim_uint32 numberOfEntries, ossim_uint32 numberOfBands=3)
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
long long ossim_int64
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
static const char * SCALAR_TYPE_KW
class ossimNBandLutDataObject
ossim_uint32 findIndex(ossim_int32 *values) const
This method gives you a way to find an entry index closest to the passed in band values.
std::basic_istringstream< char > istringstream
Class for char input memory streams.
Definition: ossimIosFwd.h:32
float distance(double lat1, double lon1, double lat2, double lon2, int units)
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Loads the state of the object by reading in the keywords listed in the save state.
static const char * FILENAME_KW
ossim_int32 getFirstNullAlphaIndex() const
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
int ossim_int32