Developer documentation
Version 3.0.3-105-gd3941f44
Running a per-datum operation (without multithreading)

This example simply computes the exponential of the intensity for each data point in the input dataset, producing a new dataset of the same size.

#include "command.h"
#include "image.h"
#include "algo/loop.h"
using namespace MR;
using namespace App;
// commmand-line description and syntax:
// (used to produce the help page and verify validity of arguments at runtime)
void usage ()
{
AUTHOR = "Joe Bloggs (joe.bloggs@acme.org)";
+ "raise each voxel intensity to the given power (default: 2)";
+ Argument ("in", "the input image.").type_image_in ()
+ Argument ("out", "the output image.").type_image_out ();
+ Option ("power", "the power by which to raise each value (default: 2)")
+ Argument ("value").type_float();
}
// It is a good idea to use typedef's to help with flexibility if types need to
// be changed later on.
using value_type = float;
// This is where execution proper starts - the equivalent of main().
// The difference is that this is invoked after all command-line parsing has
// been done.
void run ()
{
// get power from command-line if supplied, default to 2.0:
value_type power = get_option_value ("power", 2.0);
// Image to access the input data:
// get the header of the input data, and modify to suit the output dataset:
Header header (in);
header.datatype() = DataType::Float32;
// create the output Buffer to store the output data, based on the updated
// header information:
auto out = Image<value_type>::create (argument[1], header);
// create the loop structure. This version will traverse the image data in
// order of increasing stride of the input dataset, to ensure contiguous
// voxel values are most likely to be processed consecutively. This helps to
// ensure maximum performance.
//
// Note that we haven't specified any axes, so this will process datasets of
// any dimensionality, whether 3D, 4D or ND:
auto loop = Loop (in);
// run the loop:
for (auto l = loop (in, out); l; ++l)
out.value() = std::pow (in.value(), power);
// That's it! Data write-back is performed by the Image destructor,
// invoked when it goes out of scope at function exit.
}
void run()
void usage()
static Image open(const std::string &image_name, bool read_write_if_existing=false)
Definition: image.h:189
FORCE_INLINE LoopAlongAxes Loop()
Definition: loop.h:419
OptionList OPTIONS
the options accepted by the command
vector< ParsedArgument > argument
the list of arguments parsed from the command-line
const char * AUTHOR
set the author of the command
T get_option_value(const std::string &name, const T default_value)
Returns the option value if set, and the default otherwise.
Definition: app.h:465
Description DESCRIPTION
additional description of the command over and above the synopsis
ArgumentList ARGUMENTS
the arguments expected by the command
std::unique_ptr< ImageIO::Base > create(Header &H)
MR::default_type value_type
Definition: typedefs.h:33
Definition: base.h:24