Skip to content

Commit

Permalink
Initial commit of new remap tool
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarkramer committed Jul 16, 2019
1 parent 1f540ba commit 905d2db
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 1 deletion.
44 changes: 44 additions & 0 deletions include/ossim/util/ossimRemapTool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//**************************************************************************************************
//
// OSSIM Open Source Geospatial Data Processing Library
// See top level LICENSE.txt file for license information
//
//**************************************************************************************************

#ifndef ossimRemapTool_HEADER
#define ossimRemapTool_HEADER

#include <ossim/base/ossimFilename.h>
#include <ossim/base/ossimKeywordlist.h>
#include <ossim/util/ossimChipProcTool.h>
#include <ossim/base/ossimRefPtr.h>
#include <ossim/imaging/ossimSingleImageChain.h>

/*!
* Class for Performing histo ans scalar-based remapping.
*/
class OSSIMDLLEXPORT ossimRemapTool : public ossimChipProcTool
{
public:
ossimRemapTool();
~ossimRemapTool();

virtual bool initialize(ossimArgumentParser& ap);

virtual ossimString getClassName() const { return "ossimRemapTool"; }

virtual bool execute();

/** Used by ossimUtilityFactory */
static const char* DESCRIPTION;

protected:
virtual void setUsage(ossimArgumentParser& ap);
virtual void initProcessingChain();

bool m_doHistoStretch;
ossimFilename m_inputFilename;
uint32_t m_entry;
};

#endif
191 changes: 191 additions & 0 deletions src/util/ossimRemapTool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
//**************************************************************************************************
//
// OSSIM Open Source Geospatial Data Processing Library
// See top level LICENSE.txt file for license information
//
//**************************************************************************************************

#include <ossim/util/ossimRemapTool.h>
#include <ossim/init/ossimInit.h>
#include <ossim/base/ossimApplicationUsage.h>
#include <ossim/base/ossimApplicationUsage.h>
#include <ossim/base/ossimNotify.h>
#include <ossim/base/ossimKeywordNames.h>
#include <ossim/base/ossimException.h>
#include <ossim/imaging/ossimBandMergeSource.h>
#include <ossim/imaging/ossimHistogramRemapper.h>
#include <ossim/base/ossimMultiResLevelHistogram.h>
#include <ossim/base/ossimMultiBandHistogram.h>
#include <ossim/imaging/ossimImageHistogramSource.h>
#include <iostream>
#include <ossim/imaging/ossimHistogramWriter.h>
#include <ossim/base/ossimStdOutProgress.h>
#include <ossim/imaging/ossimImageHandlerRegistry.h>

using namespace std;

const char* ossimRemapTool::DESCRIPTION =
"Performs remap to 8-bit including optional histogram stretch and saves the corresponding external geometry file.";

static const std::string HISTO_STRETCH_KW = "histo_stretch";

ossimRemapTool::ossimRemapTool()
: m_doHistoStretch(true),
m_entry(0)
{
}

ossimRemapTool::~ossimRemapTool()
{
}

void ossimRemapTool::setUsage(ossimArgumentParser& ap)
{
// Add options.
ossimApplicationUsage* au = ap.getApplicationUsage();
ossimString usageString = ap.getApplicationName();
usageString += " remap [options] <input-image> ";
au->setCommandLineUsage(usageString);

// Set the command line options:
au->setDescription(DESCRIPTION);

// Base class has its own:
ossimTool::setUsage(ap);

au->addCommandLineOption("-e, --entry", "<entry> For multi image handlers which entry do you wish to extract. For list of entries use: \"ossim-info -i <your_image>\" ");
au->addCommandLineOption("-n, --no-histo", "Optionally bypass histogram-stretch. ");
au->addCommandLineOption("-o, --output", "Output filename (defaults to <input-image>-remap.<ext>).");
}

bool ossimRemapTool::initialize(ossimArgumentParser& ap)
{
if (!ossimTool::initialize(ap))
return false;
if (m_helpRequested)
return true;

ossimString ts1;
ossimArgumentParser::ossimParameter sp1 (ts1);

if ( ap.read("-e", sp1) || ap.read("--entry", sp1) )
m_entry = ts1.toUInt32();

if ( ap.read("--no-histo") || ap.read("-n"))
m_doHistoStretch = false;

if(ap.read("--output", sp1) || ap.read("-o", sp1))
m_productFilename = ts1;

if ( ap.argc() >= 2 )
{
// Input file is last arg:
m_inputFilename = ap[ap.argc()-1];
cout<<m_inputFilename<<endl;
}
else
{
ossimNotify(ossimNotifyLevel_FATAL)<<"ossimRemapTool::initialize() Input filename must be "
"provided!"<<endl;
ap.getApplicationUsage()->write(ossimNotify(ossimNotifyLevel_FATAL));
return false;
}

if (m_productFilename.empty())
{
m_productFilename = m_inputFilename.fileNoExtension() + "-remap";
m_productFilename.setExtension(m_inputFilename.ext());
}

try
{
initProcessingChain();
}
catch (ossimException& xe)
{
ossimNotify(ossimNotifyLevel_FATAL)<<xe.what()<<endl;
return false;
}

return true;
}

