OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimU16ImageData.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 ossimU16ImageData. Specialized image data object for
10 // unsigned short data.
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 = 65535.0 (2^16 - 1)
16 //
17 // If you want anything else use the less efficient ossimImageData.
18 //
19 //*************************************************************************
20 // $Id: ossimU16ImageData.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(ossimU16ImageData, "ossimU16ImageData", ossimImageData)
30 
32 
34  :
36 {
38 }
39 
41  ossim_uint32 bands)
42  :
43  ossimImageData(source, OSSIM_UINT16, bands)
44 {
45 }
46 
48  ossim_uint32 bands,
49  ossim_uint32 width,
50  ossim_uint32 height)
51  :
52  ossimImageData(source, OSSIM_UINT16, bands, width, height)
53 {
54 }
55 
57  :
58  ossimImageData(rhs)
59 {
60 }
61 
63 {
64 }
65 
67 {
68  return new ossimU16ImageData(*this);
69 }
70 
72 {
73  if (getBuf() == NULL)
74  {
76  return OSSIM_NULL;
77  }
78 
79  ossim_uint32 count = 0;
80  const ossim_uint32 SIZE = getSize();
81  const ossim_uint32 BOUNDS = getSizePerBand();
82  const ossim_uint32 NUMBER_OF_BANDS = getNumberOfBands();
83 
84  for(ossim_uint32 band = 0; band < NUMBER_OF_BANDS; ++band)
85  {
86  const ossim_uint16* p = getUshortBuf(band);
87  for (ossim_uint32 i=0; i<BOUNDS; i++)
88  {
89  // check if the band is null
90  if (p[i] != 0) count++;
91  }
92  }
93 
94  if (!count)
96  else if (count == SIZE)
98  else
100 
101  return getDataObjectStatus();
102 }
103 
105  ossim_uint32 bandNumber,
106  float& result)const
107 {
108  if( (getDataObjectStatus() != OSSIM_NULL) && isValidBand(bandNumber) )
109  {
110  const ossim_uint16* sourceBuf = getUshortBuf(bandNumber);
111  result =
112  static_cast<float>(m_remapTable.normFromPix(sourceBuf[offset]));
113  }
114 }
115 
117  ossim_uint32 bandNumber,
118  float inputValue)
119 {
120  if( (getDataObjectStatus() != OSSIM_NULL) && isValidBand(bandNumber) )
121  {
122  ossim_uint16* sourceBuf = getUshortBuf(bandNumber);
123  sourceBuf[offset]
124  = static_cast<ossim_uint16>(m_remapTable.pixFromNorm(inputValue));
125  }
126 }
127 
128 void
130 {
131  if(!result)
132  {
133  return;
134  }
135  // make sure that the types and width and height are
136  // good.
137  if( (result->getScalarType() != OSSIM_NORMALIZED_FLOAT) ||
138  (result->getNumberOfBands() != this->getNumberOfBands())||
139  (result->getWidth() != this->getWidth()) ||
140  (result->getHeight() != this->getHeight())||
141  (result->getDataObjectStatus() == OSSIM_NULL) ||
143  {
144  return;
145  }
146 
148 
149  if(size > 0)
150  {
151  for(ossim_uint32 bandCount = 0; bandCount < m_numberOfDataComponents; ++bandCount)
152  {
153  const ossim_uint16* sourceBuf = getUshortBuf(bandCount);
154  float* resultBuf = static_cast<float*>(result->getBuf(bandCount));
155  for(ossim_uint32 counter = 0; counter < size; ++counter)
156  {
157  resultBuf[counter]
158  = static_cast<float>(m_remapTable.
159  normFromPix(sourceBuf[counter]));
160  }
161  }
162  }
163 }
164 
166 {
167  if(!result)
168  {
169  return;
170  }
171 
172  // make sure that the types and width and height are
173  // good.
174  if( (result->getScalarType() != OSSIM_NORMALIZED_FLOAT) ||
175  (result->getNumberOfBands() != this->getNumberOfBands())||
176  (result->getWidth() != this->getWidth()) ||
177  (result->getHeight() != this->getHeight())||
178  (result->getDataObjectStatus() == OSSIM_NULL) ||
180  {
181  return;
182  }
183 
185 
186  if(size > 0)
187  {
188  for(ossim_uint32 bandCount = 0; bandCount < m_numberOfDataComponents; ++bandCount)
189  {
190  const ossim_uint16* sourceBuf = getUshortBuf(bandCount);
191  double* resultBuf = static_cast<double*>(result->getBuf(bandCount));
192  for(ossim_uint32 counter = 0; counter < size; ++counter)
193  {
194  resultBuf[counter] = m_remapTable.normFromPix(sourceBuf[counter]);
195  }
196  }
197  }
198 
199 }
200 
202 {
203  if((normalizedInput->getScalarType() != OSSIM_NORMALIZED_FLOAT) &&
204  (normalizedInput->getScalarType() != OSSIM_NORMALIZED_DOUBLE) )
205  {
206  //ERROR
207  return;
208  }
209 
210  ossim_uint32 counter = 0;
211  ossim_uint32 bandCount = 0;
213  ossimScalarType scalarType = normalizedInput->getScalarType();
214 
215  if(size > 0)
216  {
217  if(scalarType == OSSIM_NORMALIZED_FLOAT)
218  {
219  for(bandCount = 0; bandCount < m_numberOfDataComponents; ++bandCount)
220  {
221  float* sourceBuf =
222  static_cast<float*>(normalizedInput->getBuf(bandCount));
223  ossim_uint16* resultBuf = getUshortBuf(bandCount);
224  for(counter = 0; counter < size; ++counter)
225  {
226  resultBuf[counter]
227  = static_cast<ossim_uint16>(m_remapTable.
228  pixFromNorm(sourceBuf[counter]));
229  }
230  }
231  }
232  else // Double
233  {
234  for(bandCount = 0; bandCount < m_numberOfDataComponents; ++bandCount)
235  {
236  double* sourceBuf =
237  static_cast<double*>(normalizedInput->getBuf(bandCount));
238  ossim_uint16* resultBuf = getUshortBuf(bandCount);
239  for(counter = 0; counter < size; ++counter)
240  {
241  resultBuf[counter]
242  = static_cast<ossim_uint16>(m_remapTable.
243  pixFromNorm(sourceBuf[counter]));
244  }
245  }
246  }
247  }
248 }
249 
251  ossim_uint32 bandNumber)
252 {
253  double result = -1; // invalid MSE
254  ossim_uint32 index = 0;
255  double delta = 0;
256  ossim_uint32 validPixelCount=0;
257 
258  ossim_uint16* buffer = getUshortBuf(bandNumber);
259  if(buffer)
260  {
261  ossim_uint32 bounds = getWidth()*getHeight();
262  for(index = 0; index < bounds; ++index)
263  {
264  if(!isNull(index))
265  {
266  delta = *buffer - meanValue;
267  result += (delta*delta);
268  ++validPixelCount;
269  }
270  ++buffer;
271  }
272  if(validPixelCount > 0)
273  {
274  result /= validPixelCount;
275  }
276  }
277 
278  return result;
279 }
280 
282 {
283  double result = 0.0;
284  ossim_uint32 index = 0;
285  ossim_uint32 validPixelCount=0;
286 
287  ossim_uint16* buffer = getUshortBuf(bandNumber);
288  if(buffer)
289  {
290  ossim_uint32 bounds = getSizePerBand();
291  for(index = 0; index < bounds; ++index)
292  {
293  if(!isNull(index))
294  {
295  result += *buffer;
296  ++validPixelCount;
297  }
298  ++buffer;
299  }
300  if(validPixelCount > 0)
301  {
302  result /= validPixelCount;
303  }
304  }
305 
306  return result;
307 }
308 
309 void ossimU16ImageData::setValue(long x, long y, double color)
310 {
311  if(getBuf() != NULL && isWithin(x, y))
312  {
313  //***
314  // Compute the offset into the buffer for (x,y). This should always
315  // come out positive.
316  //***
317  ossim_uint32 ux = static_cast<ossim_uint32>(x - m_origin.x);
318  ossim_uint32 uy = static_cast<ossim_uint32>(y - m_origin.y);
319 
320  ossim_uint32 offset = uy * m_spatialExtents[0] + ux;
321 
322  for(ossim_uint32 band = 0; band < m_numberOfDataComponents; band++)
323  {
324  ossim_uint16* buf = getUshortBuf(band)+offset;
325  *buf = (ossim_uint16)color;
326  }
327  }
328 }
329 
330 void ossimU16ImageData::fill(ossim_uint32 band, double value)
331 {
332  void* s = getBuf(band);
333 
334  if (s == NULL) return; // nothing to do...
335 
336  ossim_uint32 size_in_pixels = getSizePerBand();
337  ossim_uint16* p = getUshortBuf(band);
338  ossim_uint16 np = static_cast<ossim_uint16>(value);
339 
340  for (ossim_uint32 i=0; i<size_in_pixels; i++) p[i] = np;
341 
342  // Set the status to unknown since we don't know about the other bands.
344 }
345 
347 {
348  if (!buf)
349  {
352  "ossimU16ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
353  __FILE__,
354  __LINE__);
355  return;
356  }
357 
359 
360  if(size > 0)
361  {
362  for(ossim_uint32 band = 0; band < getNumberOfBands(); band++)
363  {
364  const ossim_uint16* s = getUshortBuf(band); // source
365  double* d = buf + (band*size); // destination
366 
367  for(ossim_uint32 index = 0; index < size; index++)
368  {
369  d[index] = m_remapTable.
370  normFromPix(static_cast<ossim_int32>(s[index]));
371  }
372  }
373  }
374 }
375 
377  double* buf) const
378 {
379  if (!buf)
380  {
383  "ossimU11ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
384  __FILE__,
385  __LINE__);
386  return;
387  }
388  if(!getBuf(band)) return;
389 
391 
392  if(size > 0)
393  {
394  const ossim_uint16* s = getUshortBuf(band); // source
395  double* d = buf; // destination
396 
397  for(ossim_uint32 index = 0; index < size; index++)
398  {
399  *d = m_remapTable.
400  normFromPix(static_cast<ossim_int32>(*s));
401  }
402  }
403 }
404 
406 {
407  if (!buf)
408  {
411  "ossimU16ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
412  __FILE__,
413  __LINE__);
414  return;
415  }
416 
418 
419  if(size > 0)
420  {
421  for(ossim_uint32 band = 0; band < getNumberOfBands(); band++)
422  {
423  double* s = buf + (band*size); // source
424  ossim_uint16* d = getUshortBuf(band); // destination
425 
426  for(ossim_uint32 index = 0; index < size; index++)
427  {
428  d[index] = m_remapTable.pixFromNorm(s[index]);
429  }
430  }
431  }
432 }
433 
435  double* buf)
436 {
437  if (!buf)
438  {
441  "ossimU11ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
442  __FILE__,
443  __LINE__);
444  return;
445  }
446 
448 
449  if((size > 0)&&getBuf(band))
450  {
451  double* s = buf; // source
452  ossim_uint16* d = getUshortBuf(band); // destination
453 
454  for(ossim_uint32 index = 0; index < size; index++)
455  {
456  *d = m_remapTable.pixFromNorm(*s);
457  ++d;
458  ++s;
459  }
460  }
461 }
462 
464 {
465  if (!buf)
466  {
469  "ossimU16ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
470  __FILE__,
471  __LINE__);
472  return;
473  }
474 
476 
477  if(size > 0)
478  {
479  for(ossim_uint32 band = 0; band < getNumberOfBands(); band++)
480  {
481  const ossim_uint16* s = getUshortBuf(band); // source
482  float* d = buf + (band*size); // destination
483 
484  for(ossim_uint32 index = 0; index < size; index++)
485  {
486  d[index] = m_remapTable.
487  normFromPix(static_cast<ossim_int32>(s[index]));
488  }
489  }
490  }
491 }
492 
494  float* buf) const
495 {
496  if (!buf)
497  {
500  "ossimU11ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
501  __FILE__,
502  __LINE__);
503  return;
504  }
505  if(!getBuf(band)) return;
506 
508 
509  if(size > 0)
510  {
511  const ossim_uint16* s = getUshortBuf(band); // source
512  float* d = buf; // destination
513 
514  for(ossim_uint32 index = 0; index < size; index++)
515  {
516  *d = m_remapTable.
517  normFromPix(static_cast<ossim_int32>(*s));
518  }
519  }
520 }
521 
523 {
524  if (!buf)
525  {
528  "ossimU16ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
529  __FILE__,
530  __LINE__);
531  return;
532  }
533 
535 
536  if(size > 0)
537  {
538  for(ossim_uint32 band = 0; band < getNumberOfBands(); band++)
539  {
540  float* s = buf + (band*size); // source
541  ossim_uint16* d = getUshortBuf(band); // destination
542 
543  for(ossim_uint32 index = 0; index < size; index++)
544  {
545  d[index] = m_remapTable.pixFromNorm(s[index]);
546  }
547  }
548  }
549 }
550 
552  float* buf)
553 {
554  if (!buf)
555  {
558  "ossimU11ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
559  __FILE__,
560  __LINE__);
561  return;
562  }
563 
565 
566  if((size > 0)&&getBuf(band))
567  {
568  float* s = buf; // source
569  ossim_uint16* d = getUshortBuf(band); // destination
570 
571  for(ossim_uint32 index = 0; index < size; index++)
572  {
573  *d = m_remapTable.pixFromNorm(*s);
574  ++d;
575  ++s;
576  }
577  }
578 }
579 
OSSIMDLLEXPORT void ossimSetError(const char *className, ossim_int32 error, const char *fmtString=0,...)
virtual ossim_uint32 getWidth() const
ossim_uint32 x
virtual ossim_uint32 getNumberOfBands() const
virtual const ossim_uint16 * getUshortBuf() const
16 bit unsigned integer
virtual double computeMeanSquaredError(double meanValue, ossim_uint32 bandNumber=0)
virtual void copyTileToNormalizedBuffer(double *buf) const
ossim_uint32 y
virtual void convertToNormalizedFloat(ossimImageData *result) const
virtual void unnormalizeInput(ossimImageData *normalizedInput)
virtual ossimDataObjectStatus getDataObjectStatus() const
virtual ossim_uint32 getHeight() const
static const ossimErrorCode OSSIM_ERROR
virtual ossimString getClassName() const
Definition: ossimObject.cpp:64
void fill(ossim_uint32 band, double value)
unsigned short ossim_uint16
virtual void copyNormalizedBufferToTile(double *buf)
virtual ossimObject * dup() const
virtual void setValue(long x, long y, double color)
static const ossimNormalizedU16RemapTable m_remapTable
bool isNull(ossim_uint32 offset) const
virtual ossimDataObjectStatus validate() const
yy_size_t size
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)
virtual double computeAverageBandValue(ossim_uint32 bandNumber=0)
virtual bool isValidBand(ossim_uint32 band) const
ossimScalarType
virtual ossimScalarType getScalarType() const
64 bit normalized floating point
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 void getNormalizedFloat(ossim_uint32 offset, ossim_uint32 bandNumber, float &result) const
virtual const void * getBuf() const
virtual void setDataObjectStatus(ossimDataObjectStatus status) const
Full list found in ossimConstants.h.
ossim_int32 x
Definition: ossimIpt.h:141
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.
#define RTTI_DEF1(cls, name, b1)
Definition: ossimRtti.h:485
ossimDataObjectStatus
Definitions for data object status.
virtual ossim_int32 pixFromNorm(ossim_float64 normPix) const
Returns an pixel value as an int from a normalized value.
virtual void convertToNormalizedDouble(ossimImageData *result) const
virtual void setNormalizedFloat(ossim_uint32 offset, ossim_uint32 bandNumber, float input)
Unsigned 16 bit normalized remap table to go to/from normalized value to pixel value.