如何编写一个带有指定为 GeneratorParam 类型的 ImageParam 的生成器?

How can I write a Generator with an ImageParam that has a type specified as a GeneratorParam?

我想为各种图像数据类型实现图像管道。我正在定义一个 Generator class 包含描述管道的 build() 方法,一个 GeneratorParam<type> 指定数据类型参数和一个 ImageParam 成员指定输入图片。如果我指定 ImageParam 的类型为我上面定义的 GeneratorParam<Type> ,那么无论我在执行生成器时指定什么类型,输入图像的类型始终是默认类型.如果我在 build() 方法体内复制 ImageParam 的声明,那么它似乎工作正常。这是使用可以具有不同类型的输入图像定义管道的正确方法吗?

这是我最初写的class:

#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,但它需要一个 float 类型的图像(我在生成器中指定的默认值) class):

Error: Input buffer input has type float32 but elem_size of the buffer passed in is 1 instead of 4

我已经通过在 build() 块中复制 ImageParam 的声明解决了这个问题,但这对我来说似乎有点脏。有没有更好的办法?现在是class:

#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"};

        // ...
    }
};

谢谢。

确实很脏。当前最著名的解决方案是在构建顶部使用正确的类型重新初始化输入,而不是使用另一个同名的 ImageParam 对其进行隐藏:

Func build() 
{
    input = ImageParam{datatype, 3, "input"};
    ...
}