Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory safety and output format #3

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion include/mtx2img/mtx2img.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum class Object
enum class Format
{
Coordinate, // <== supported (sparse representation)
Array // <== unsupported, planned (dense representation)
Array // <== supported (dense representation)
}; // enum class Format


Expand All @@ -47,9 +47,17 @@ enum class Structure
} // namespace format


enum class Aggregation
{
Count, // <== count the number of nonzeros referencing each pixel
Sum // <== sum the values of each entry referencing a pixel
}; // enum class Aggregation


std::vector<unsigned char> convert(std::istream& r_stream,
std::size_t& r_imageWidth,
std::size_t& r_imageHeight,
const Aggregation aggregation,
const std::string& r_colormapName);


Expand Down
24 changes: 21 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
const std::map<std::string,std::string> defaultArguments {
{"-r", "1080"},
{"-a", "count"},
{"-c", "binary"}
};

Expand All @@ -32,6 +33,7 @@ struct Arguments
std::filesystem::path inputPath;
std::filesystem::path outputPath;
std::size_t resolution;
mtx2img::Aggregation aggregation;
std::string colormap;
}; // struct Arguments

Expand All @@ -43,7 +45,10 @@ void printHelp()
<< "Usage: mtx2img <path-to-source> <path-to-output> [OPTION ARGUMENT] ...\n"
<< "Options:\n"
<< " -r <resolution> : highest resolution of the output image in pixels (default: " << defaultArguments.at("-r") << ").\n"
<< " -c <colormap> : colormap to use for per pixel nonzero density. Options: [binary, kindlmann, viridis] (default: " << defaultArguments.at("-c") << ").\n"
<< " -a <aggregation> : controls how sparse entries are aggregated to pixels. Options: [count, sum]\n"
<< " \"count\" ignores values and counts the number of entries referencing each pixel.\n"
<< " \"sum\" reads values and sums them up for each pixel.\n"
<< " -c <colormap> : colormap to use for aggregated pixel values. Options: [binary, kindlmann, viridis] (default: " << defaultArguments.at("-c") << ").\n"
<< "\n"
<< "The input path must point to an existing MatrixMarket file (or pass '-' to read the same format from stdin).\n"
<< "The parent directory of the output path must exist, and the output path is assumed to either not exist, or\n"
Expand All @@ -66,8 +71,8 @@ std::optional<Arguments> parseArguments(int argc, char const* const* argv)
if (it_argument == argMap.end()) {
if ((it_argument = argMap.find(arg)) == argMap.end()) {
// The provided key does not exist in the argument map
// Special case: "--help"
if (arg == "--help") {
// Special case: "--help" or "-h"
if (arg == "--help" || arg == "-h") {
return {};
}

Expand Down Expand Up @@ -178,6 +183,18 @@ std::optional<Arguments> parseArguments(int argc, char const* const* argv)
} // if !writePermission
} // if arguments.outputPath != "-"

// Validate aggregation method
if (argMap["-a"] == "count") {
arguments.aggregation = mtx2img::Aggregation::Count;
} else if (argMap["-a"] == "sum") {
arguments.aggregation = mtx2img::Aggregation::Sum;
} else {
throw std::invalid_argument(std::format(
"Error: invalid aggregation method: {}\n",
argMap["a"]
));
}

// Validate colormap
arguments.colormap = argMap["-c"];
{
Expand Down Expand Up @@ -295,6 +312,7 @@ int main(int argc, char const* const* argv)
image = mtx2img::convert(*p_inputStream,
imageSize.first,
imageSize.second,
arguments.aggregation,
arguments.colormap);

#ifdef NDEBUG
Expand Down
Loading