OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Functions
image_data.cpp File Reference
#include "init/ossimInit.h"
#include "base/data_types/ossimFilename.h"
#include "base/data_types/ossimString.h"
#include "imaging/ossimImageData.h"
#include "imaging/formats/ossimImageHandler.h"
#include "imaging/factory/ossimImageHandlerRegistry.h"
#include "imaging/factory/ossimImageDataFactory.h"
#include "base/misc/lookup_tables/ossimScalarTypeLut.h"
#include <iterator>
#include <iostream>

Go to the source code of this file.

Functions

void usage ()
 
void printDataStatus (ossimDataObjectStatus status)
 
void demo1 (const ossimFilename &filename)
 
void demo2 (const ossimFilename &filename)
 
void demo3 (const ossimFilename &filename)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ demo1()

void demo1 ( const ossimFilename filename)

Definition at line 145 of file image_data.cpp.

References ossimImageData::getBuf(), ossimDataObject::getDataObjectStatus(), ossimLookUpTable::getEntryString(), ossimImageData::getHeight(), ossimImageData::getImageRectangle(), ossimImageData::getMaxPix(), ossimImageData::getMinPix(), ossimImageData::getNullPix(), ossimImageData::getNumberOfBands(), ossimRectilinearDataObject::getScalarType(), ossimImageSource::getTile(), ossimImageData::getWidth(), ossimScalarTypeLut::instance(), ossimImageHandlerRegistry::instance(), ossimImageHandlerRegistry::open(), OSSIM_FULL, OSSIM_PARTIAL, OSSIM_UCHAR, printDataStatus(), and ossimRefPtr< T >::valid().

Referenced by main().

