~ubuntu-branches/ubuntu/oneiric/nux/oneiric

« back to all changes in this revision

Viewing changes to NuxImage/OpenEXR.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-11-18 19:17:32 UTC
  • Revision ID: james.westby@ubuntu.com-20101118191732-rn35790vekj6o4my
Tags: upstream-0.9.4
ImportĀ upstreamĀ versionĀ 0.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 Inalogic Inc.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3, as
 
6
 * published by the  Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 
12
 * License for more details.
 
13
 *
 
14
 * You should have received a copy of both the GNU Lesser General Public
 
15
 * License version 3 along with this program.  If not, see
 
16
 * <http://www.gnu.org/licenses/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
 
19
 *
 
20
 */
 
21
 
 
22
 
 
23
#include "NuxCore/NuxCore.h"
 
24
 
 
25
#include "NuxCore/NuxCore.h"
 
26
#include "BitmapFormats.h"
 
27
 
 
28
 
 
29
#ifdef NUX_OPENEXR_SUPPORT
 
30
#include "OpenEXR.h"
 
31
//#include "OpenEXR/ctlToLut.h"
 
32
//#include "OpenEXR/fileNameForFrame.h"
 
33
//#include "OpenEXR/FileReadingThread.h"
 
34
//#include "OpenEXR/osDependent.h"
 
35
//#include "OpenEXR/ImageBuffers.h"
 
36
//#include "OpenEXR/Timer.h"
 
37
#include "OpenEXR/ImfThreading.h"
 
38
#include "OpenEXR/ImfInputFile.h"
 
39
#include "OpenEXR/ImfChannelList.h"
 
40
#include "OpenEXR/ImfStandardAttributes.h"
 
41
#include "OpenEXR/ImfRgbaYca.h"
 
42
#include "OpenEXR/ImfArray.h"
 
43
#include "OpenEXR/ImfRgbaFile.h"
 
44
#include "OpenEXR/ImfChannelList.h"
 
45
#include "OpenEXR/half.h"
 
46
#include "OpenEXR/imathbox.h"
 
47
#include "OpenEXR/ImfFrameBuffer.h"
 
48
 
 
49
 
 
50
#include <stdlib.h>
 
51
#include <sstream>
 
52
#include <iomanip>
 
53
#include <cmath>
 
54
 
 
55
namespace nux
 
