File: openexr.cc

package info (click to toggle)
exactimage 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,048 kB
  • sloc: cpp: 35,940; ansic: 1,952; xml: 1,447; makefile: 338; perl: 138; sh: 110; python: 45; php: 37; ruby: 12
file content (226 lines) | stat: -rw-r--r-- 4,838 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2006 - 2024 René Rebe
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * Alternatively, commercial licensing options are available from the
 * copyright holder ExactCODE GmbH Germany.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <ImfIO.h>

#include <ImfInputFile.h>
#include <ImfOutputFile.h>

#include <ImfRgbaFile.h>
#include <ImfArray.h>
#include <IexThrowErrnoExc.h>

#include <algorithm>

#include "openexr.hh"

#include "Colorspace.hh"

using namespace Imf;
using namespace Imath;

using std::cout;
using std::endl;

class STDIStream : public IStream
{
public:
  STDIStream (std::istream* stream, const char fileName[])
    : IStream (fileName), _stream (stream) {
  }
  
  virtual bool read (char c[], int n)
  {
    try {
    _stream->read (c, n);
    }
    catch (...)
      {
	if (!*_stream)
	  Iex::throwErrnoExc();
	else
	  throw Iex::InputExc ("Unexpected end of file.");
      }
    return _stream->eof();
  }
    
  virtual uint64_t tellg ()
  {
    return _stream->tellg ();
  }
  
  virtual void seekg (uint64_t pos)
  {
    _stream->clear ();
    _stream->seekg (pos);
  }
  
  virtual void clear ()
  {
    _stream->clear ();
  }
  
private:
  std::istream* _stream;
};

class STDOStream : public OStream
{
public:
  STDOStream (std::ostream* stream, const char fileName[])
    : OStream (fileName),  _stream (stream) {
  }
  
  virtual void write (const char c[], int n)
  {
    try {
      _stream->write (c, n);
    }
    catch (...)
      {
	if (!*_stream)
	  Iex::throwErrnoExc();
	else
	  throw Iex::InputExc ("Unexpected end of file.");
      }
  }
    
  virtual uint64_t tellp ()
  {
    return _stream->tellp ();
  }
  
  virtual void seekp (uint64_t pos)
  {
    _stream->clear ();
    _stream->seekp (pos);
  }
  
  virtual void clear ()
  {
    _stream->clear ();
  }
  
private:
  std::ostream* _stream;
};

int OpenEXRCodec::readImage (std::istream* stream, Image& image, const std::string& decompress)
{
  STDIStream istream (stream, "");
  
  {
    // quick magic check
    char buf [3];
    stream->read (buf, sizeof (buf));
    stream->seekg (0);
    
    // inaccurate, but enough for now
    if (buf[0] != 'v' || buf[1] != '/' || buf[2] != '1')
      return false;
  }
  
try {
  RgbaInputFile exrfile (istream);
  Box2i dw = exrfile.dataWindow ();
  
  image.spp = 4;
  image.bps = 16;
  
  image.resize (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1);
  
  Array2D<Rgba> pixels (1, image.w); // working data
  
  uint16_t* it = (uint16_t*) image.getRawData();
  for (int y = 0; y < image.h; ++y)
    {
      exrfile.setFrameBuffer (&pixels[0][0] - y * image.w, 1, image.w);
      exrfile.readPixels (y, y);
      
      for (int x = 0; x < image.w; ++x) {
	double r = pixels[0][x].r;
	double g = pixels[0][x].g;
	double b = pixels[0][x].b;
	double a = pixels[0][x].a;
	
	r = std::min (std::max (r,0.0),1.0) * 0xFFFF;
	g = std::min (std::max (g,0.0),1.0) * 0xFFFF;
	b = std::min (std::max (b,0.0),1.0) * 0xFFFF;
	a = std::min (std::max (a,0.0),1.0) * 0xFFFF;
	
	*it++ = (int) r; *it++ = (int)g; *it++ = (int)b; *it++ = (int)a;
      }
    }
  
  return true;
 } catch (...) {
  return false;
 }
}

bool OpenEXRCodec::writeImage (std::ostream* stream, Image& image,
			       int quality, const std::string& compress)
{
  RgbaChannels type;
  switch (image.spp) {
  case 1:
    type = WRITE_Y; break;
  case 2:
    type = WRITE_YA; break;
  case 3:
    type = WRITE_RGB; break;
  case 4:
    type = WRITE_RGBA; break;
    break;
  default:
    std::cerr << "Unsupported image format." << std::endl;
    return false;
  }
      
  
  Box2i displayWindow (V2i (0, 0), V2i (image.w - 1, image.h - 1));
  
  STDOStream ostream (stream, "");
    
  Header header (image.w, image.h);
  RgbaOutputFile exrfile (ostream, header, type);
  
  Array2D<Rgba> pixels (1, image.w); // working data
  
  uint16_t* it = (uint16_t*) image.getRawData();
  for (int y = 0; y < image.h; ++y)
    {
      exrfile.setFrameBuffer (&pixels[0][0] - y * image.w, 1, image.w);
      
      for (int x = 0; x < image.w; ++x) {
	pixels[0][x].r = (double)*it++ / 0xFFFF;
	pixels[0][x].g = (double)*it++ / 0xFFFF;
	pixels[0][x].b = (double)*it++ / 0xFFFF;
	pixels[0][x].a = (double)*it++ / 0xFFFF;
      }
      
      exrfile.writePixels (1);
    }
  
  return true;
}

OpenEXRCodec openexr_loader;