146 {
147  cout << "___________________________________DEMO 1_____________________________________\n";
149 
150  if(handler)
151  {
152  }
153  else
154  {
155  cerr <<"Unable to open image " << filename << endl;
156  return;
157  }
158 
159  // we will query the first 100 by 100 pixel of data from the input
160  // and compute the average value of each band. We will only allow
161  // this to happen with unsigned char data.
162  //
163  // Rectangles in ossim take absolute coordinates for upper
164  // left and lower right and does not take an upper left
165  // point and then a width height. So if I want the data
166  // starting at point location 0,0 and ending at 100 pixels along
167  // the x and y direction then we have 0 to 99 along x = 100 and
168  // 0 to 99 along y = 100.
169  //
170  // the second argument to getTile is optional and corresponds
171  // to the resolution level or overview.
172  //
173  ossimIrect regionsOfInterest(0,0, 99, 99);
174 
175  // the data returned from the call getTile is not owned by you and
176  // you should not delete the pointer.
177  //
178  ossimRefPtr<ossimImageData> data = handler->getTile(regionsOfInterest);
179 
180  if(data.valid())
181  {
182  // output the rectangle of the tile.
183  cout << "tile rect = " << data->getImageRectangle() << endl;
184 
185  // output the status of the tile. See printDataStatus above for
186  // documentation of status.
188 
189  // how many bands are there.
190  //
191  cout << "Number of bands = " << data->getNumberOfBands() << endl;
192 
193  // as in image_open lets use te scalr lut to pint the scalar type as a
194  // string.
195  cout << "Pixel scalar type = "
197  << endl;
198 
199  // for each band let's print the min, max and null pixel values
200  // note: the min max values are not for the tile but for the entire
201  // input. Typically these are used for tile normalization
202  // and clamping to data bounds.
203  //
204  cout << "min pix: ";
205  std::copy(data->getMinPix(),
206  data->getMinPix()+data->getNumberOfBands(),
207  ostream_iterator<double>(cout, ", "));
208  cout << "\nmax pix: ";
209  std::copy(data->getMaxPix(),
210  data->getMaxPix()+data->getNumberOfBands(),
211  ostream_iterator<double>(cout, ", "));
212  cout << "\nnull pix: ";
213  std::copy(data->getNullPix(),
214  data->getNullPix()+data->getNumberOfBands(),
215  ostream_iterator<double>(cout, " "));
216  cout << endl;
217 
218  if(data->getScalarType()!=OSSIM_UCHAR)
219  {
220  delete handler;
221  cerr << "Demo 1 only works for uchar data images" << endl;
222  return;
223  }
224  // Now lets compute the average pixel for each band. The data buffer is
225  // internally stored in osismImageData object as a void* buffer. We must cast
226  // to the scalar type or work in normalized space. For this example we will
227  // work the tile in its native type and will not normalize and we will also
228  // only work with unsigned char or uchar data.
229  //
230  // Note: ossimImageData already has a compute mean and sigma. We will re-implement
231  // some code here
232  //
233  // I will implement a more efficient algorithm by only checking for invalid data
234  // if the status is not full. If its full we don't have to check for
235  // null value and all we need to do is compute the sum. Although we can just check for null
236  // all the time and not worry about 2 different loops.
237  //
238  int upperBound = data->getWidth()*data->getHeight();
239  if(data->getDataObjectStatus() == OSSIM_FULL)
240  {
241  // since te data is full all pixls are used in the avverage
242  double totalNumberOfPixels = upperBound;
243  for(ossim_uint32 bandIndex = 0; bandIndex < data->getNumberOfBands(); ++bandIndex)
244  {
245  double sumOfThePixels = 0;
246  // get access to the raw band data.
247  ossim_uint8* buf = (ossim_uint8*)data->getBuf(bandIndex);
248  for(int offset = 0; offset < upperBound; ++offset)
249  {
250  sumOfThePixels += *buf;
251  ++buf;
252  }
253  if(totalNumberOfPixels > 0)
254  {
255  cout << "band " << bandIndex << " average = " << sumOfThePixels/totalNumberOfPixels << endl;
256  }
257  else
258  {
259  cout << "band " << bandIndex << " average = " << 0.0 << endl;
260  }
261  }
262  }
263  else if(data->getDataObjectStatus() == OSSIM_PARTIAL)
264  {
265  for(ossim_uint32 bandIndex = 0; bandIndex < data->getNumberOfBands(); ++bandIndex)
266  {
267  double totalNumberOfPixels = 0;
268  double sumOfThePixels = 0;
269  ossim_uint8* buf = (ossim_uint8*)data->getBuf(bandIndex);
270  ossim_uint8 np = (ossim_uint8)data->getNullPix(bandIndex);
271  for(int offset = 0; offset < upperBound; ++offset)
272  {
273  if(np != *buf)
274  {
275  sumOfThePixels += *buf;
276  ++totalNumberOfPixels;
277  }
278  ++buf;
279  }
280  if(totalNumberOfPixels > 0)
281  {
282  cout << "band " << bandIndex << " average = " << sumOfThePixels/totalNumberOfPixels << endl;
283  }
284  else
285  {
286  cout << "band " << bandIndex << " average = " << 0.0 << endl;
287  }
288 
289  }
290  }
291  }
292 
293  cout << "___________________________________END DEMO 1_____________________________________\n";
294  delete handler;
295 }
virtual ossim_uint32 getWidth() const
virtual const ossim_float64 * getMaxPix() const
virtual ossim_uint32 getNumberOfBands() const
virtual ossimImageHandler * open(const ossimFilename &fileName, bool trySuffixFirst=true, bool openOverview=true) const
open that takes a filename.
bool valid() const
Definition: ossimRefPtr.h:75
virtual ossimString getEntryString(ossim_int32 entry_number) const
virtual ossimDataObjectStatus getDataObjectStatus() const
virtual ossim_uint32 getHeight() const
static ossimScalarTypeLut * instance()
Returns the static instance of an ossimScalarTypeLut object.
unsigned int ossim_uint32
virtual const ossim_float64 * getNullPix() const
virtual ossimIrect getImageRectangle() const
virtual const ossim_float64 * getMinPix() const
virtual ossimScalarType getScalarType() const
This class defines an abstract Handler which all image handlers(loaders) should derive from...
virtual const void * getBuf() const
static ossimImageHandlerRegistry * instance()
unsigned char ossim_uint8
void printDataStatus(ossimDataObjectStatus status)
Definition: image_data.cpp:109
8 bit unsigned iteger
virtual ossimRefPtr< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)