56
{
 
57
 
 
58
  NBitmapData *Load_OpenEXR (const TCHAR *fileName)
 
59
  {
 
60
    Imf::Array2D<float> rPixels;
 
61
    Imf::Array2D<float> gPixels;
 
62
    Imf::Array2D<float> bPixels;
 
63
 
 
64
    Imf::InputFile in (fileName);
 
65
 
 
66
    Imf::Header header = in.header();
 
67
    const Imath::Box2i &dw = in.header().dataWindow();
 
68
 
 
69
    int width = dw.max.x - dw.min.x + 1;
 
70
    int height = dw.max.y - dw.min.y + 1;
 
71
 
 
72
    const Imf::ChannelList &ch = in.header().channels();
 
73
 
 
74
 
 
75
    rPixels.resizeErase (height, width);
 
76
    gPixels.resizeErase (height, width);
 
77
    bPixels.resizeErase (height, width);
 
78
 
 
79
    float *FloatArray = 0;
 
80
    NBitmapData *BitmapData = 0;
 
81
 
 
82
    if (ch.findChannel ("Y") || ch.findChannel ("RY") || ch.findChannel ("BY") )
 
83
    {
 
84
 
 
85
    }
 
86
    else if (ch.findChannel ("R") && ch.findChannel ("G") && ch.findChannel ("B") && ch.findChannel ("A") )
 
87
    {
 
88
      //
 
89
      // RGBA mode
 
90
      //
 
91
      // The pixel buffers for the tree image channels (RGB)
 
92
      // are padded with a fourth dummy channel (A) and interleaved
 
93
      // (RGBARGBARGBA...).  All three buffers have the same width
 
94
      // and height as the frame.
 
95
      //
 
96
 
 
97
      BitmapData = (NBitmapData *) new NTextureData (
 
98
                     BITFMT_RGBA32F,
 
99
                     width,
 
100
                     height, 1);
 
101
 
 
102
      BYTE *Dest = BitmapData->GetSurface (0).GetPtrRawData(); //new float[4*width*height];
 
103
 
 
104
      Imf::FrameBuffer frameBuffer;
 
105
 
 
106
      frameBuffer.insert ("R",        // name
 
107
                          Imf::Slice (Imf::FLOAT,     // type
 
108
                                      (char *) (&Dest[0] -        // base
 
109
                                          dw.min.x -
 
110
                                          dw.min.y * width),
 
111
                                      sizeof (float) * 4,         // xStride
 
112
                                      sizeof (float) * 4 * width, // yStride
 
113
                                      1, 1,                       // x/y sampling
 
114
                                      0.0) );                     // fillValue
 
115
 
 
116
      frameBuffer.insert ("G",        // name
 
117
                          Imf::Slice (Imf::FLOAT,     // type
 
118
                                      (char *) (&Dest[4] -        // base
 
119
                                          dw.min.x -
 
120
                                          dw.min.y * width),
 
121
                                      sizeof (float) * 4,         // xStride
 
122
                                      sizeof (float) * 4 * width, // yStride
 
123
                                      1, 1,                       // x/y sampling
 
124
                                      0.0) );                     // fillValue
 
125
 
 
126
      frameBuffer.insert ("B",        // name
 
127
                          Imf::Slice (Imf::FLOAT,     // type
 
128
                                      (char *) (&Dest[8] -        // base
 
129
                                          dw.min.x -
 
130
                                          dw.min.y * width),
 
131
                                      sizeof (float) * 4,         // xStride
 
132
                                      sizeof (float) * 4 * width, // yStride
 
133
                                      1, 1,                       // x/y sampling
 
134
                                      0.0) );                     // fillValue
 
135
 
 
136
      frameBuffer.insert ("A",        // name
 
137
                          Imf::Slice (Imf::FLOAT,     // type
 
138
                                      (char *) (&Dest[12] -        // base
 
139
                                          dw.min.x -
 
140
                                          dw.min.y * width),
 
141
                                      sizeof (float) * 4,         // xStride
 
142
                                      sizeof (float) * 4 * width, // yStride
 
143
                                      1, 1,                       // x/y sampling
 
144
                                      1.0) );                     // fillValue
 
145
 
 
146
      in.setFrameBuffer (frameBuffer);
 
147
      in.readPixels (dw.min.y, dw.max.y);
 
148
 
 
149
    }
 
150
    else if (ch.findChannel ("R") && ch.findChannel ("G") && ch.findChannel ("B") )
 
151
    {
 
152
      //
 
153
      // RGB mode
 
154
      //
 
155
      // The pixel buffers for the tree image channels (RGB)
 
156
      // are padded with a fourth dummy channel (A) and interleaved
 
157
      // (RGBRGBRGB...).  All three buffers have the same width
 
158
      // and height as the frame.
 
159
      //
 
160
 
 
161
      BitmapData = (NBitmapData *) new NTextureData (
 
162
                     BITFMT_RGB32F,
 
163
                     width,
 
164
                     height, 1);
 
165
 
 
166
      BYTE *Dest = BitmapData->GetSurface (0).GetPtrRawData(); //new float[4*width*height];
 
167
 
 
168
      Imf::FrameBuffer frameBuffer;
 
169
 
 
170
      frameBuffer.insert ("R",        // name
 
171
                          Imf::Slice (Imf::FLOAT,     // type
 
172
                                      (char *) (&Dest[0] -        // base
 
173
                                          dw.min.x -
 
174
                                          dw.min.y * width),
 
175
                                      sizeof (float) * 3,         // xStride
 
176
                                      sizeof (float) * 3 * width, // yStride
 
177
                                      1, 1,                       // x/y sampling
 
178
                                      0.0) );                     // fillValue
 
179
 
 
180
      frameBuffer.insert ("G",        // name
 
181
                          Imf::Slice (Imf::FLOAT,     // type
 
182
                                      (char *) (&Dest[4] -        // base
 
183
                                          dw.min.x -
 
184
                                          dw.min.y * width),
 
185
                                      sizeof (float) * 3,         // xStride
 
186
                                      sizeof (float) * 3 * width, // yStride
 
187
                                      1, 1,                       // x/y sampling
 
188
                                      0.0) );                     // fillValue
 
189
 
 
190
      frameBuffer.insert ("B",        // name
 
191
                          Imf::Slice (Imf::FLOAT,     // type
 
192
                                      (char *) (&Dest[8] -        // base
 
193
                                          dw.min.x -
 
194
                                          dw.min.y * width),
 
195
                                      sizeof (float) * 3,         // xStride
 
196
                                      sizeof (float) * 3 * width, // yStride
 
197
                                      1, 1,                       // x/y sampling
 
198
                                      0.0) );                     // fillValue
 
199
 
 
200
      in.setFrameBuffer (frameBuffer);
 
201
      in.readPixels (dw.min.y, dw.max.y);
 
202
 
 
203
    }
 
204
 
 
205
    return BitmapData;
 
206
  }
 
207
 
 
208
}
 
209
 
 
210
#endif // NUX_OPENEXR_SUPPORT