OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimU8ImageData.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 ossimU8ImageData. Specialized image data object for
10 // unsigned char data.
11 //
12 // NOTE: This object is optimized for unsigned char data and assumes the
13 // following: null pixel value = 0.0
14 // min pixel value = 1.0
15 // max pixel value = 255.0
16 //
17 // If you want anything else use the less efficient ossimImageData.
18 //
19 //*************************************************************************
20 // $Id: ossimU8ImageData.cpp 16052 2009-12-08 22:20:40Z dburken $
21 
22 #include <cstring> // for memset function
23 using namespace std;
24 
26 #include <ossim/base/ossimSource.h>
28 #include <ossim/base/ossimIrect.h>
31 
32 RTTI_DEF1(ossimU8ImageData, "ossimU8ImageData", ossimImageData)
33 
35 
37  ossim_uint32 bands)
38  :
39  ossimImageData(source,
41  bands)
42 {
43 }
44 
46  ossim_uint32 bands,
47  ossim_uint32 width,
48  ossim_uint32 height)
49  :
50  ossimImageData(source,
52  bands,
53  width,
54  height)
55 {
56 }
57 
59 {
60 }
61 
63 {
64  return new ossimU8ImageData(*this);
65 }
66 
67 void ossimU8ImageData::fill(double value)
68 {
69  ossimImageData::fill(value);
70 }
71 
73 {
74  if (m_dataBuffer.size() == 0)
75  {
77  return OSSIM_NULL;
78  }
79 
80  ossim_uint32 count = 0;
81  const ossim_uint32 SIZE = getSize();
82  const ossim_uint32 BOUNDS = getSizePerBand();
83  const ossim_uint32 NUMBER_OF_BANDS = getNumberOfBands();
84 
85  for(ossim_uint32 band = 0; band < NUMBER_OF_BANDS; ++band)
86  {
87  const ossim_uint8* p = getUcharBuf(band);
89  for (ossim_uint32 i=0; i<BOUNDS; ++i)
90  {
91  // check if the band is null
92  if (p[i] != np) ++count;
93  }
94  }
95 
96  if (!count)
98  else if (count == SIZE)
100  else
102 
103  return getDataObjectStatus();
104 }
105 
107  ossim_uint32 bandNumber,
108  float& result)const
109 {
110  // make sure that the types and width and height are good.
111  if( (getDataObjectStatus() == OSSIM_NULL) && isValidBand(bandNumber) )
112  {
113  return;
114  }
115 
116  const ossim_uint8* sourceBuf = getUcharBuf(bandNumber);
117  result = static_cast<float>(m_remapTable.normFromPix(sourceBuf[offset]));
118 }
119 
121  ossim_uint32 bandNumber,
122  float inputValue)
123 {
124  // make sure that the types and width and height are
125  // good.
126  if((getDataObjectStatus() == OSSIM_NULL)&&
127  (bandNumber < getNumberOfBands()))
128  {
129  return;
130  }
131 
132  ossim_uint8* sourceBuf = getUcharBuf(bandNumber);
133  sourceBuf[offset]
134  = static_cast<ossim_uint8>(m_remapTable.pixFromNorm(inputValue));
135 }
136 
138 {
139  if(!result)
140  {
141  return;
142  }
143  // make sure that the types and width and height are
144  // good.
145  if( (result->getScalarType() != OSSIM_NORMALIZED_FLOAT) ||
146  (result->getNumberOfBands() != this->getNumberOfBands())||
147  (result->getWidth() != this->getWidth()) ||
148  (result->getHeight() != this->getHeight())||
149  (result->getDataObjectStatus() == OSSIM_NULL) ||
151  {
152  return;
153  }
154 
156 
157  if(size > 0)
158  {
159  for(ossim_uint32 bandCount = 0; bandCount < m_numberOfDataComponents; ++bandCount)
160  {
161  const ossim_uint8* sourceBuf = getUcharBuf(bandCount);
162  float* resultBuf = static_cast<float*>(result->getBuf(bandCount));
163  for(ossim_uint32 counter = 0; counter < size; ++counter)
164  {
165  resultBuf[counter]
166  = static_cast<float>(m_remapTable.
167  normFromPix(sourceBuf[counter]));
168  }
169  }
170  }
171 }
172 
174 {
175  if(!result)
176  {
177  return;
178  }
179 
180  // make sure that the types and width and height are
181  // good.
182  if( (result->getScalarType() != OSSIM_NORMALIZED_FLOAT) ||
183  (result->getNumberOfBands() != this->getNumberOfBands())||
184  (result->getWidth() != this->getWidth()) ||
185  (result->getHeight() != this->getHeight())||
186  (result->getDataObjectStatus() == OSSIM_NULL) ||
188  {
189  return;
190  }
191 
193 
194  if(size > 0)
195  {
196  for(ossim_uint32 bandCount = 0; bandCount < m_numberOfDataComponents; ++bandCount)
197  {
198  const ossim_uint8* sourceBuf = getUcharBuf(bandCount);
199  double* resultBuf = static_cast<double*>(result->getBuf(bandCount));
200  for(ossim_uint32 counter = 0; counter < size; ++counter)
201  {
202  resultBuf[counter] = m_remapTable.normFromPix(sourceBuf[counter]);
203  }
204  }
205  }
206 }
207 
209 {
210  if((normalizedInput->getScalarType() != OSSIM_NORMALIZED_FLOAT) &&
211  (normalizedInput->getScalarType() != OSSIM_NORMALIZED_DOUBLE) )
212  {
213  //ERROR
214  return;
215  }
216 
217  ossim_uint32 counter = 0;
218  ossim_uint32 bandCount = 0;
220  ossimScalarType scalarType = normalizedInput->getScalarType();
221 
222  if(size > 0)
223  {
224  if(scalarType == OSSIM_NORMALIZED_FLOAT)
225  {
226  for(bandCount = 0; bandCount < m_numberOfDataComponents; ++bandCount)
227  {
228  float* sourceBuf =
229  static_cast<float*>(normalizedInput->getBuf(bandCount));
230  ossim_uint8* resultBuf = getUcharBuf(bandCount);
231  for(counter = 0; counter < size; ++counter)
232  {
233  resultBuf[counter]
234  = static_cast<ossim_uint8>(m_remapTable.
235  pixFromNorm(sourceBuf[counter]));
236  }
237  }
238  }
239  else // Double
240  {
241  for(bandCount = 0; bandCount < m_numberOfDataComponents; ++bandCount)
242  {
243  double* sourceBuf =
244  static_cast<double*>(normalizedInput->getBuf(bandCount));
245  ossim_uint8* resultBuf = getUcharBuf(bandCount);
246  for(counter = 0; counter < size; ++counter)
247  {
248  resultBuf[counter]
249  = static_cast<ossim_uint8>(m_remapTable.
250  pixFromNorm(sourceBuf[counter]));
251  }
252  }
253  }
254  }
255 }
256 
258  ossim_uint32 bandNumber)
259 {
260  double result = -1; // invalid MSE
261  ossim_uint32 index = 0;
262  double delta = 0;
263  ossim_uint32 validPixelCount=0;
264 
265  ossim_uint8* buffer = getUcharBuf(bandNumber);
266  if(buffer)
267  {
268  ossim_uint32 bounds = getWidth()*getHeight();
269  for(index = 0; index < bounds; ++index)
270  {
271  if(!isNull(index))
272  {
273  delta = *buffer - meanValue;
274  result += (delta*delta);
275  ++validPixelCount;
276  }
277  ++buffer;
278  }
279  if(validPixelCount > 0)
280  {
281  result /= validPixelCount;
282  }
283  }
284 
285  return result;
286 }
287 
289 {
290  double result = 0.0;
291  ossim_uint32 index = 0;
292  ossim_uint32 validPixelCount=0;
293 
294  ossim_uint8* buffer = getUcharBuf(bandNumber);
295  if(buffer)
296  {
297  ossim_uint32 bounds = getWidth()*getHeight();
298  for(index = 0; index < bounds; ++index)
299  {
300  if(!isNull(index))
301  {
302  result += *buffer;
303  ++validPixelCount;
304  }
305  ++buffer;
306  }
307  if(validPixelCount > 0)
308  {
309  result /= validPixelCount;
310  }
311  }
312 
313  return result;
314 }
315 
317 {
318  if(getBuf() != NULL && isWithin(x, y))
319  {
320  //***
321  // Compute the offset into the buffer for (x,y). This should always
322  // come out positive.
323  //***
324  ossim_uint32 ux = static_cast<ossim_uint32>(x - m_origin.x);
325  ossim_uint32 uy = static_cast<ossim_uint32>(y - m_origin.y);
326 
327  ossim_uint32 offset = uy * m_spatialExtents[0] + ux;
328 
329  for(ossim_uint32 band = 0; band < m_numberOfDataComponents; ++band)
330  {
331  ossim_uint8* buf = getUcharBuf(band)+offset;
332  *buf = (ossim_uint8)color;
333  }
334  }
335 }
336 
337 void ossimU8ImageData::fill(ossim_uint32 band, double value)
338 {
339  void* s = getBuf(band);
340 
341  if (s == NULL) return; // nothing to do...
342 
343  ossim_uint32 size_in_pixels = getSizePerBand();
344  ossim_uint8* p = getUcharBuf(band);
345  ossim_uint8 np = static_cast<ossim_uint8>(value);
346 
347  for (ossim_uint32 i=0; i<size_in_pixels; ++i) p[i] = np;
348 
349  // Set the status to unknown since we don't know about the other bands.
351 }
352 
353 
355 {
356  for(ossim_uint32 band = 0; band < getNumberOfBands(); ++band)
357  {
358  const ossim_uint8* buf = getUcharBuf(band)+offset;
359 
360  if((*buf) != m_nullPixelValue[band])
361  {
362  return false;
363  }
364  }
365 
366  return true;
367 }
368 
370 {
371  for(ossim_uint32 band = 0; band < getNumberOfBands(); ++band)
372  {
373  ossim_uint8* buf = (getUcharBuf(band))+offset;
374  *buf = (ossim_uint8)m_nullPixelValue[band];
375  }
376 }
377 
379 {
380  if (!buf)
381  {
384  "ossimU8ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
385  __FILE__,
386  __LINE__);
387  return;
388  }
390  if(!getBuf())
391  {
392  memset(buf, '\0', size);
393  return;
394  }
395 
396  if(size > 0)
397  {
398  for(ossim_uint32 band = 0; band < getNumberOfBands(); ++band)
399  {
400  const ossim_uint8* s = getUcharBuf(band); // source
401  double* d = buf + (band*size); // destination
402  if(s&&d)
403  {
404  for(ossim_uint32 index = 0; index < size; ++index)
405  {
406  d[index] = m_remapTable.
407  normFromPix(static_cast<ossim_int32>(s[index]));
408  }
409  }
410  }
411  }
412 }
413 
415 {
416  if (!buf)
417  {
420  "ossimU8ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
421  __FILE__,
422  __LINE__);
423  return;
424  }
426  if(!getBuf())
427  {
428  memset(buf, '\0', size);
429  return;
430  }
431 
432  if(size > 0)
433  {
434  for(ossim_uint32 band = 0; band < getNumberOfBands(); ++band)
435  {
436  const ossim_uint8* s = getUcharBuf(band); // source
437  float* d = buf + (band*size); // destination
438  if(s&&d)
439  {
440  for(ossim_uint32 index = 0; index < size; ++index)
441  {
442  d[index] = m_remapTable.
443  normFromPix(static_cast<ossim_int32>(s[index]));
444  }
445  }
446  }
447  }
448 }
449 
451  double* buf) const
452 {
453  if (!buf)
454  {
457  "ossimU8ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
458  __FILE__,
459  __LINE__);
460  return;
461  }
463  if(!getBuf(band))
464  {
465  memset(buf, '\0', size);
466  return;
467  }
468 
469  if(size > 0)
470  {
471  const ossim_uint8* s = getUcharBuf(band); // source
472  double* d = buf; // destination
473  for(ossim_uint32 index = 0; index < size; ++index)
474  {
475  *d = m_remapTable.
476  normFromPix(static_cast<ossim_int32>(*s));
477  ++d;
478  ++s;
479  }
480  }
481 }
482 
484  float* buf) const
485 {
486  if (!buf)
487  {
490  "ossimU8ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
491  __FILE__,
492  __LINE__);
493  return;
494  }
496  if(!getBuf(band))
497  {
498  memset(buf, '\0', size);
499  return;
500  }
501 
502  if(size > 0)
503  {
504  const ossim_uint8* s = getUcharBuf(band); // source
505  float* d = buf; // destination
506  for(ossim_uint32 index = 0; index < size; ++index)
507  {
508  *d = m_remapTable.
509  normFromPix(static_cast<ossim_int32>(*s));
510  ++d;
511  ++s;
512  }
513  }
514 }
515 
517 {
518  if (!buf)
519  {
522  "ossimU8ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
523  __FILE__,
524  __LINE__);
525  return;
526  }
527  if(!getBuf()) return;
529 
530  if(size > 0)
531  {
532  for(ossim_uint32 band = 0; band < getNumberOfBands(); ++band)
533  {
534  double* s = buf + (band*size); // source
535  ossim_uint8* d = getUcharBuf(band); // destination
536 
537  for(ossim_uint32 index = 0; index < size; ++index)
538  {
539  d[index] = m_remapTable.pixFromNorm(s[index]);
540  }
541  }
542  }
543 }
544 
546 {
547  if (!buf)
548  {
551  "ossimU8ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
552  __FILE__,
553  __LINE__);
554  return;
555  }
556  if(!getBuf()) return;
558 
559  if(size > 0)
560  {
561  for(ossim_uint32 band = 0; band < getNumberOfBands(); ++band)
562  {
563  float* s = buf + (band*size); // source
564  ossim_uint8* d = getUcharBuf(band); // destination
565 
566  for(ossim_uint32 index = 0; index < size; ++index)
567  {
568  d[index] = m_remapTable.pixFromNorm(s[index]);
569  }
570  }
571  }
572 }
573 
575  double* buf)
576 {
577  if (!buf)
578  {
581  "ossimU8ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
582  __FILE__,
583  __LINE__);
584  return;
585  }
586  if(!getBuf(band)) return;
588 
589  if(size > 0)
590  {
591  double* s = buf; // source
592  ossim_uint8* d = getUcharBuf(band); // destination
593 
594  for(ossim_uint32 index = 0; index < size; ++index)
595  {
596  d[index] = m_remapTable.pixFromNorm(s[index]);
597  }
598  }
599 }
600 
602  float* buf)
603 {
604  if (!buf)
605  {
608  "ossimU8ImageData::copyTileToNormalizedBuffer File %s line %d\nNull pointer passed to method!",
609  __FILE__,
610  __LINE__);
611  return;
612  }
613  if(!getBuf(band)) return;
615 
616  if(size > 0)
617  {
618  float* s = buf; // source
619  ossim_uint8* d = getUcharBuf(band); // destination
620 
621  for(ossim_uint32 index = 0; index < size; ++index)
622  {
623  d[index] = m_remapTable.pixFromNorm(s[index]);
624  }
625  }
626 }
627 
virtual void setValue(ossim_int32 x, ossim_int32 y, double color)
OSSIMDLLEXPORT void ossimSetError(const char *className, ossim_int32 error, const char *fmtString=0,...)
virtual ossim_uint32 getWidth() const
ossim_uint32 x
void fill(ossim_uint32 band, ossim_float64 value)
will fill the entire band with the value.
virtual void copyTileToNormalizedBuffer(double *buf) const
virtual void unnormalizeInput(ossimImageData *normalizedInput)
virtual ossim_uint32 getNumberOfBands() const
virtual void copyNormalizedBufferToTile(double *buf)
virtual double computeMeanSquaredError(double meanValue, ossim_uint32 bandNumber=0)
virtual double computeAverageBandValue(ossim_uint32 bandNumber=0)
virtual const ossim_uint8 * getUcharBuf() const
ossim_uint32 y
virtual ossimDataObjectStatus getDataObjectStatus() const
virtual ossim_uint32 getHeight() const
static const ossimErrorCode OSSIM_ERROR
virtual ossimString getClassName() const
Definition: ossimObject.cpp:64
virtual void getNormalizedFloat(ossim_uint32 offset, ossim_uint32 bandNumber, float &result) const
bool isNull(ossim_uint32 offset) const
Eight bit normalized remap table to go to/from normalized value to pixel value.
ossimU8ImageData(ossimSource *source=NULL, ossim_uint32 bands=1)
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.
std::vector< ossim_uint8 > m_dataBuffer
virtual ~ossimU8ImageData()
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
virtual const ossim_float64 * getNullPix() const
void setNull(ossim_uint32 offset)
32 bit normalized floating point
std::vector< ossim_uint32 > m_spatialExtents
static const ossimNormalizedU8RemapTable m_remapTable
virtual bool isWithin(ossim_int32 x, ossim_int32 y)
virtual ossimDataObjectStatus validate() const
void fill(ossim_uint32 band, double value)
virtual bool isValidBand(ossim_uint32 band) const
ossimScalarType
virtual ossimScalarType getScalarType() const
64 bit normalized floating point
std::vector< ossim_float64 > m_nullPixelValue
Null pixel value for each band.
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 setDataObjectStatus(ossimDataObjectStatus status) const
Full list found in ossimConstants.h.
ossim_int32 x
Definition: ossimIpt.h:141
#define RTTI_DEF1(cls, name, b1)
Definition: ossimRtti.h:485
ossimDataObjectStatus
Definitions for data object status.
virtual void convertToNormalizedDouble(ossimImageData *result) const
virtual ossim_int32 pixFromNorm(ossim_float64 normPix) const
Returns an pixel value as an int from a normalized value.
virtual ossimObject * dup() const
virtual void setNormalizedFloat(ossim_uint32 offset, ossim_uint32 bandNumber, float input)
unsigned char ossim_uint8
8 bit unsigned iteger
int ossim_int32