◆ demo2()

void demo2 ( const ossimFilename filename)

Definition at line 299 of file image_data.cpp.

References ossimImageDataFactory::create(), ossimImageData::dup(), ossimImageData::initialize(), ossimImageDataFactory::instance(), ossimImageHandlerRegistry::instance(), ossimDataObject::isInitialize(), ossimImageHandlerRegistry::open(), OSSIM_UCHAR, and ossimImageData::setImageRectangle().

Referenced by main().

300 {
301  cout << "___________________________________DEMO 2_____________________________________\n";
303 
304  if(!handler)
305  {
306  cerr << "Unable to open file " << filename << " for demo 2\n";
307  return;
308  }
309  // Create image data using the factory technique.
310  // This might be the best way to do it. We might have optimized
311  // implementations for certain input types. For instance you might
312  // want to create an ossimImageData that is optimized for 3 band
313  // ossim_uint8 data.
314  //
315  // there other create methods but this takes as its
316  // first argument a source that owns this data object
317  // and the second argument is the input source to use
318  // to help instantiate the object. Please refer to
319  // ossim_core/imaging/factory/ossimImageDataFactory
320  // for further implementation
321  //
323  handler);
324 
325  // note: the data is not initialized/allocated. We have
326  // a concept of a NULL or un initialized tile so all the
327  // meta data is carried with it but just don't take up space
328  // by allocating the buffer. The number of bands, the rectangle
329  // of interest, min, max, null are all set.
330  //
331  // We will now initialize the data. The initialize will
332  // allocate the buffer based on the number of bands, width,
333  // height and scalar type and then will set the buffer to
334  // the null pixel value and set the status OSSIM_EMPTY.
335  //
336  data->initialize();
337 
338 
339 
340  // if you ever want to see if the initialization has been
341  // done then you must call isInitialize.
342  //
343  cout << "data initialized: " << data->isInitialize() << endl;
344 
345 
346  // To make a duplicate copy of the ossimImageData the easiest
347  // way to do this is to call the dup method.
348  //
349  ossimRefPtr<ossimImageData> dupData = (ossimImageData*)data->dup();
350 
351  ossimIrect newRect(10,20,200,200);
352 
353  // this particualr method will reallocate the tile
354  // if already initialized and set it to the new
355  // rectangle of interest.
356  //
357  dupData->setImageRectangle(newRect);
358 
359 
360  // allocating without the factory
361  //
362  ossimRefPtr<ossimImageData> data2 = new ossimImageData(NULL, // owner of the data object
363  OSSIM_UCHAR, // what scalar type
364  3, // number of bands
365  128, // width
366  128); // height
367 
368  data2->initialize();
369 
370  // delete the allocated data objects.
371  cout << "___________________________________END DEMO 2_____________________________________\n";
372 }
virtual void setImageRectangle(const ossimIrect &rect)
virtual ossimImageHandler * open(const ossimFilename &fileName, bool trySuffixFirst=true, bool openOverview=true) const
open that takes a filename.
virtual bool isInitialize() const
This is to be overriden in the derived classes so they can check If their data is null...
virtual void initialize()
Initialize the data buffer.
virtual ossimObject * dup() const
static ossimImageDataFactory * instance()
virtual ossimRefPtr< ossimImageData > create(ossimSource *owner, ossimScalarType scalar, ossim_uint32 bands=1) const
This class defines an abstract Handler which all image handlers(loaders) should derive from...
static ossimImageHandlerRegistry * instance()
8 bit unsigned iteger

