openexr-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Openexr-devel] tiff to exr problem


From: Florian Kainz
Subject: Re: [Openexr-devel] tiff to exr problem
Date: Sun, 23 Oct 2005 17:21:32 -0700
User-agent: Mozilla Thunderbird 1.0 (X11/20041207)

Your inputTexture array appears to be too large.  It should have
npixels elements, not npixels*4.  TIFFRGBAImageGet packs all four
channels of an RGBA pixel into a single uint32.  To access an
individual channel of the pixel that is stored in inputTexture[i],
you have to shift and mask the bits:

    uint32 value = (inputTexture[i] >> (channel * 8)) & 0x000000ff;

where channel is between 0 and 3.

The value will be in the range from 0 to 255, and it will most likely
have a gamma of about 2.2, which is probably not what you want.  After
converting to float, you will have to undo the gamma correction and
scale the value to a more useful range, e.g. 0 to 1 (or 0 to 2.036,
if you go with exrdisplay's way of displaying images).

You will need three output arrays, one for each channel in the output
file, and you will have to change your calls to framebuffer.insert()
accordingly.



the nrgy wrote:
I've been messing around with OpenEXR and recently I've been coding a small util to read tiff images and write exr files. I am having problems when I pass the array which represents the tiff information to the one which I will be passing to the exr calls. When I extract a channel from my tiff array and pass it to an array exr will use it is all messed up. One thing I have noticed is when I view what is inside this array exr will be using is that only a small portion is filled with information. I think I can tell what some of my problems are, these being "my uint to float conversions which I have googled to no end without any results, maybe I'm inproperly setting up my data arrays, and last but not least I just dont get it :D". I have included some mangled code which is a sample for my problem and any replys would be welcome. I should mention I'm developing under Linux x86_64, using libtiff for my tiff functions, and using gcc v4.

Thanks Nathan

#include <iostream>
#include <stdlib.h>
#include <tiffio.h>
#include <ImfOutputFile.h>
#include <ImfInputFile.h>
#include <ImfChannelList.h>
#include <ImfStringAttribute.h>
#include <ImfMatrixAttribute.h>
#include <ImfArray.h>

using namespace std;
using namespace Imath;
using namespace Imf;

int main() {
    TIFF *tif;
    TIFFRGBAImage img;
    char emsg[1024];
int i;
    uint32 *inputTexture;
    float *outputTexture;
    long int npixels;
    long int imageSizeOf;

    tif = TIFFOpen("/home/nrgy/test/test.tif", "rb");
    if (tif != NULL) {
        if (TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
            npixels = img.width * img.height;
            imageSizeOf = npixels * 4;
            inputTexture = new uint32 [imageSizeOf];
            if (inputTexture != NULL) {
if (TIFFRGBAImageGet(&img, inputTexture, img.width, img.height) == 0 ) {
                    TIFFError("/home/nrgy/test/test.tif", emsg);
                    exit(1);
                }
            }
            TIFFRGBAImageEnd(&img);
        }
    }
outputTexture = new float [npixels];
    for (i = 0; i < npixels; i++) {
        outputTexture[i] = (float)inputTexture[i * 4];
        cout << "hey! " << i;
        printf(" - %f\n", (float)inputTexture[i * 4]);
        if ((float)inputTexture[i * 4] == 0) exit(0);
    }

    Header header(img.width, img.height);
    header.channels().insert("R", Channel (FLOAT));
    header.channels().insert("G", Channel (FLOAT));
    header.channels().insert("B", Channel (FLOAT));
    header.compression() = Compression(1);

    OutputFile file ("/home/nrgy/test/test.exr", header);
    FrameBuffer frameBuffer;
    frameBuffer.insert ("R",
            Slice (FLOAT,
               (char *) outputTexture,
               sizeof (*outputTexture) * 1,
               sizeof (*outputTexture) * img.width));
    frameBuffer.insert ("G",
            Slice (FLOAT,
               (char *) outputTexture,
               sizeof (*outputTexture) * 1,
               sizeof (*outputTexture) * img.width));
    frameBuffer.insert ("B",
            Slice (FLOAT,
               (char *) outputTexture,
               sizeof (*outputTexture) * 1,
               sizeof (*outputTexture) * img.width));
    file.setFrameBuffer(frameBuffer);
    file.writePixels(img.height);

    if (inputTexture) delete[] inputTexture;
    if (outputTexture) delete[] outputTexture;

    return 0;
}


------------------------------------------------------------------------

_______________________________________________
Openexr-devel mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/openexr-devel




reply via email to

[Prev in Thread] Current Thread [Next in Thread]