OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimRgbLutDataObject.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: LGPL
5 //
6 // See LICENSE.txt file in the top level directory for more details.
7 //
8 // Author: Garrett Potts
9 //
10 //*************************************************************************
11 // $Id: ossimRgbLutDataObject.cpp 13710 2008-10-14 16:27:57Z gpotts $
17 
18 #include <sstream>
19 using namespace std;
20 
21 RTTI_DEF1(ossimRgbLutDataObject, "ossimRgbLutDataObject", ossimObject);
22 
24  const ossimRgbLutDataObject& lut)
25 {
26  for(ossim_uint32 index = 0; index < lut.theNumberOfEntries; ++index)
27  {
28  out << "entry" << index << " " << lut[index] << endl;
29  }
30 
31  return out;
32 }
33 
34 ossimRgbLutDataObject::ossimRgbLutDataObject(unsigned long numberOfEntries)
35  :
36  theLut(NULL),
37  theNumberOfEntries(numberOfEntries)
38 {
39  if(theNumberOfEntries > 0)
40  {
41  // allocate 256 entries for the data object;
43  }
44 
45 }
46 
48  :
49  theLut(NULL),
50  theNumberOfEntries(0)
51 {
53  if(theNumberOfEntries > 0)
54  {
56  for(ossim_uint32 index = 0; index < theNumberOfEntries; ++index)
57  {
58  theLut[index] = lut.theLut[index];
59  }
60  }
61 }
62 
64 {
65  if(theLut)
66  {
67  delete [] theLut;
68  theLut = NULL;
69  }
71 }
72 
74 {
75  ossim_uint32 distance = 0x7FFFFFFF; // max 4 byte signed
76  ossim_int32 result = 0;
77 
78  if(theNumberOfEntries > 0)
79  {
80  for(ossim_uint32 i = 0; i < theNumberOfEntries; ++i)
81  {
82  ossim_uint32 rDelta = r - theLut[i].getR();
83  ossim_uint32 gDelta = g - theLut[i].getG();
84  ossim_uint32 bDelta = b - theLut[i].getB();
85 
86  ossim_uint32 deltaSumSquare = (rDelta*rDelta +
87  gDelta*gDelta +
88  bDelta*bDelta);
89  if(deltaSumSquare == 0)
90  {
91  return static_cast<int>(i);
92  }
93  else if( deltaSumSquare < distance)
94  {
95  result = static_cast<int>(i);
96  distance = deltaSumSquare;
97  }
98  }
99  }
100 
101  return result;
102 }
103 
105 {
106  if(numberOfElements < 0)
107  {
108  ossimNotify(ossimNotifyLevel_FATAL) << "FATAL: Negative rotation is not supported yet in ossimRgbLutDataObject::rotate" << endl;
109  return *this;
110  }
112  for(ossim_uint32 index = 0; index < theNumberOfEntries; ++index)
113  {
114  int adjustedDestinationIndex = (index+numberOfElements)%theNumberOfEntries;
115  lut[adjustedDestinationIndex] = theLut[index] ;
116  }
117 
118  return lut;
119 }
120 
122 {
123  if(numberOfElements < 0)
124  {
125  ossimNotify(ossimNotifyLevel_FATAL) << "FATAL: Negative rotation is not supported yet in ossimRgbLutDataObject::rotate" << endl;
126  return *this;
127  }
128  const ossimRgbLutDataObject* temp = this;
129 
130  *this = temp->rotate(numberOfElements);
131 
132  return *this;
133 }
134 
136 {
138  {
139  delete [] theLut;
140  theLut = NULL;
141  }
142 
144  if(!theLut&&(theNumberOfEntries > 0))
145  {
147  }
148  for(unsigned long index = 0; index < theNumberOfEntries; ++index)
149  {
150  theLut[index] = lut.theLut[index];
151  }
152 
153  return *this;
154 }
155 
157 {
159  {
160  return false;
161  }
162  for(unsigned long index = 0; index < theNumberOfEntries; ++index)
163  {
164  if(theLut[index] != lut.theLut[index])
165  {
166  return false;
167  }
168  }
169  return true;
170 }
171 
172 bool ossimRgbLutDataObject::saveState(ossimKeywordlist& kwl, const char* prefix)const
173 {
174  kwl.add(prefix,
175  "type",
176  getClassName(),
177  true);
178  kwl.add(prefix,
179  "number_of_entries",
181  true);
182  for(ossim_uint32 index = 0; index < theNumberOfEntries; ++index)
183  {
184  ossimString newPrefix = "entry";
185  newPrefix += ossimString::toString(index);
186  ostringstream ostr;
187  ostr << (int)(theLut[index].getR())
188  << " " << (int)(theLut[index].getG())
189  << " " << (int)(theLut[index].getB());
190  kwl.add(prefix,
191  newPrefix,
192  ostr.str().c_str(),
193  true);
194  }
195 
196  return true;
197 }
198 
199 bool ossimRgbLutDataObject::loadState(const ossimKeywordlist& kwl, const char* prefix)
200 {
201  const char* lutFile = kwl.find(prefix, "lut_file");
202  ossimKeywordlist fileLut;
203  ossimKeywordlist* tempKwl = (const_cast<ossimKeywordlist*>(&kwl));
204  ossimString tempPrefix = prefix;
205 
206  // this should have been used instead of lut_file. We will still look
207  // for lut_file for backward compatibility.
208  //
209  if(!lutFile)
210  {
211  lutFile = kwl.find(prefix, "filename");
212  }
213  // check to see if we should open an external file
214  // if so point the fileLut to the one that we use
215  if(lutFile)
216  {
217  ossimFilename filename(lutFile);
218  if(filename.exists())
219  {
220  fileLut.addFile(filename.c_str());
221  tempKwl = &fileLut;
222  tempPrefix = "";
223  }
224  }
225 
226  const char* numberOfEntries = tempKwl->find(tempPrefix, "number_of_entries");
227  if(!numberOfEntries)
228  {
229  numberOfEntries = tempKwl->find(tempPrefix, "number_entries");
230  }
231  if(!numberOfEntries) return false;
232  theNumberOfEntries = ossimString(numberOfEntries).toULong();
233 
234  delete [] theLut;
236 
237  if(tempKwl->find(tempPrefix, "entry0"))
238  {
239  for(ossim_uint32 index = 0; index < theNumberOfEntries; ++index)
240  {
241  ossimString newPrefix = "entry";
242  newPrefix += ossimString::toString(index);
243  ossimString v = tempKwl->find(tempPrefix, newPrefix.c_str());
244  istringstream istr(v);
245 
246  ossimString r, g, b;
247  istr >> r >> g >> b;
248  theLut[index].setR((unsigned char)r.toInt32());
249  theLut[index].setG((unsigned char)g.toInt32());
250  theLut[index].setB((unsigned char)b.toInt32());
251  }
252  }
253  else
254  {
255  for(ossim_uint32 index = 0; index < theNumberOfEntries; ++index)
256  {
257  ossimString newPrefix = "entry";
258  newPrefix += ossimString::toString(index);
259 
260  const char* r = tempKwl->find(tempPrefix, (newPrefix+".r").c_str());
261  const char* g = tempKwl->find(tempPrefix, (newPrefix+".g").c_str());
262  const char* b = tempKwl->find(tempPrefix, (newPrefix+".b").c_str());
263 
264  if(r)
265  {
266  theLut[index].setR((unsigned char)ossimString(r).toLong());
267  }
268  if(g)
269  {
270  theLut[index].setG((unsigned char)ossimString(g).toLong());
271  }
272  if(b)
273  {
274  theLut[index].setB((unsigned char)ossimString(b).toLong());
275  }
276  }
277  }
278  return true;
279 }
std::basic_ostringstream< char > ostringstream
Class for char output memory streams.
Definition: ossimIosFwd.h:35
unsigned char getR() const
Represents serializable keyword/value map.
bool addFile(const char *file)
const char * find(const char *key) const
void setR(unsigned char R)
static ossimString toString(bool aValue)
Numeric to string methods.
virtual ossimString getClassName() const
Definition: ossimObject.cpp:64
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
void setB(unsigned char B)
ossim_int32 toInt32() const
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
bool operator==(const ossimRgbLutDataObject &lut) const
bool exists() const
ossimRgbLutDataObject & rotate(long numberOfElements=1)
unsigned int ossim_uint32
unsigned long toULong() const
unsigned char getB() const
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
void setG(unsigned char G)
ossimRgbLutDataObject(unsigned long numberOfEntries=0)
const ossimRgbLutDataObject & operator=(const ossimRgbLutDataObject &lut)
unsigned char getG() const
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
RTTI_DEF1(ossimRgbLutDataObject, "ossimRgbLutDataObject", ossimObject)
std::basic_istringstream< char > istringstream
Class for char input memory streams.
Definition: ossimIosFwd.h:32
int findIndex(const ossimRgbVector &rgb)
float distance(double lat1, double lon1, double lat2, double lon2, int units)
unsigned char ossim_uint8
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
ostream & operator<<(ostream &out, const ossimRgbLutDataObject &lut)
int ossim_int32