◆ demo3()

void demo3 ( const ossimFilename filename)

Definition at line 376 of file image_data.cpp.

References ossimImageDataFactory::create(), ossimRefPtr< T >::get(), ossimImageData::getBuf(), ossimDataObject::getDataObjectStatus(), ossimImageData::getImageRectangle(), ossimImageData::getNumberOfBands(), ossimImageSource::getTile(), ossimImageData::initialize(), ossimImageDataFactory::instance(), ossimImageHandlerRegistry::instance(), ossimImageData::loadBand(), ossimImageData::loadTile(), ossimImageHandlerRegistry::open(), OSSIM_BSQ, printDataStatus(), ossimImageData::setImageRectangle(), ossimRefPtr< T >::valid(), and ossimImageData::validate().

Referenced by main().

377 {
378  cout << "___________________________________DEMO 3_____________________________________\n";
380 
381  if(!handler)
382  {
383  cerr << "Unable to open file " << filename << " for demo 2\n";
384  return;
385  }
386 
387  // lets just get some data. we will get a 128 by
388  // 128 tile that has upper left origin at 10, 10.
389  //
391  10,
392  10 + 127,
393  10 + 127));
394 
395  if(data.valid())
396  {
397  cout << "Data rectangle = " << data->getImageRectangle() << endl;
398 
400  handler);
401 
402  data2->setImageRectangle(ossimIrect(0,0,127,127));
403  data2->initialize();
404  cout << "Data 2 rectangle = " << data2->getImageRectangle() << endl;
405 
406  cout << "loading data2 rectangle\n";
407  // now load the data 2 region with data. Notice the
408  // tile overlaps the data tile and only the
409  // overlapping region is copied.
410  //
411  data2->loadTile(data.get());
412 
413  // now we can use the raw load tile where you supply the
414  // rectangle. Now if you do this your buffer is assumed
415  // to have the same number of bands.
416  //
417  // Now you must pass in the interleave type of your buffer
418  // The ossimImageData has interleave band sequential, OSSIM_BSQ
419  // which means the bands are sequential in memory where
420  // all of band 1 data followed by all of band 2 ... etc.
421  // The other interleave type are OSSIM_BIP or band
422  // interleaved by pixel. For instance if you had an RGB
423  // data object then it would be RGB, RGB, RGB ... etc.
424  // The final interleave type is by line OSSIM_BIL. This
425  // just says band 1 line1 followed by band2 line2 ... etc until all
426  // lines are stored.
427  //
428  data2->loadTile(data->getBuf(),
429  data->getImageRectangle(),
430  OSSIM_BSQ);
431 
432  // I manipulated the buffer so lets validate it for future
433  // use. I would only validate after you get done doing your
434  // data manipulation.
435  //
436  data2->validate();
437 
438  cout << "status after load is should be partial \n";
440 
441  // loadBand has simalar arguments but allows
442  // you to do band loads.
443  //
444  for(ossim_uint32 band = 0; band < data->getNumberOfBands(); ++band)
445  {
446  data2->loadBand(data->getBuf(band),
447  data->getImageRectangle(),
448  band);
449  }
450  data2->validate();
451 
452 
453  // please refer to ossim_core/imaging/ossimImageData.h for
454  // other load methods
455  //
456  }
457  cout << "___________________________________END DEMO 3_____________________________________\n";
458 }
virtual void loadBand(const void *src, const ossimIrect &src_rect, ossim_uint32 band)
virtual ossim_uint32 getNumberOfBands() const
virtual void setImageRectangle(const ossimIrect &rect)
virtual ossimImageHandler * open(const ossimFilename &fileName, bool trySuffixFirst=true, bool openOverview=true) const
open that takes a filename.
bool valid() const
Definition: ossimRefPtr.h:75
virtual ossimDataObjectStatus getDataObjectStatus() const
virtual void initialize()
Initialize the data buffer.
virtual void loadTile(const void *src, const ossimIrect &src_rect, ossimInterleaveType il_type)
static ossimImageDataFactory * instance()
virtual ossimDataObjectStatus validate() const
unsigned int ossim_uint32
virtual ossimIrect getImageRectangle() const
virtual ossimRefPtr< ossimImageData > create(ossimSource *owner, ossimScalarType scalar, ossim_uint32 bands=1) const
This class defines an abstract Handler which all image handlers(loaders) should derive from...
virtual const void * getBuf() const
static ossimImageHandlerRegistry * instance()
void printDataStatus(ossimDataObjectStatus status)
Definition: image_data.cpp:109
virtual ossimRefPtr< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 56 of file image_data.cpp.

