1

Been trying for 2 days now without any luck. My goal is to take 2 arrays of data that we normally create 2 exr files from, and create a multilayered/multipage TIFF file (FreeImage only support multipage for tif, gif and ico, and we need this to work well in python too).

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);
        }
    }
}

The tiff file is created, but only contains 1kb. Also, bSuccess returns false. The code to generate the individual images have worked in the past, but haven't done mulipage before.

chikuba
  • 4,229
  • 6
  • 43
  • 75

0 Answers0