Skip to content

Commit

Permalink
Merge branch 'feature-ppm-output' into develop
Browse files Browse the repository at this point in the history
Added PPM export (3-byte binary, type 6).
Added PNG export (libPNG).
Removed third-party BMP export.
Removed throw(..) and noexcept statements (pre-C++11).

Re #45
Re #46
  • Loading branch information
mfranke93 committed Sep 25, 2017
2 parents a61e296 + d8e3154 commit 9a30c04
Show file tree
Hide file tree
Showing 35 changed files with 425 additions and 4,519 deletions.
11 changes: 8 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
FIND_PACKAGE(Boost 1.55 COMPONENTS program_options REQUIRED)
FIND_PACKAGE(Boost REQUIRED COMPONENTS program_options)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})

FIND_PACKAGE(PNG REQUIRED)
INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIR})

include_directories(${VISUALIZER_SOURCE_DIR}/src)
link_directories(${VISUALIZER_BINARY_DIR}/src)

add_executable(main main.cpp
io/filereader.cpp
io/bmp_writer.cpp
io/png_writer.cpp
io/ppm_writer.cpp
data/grid.cpp
data/histogram.cpp
data/standard_single_byte_transition_counter.cpp
Expand All @@ -18,4 +22,5 @@ add_executable(main main.cpp
vis/image_builder.cpp
cmdline/options.cpp
)
TARGET_LINK_LIBRARIES(main ${Boost_LIBRARIES})

TARGET_LINK_LIBRARIES(main ${Boost_LIBRARIES} ${PNG_LIBRARIES})
16 changes: 16 additions & 0 deletions src/cmdline/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <string>
#include "../data/normalizer.hpp"
#include "../vis/standard_colormap.hpp"
#include "../io/ppm_writer.hpp"
#include "../io/png_writer.hpp"

#pragma once

Expand Down Expand Up @@ -37,4 +39,18 @@ namespace config
{ "deepsea" , vis::PredefinedColormaps::DEEP_SEA },
{ "ocean" , vis::PredefinedColormaps::DEEP_SEA }
};

/**
* Map "factory" for image outputs.
*/
std::map<std::string, std::function<std::shared_ptr<io::FileWriter>(std::shared_ptr<vis::Image>)>> const filewriters = {
{ "ppm" , [](std::shared_ptr<vis::Image> img) -> std::shared_ptr<io::FileWriter>
{
return std::make_shared<io::PpmWriter>(img);
}},
{ "png" , [](std::shared_ptr<vis::Image> img) -> std::shared_ptr<io::FileWriter>
{
return std::make_shared<io::PngWriter>(img);
}}
};
}
40 changes: 33 additions & 7 deletions src/cmdline/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ cmdline::CommandlineInterface::CommandlineInterface()
{
this->desc.add_options()
("help,h", "Print help messages")
("in,i", po::value<std::string>(&inputFile), "Input file")
("out,o", po::value<std::string>(&outputFile)->default_value("out.bmp"), "Output file")
("upscale,s", po::value<size_t>(&upscaleFactor)->default_value(1), "Size scaling of output image. May not be 0")
("cmap,c", po::value<std::string>(), "Colormap to use. Defaults to heat map. Options are: RdBu, heat, deepsea, gray/grey")
("normalizer,n", po::value<std::string>()->default_value("log"), "Normalizer to use. Defaults to logarithmic. Options are: log, lin")
("ascii,a", po::bool_switch(&useAscii), "Use ASCII (7 bit charset) only")
("force-size", po::bool_switch(&forceSizeOverride), "Force a larger size for image than normally allowed")

("in,i", po::value<std::string>(&inputFile), "Input file")
("out,o", po::value<std::string>(&outputFile)->default_value("out.ppm"), "Output file")
("upscale,s", po::value<size_t>(&upscaleFactor)->default_value(1), "Size scaling of output image. May not be 0")
("cmap,c", po::value<std::string>(), "Colormap to use. Defaults to heat map."
" Options are: RdBu, heat, deepsea, gray/grey")
("normalizer,n", po::value<std::string>()->default_value("log"), "Normalizer to use. Defaults to logarithmic."
" Options are: log, lin")
("ascii,a", po::bool_switch(&useAscii), "Use ASCII (7 bit charset) only")
("force-size", po::bool_switch(&forceSizeOverride), "Force a larger size for image than normally allowed")
("file-type,T", po::value<std::string>()->default_value("ppm"), "Filetype of output. Defaults to ppm."
" Options are ppm, png.")
;
}

Expand Down Expand Up @@ -112,6 +117,27 @@ cmdline::CommandlineInterface::store(int const& argc, char ** argv)
{
this->normalizerType = data::NormalizerType::LOGARITHMIC_PLUS_ONE;
}

