Skip to content

Commit

Permalink
Fixed a bug that caused isimagefile function not to work in the distr…
Browse files Browse the repository at this point in the history
…ibuted mode.
  • Loading branch information
Arttu Ilari Miettinen committed Oct 10, 2022
1 parent cda3683 commit e06bfbf
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pilib/iocommands.h
Expand Up @@ -18,7 +18,7 @@ namespace pilib
protected:
friend class CommandList;

IsImageFileCommand() : Command("isimagefile", "Checks if a file with given name is image file.",
IsImageFileCommand() : Command("isimagefile", "Checks if a file with given name is an image file.",
{
CommandArgument<std::string>(ParameterDirection::In, "filename", "Name of image file."),
CommandArgument<Image<uint8_t> >(ParameterDirection::In, "result", "Set to 1 if the given file name is a readable image file, and to 0 otherwise.")
Expand Down
65 changes: 64 additions & 1 deletion pilib/trivialdistributable.h
Expand Up @@ -2,9 +2,25 @@

#include "command.h"
#include "distributable.h"
#include "io/imagedatatype.h"

namespace pilib
{

namespace internals
{
/**
Functor that creates a new image and stores it to images vector.
*/
template<typename pixel_t> struct AssignImage
{
static void run(ParamVariant& param, ImageBase* img)
{
param = (Image<pixel_t>*)img;
}
};
}

/**
Identifies command to be trivially distributable, i.e. it runs the same way regardless of distributed processing state.
Used for example for info and help commands.
Expand All @@ -24,7 +40,54 @@ namespace pilib

virtual std::vector<std::string> runDistributed(Distributor& distributor, std::vector<ParamVariant>& args) const override
{
runInternal(distributor.getSystem(), args);
// Convert DistributedImage to Image.
const std::vector<CommandArgumentBase>& argdefs = this->args();

std::vector<ParamVariant> modifiedArgs, modifiedArgs2;

std::vector<std::unique_ptr<ImageBase>> images;
for (size_t n = 0; n < argdefs.size(); n++)
{
const CommandArgumentBase& argdef = argdefs[n];
if (isImage(argdef.dataType()))
{
DistributedImageBase* dimage = getDistributedImage(args[n]);
std::unique_ptr<ImageBase> nimage = dimage->toNormalImage();

ParamVariant param;
itl2::pick<internals::AssignImage>(dimage->dataType(), param, nimage.get());

modifiedArgs.push_back(param);
modifiedArgs2.push_back(param);

images.push_back(std::move(nimage));
}
else
{
modifiedArgs.push_back(args[n]);
modifiedArgs2.push_back(args[n]);
}
}

// Run the command.
runInternal(distributor.getSystem(), modifiedArgs2); // Note: this line usually clears argument list given as parameter.

// Put data from Images to the DistributedImages.
for (size_t n = 0; n < argdefs.size(); n++)
{
const CommandArgumentBase& argdef = argdefs[n];
if (isImage(argdef.dataType()))
{
DistributedImageBase* dimage = getDistributedImage(args[n]);
ImageBase* nimage = getImage(modifiedArgs[n]);
dimage->setData(nimage);
}
else
{
args[n] = modifiedArgs[n];
}
}

return std::vector<std::string>();
}
};
Expand Down
12 changes: 11 additions & 1 deletion python_scripts/test_pi2py2.py
Expand Up @@ -1644,7 +1644,17 @@ def test_eval3_dist(self):

def test_ramp3_1_dist(self):
self.check_difference_normal_distributed('ramp3', ['img'], 'img', maxmem=5)


def test_isimagefile(self):
pi = self.pi2
self.check_result(pi.isimagefile(input_file()) == True, "input image is tested not to exist")
self.check_result(pi.isimagefile('non-existing file name') == False, "not existing image is test to exist")
pi.distribute(Distributor.Local)
self.check_result(pi.isimagefile(input_file()) == True, "input image is tested not to exist (distributed)")
self.check_result(pi.isimagefile('non-existing file name') == False, "not existing image is test to exist (distributed)")
pi.distribute(Distributor.No);


def test_delaying1(self):
infile = input_file()
self.check_difference_delaying('delaying_1', f"read(img, {infile}); gaussfilter(img, out, 1); add(img, 100); clear(img); convert(out, conv, uint8);", 'conv');
Expand Down

0 comments on commit e06bfbf

Please sign in to comment.