This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathil_exr.cpp
More file actions
435 lines (337 loc) · 9.81 KB
/
il_exr.cpp
File metadata and controls
435 lines (337 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//-----------------------------------------------------------------------------
//
// ImageLib Sources
// Copyright (C) 2000-2009 by Denton Woods
// Last modified: 03/07/2009
//
// Filename: src-IL/src/il_exr.cpp
//
// Description: Reads from an OpenEXR (.exr) file using the OpenEXR library.
//
//-----------------------------------------------------------------------------
#include "il_internal.h"
#ifndef IL_NO_EXR
#ifndef HAVE_CONFIG_H // We are probably on a Windows box .
//#define OPENEXR_DLL
#define HALF_EXPORTS
#endif //HAVE_CONFIG_H
#include "il_exr.h"
#include <ImfRgba.h>
#include <ImfArray.h>
#include <ImfRgbaFile.h>
//#include <ImfTiledRgbaFile.h>
//#include <ImfInputFile.h>
//#include <ImfTiledInputFile.h>
//#include <ImfPreviewImage.h>
//#include <ImfChannelList.h>
#if (defined(_WIN32) || defined(_WIN64)) && defined(IL_USE_PRAGMA_LIBS)
#if defined(_MSC_VER) || defined(__BORLANDC__)
#ifndef _DEBUG
#pragma comment(lib, "openexr.lib")
#else
#pragma comment(lib, "openexr-d.lib")
#endif
#endif
#endif
//! Checks if the file specified in FileName is a valid EXR file.
ILboolean ilIsValidExr(ILconst_string FileName)
{
ILHANDLE ExrFile;
ILboolean bExr = IL_FALSE;
if (!iCheckExtension(FileName, IL_TEXT("exr"))) {
ilSetError(IL_INVALID_EXTENSION);
return bExr;
}
ExrFile = iopenr(FileName);
if (ExrFile == NULL) {
ilSetError(IL_COULD_NOT_OPEN_FILE);
return bExr;
}
bExr = ilIsValidExrF(ExrFile);
icloser(ExrFile);
return bExr;
}
//! Checks if the ILHANDLE contains a valid EXR file at the current position.
ILboolean ilIsValidExrF(ILHANDLE File)
{
ILuint FirstPos;
ILboolean bRet;
iSetInputFile(File);
FirstPos = itell();
bRet = iIsValidExr();
iseek(FirstPos, IL_SEEK_SET);
return bRet;
}
//! Checks if Lump is a valid EXR lump.
ILboolean ilIsValidExrL(const void *Lump, ILuint Size)
{
iSetInputLump(Lump, Size);
return iIsValidExr();
}
// Internal function used to get the EXR header from the current file.
ILboolean iGetExrHead(EXRHEAD *Header)
{
Header->MagicNumber = GetLittleUInt();
Header->Version = GetLittleUInt();
return IL_TRUE;
}
// Internal function to get the header and check it.
ILboolean iIsValidExr()
{
EXRHEAD Head;
if (!iGetExrHead(&Head))
return IL_FALSE;
iseek(-8, IL_SEEK_CUR);
return iCheckExr(&Head);
}
// Internal function used to check if the HEADER is a valid EXR header.
ILboolean iCheckExr(EXRHEAD *Header)
{
// The file magic number (signature) is 0x76, 0x2f, 0x31, 0x01
if (Header->MagicNumber != 0x01312F76)
return IL_FALSE;
// The only valid version so far is version 2. The upper value has
// to do with tiling.
if (Header->Version != 0x002 && Header->Version != 0x202)
return IL_FALSE;
return IL_TRUE;
}
// Nothing to do here in the constructor.
ilIStream::ilIStream() : Imf::IStream("N/A")
{
return;
}
bool ilIStream::read(char c[], int n)
{
if (iread(c, 1, n) != n)
return false;
return true;
}
//@TODO: Make this work with 64-bit values.
Imf::Int64 ilIStream::tellg()
{
Imf::Int64 Pos;
// itell only returns a 32-bit value!
Pos = itell();
return Pos;
}
// Note that there is no return value here, even though there probably should be.
//@TODO: Make this work with 64-bit values.
void ilIStream::seekg(Imf::Int64 Pos)
{
// iseek only uses a 32-bit value!
iseek((ILint)Pos, IL_SEEK_SET); // I am assuming this is seeking from the beginning.
return;
}
void ilIStream::clear()
{
return;
}
//! Reads an .exr file.
ILboolean ilLoadExr(ILconst_string FileName)
{
ILHANDLE ExrFile;
ILboolean bExr = IL_FALSE;
ExrFile = iopenr(FileName);
if (ExrFile == NULL) {
ilSetError(IL_COULD_NOT_OPEN_FILE);
return bExr;
}
bExr = ilLoadExrF(ExrFile);
icloser(ExrFile);
return bExr;
}
//! Reads an already-opened .exr file
ILboolean ilLoadExrF(ILHANDLE File)
{
ILuint FirstPos;
ILboolean bRet;
iSetInputFile(File);
FirstPos = itell();
bRet = iLoadExrInternal();
iseek(FirstPos, IL_SEEK_SET);
return bRet;
}
//! Reads from a memory "lump" that contains an .exr
ILboolean ilLoadExrL(const void *Lump, ILuint Size)
{
iSetInputLump(Lump, Size);
return iLoadExrInternal();
}
using namespace Imath;
using namespace Imf;
using namespace std;
ILboolean iLoadExrInternal()
{
Array<Rgba> pixels;
Box2i dataWindow;
float pixelAspectRatio;
ILfloat *FloatData;
ilIStream File;
RgbaInputFile in(File);
Rgba a;
dataWindow = in.dataWindow();
pixelAspectRatio = in.pixelAspectRatio();
int dw, dh, dx, dy;
dw = dataWindow.max.x - dataWindow.min.x + 1;
dh = dataWindow.max.y - dataWindow.min.y + 1;
dx = dataWindow.min.x;
dy = dataWindow.min.y;
pixels.resizeErase (dw * dh);
in.setFrameBuffer (pixels - dx - dy * dw, 1, dw);
try
{
in.readPixels (dataWindow.min.y, dataWindow.max.y);
}
catch (const exception &e)
{
// If some of the pixels in the file cannot be read,
// print an error message, and return a partial image
// to the caller.
ilSetError(IL_LIB_EXR_ERROR); // Could I use something a bit more descriptive based on e?
e; // Prevent the compiler from yelling at us about this being unused.
return IL_FALSE;
}
//if (ilTexImage(dw, dh, 1, 4, IL_RGBA, IL_UNSIGNED_BYTE, NULL) == IL_FALSE)
//if (ilTexImage(dw, dh, 1, 4, IL_RGBA, IL_UNSIGNED_SHORT, NULL) == IL_FALSE)
if (ilTexImage(dw, dh, 1, 4, IL_RGBA, IL_FLOAT, NULL) == IL_FALSE)
return IL_FALSE;
// Determine where the origin is in the original file.
if (in.lineOrder() == INCREASING_Y)
iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
else
iCurImage->Origin = IL_ORIGIN_LOWER_LEFT;
// Better to access FloatData instead of recasting everytime.
FloatData = (ILfloat*)iCurImage->Data;
for (int i = 0; i < dw * dh; i++)
{
// Too much data lost
//iCurImage->Data[i * 4 + 0] = (ILubyte)(pixels[i].r.bits() >> 8);
//iCurImage->Data[i * 4 + 1] = (ILubyte)(pixels[i].g.bits() >> 8);
//iCurImage->Data[i * 4 + 2] = (ILubyte)(pixels[i].b.bits() >> 8);
//iCurImage->Data[i * 4 + 3] = (ILubyte)(pixels[i].a.bits() >> 8);
// The images look kind of washed out with this.
//((ILshort*)(iCurImage->Data))[i * 4 + 0] = pixels[i].r.bits();
//((ILshort*)(iCurImage->Data))[i * 4 + 1] = pixels[i].g.bits();
//((ILshort*)(iCurImage->Data))[i * 4 + 2] = pixels[i].b.bits();
//((ILshort*)(iCurImage->Data))[i * 4 + 3] = pixels[i].a.bits();
// This gives the best results, since no data is lost.
FloatData[i * 4] = pixels[i].r;
FloatData[i * 4 + 1] = pixels[i].g;
FloatData[i * 4 + 2] = pixels[i].b;
FloatData[i * 4 + 3] = pixels[i].a;
}
// Converts the image to predefined type, format and/or origin if needed.
return ilFixImage();
}
// Nothing to do here in the constructor.
ilOStream::ilOStream() : Imf::OStream("N/A")
{
return;
}
void ilOStream::write(const char c[], int n)
{
iwrite(c, 1, n); //@TODO: Throw an exception here.
return;
}
//@TODO: Make this work with 64-bit values.
Imf::Int64 ilOStream::tellp()
{
Imf::Int64 Pos;
// itellw only returns a 32-bit value!
Pos = itellw();
return Pos;
}
// Note that there is no return value here, even though there probably should be.
//@TODO: Make this work with 64-bit values.
void ilOStream::seekp(Imf::Int64 Pos)
{
// iseekw only uses a 32-bit value!
iseekw((ILint)Pos, IL_SEEK_SET); // I am assuming this is seeking from the beginning.
return;
}
//! Writes a Exr file
ILboolean ilSaveExr(const ILstring FileName)
{
ILHANDLE ExrFile;
ILuint ExrSize;
if (ilGetBoolean(IL_FILE_MODE) == IL_FALSE) {
if (iFileExists(FileName)) {
ilSetError(IL_FILE_ALREADY_EXISTS);
return IL_FALSE;
}
}
ExrFile = iopenw(FileName);
if (ExrFile == NULL) {
ilSetError(IL_COULD_NOT_OPEN_FILE);
return IL_FALSE;
}
ExrSize = ilSaveExrF(ExrFile);
iclosew(ExrFile);
if (ExrSize == 0)
return IL_FALSE;
return IL_TRUE;
}
//! Writes a Exr to an already-opened file
ILuint ilSaveExrF(ILHANDLE File)
{
ILuint Pos;
iSetOutputFile(File);
Pos = itellw();
if (iSaveExrInternal() == IL_FALSE)
return 0; // Error occurred
return itellw() - Pos; // Return the number of bytes written.
}
//! Writes a Exr to a memory "lump"
ILuint ilSaveExrL(void *Lump, ILuint Size)
{
ILuint Pos = itellw();
iSetOutputLump(Lump, Size);
if (iSaveExrInternal() == IL_FALSE)
return 0; // Error occurred
return itellw() - Pos; // Return the number of bytes written.
}
ILboolean iSaveExrInternal()
{
Imath::Box2i DataWindow(Imath::V2i(0, 0), Imath::V2i(iCurImage->Width-1, iCurImage->Height-1));
Imf::LineOrder Order;
if (iCurImage->Origin == IL_ORIGIN_LOWER_LEFT)
Order = DECREASING_Y;
else
Order = INCREASING_Y;
Imf::Header Head(iCurImage->Width, iCurImage->Height, DataWindow, 1, Imath::V2f (0, 0), 1, Order);
ilOStream File;
Imf::RgbaOutputFile Out(File, Head);
ILimage *TempImage = iCurImage;
//@TODO: Can we always assume that Rgba is packed the same?
Rgba *HalfData = (Rgba*)ialloc(TempImage->Width * TempImage->Height * sizeof(Rgba));
if (HalfData == NULL)
return IL_FALSE;
if (iCurImage->Format != IL_RGBA || iCurImage->Type != IL_FLOAT) {
TempImage = iConvertImage(iCurImage, IL_RGBA, IL_FLOAT);
if (TempImage == NULL) {
ifree(HalfData);
return IL_FALSE;
}
}
ILuint Offset = 0;
ILfloat *FloatPtr = (ILfloat*)TempImage->Data;
for (unsigned int y = 0; y < TempImage->Height; y++) {
for (unsigned int x = 0; x < TempImage->Width; x++) {
HalfData[y * TempImage->Width + x].r = FloatPtr[Offset];
HalfData[y * TempImage->Width + x].g = FloatPtr[Offset + 1];
HalfData[y * TempImage->Width + x].b = FloatPtr[Offset + 2];
HalfData[y * TempImage->Width + x].a = FloatPtr[Offset + 3];
Offset += 4; // 4 floats
}
}
Out.setFrameBuffer(HalfData, 1, TempImage->Width);
Out.writePixels(TempImage->Height); //@TODO: Do each scanline separately to keep from using so much memory.
// Free our half data.
ifree(HalfData);
// Destroy our temporary image if we used one.
if (TempImage != iCurImage)
ilCloseImage(TempImage);
return IL_TRUE;
}
#endif //IL_NO_EXR