我想为各种图像数据类型实现图像管道。我正在定义一个Generator
类,其中包含build()
描述管道的方法、一个GeneratorParam<type>
指定数据类型参数和一个ImageParam
指定输入图像的成员。如果我指定的类型是ImageParam
我GeneratorParam<Type>
上面定义的,那么无论我在执行生成器时指定什么类型,输入图像的类型始终是默认类型。如果我复制方法ImageParam
主体内部的声明build()
,那么它似乎工作正常。这是使用可以具有不同类型的输入图像定义管道的正确方法吗?
这是我最初编写的课程:
#include "Halide.h"
using namespace Halide;
class myGenerator : public Generator<myGenerator>
{
public:
// Image data type as a parameter of the generator; default: float
GeneratorParam<Type> datatype{"datatype", Float(32)};
// Input image to the pipeline
ImageParam input{datatype, 3, "input"}; // datatype=Float(32) always
// Pipeline
Func build()
{
// ...
}
};
如果我编译生成器并运行它以生成datatype
与默认不同的管道:
$ ./myGenerator -f pipeline_uint8 -o . datatype=uint8
然后一切看起来都很好,但是管道在运行时崩溃,因为我传递给它的缓冲区是 uint8,但它需要一个浮点类型的图像(我在生成器类中指定的默认值):
Error: Input buffer input has type float32 but elem_size of the buffer passed in is 1 instead of 4
我通过复制块ImageParam
内部的声明解决了这个问题build()
,但这对我来说似乎有点脏。有没有更好的办法?这是现在的课程:
#include "Halide.h"
using namespace Halide;
class myGenerator : public Generator<myGenerator>
{
public:
// Image data type as a parameter of the generator; default: float
GeneratorParam<Type> datatype{"datatype", Float(32)};
// Input image to the pipeline
ImageParam input{datatype, 3, "input"};
// Pipeline
Func build()
{
// Copy declaration. This time, it picks up datatype
// as being the type inputted when executing the
// generator instead of using the default.
ImageParam input{datatype, 3, "input"};
// ...
}
};
谢谢。