void ossimRemapTool::initProcessingChain()
{
ostringstream errMsg ("ossimRemapTool::initProcessingChain() ERROR: ");

ossimRefPtr<ossimImageHandler> handler =
ossimImageHandlerRegistry::instance()->open(m_inputFilename);
if (!handler)
{
errMsg<<"Could not open input image file at <"<<m_inputFilename<<">.";
throw ossimException(errMsg.str());
}
if (m_entry)
handler->setCurrentEntry(m_entry);
m_procChain->add(handler.get());
m_geom = handler->getImageGeometry();

// Add histogram remapper if requested:
if (m_doHistoStretch)
{
ossimRefPtr<ossimMultiResLevelHistogram> histogram = handler->getImageHistogram();
if (!histogram)
{
// Need to create a histogram:
ossimRefPtr<ossimImageHistogramSource> histoSource = new ossimImageHistogramSource;
ossimRefPtr<ossimHistogramWriter> writer = new ossimHistogramWriter;
histoSource->connectMyInputTo(0, handler.get());
histoSource->enableSource();
writer->connectMyInputTo(0, histoSource.get());

ossimFilename histoFile;
handler->getFilenameWithThisExt("his", histoFile);
writer->setFilename(histoFile);
writer->addListener(&theStdOutProgress);
writer->execute();

histogram = handler->getImageHistogram();
if (!histogram)
{
errMsg<<"Could not create histogram from <"<<histoFile<<">.";
throw ossimException(errMsg.str());
}
ossimRefPtr<ossimHistogramRemapper> histogramRemapper = new ossimHistogramRemapper();
histogramRemapper->setEnableFlag(true);
histogramRemapper->setStretchMode( ossimHistogramRemapper::LINEAR_AUTO_MIN_MAX );
histogramRemapper->setHistogram(histogram);
m_procChain->add(histogramRemapper.get());
}
}

// Add scalar remapper:
ossimRefPtr<ossimScalarRemapper> scalarRemapper = new ossimScalarRemapper();
scalarRemapper->setOutputScalarType(OSSIM_UINT8);
m_procChain->add(scalarRemapper.get());
}

bool ossimRemapTool::execute()
{
m_geom->getBoundingRect(m_aoiViewRect);

// Parent class has service to create writer:
ossimRefPtr<ossimImageFileWriter>writer = newWriter();
m_writer->connectMyInputTo(0, m_procChain.get());

// Add a listener to get percent complete.
m_writer->addListener(&theStdOutProgress);

// Write the file and external geometry:
;
if(!m_writer->execute())
return false;

if (!m_writer->writeExternalGeometryFile())
return false;

ossimNotify(ossimNotifyLevel_INFO)<<"Wrote product image to <"<<m_productFilename<<">"<<endl;

return true;
}

2 changes: 1 addition & 1 deletion src/util/ossimTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ void ossimTool::setUsage(ossimArgumentParser& ap)
bool ossimTool::initialize(ossimArgumentParser& ap)
{
m_helpRequested = false;
setUsage(ap);
if (ap.read("-h") || ap.read("--help") )
{
// Write usage.
setUsage(ap);
ap.getApplicationUsage()->write(ossimNotify(ossimNotifyLevel_INFO));
m_helpRequested = true;
return true;
Expand Down
8 changes: 8 additions & 0 deletions src/util/ossimToolFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <ossim/util/ossimViewshedTool.h>
#include <ossim/util/ossimSubImageTool.h>
#include <ossim/util/ossimPointCloudTool.h>
#include <ossim/util/ossimRemapTool.h>
#if OSSIM_HAS_HDF5
#include <ossim/hdf5/ossimHdf5Tool.h>
#endif
Expand Down Expand Up @@ -78,6 +79,9 @@ ossimTool* ossimToolFactory::createTool(const std::string& argName) const
if ((utilName == "pointcloud") || (argName == "ossimPointCloudTool"))
return new ossimPointCloudTool;

if ((utilName == "remap") || (argName == "ossimRemapTool"))
return new ossimRemapTool;

#if OSSIM_HAS_HDF5
if ((utilName == "hdf5") || (argName == "ossimHdf5Tool"))
return new ossimHdf5Tool;
Expand All @@ -98,6 +102,8 @@ void ossimToolFactory::getCapabilities(std::map<std::string, std::string>& capab
capabilities.insert(pair<string, string>("vertices", ossimVerticesFinderTool::DESCRIPTION));
capabilities.insert(pair<string, string>("bandmerge", ossimBandMergeTool::DESCRIPTION));
capabilities.insert(pair<string, string>("subimage", ossimSubImageTool::DESCRIPTION));
capabilities.insert(pair<string, string>("pointcloud", ossimPointCloudTool::DESCRIPTION));
capabilities.insert(pair<string, string>("remap", ossimRemapTool::DESCRIPTION));
#if OSSIM_HAS_HDF5
capabilities.insert(pair<string, string>("hdf5", ossimHdf5Tool::DESCRIPTION));
#endif
Expand All @@ -122,6 +128,8 @@ void ossimToolFactory::getTypeNameList(vector<ossimString>& typeList) const
typeList.push_back("ossimVerticesFinderUtil");
typeList.push_back("ossimBandMergeUtil");
typeList.push_back("ossimSubImageTool");
typeList.push_back("ossimPointCloudTool");
typeList.push_back("ossimRemapTool");
#if OSSIM_HAS_HDF5
typeList.push_back("ossimHdf5Tool");
#endif
Expand Down

0 comments on commit 905d2db

Please sign in to comment.