1

现在已经尝试了2天,没有任何运气。我的目标是获取我们通常从中创建 2 个 exr 文件的 2 个数据数组,并创建一个多层/多页 TIFF 文件(FreeImage 仅支持 tif、gif 和 ico 的多页,我们也需要它在 python 中也能很好地工作)。

static unsigned DLL_CALLCONV
myReadProc(void *buffer, unsigned size, unsigned count, freeimage::fi_handle handle) {
    return (unsigned)fread(buffer, size, count, (FILE *)handle);
}

static unsigned DLL_CALLCONV
myWriteProc(void *buffer, unsigned size, unsigned count, freeimage::fi_handle handle) {
    return (unsigned)fwrite(buffer, size, count, (FILE *)handle);
}

static int DLL_CALLCONV
mySeekProc(freeimage::fi_handle handle, long offset, int origin) {
    return fseek((FILE *)handle, offset, origin);
}

static long DLL_CALLCONV
myTellProc(freeimage::fi_handle handle) {
    return ftell((FILE *)handle);
}   

void MyClass::TestMultilayeredFile(math::float4 *data1, math::float4 *data2, Hash128 &hash, const int width, const int height)
{
    freeimage::FreeImageIO io;

    io.read_proc = myReadProc;
    io.write_proc = myWriteProc;
    io.seek_proc = mySeekProc;
    io.tell_proc = myTellProc;

    core::string cachePathAsset = GetAbsoluteHashFilePath(GetRelativeHashFilePath(hash, "_combined.tiff"));

    const int pixelCount = width * height;

    enum Layers
    {
        kData1 = 0,
        kData2 = 1,
        kLayerCount = 2
    };

    FILE *file = fopen(cachePathAsset.c_str(), "w+b");
    if (file != NULL) {


        freeimage::FIMULTIBITMAP *out = freeimage::FreeImage_OpenMultiBitmapFromHandle(freeimage::FREE_IMAGE_FORMAT::FIF_TIFF, &io, (freeimage::fi_handle)file, 0x0800);

        if (out)
        {
            const math::float4* kLayers[2] = { data1, data2 };

            for (int layer = 0; layer < kLayerCount; ++layer)
            {
                freeimage::FIBITMAP* bitmap = freeimage::FreeImage_AllocateT(freeimage::FIT_RGBAF, width, height);
                const int pitch = freeimage::FreeImage_GetPitch(bitmap);
                void* bytes = (freeimage::BYTE*)freeimage::FreeImage_GetBits(bitmap);
                const int bytesPerPixel = freeimage::FreeImage_GetBPP(bitmap) / 8;
                DebugAssert(pitch == width * bytesPerPixel);
                DebugAssert(bytes);
                DebugAssert(bytesPerPixel == 16);

                memcpy(bytes, kLayers[layer], pixelCount * bytesPerPixel);

                freeimage::FreeImage_AppendPage(out, bitmap);
                freeimage::FreeImage_Unload(bitmap);
            }

            // Save the multi-page file to the stream
            BOOL bSuccess = freeimage::FreeImage_SaveMultiBitmapToHandle(freeimage::FREE_IMAGE_FORMAT::FIF_TIFF, out, &io, (freeimage::fi_handle)file, 0x0800);

            freeimage::FreeImage_CloseMultiBitmap(out, 0);
        }
    }
}

tiff 文件已创建,但仅包含 1kb。此外,bSuccess返回 false。生成单个图像的代码在过去一直有效,但以前没有做过 mulipage。

4

0 回答 0