OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimU13ImageData.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: See top level LICENSE.txt file.
4 //
5 // Author: David Burken
6 //
7 // Description:
8 //
9 // Class definition of ossimU13ImageData. Specialized image data object for
10 // unsigned short data with an 11 bit depth.
11 //
12 // NOTE: This object is optimized for unsigned short data and assumes the
13 // following: null pixel value = 0.0
14 // min pixel value = 1.0
15 // max pixel value = 8191.0 (2^13 - 1)
16 //
17 // If you want anything else use the less efficient ossimImageData.
18 //
19 //*************************************************************************
20 // $Id: ossimU13ImageData.cpp 16052 2009-12-08 22:20:40Z dburken $
21 
23 #include <ossim/base/ossimSource.h>
25 #include <ossim/base/ossimIrect.h>
28 
29 RTTI_DEF1(ossimU13ImageData, "ossimU13ImageData", ossimImageData)
30 
32 
34  :
36 {
38 }
39 
41  ossim_uint32 bands)
42  :
43  ossimImageData(source,
45  bands)
46 {
47 }
48 
50  ossim_uint32 bands,
51  ossim_uint32 width,
52  ossim_uint32 height)
53  :
54  ossimImageData(source,
56  bands,
57  width,
58  height)
59 {
60 }
61 
63  :
64  ossimImageData(rhs)
65 {}
66 
68 {
69 }
70 
72 {
73  return new ossimU13ImageData(*this);
74 }
75 
77 {
78  if (m_dataBuffer.size() == 0)
79  {
81  return OSSIM_NULL;
82  }
83 
84  ossim_uint32 count = 0;
85  const ossim_uint32 SIZE = getSize();
86  const ossim_uint32 BOUNDS = getSizePerBand();
87  const ossim_uint32 NUMBER_OF_BANDS = getNumberOfBands();
88 
89  for(ossim_uint32 band = 0; band < NUMBER_OF_BANDS; ++band)
90  {
91  const ossim_uint16* p = getUshortBuf(band);
92  for (ossim_uint32 i=0; i<BOUNDS; ++i)
93  {
94  // check if the band is null
95  if (p[i] != 0) ++count;
96  }
97  }
98 
99  if (!count)
101  else if (count == SIZE)
103  else
105 
106  return getDataObjectStatus();
107 }
108 
110  ossim_uint32 bandNumber,
111  float& result)const
112 {
113  if( (getDataObjectStatus() != OSSIM_NULL) && isValidBand(bandNumber) )
114  {
115  const ossim_uint16* sourceBuf = getUshortBuf(bandNumber);
116  result =
117  static_cast<float>(m_remapTable.normFromPix(sourceBuf[offset]));
118  }
119 }
120 
122  ossim_uint32 bandNumber,
123  float inputValue)
124 {
125  if( (getDataObjectStatus() != OSSIM_NULL) && isValidBand(bandNumber) )
126  {
127  ossim_uint16* sourceBuf = getUshortBuf(bandNumber);
128  sourceBuf[offset]
129  = static_cast<ossim_uint16>(m_remapTable.pixFromNorm(inputValue));
130  }
131 }
132 
133 void
135 {
136  if(!result)
137  {
138  return;
139  }
140  // make sure that the types and width and height are
141  // good.
142  if( (result->getScalarType() != OSSIM_NORMALIZED_FLOAT) ||
143  (result->getNumberOfBands() != this->getNumberOfBands())||
144  (result->getWidth() != this->getWidth()) ||
145  (result->getHeight() != this->getHeight())||
146  (result->getDataObjectStatus() == OSSIM_NULL) ||
148  {
149  return;
150  }
151 
153 
154  if(size > 0)
155  {
156  for(ossim_uint32 bandCount = 0; bandCount < m_numberOfDataComponents; ++bandCount)
157  {
158  const ossim_uint16* sourceBuf = getUshortBuf(bandCount);
159  float* resultBuf = static_cast<float*>(result->getBuf(bandCount));
160  for(ossim_uint32 counter = 0; counter < size; ++counter)
161  {
162  resultBuf[counter]
163  = static_cast<float>(m_remapTable.
164  normFromPix(sourceBuf[counter]));
165  }
166  }
167  }
168 }
169 
171 {
172  if(!result)
173  {
174  return;
175  }
176 
177  // make sure that the types and width and height are
178  // good.
179  if( (result->getScalarType() != OSSIM_NORMALIZED_FLOAT) ||
180  (result->getNumberOfBands() != this->getNumberOfBands())||
181  (result->getWidth() != this->getWidth()) ||
182  (result->getHeight() != this->getHeight())||
183  (result->getDataObjectStatus() == OSSIM_NULL) ||
185  {
186  return;
187  }
188 
190 
191  if(size > 0)
192  {
193  for(ossim_uint32 bandCount = 0; bandCount < m_numberOfDataComponents; ++bandCount)
194  {
195  const ossim_uint16* sourceBuf = getUshortBuf(bandCount);
196  double* resultBuf = static_cast<double*>(result->getBuf(bandCount));
197  for(ossim_uint32 counter = 0; counter < size; ++counter)
198  {
199  resultBuf[counter] = m_remapTable.normFromPix(sourceBuf[counter]);
200  }
201  }
202  }
203 
204 }
205 
207 {
208  if((normalizedInput->getScalarType() != OSSIM_NORMALIZED_FLOAT) &&
209  (normalizedInput->getScalarType() != OSSIM_NORMALIZED_DOUBLE) )
210  {
211  //ERROR
212  return;
213  }
214 
215  ossim_uint32 counter = 0;
216  ossim_uint32 bandCount = 0;
218  ossimScalarType scalarType = normalizedInput->getScalarType();
219 
220  if(size > 0)
221  {
222  if(scalarType == OSSIM_NORMALIZED_FLOAT)
223  {
224  for(bandCount = 0; bandCount < m_numberOfDataComponents; ++bandCount)
225  {
226  float* sourceBuf =
227  static_cast<float*>(normalizedInput->getBuf(bandCount));
228  ossim_uint16* resultBuf = getUshortBuf(bandCount);
229  for(counter = 0; counter < size; ++counter)
230  {
231  resultBuf[counter]
232  = static_cast<ossim_uint16>(m_remapTable.
233  pixFromNorm(sourceBuf[counter]));
234  }
235  }
236  }
237  else // Double
238  {
239  for(bandCount = 0; bandCount < m_numberOfDataComponents; ++bandCount)
240  {
241  double* sourceBuf =
242  static_cast<double*>(normalizedInput->getBuf(bandCount));
243  ossim_uint16* resultBuf = getUshortBuf(bandCount);
244  for(counter = 0; counter < size; ++counter)
245  {
246  resultBuf[counter]
247  = static_cast<ossim_uint16>(m_remapTable.
248  pixFromNorm(sourceBuf[counter]));
249  }
250  }
251  }
252  }
253 }
254 
256  ossim_uint32 bandNumber)
257 {
258  double result = -1; // invalid MSE
259  ossim_uint32 index = 0;
260  double delta = 0;
261  ossim_uint32 validPixelCount=0;
262 
263  ossim_uint16* buffer = getUshortBuf(bandNumber);
264  if(buffer)
265  {
266  ossim_uint32 bounds = getWidth()*getHeight();
267  for(index = 0; index < bounds; ++index)
268  {
269  if(!isNull(index))
270  {
271  delta = *buffer - meanValue;
272  result += (delta*delta);
273  ++validPixelCount;
274  }
275  ++buffer;
276  }
277  if(validPixelCount > 0)
278  {
279  result /= validPixelCount;
280  }
281  }
282 
283  return result;
284 }
285 
287 {
288  double result = 0.0;
289  ossim_uint32 index = 0;
290  ossim_uint32 validPixelCount=0;
291 
292  ossim_uint16* buffer = getUshortBuf(bandNumber);
293  if(buffer)
294  {
295  ossim_uint32 bounds = getSizePerBand();
296  for(index = 0; index < bounds; ++index)
297  {
298  if(!isNull(index))
299  {
300  result += *buffer;
301  ++validPixelCount;
302  }
303  ++buffer;
304  }
305  if(validPixelCount > 0)
306  {
307  result /= validPixelCount;
308  }
309  }
310 
311  return result;
312 }
313 
314 void ossimU13ImageData::setValue(long x, long y, double color)
315 {
316  if(m_dataBuffer.size() > 0 && isWithin(x, y))
317  {
318  //***
319  // Compute the offset into the buffer for (x,y). This should always
320  // come out positive.
321  //***
322  ossim_uint32 ux = static_cast<ossim_uint32>(x - m_origin.x);
323  ossim_uint32 uy = static_cast<ossim_uint32>(y - m_origin.y);
324 
325  ossim_uint32 offset = uy * m_spatialExtents[0] + ux;
326 
327  for(ossim_uint32 band = 0; offset < m_dataBuffer.size() && // prevent buffer overrun
328  band < m_numberOfDataComponents; band++)
329  {
330  ossim_uint16* buf = getUshortBuf(band)+offset;
331  *buf = (ossim_uint16)color;
332  }
333  }
334 }
335 
336 void ossimU13ImageData::fill(ossim_uint32 band, double value)
337 {
338  void* s = getBuf(band);
339 
340  if (s == NULL) return; // nothing to do...
341 
342  ossim_uint32 size_in_pixels = getSizePerBand();
343  ossim_uint16* p = getUshortBuf(band);
344  ossim_uint16 np = static_cast<ossim_uint16>(value);
345 
346  for (ossim_uint32 i=0; i<size_in_pixels; i++) p[i] = np;
347 
348  // Set the status to unknown since we don't know about the other bands.
350 }
351 
352 
354 {
355  for(ossim_uint32 band = 0; band < getNumberOfBands(); ++band)
356  {
357  const ossim_uint16* buf = getUshortBuf(band)+offset;
358 
359  if((*buf) != m_nullPixelValue[band])
360  {
361  return false;
362  }
363  }
364 
365  return true;
366 }
367 
369 {
370  ossim_uint32 bands = getNumberOfBands();
371  for(ossim_uint32 band = 0; band < bands; ++band)
372  {
373  ossim_uint16* buf = getUshortBuf(band)+offset;
374  *buf = (ossim_uint16)m_nullPixelValue[band];
375  }
376 }
377 
378 
380 {
381  if (!buf)
382  {
385  "ossimU13ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
386  __FILE__,
387  __LINE__);
388  return;
389  }
390 
392 
393  if(size > 0)
394  {
395  for(ossim_uint32 band = 0; band < getNumberOfBands(); ++band)
396  {
397  const ossim_uint16* s = getUshortBuf(band); // source
398  double* d = buf + (band*size); // destination
399 
400  for(ossim_uint32 index = 0; index < size; ++index)
401  {
402  d[index] = m_remapTable.
403  normFromPix(static_cast<ossim_int32>(s[index]));
404  }
405 
406  }
407  }
408 }
409 
411  double* buf) const
412 {
413  if (!buf)
414  {
417  "ossimU13ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
418  __FILE__,
419  __LINE__);
420  return;
421  }
422 
423  if(!getBuf(band)) return;
424 
426 
427  if(size > 0)
428  {
429  const ossim_uint16* s = getUshortBuf(band); // source
430  double* d = buf; // destination
431 
432  for(ossim_uint32 index = 0; index < size; index++)
433  {
434  *d = m_remapTable.normFromPix(static_cast<ossim_int32>(*s));
435  }
436  }
437 }
438 
440 {
441  if (!buf)
442  {
445  "ossimU13ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
446  __FILE__,
447  __LINE__);
448  return;
449  }
450 
452 
453  if(size > 0)
454  {
455  for(ossim_uint32 band = 0; band < getNumberOfBands(); band++)
456  {
457  double* s = buf + (band*size); // source
458  ossim_uint16* d = getUshortBuf(band); // destination
459 
460  for(ossim_uint32 index = 0; index < size; index++)
461  {
462  d[index] = m_remapTable.pixFromNorm(s[index]);
463  }
464  }
465  }
466 }
467 
469  double* buf)
470 {
471  if (!buf)
472  {
475  "ossimU13ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
476  __FILE__,
477  __LINE__);
478  return;
479  }
480 
482 
483  if((size > 0)&&getBuf(band))
484  {
485  double* s = buf; // source
486  ossim_uint16* d = getUshortBuf(band); // destination
487 
488  for(ossim_uint32 index = 0; index < size; ++index)
489  {
490  *d = m_remapTable.pixFromNorm(*s);
491  ++d;
492  ++s;
493  }
494  }
495 }
496 
497 
499 {
500  if (!buf)
501  {
504  "ossimU13ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
505  __FILE__,
506  __LINE__);
507  return;
508  }
509 
511 
512  if(size > 0)
513  {
514  for(ossim_uint32 band = 0; band < getNumberOfBands(); band++)
515  {
516  const ossim_uint16* s = getUshortBuf(band); // source
517  float* d = buf + (band*size); // destination
518 
519  for(ossim_uint32 index = 0; index < size; index++)
520  {
521  d[index] = m_remapTable.
522  normFromPix(static_cast<ossim_int32>(s[index]));
523  }
524  }
525  }
526 }
527 
529  float* buf) const
530 {
531  if (!buf)
532  {
535  "ossimU13ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
536  __FILE__,
537  __LINE__);
538  return;
539  }
540  if(!getBuf(band)) return;
541 
543 
544  if(size > 0)
545  {
546  const ossim_uint16* s = getUshortBuf(band); // source
547  float* d = buf; // destination
548 
549  for(ossim_uint32 index = 0; index < size; ++index)
550  {
551  *d = m_remapTable.normFromPix(static_cast<ossim_int32>(*s));
552  }
553  }
554 }
555 
557 {
558  if (!buf)
559  {
562  "ossimU13ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
563  __FILE__,
564  __LINE__);
565  return;
566  }
567 
569 
570  if(size > 0)
571  {
572  for(ossim_uint32 band = 0; band < getNumberOfBands(); ++band)
573  {
574  float* s = buf + (band*size); // source
575  ossim_uint16* d = getUshortBuf(band); // destination
576 
577  for(ossim_uint32 index = 0; index < size; ++index)
578  {
579  d[index] = m_remapTable.pixFromNorm(s[index]);
580  }
581  }
582  }
583 }
584 
585 
587  float* buf)
588 {
589  if (!buf)
590  {
593  "ossimU13ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
594  __FILE__,
595  __LINE__);
596  return;
597  }
598 
600 
601  if((size > 0)&&getBuf(band))
602  {
603  float* s = buf; // source
604  ossim_uint16* d = getUshortBuf(band); // destination
605 
606  for(ossim_uint32 index = 0; index < size; ++index)
607  {
608  *d = m_remapTable.pixFromNorm(*s);
609  ++d;
610  ++s;
611  }
612  }
613 }
614 
OSSIMDLLEXPORT void ossimSetError(const char *className, ossim_int32 error, const char *fmtString=0,...)
virtual ossim_uint32 getWidth() const
ossim_uint32 x
virtual void copyNormalizedBufferToTile(double *buf)
virtual double computeMeanSquaredError(double meanValue, ossim_uint32 bandNumber=0)
virtual ossim_uint32 getNumberOfBands() const
virtual const ossim_uint16 * getUshortBuf() const
ossim_uint32 y
virtual void setValue(long x, long y, double color)
virtual double computeAverageBandValue(ossim_uint32 bandNumber=0)
virtual ossimObject * dup() const
virtual ossimDataObjectStatus getDataObjectStatus() const
virtual ossim_uint32 getHeight() const
static const ossimErrorCode OSSIM_ERROR
virtual ossimString getClassName() const
Definition: ossimObject.cpp:64
16 bit unsigned integer (13 bits used)
unsigned short ossim_uint16
virtual ossim_float64 normFromPix(ossim_int32 pix) const
Gets a normalized value (between &#39;0.0&#39; and &#39;1.0&#39;) from a pixel value.
virtual ossimDataObjectStatus validate() const
virtual void getNormalizedFloat(ossim_uint32 offset, ossim_uint32 bandNumber, float &result) const
std::vector< ossim_uint8 > m_dataBuffer
yy_size_t size
void setNull(ossim_uint32 offset)
virtual ossim_uint32 getSizePerBand() const
Returns the number of pixels in a single band in a tile.
unsigned int ossim_uint32
32 bit normalized floating point
std::vector< ossim_uint32 > m_spatialExtents
virtual bool isWithin(ossim_int32 x, ossim_int32 y)
Thirteen bit normalized remap table to go to/from normalized value to pixel value.
virtual ossim_int32 pixFromNorm(ossim_float64 normPix) const
Returns an pixel value as an int from a normalized value.
virtual bool isValidBand(ossim_uint32 band) const
ossimScalarType
virtual void setNormalizedFloat(ossim_uint32 offset, ossim_uint32 bandNumber, float input)
static const ossimNormalizedU13RemapTable m_remapTable
virtual ossimScalarType getScalarType() const
64 bit normalized floating point
virtual void copyTileToNormalizedBuffer(double *buf) const
std::vector< ossim_float64 > m_nullPixelValue
Null pixel value for each band.
virtual void convertToNormalizedDouble(ossimImageData *result) const
virtual ossim_uint32 getSize() const
Returns the total number of pixels in a tile for all bands.
ossim_int32 y
Definition: ossimIpt.h:142
virtual const void * getBuf() const
virtual void convertToNormalizedFloat(ossimImageData *result) const
virtual void unnormalizeInput(ossimImageData *normalizedInput)
virtual void setDataObjectStatus(ossimDataObjectStatus status) const
Full list found in ossimConstants.h.
ossim_int32 x
Definition: ossimIpt.h:141
bool isNull(ossim_uint32 offset) const
#define RTTI_DEF1(cls, name, b1)
Definition: ossimRtti.h:485
ossimDataObjectStatus
Definitions for data object status.
void fill(ossim_uint32 band, double value)