如何使用 OpenImageIO 和 read_tiles() 读取平铺图像

How to read tiled images with OpenImageIO and read_tiles()

本文关键字:读取 图像 tiles OpenImageIO 何使用 read      更新时间:2023-10-16

我在使用 VisualStudio 和 OIIO 2.0.8 的 Windows 上读取平铺图像时遇到问题。 为了进行测试,我渲染了一个带有选中平铺选项且没有平铺选项的 Arnold 图像。虽然读取扫描线图像工作正常,但平铺渲染不会读取任何内容。我可以在调试模式下看到,tilePixel 数组在读取瓷砖之前和之后根本没有变化。read_tiles调用的结果始终为 true。

也许任何人都可以看看并告诉我是否有明显的问题。

这是我使用的仍然有点混乱的代码。

std::string filename = "C:/daten/images/tiledRender.exr";
auto in = ImageInput::open(filename);
if (in)
{
int tw = spec.tile_width;
int th = spec.tile_height;
int w = spec.width;
int h = spec.height;
int numBytesPerPixel = 3;
size_t numBytesPerImage = w*h*numBytesPerPixel;
size_t numBytesPerLine = w*numBytesPerPixel;
std::vector<unsigned char> pixels(numBytesPerImage, 120);
unsigned char* line = &pixels[0];
unsigned char *bit = image->bits(); //this comes from QImage
if (tw == 0) // no tiles read scanlines
{
qDebug() << "Found scanline rendering.n";
for (int i = 0; i < h; i++)
{
bool success = in->read_scanlines(0, 0, i, i+1, 0, 0, 3, TypeDesc::UCHAR, line);
if (!success)
qDebug() << "read scanline problem at scanline " << i << "n";
line += numBytesPerLine;
}
memcpy(bit, &pixels[0], numBytesPerImage);
}
else {
qDebug() << "Found tiled rendering.n";
int numTilePixels = tw * th;
int numBytesPerTile = numTilePixels * 3;
std::vector<unsigned char> tilePixels(numBytesPerTile, 80);
unsigned char* tilePtr = &tilePixels[0];
for (int x = 0; x < w; x += tw)
{
for (int y = 0; y < h; y += th)
{
int ttw = tw;
int tth = th;
if ((x + tw) >= w)
ttw = w - x;
if ((y + th) >= h)
tth = h - y;
bool success = in->read_tiles(0, 0, x, x+ttw, y, y+tth, 0, 0, 0, 3, TypeDesc::UCHAR, tilePtr);
if (!success)
qDebug() << "read tiles problemn";
}
}
}

解决方案在于读取瓷砖的方式。我必须使用 zEnd = 1,而不是读取 zStart = 0 和 zEnd = 0。

所以代替:

bool success = in->read_tiles(0, 0, x, x+ttw, y, y+tth, 0, 0, 0, 3, TypeDesc::UCHAR, tilePtr);

它必须是

bool success = in->read_tiles(0, 0, x, x+ttw, y, y+tth, 0, 1, 0, 3, TypeDesc::UCHAR, tilePtr);