if (vm.count("file-type"))
{
std::string name = vm["file-type"].as<std::string>();
// lower case
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
try
{
this->outputFilewriterGenerator = config::filewriters.at(name);
}
catch (std::out_of_range& e)
{
char buf [256];
sprintf(buf, "No such output filetype: %s", name.c_str());
throw std::invalid_argument(buf);
}
}
else
{
this->outputFilewriterGenerator = config::filewriters.at("ppm");
}
}
catch (std::exception& e)
{
Expand Down
13 changes: 13 additions & 0 deletions src/cmdline/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ namespace cmdline
*/
bool const& getUseAscii() const { return this->useAscii; };

/**
* Get output file writer.
*
* \return file writer
*/
std::shared_ptr<io::FileWriter> getOutputFilewriter(std::shared_ptr<vis::Image> img) const
{ return this->outputFilewriterGenerator(img); };

/**
* Get the colormap.
*
Expand Down Expand Up @@ -123,6 +131,11 @@ namespace cmdline
*/
std::string outputFile;

/**
* Output file writer generator.
*/
std::function<std::shared_ptr<io::FileWriter>(std::shared_ptr<vis::Image>)> outputFilewriterGenerator;

/**
* Colormap to use.
*/
Expand Down
8 changes: 0 additions & 8 deletions src/data/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

template <typename _T>
data::Grid<_T>::~Grid()
noexcept
{
if (this->data == nullptr) delete [] data;
}

template <typename _T>
data::Grid<_T>::Grid(Grid<_T> const& other)
noexcept
: x_size(other.x_size), y_size(other.y_size)
{
this->data = new _T [x_size * y_size];
Expand All @@ -18,15 +16,13 @@ noexcept

template <typename _T>
data::Grid<_T>::Grid(size_t const& x_size)
throw(except::illegal_size)
: data::Grid<_T>::Grid(x_size, x_size)
{
// ctor
}

template <typename _T>
data::Grid<_T>::Grid(size_t const& x_size, size_t const& y_size)
throw(except::illegal_size)
: x_size(x_size), y_size(y_size)
{
if (x_size == 0 || y_size == 0)
Expand All @@ -41,7 +37,6 @@ throw(except::illegal_size)

template <typename _T>
_T& data::Grid<_T>::operator() (size_t const& x, size_t const& y)
throw(std::out_of_range)
{
if (x >= this->x_size || y >= this->y_size)
{
Expand All @@ -55,7 +50,6 @@ throw(std::out_of_range)

template <typename _T>
_T const& data::Grid<_T>::operator() (size_t const& x, size_t const& y) const
throw(std::out_of_range)
{
if (x >= this->x_size || y >= this->y_size)
{
Expand All @@ -69,7 +63,6 @@ throw(std::out_of_range)

template <typename _T>
_T data::Grid<_T>::getMaximum() const
noexcept
{
_T a = this->data[0];
for (size_t i = 0; i < this->x_size * this->y_size; ++i)
Expand All @@ -86,7 +79,6 @@ noexcept
template <typename _T>
std::shared_ptr<std::vector<_T>>
data::Grid<_T>::asVector() const
noexcept
{
std::shared_ptr<std::vector<_T>> vec = std::make_shared<std::vector<_T>>();
for (size_t y = 0; y < this->y_size; ++y)
Expand Down
16 changes: 8 additions & 8 deletions src/data/grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ namespace data {
* Copy constructor.
* \param other
*/
Grid(Grid const&) noexcept;
Grid(Grid const&);

/**
* Constructor.
* For width=height.
* \param width
* \throw if size is 0
*/
explicit Grid(size_t const&) throw (except::illegal_size);
explicit Grid(size_t const&);

/**
* Constructor.
* \param width
* \param height
* \throw if total size is 0
*/
explicit Grid(size_t const&, size_t const&) throw (except::illegal_size);
explicit Grid(size_t const&, size_t const&);

/**
* Destructor.
*/
~Grid() noexcept;
~Grid();

/**
* Non-const accessor.
Expand All @@ -49,7 +49,7 @@ namespace data {
* \return value at x,y
* \throw if any coordinate is too large
*/
_T& operator() (size_t const&, size_t const&) throw(std::out_of_range);
_T& operator() (size_t const&, size_t const&);

/**
* const accessor.
Expand All @@ -59,19 +59,19 @@ namespace data {
* \return value at x,y
* \throw if any coordinate is too large
*/
_T const& operator() (size_t const&, size_t const&) const throw(std::out_of_range);
_T const& operator() (size_t const&, size_t const&) const;

/**
* Get highest value.
* \return maximum
*/
_T getMaximum() const noexcept;
_T getMaximum() const;

/**
* Flatten to list.
* \return vector
*/
std::shared_ptr<std::vector<_T>> asVector() const noexcept;
std::shared_ptr<std::vector<_T>> asVector() const;
protected:
Grid() = delete;
Grid& operator=(Grid const&) = delete;
Expand Down
5 changes: 0 additions & 5 deletions src/data/histogram.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
#include "histogram.hpp"

data::Histogram::Histogram(size_t const& numBins)
throw (except::illegal_size)
: numBins(numBins), bins(numBins)
{
this->normalizer = std::make_shared<data::LinearNormalizer<size_t>>();
}

void
data::Histogram::addEntry(size_t const& x, size_t const& y, size_t const& amount)
throw (std::out_of_range)
{
this->bins(x, y) += amount;
}

data::Grid<double>
data::Histogram::getNormalized() const
noexcept
{
Grid<double> normalized (this->numBins);
for (size_t x = 0; x < this->numBins; ++x)
Expand All @@ -40,14 +37,12 @@ noexcept

size_t const&
data::Histogram::at(size_t const& x, size_t const&y) const
throw(std::out_of_range)
{
return this->bins(x,y);
}

void
data::Histogram::setNormalizer(std::shared_ptr<data::Normalizer<size_t>> const& norm)
throw(except::uninitialized)
{
if (!norm) throw except::uninitialized("Normalizer pointer must not be null!");
this->normalizer = norm;
Expand Down
12 changes: 6 additions & 6 deletions src/data/histogram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace data
*
* \param Size in x and y direction.
*/
Histogram(size_t const&) throw(except::illegal_size);
Histogram(size_t const&);

/**
* Add an entry.
Expand All @@ -28,19 +28,19 @@ namespace data
* \param y position
* \param amount to add, default 1
*/
void addEntry(size_t const&, size_t const&, size_t const& amount = 1) throw(std::out_of_range);
void addEntry(size_t const&, size_t const&, size_t const& amount = 1);

/**
* Get a grid of normalized values.
* \return grid
*/
Grid<double> getNormalized() const noexcept;
Grid<double> getNormalized() const;

/**
* Get width or height
* \return grid size
*/
size_t getNumBins() const noexcept { return this->numBins; }
size_t getNumBins() const { return this->numBins; }

/**
* Get value at.
Expand All @@ -49,13 +49,13 @@ namespace data
* \param y position
* \return const reference to value at position
*/
size_t const& at(size_t const&, size_t const&) const throw(std::out_of_range);
size_t const& at(size_t const&, size_t const&) const;

/**
* Set normalizer to use.
* \param normalizer
*/
void setNormalizer(std::shared_ptr<data::Normalizer<size_t>> const&) throw(except::uninitialized);
void setNormalizer(std::shared_ptr<data::Normalizer<size_t>> const&);
protected:
Histogram() = delete;
Histogram(Histogram const&) = delete;
Expand Down
2 changes: 0 additions & 2 deletions src/data/linear_normalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ data::LinearNormalizer<_T>::LinearNormalizer(_T const& minimum, _T const& maximu
template<typename _T>
void
data::LinearNormalizer<_T>::seed(_T const& value)
noexcept
{
if (!this->initialized)
{
Expand Down Expand Up @@ -62,7 +61,6 @@ data::LinearNormalizer<_T>::calculateNewLinearParameters()
template<typename _T>
double
data::LinearNormalizer<_T>::normalize(_T const& value) const
throw(except::normalizer_exception, except::uninitialized)
{
if (!this->initialized)
{
Expand Down
4 changes: 2 additions & 2 deletions src/data/linear_normalizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace data
* Add value to seed.
* \param value
*/
void seed(_T const&) noexcept override;
void seed(_T const&) override;

/**
* Normalize a value.
Expand All @@ -44,7 +44,7 @@ namespace data
* \throw if value not in range
* \throw if object not initialized
*/
double normalize(_T const&) const throw(except::normalizer_exception, except::uninitialized) override;
double normalize(_T const&) const override;
protected:
LinearNormalizer(LinearNormalizer const&) = delete;
LinearNormalizer& operator= (LinearNormalizer const&) = delete;
Expand Down
2 changes: 0 additions & 2 deletions src/data/logarithmic_plus_one_normalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ data::LogarithmicPlusOneNormalizer::LogarithmicPlusOneNormalizer(size_t const& m

void
data::LogarithmicPlusOneNormalizer::seed(size_t const& value)
noexcept
{
if (!this->initialized)
{
Expand Down Expand Up @@ -49,7 +48,6 @@ noexcept

double
data::LogarithmicPlusOneNormalizer::normalize(size_t const& value) const
throw(except::normalizer_exception, except::uninitialized)
{
if (!this->initialized)
{
Expand Down
Loading

0 comments on commit 9a30c04

Please sign in to comment.