从RGB到YUV的颜色转换(YCoCg)
问题描述:
我试图执行颜色转换Func
,它将输出到3个单独的缓冲区。函数有一个4×8位通道交错缓冲器(BGRA)和3个输出缓冲器(Y,Co和Cg),每个缓冲器的值均为16位。目前,我使用这段代码:从RGB到YUV的颜色转换(YCoCg)
void rgb_to_ycocg(const uint8_t *pSrc, int32_t srcStep, int16_t *pDst[3], int32_t dstStep[3], int width, int height)
{
Buffer<uint8_t> inRgb((uint8_t *)pSrc, 4, width, height);
Buffer<int16_t> outY(pDst[0], width, height);
Buffer<int16_t> outCo(pDst[1], width, height);
Buffer<int16_t> outCg(pDst[2], width, height);
Var x, y, c;
Func calcY, calcCo, calcCg, inRgb16;
inRgb16(c, x, y) = cast<int16_t>(inRgb(c, x, y));
calcY(x, y) = (inRgb16(0, x, y) + ((inRgb16(2, x, y) - inRgb16(0, x, y)) >> 1)) + ((inRgb16(1, x, y) - (inRgb16(0, x, y) + ((inRgb16(2, x, y) - inRgb16(0, x, y)) >> 1))) >> 1);
calcCo(x, y) = inRgb16(2, x, y) - inRgb16(0, x, y);
calcCg(x, y) = inRgb16(1, x, y) - (inRgb16(0, x, y) + ((inRgb16(2, x, y) - inRgb16(0, x, y)) >> 1));
Pipeline p =Pipeline({calcY, calcCo, calcCg});
p.vectorize(x, 16).parallel(y);
p.realize({ outY, outCo, outCg });
}
的问题是,我比参考实现(基本在C循环)越来越表现不佳。我知道我需要尝试更好的调度,但我认为我在输入/输出缓冲区方面做了错误。我看过这些教程并试图想出一种方法来输出到多个缓冲区。使用Pipeline
是我能找到的唯一方法。我会更好地制作3 Func
并分别打电话给他们吗?这是否正确使用Pipeline
类?
答
这里可能存在的一个大问题是,您每次要转换单个图像时都会编译和编译代码。那真的很慢。使用ImageParams代替缓冲区,定义管道一次,然后多次实现。
二阶效应是我认为你实际上需要一个Tuple而不是一个Pipeline。一个元组Func键计算在同一个内循环,这将重用inRgb等暂时忽略重新编译问题加载所有它的价值,尝试:
void rgb_to_ycocg(const uint8_t *pSrc, int32_t srcStep, int16_t *pDst[3], int32_t dstStep[3], int width, int height)
{
Buffer<uint8_t> inRgb((uint8_t *)pSrc, 4, width, height);
Buffer<int16_t> outY(pDst[0], width, height);
Buffer<int16_t> outCo(pDst[1], width, height);
Buffer<int16_t> outCg(pDst[2], width, height);
Var x, y, c;
Func calcY, calcCo, calcCg, inRgb16;
inRgb16(c, x, y) = cast<int16_t>(inRgb(c, x, y));
out(x, y) = {
inRgb16(0, x, y) + ((inRgb16(2, x, y) - inRgb16(0, x, y)) >> 1)) + ((inRgb16(1, x, y) - (inRgb16(0, x, y) + ((inRgb16(2, x, y) - inRgb16(0, x, y)) >> 1))) >> 1),
inRgb16(2, x, y) - inRgb16(0, x, y),
inRgb16(1, x, y) - (inRgb16(0, x, y) + ((inRgb16(2, x, y) - inRgb16(0, x, y)) >> 1))
};
out.vectorize(x, 16).parallel(y);
out.realize({ outY, outCo, outCg });
}
感谢您的回答,我知道的重新编译的问题,但我很高兴看到它可以用元组完成。我明天会试试这个。再次感谢! –