openexr-devel
[Top][All Lists]
Advanced

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

[Openexr-devel] Reading an arbitrary rectangle from OpenEXR


From: Михаил
Subject: [Openexr-devel] Reading an arbitrary rectangle from OpenEXR
Date: Wed, 06 Apr 2005 14:24:40 +0400
User-agent: Opera M2/7.52 (Win32, build 3834)

Is it possible to read an arbitrary rectangle from OpenEXR file?
I tried to do so but failed, maybe i am doing something wrong?

here is the small test program below that doesnt work for me.
Kind of errors that appear in debug mode pointing that something wrong occurs in memory accesses.

#define PLATFORM_WIN32
#define HAVE_IOS_BASE
#define HAVE_STL_LIMITS
#define NOMINMAX
#include <windows.h>

#include <half.h>
#include <ImfInputFile.h>
#include <ImfOutputFile.h>
#include <ImfPixelType.h>
#include <ImfChannelList.h>
#include <ImfHeader.h>
#include <ImfArray.h>
#include <ImathBox.h>

#include <iostream>

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

void writeExr (const char fileName[], const half *rPixels, const half *gPixels,
                                                         const half *bPixels, 
int width, int height){
        Header header (width, height);
        header.channels().insert ("R", Channel (HALF));
        header.channels().insert ("G", Channel (HALF));
        header.channels().insert ("B", Channel (HALF));
        OutputFile file (fileName, header);
        FrameBuffer frameBuffer;
        frameBuffer.insert ("R", Slice (HALF, // type
                (char *) rPixels, // base
                sizeof (*rPixels) * 1, // xStride
                sizeof (*rPixels) * width)); // yStride
        frameBuffer.insert ("G", Slice (HALF, // type
                (char *) gPixels, // base
                sizeof (*gPixels) * 1, // xStride
                sizeof (*gPixels) * width)); // yStride
        frameBuffer.insert ("B", Slice (HALF, // type
                (char *) bPixels, // base
                sizeof (*bPixels) * 1, // xStride
                sizeof (*bPixels) * width)); // yStride
        file.setFrameBuffer (frameBuffer);
        file.writePixels (height);
}

int main(){

        cout<<"Generating image 'A'"<<endl;

        int aW = 1024;
        int aH = 512;
        Array2D<half>aRPixels(aH, aW);
        Array2D<half>aGPixels(aH, aW);
        Array2D<half>aBPixels(aH, aW);

        for( int y=0; y<aH; ++y)
                for( int x=0; x<aW; ++x){
                        half value = fmod(((float)y/aH+(float)x/aW)*20.0,1);
                        aRPixels[y][x] = value;
                        aGPixels[y][x] = value*0.5;
                        aBPixels[y][x] = value*0.25;
                }

        cout<<"Writing a.exr..."<<endl;

writeExr( "a.exr", &aRPixels[0][0], &aGPixels[0][0], &aBPixels[0][0], aW, aH);

// here is interesting part begins...................................

        cout<<"Reading part of image 'A'"<<endl;

        Array2D<half>bRPixels;
        Array2D<half>bGPixels;
        Array2D<half>bBPixels;

        InputFile file ("a.exr");

Box2i dwA = file.header().dataWindow(); //original data window stored in a file
        int wA = dwA.max.x - dwA.min.x + 1;
        int hA = dwA.max.y - dwA.min.y + 1;

        Box2i dw; //defining a window of our interest
        dw.min.x = 100; dw.max.x = 227;
        dw.min.y = 50;  dw.max.y = 249;
        int width = dw.max.x - dw.min.x + 1;
        int height = dw.max.y - dw.min.y + 1;

        bRPixels.resizeErase (height, width);
        bGPixels.resizeErase (height, width);
        bBPixels.resizeErase (height, width);

        FrameBuffer frameBuffer;
        int xstride = sizeof (bRPixels[0][0]) * 1;
        int ystride = sizeof (bRPixels[0][0]) * width;
        int dataOffset = dw.min.x + dw.min.y * width;
frameBuffer.insert ("R", Slice (HALF, (char *) (&bRPixels[0][0] - dataOffset), xstride, ystride, 1, 1, 0.0)); frameBuffer.insert ("G", Slice (HALF, (char *) (&bGPixels[0][0] - dataOffset), xstride, ystride, 1, 1, 0.0)); frameBuffer.insert ("B", Slice (HALF, (char *) (&bBPixels[0][0] - dataOffset), xstride, ystride, 1, 1, 0.0));
        file.setFrameBuffer (frameBuffer);
        file.readPixels (dw.min.y, dw.max.y);

writeExr( "b.exr", &bRPixels[0][0], &bGPixels[0][0], &bBPixels[0][0], width, height);

        return 0;
}



reply via email to

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