References demo1(), demo2(), demo3(), ossimInit::finalize(), ossimInit::initialize(), ossimInit::instance(), and usage().

57 {
58  ossimInit::instance()->initialize(argc, argv);
59 
60  if(argc != 2)
61  {
62  usage();
63  }
64  else
65  {
66  // demo 1 we will access some data from the input and print
67  // some basic information about the data dn then access the data
68  // buffers for each band.
69  //
70  demo1(ossimFilename(argv[1]));
71 
72 
73  // Demo 2 just shows ways to allocate the ossimImageData
74  //
75  demo2(ossimFilename(argv[1]));
76 
77  // Demo 3 just show how to load region of a tile
78  //
79  demo3(ossimFilename(argv[1]));
80 
81  }
82 
84 }
void demo3(const ossimFilename &filename)
Definition: image_data.cpp:376
void initialize(int &argc, char **argv)
Definition: ossimInit.cpp:119
void usage()
Definition: image_data.cpp:86
void finalize()
Definition: ossimInit.cpp:261
void demo1(const ossimFilename &filename)
Definition: image_data.cpp:145
void demo2(const ossimFilename &filename)
Definition: image_data.cpp:299
static ossimInit * instance()
Definition: ossimInit.cpp:89

◆ printDataStatus()

void printDataStatus ( ossimDataObjectStatus  status)

Definition at line 109 of file image_data.cpp.

References OSSIM_EMPTY, OSSIM_FULL, OSSIM_NULL, OSSIM_PARTIAL, and status.

Referenced by demo1(), and demo3().

110 {
111  // now lets output some of the information about the tile. The tile
112  //
113  switch(status)
114  {
115  case OSSIM_NULL:
116  {
117  cout << "data status = " << "null" << endl;
118  break;
119  }
120  case OSSIM_EMPTY:
121  {
122  cout << "data status = " << "empty" << endl;
123  break;
124  }
125  case OSSIM_PARTIAL:
126  {
127  cout << "data status = " << "partial" << endl;
128  break;
129  }
130  case OSSIM_FULL:
131  {
132  cout << "data status = " << "full" << endl;
133  break;
134  }
135  default:
136  {
137  cout << "data status = " << "unknown" << endl;
138  }
139  }
140 }
return status

◆ usage()

void usage ( )

OVERVIEW:

We will retrieve information from the input source and manipulate the data. All filters will have a data access point called getTile that can take a rectangle/region of interest and a resolution level. Once we have retrieved the data we will show documented examples on how to manipulate the data.

PURPOSE:

  1. Learn how to request data from the connected input and output some basic information about the requested area of interest
  2. Learn how to use ossimImageData object to manipulate pixel data.
  3. Learn how to query subregions and copy subregions.
  4. Understand what is a NULL, EMPTY, FULL, and PARTIAL data object. this is very import for mosaicking and other pixel manipulation filters.

Definition at line 86 of file image_data.cpp.

Referenced by main().

87 {
88  cout <<"image_data <image file>"<< endl;
89 }