hornetseye-openexr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ hornetseye-openexr
2
+ ======
3
+ This Ruby extension provides reading and writing high dynamic range images using OpenEXR.
4
+
5
+
data/Rakefile ADDED
@@ -0,0 +1,195 @@
1
+ #!/usr/bin/env ruby
2
+ require 'date'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/packagetask'
6
+ require 'rake/loaders/makefile'
7
+ require 'rbconfig'
8
+
9
+ PKG_NAME = 'hornetseye-openexr'
10
+ PKG_VERSION = '0.1.0'
11
+ CXX = ENV[ 'CXX' ] || 'g++'
12
+ STRIP = ENV[ 'STRIP' ] || 'strip'
13
+ RB_FILES = FileList[ 'lib/**/*.rb' ]
14
+ CC_FILES = FileList[ 'ext/*.cc' ]
15
+ HH_FILES = FileList[ 'ext/*.hh' ] + FileList[ 'ext/*.tcc' ]
16
+ TC_FILES = FileList[ 'test/tc_*.rb' ]
17
+ TS_FILES = FileList[ 'test/ts_*.rb' ]
18
+ SO_FILE = "ext/#{PKG_NAME.tr '\-', '_'}.so"
19
+ PKG_FILES = [ 'Rakefile', 'README.md', 'COPYING', '.document' ] +
20
+ RB_FILES + CC_FILES + HH_FILES + TS_FILES + TC_FILES
21
+ BIN_FILES = [ 'README.md', 'COPYING', '.document', SO_FILE ] +
22
+ RB_FILES + TS_FILES + TC_FILES
23
+ SUMMARY = %q{Loading and saving images using OpenEXR}
24
+ DESCRIPTION = %q{This Ruby extension provides reading and writing high dynamic range images using OpenEXR.}
25
+ AUTHOR = %q{Jan Wedekind}
26
+ EMAIL = %q{jan@wedesoft.de}
27
+ HOMEPAGE = %q{http://wedesoft.github.com/hornetseye-openexr/}
28
+
29
+ OBJ = CC_FILES.ext 'o'
30
+ $CXXFLAGS = ENV[ 'CXXFLAGS' ] || ''
31
+ $CXXFLAGS = "#{$CXXFLAGS} -fPIC -DNDEBUG -DHAVE_CONFIG_H -pthread -I/usr/include/OpenEXR"
32
+ if RbConfig::CONFIG[ 'rubyhdrdir' ]
33
+ $CXXFLAGS = "#{$CXXFLAGS} -I#{RbConfig::CONFIG[ 'rubyhdrdir' ]} " +
34
+ "-I#{RbConfig::CONFIG[ 'rubyhdrdir' ]}/#{RbConfig::CONFIG[ 'arch' ]}"
35
+ else
36
+ $CXXFLAGS = "#{$CXXFLAGS} -I#{RbConfig::CONFIG[ 'archdir' ]}"
37
+ end
38
+ $LIBRUBYARG = RbConfig::CONFIG[ 'LIBRUBYARG' ]
39
+ $SITELIBDIR = RbConfig::CONFIG[ 'sitelibdir' ]
40
+ $SITEARCHDIR = RbConfig::CONFIG[ 'sitearchdir' ]
41
+
42
+ task :default => :all
43
+
44
+ desc 'Compile Ruby extension (default)'
45
+ task :all => [ SO_FILE ]
46
+
47
+ file SO_FILE => OBJ do |t|
48
+ sh "#{CXX} -shared -o #{t.name} #{OBJ} -pthread -lIlmImf -lz -lImath -lHalf -lIex -lIlmThread #{$LIBRUBYARG}"
49
+ sh "#{STRIP} --strip-all #{t.name}"
50
+ end
51
+
52
+ task :test => [ SO_FILE ]
53
+
54
+ desc 'Install Ruby extension'
55
+ task :install => :all do
56
+ verbose true do
57
+ for f in RB_FILES do
58
+ FileUtils.mkdir_p "#{$SITELIBDIR}/#{File.dirname f.gsub( /^lib\//, '' )}"
59
+ FileUtils.cp_r f, "#{$SITELIBDIR}/#{f.gsub( /^lib\//, '' )}"
60
+ end
61
+ FileUtils.mkdir_p $SITEARCHDIR
62
+ FileUtils.cp SO_FILE, "#{$SITEARCHDIR}/#{File.basename SO_FILE}"
63
+ end
64
+ end
65
+
66
+ desc 'Uninstall Ruby extension'
67
+ task :uninstall do
68
+ verbose true do
69
+ for f in RB_FILES do
70
+ FileUtils.rm_f "#{$SITELIBDIR}/#{f.gsub /^lib\//, ''}"
71
+ end
72
+ FileUtils.rm_f "#{$SITEARCHDIR}/#{File.basename SO_FILE}"
73
+ end
74
+ end
75
+
76
+ desc 'Create config.h'
77
+ task :config_h => 'ext/config.h'
78
+
79
+ file 'ext/config.h' do |t|
80
+ s = "/* config.h. Generated from Rakefile by rake. */\n"
81
+ rubyvm_version = RUBY_VERSION.split( '.' ).inject( 0 ) { |a,b| a * 100 + b.to_i }
82
+ s << "#define RUBYVM_VERSION #{rubyvm_version}\n"
83
+ File.open( t.name, 'w' ) { |f| f.puts s }
84
+ end
85
+
86
+ Rake::TestTask.new do |t|
87
+ t.libs << 'ext'
88
+ t.test_files = TC_FILES
89
+ end
90
+
91
+ begin
92
+ require 'yard'
93
+ YARD::Rake::YardocTask.new :yard do |y|
94
+ y.options << '--no-private'
95
+ y.files << FileList[ 'lib/**/*.rb' ]
96
+ end
97
+ rescue LoadError
98
+ STDERR.puts 'Please install \'yard\' if you want to generate documentation'
99
+ end
100
+
101
+ Rake::PackageTask.new PKG_NAME, PKG_VERSION do |p|
102
+ p.need_tar = true
103
+ p.package_files = PKG_FILES
104
+ end
105
+
106
+ begin
107
+ require 'rubygems'
108
+ require 'rubygems/builder'
109
+ $SPEC = Gem::Specification.new do |s|
110
+ s.name = PKG_NAME
111
+ s.version = PKG_VERSION
112
+ s.platform = Gem::Platform::RUBY
113
+ s.date = Date.today.to_s
114
+ s.summary = SUMMARY
115
+ s.description = DESCRIPTION
116
+ s.author = AUTHOR
117
+ s.email = EMAIL
118
+ s.homepage = HOMEPAGE
119
+ s.files = PKG_FILES
120
+ s.test_files = TC_FILES
121
+ s.require_paths = [ 'lib', 'ext' ]
122
+ s.rubyforge_project = %q{hornetseye}
123
+ s.extensions = %w{Rakefile}
124
+ s.has_rdoc = 'yard'
125
+ s.extra_rdoc_files = []
126
+ s.rdoc_options = %w{--no-private}
127
+ s.add_dependency %<malloc>, [ '~> 1.1' ]
128
+ s.add_dependency %<multiarray>, [ '~> 0.10' ]
129
+ s.add_dependency %<hornetseye-frame>, [ '~> 0.6' ]
130
+ s.add_development_dependency %q{rake}
131
+ end
132
+ GEM_SOURCE = "#{PKG_NAME}-#{PKG_VERSION}.gem"
133
+ $BINSPEC = Gem::Specification.new do |s|
134
+ s.name = PKG_NAME
135
+ s.version = PKG_VERSION
136
+ s.platform = Gem::Platform::CURRENT
137
+ s.date = Date.today.to_s
138
+ s.summary = SUMMARY
139
+ s.description = DESCRIPTION
140
+ s.author = AUTHOR
141
+ s.email = EMAIL
142
+ s.homepage = HOMEPAGE
143
+ s.files = BIN_FILES
144
+ s.test_files = TC_FILES
145
+ s.require_paths = [ 'lib', 'ext' ]
146
+ s.rubyforge_project = %q{hornetseye}
147
+ s.has_rdoc = 'yard'
148
+ s.extra_rdoc_files = []
149
+ s.rdoc_options = %w{--no-private}
150
+ s.add_dependency %<malloc>, [ '~> 1.1' ]
151
+ s.add_dependency %<multiarray>, [ '~> 0.10' ]
152
+ s.add_dependency %<hornetseye-frame>, [ '~> 0.6' ]
153
+ end
154
+ GEM_BINARY = "#{PKG_NAME}-#{PKG_VERSION}-#{$BINSPEC.platform}.gem"
155
+ desc "Build the gem file #{GEM_SOURCE}"
156
+ task :gem => [ "pkg/#{GEM_SOURCE}" ]
157
+ file "pkg/#{GEM_SOURCE}" => [ 'pkg' ] + $SPEC.files do
158
+ when_writing 'Creating GEM' do
159
+ Gem::Builder.new( $SPEC ).build
160
+ verbose true do
161
+ FileUtils.mv GEM_SOURCE, "pkg/#{GEM_SOURCE}"
162
+ end
163
+ end
164
+ end
165
+ desc "Build the gem file #{GEM_BINARY}"
166
+ task :gem_binary => [ "pkg/#{GEM_BINARY}" ]
167
+ file "pkg/#{GEM_BINARY}" => [ 'pkg' ] + $BINSPEC.files do
168
+ when_writing 'Creating binary GEM' do
169
+ Gem::Builder.new( $BINSPEC ).build
170
+ verbose true do
171
+ FileUtils.mv GEM_BINARY, "pkg/#{GEM_BINARY}"
172
+ end
173
+ end
174
+ end
175
+ rescue LoadError
176
+ STDERR.puts 'Please install \'rubygems\' if you want to create Gem packages'
177
+ end
178
+
179
+ rule '.o' => '.cc' do |t|
180
+ sh "#{CXX} #{$CXXFLAGS} -c -o #{t.name} #{t.source}"
181
+ end
182
+
183
+ file ".depends.mf" => :config_h do |t|
184
+ sh "g++ -MM #{$CXXFLAGS} #{CC_FILES.join ' '} | " +
185
+ "sed -e :a -e N -e 's/\\n/\\$/g' -e ta | " +
186
+ "sed -e 's/ *\\\\\\$ */ /g' -e 's/\\$/\\n/g' | sed -e 's/^/ext\\//' > #{t.name}"
187
+ end
188
+ CC_FILES.each do |t|
189
+ file t.ext(".o") => t
190
+ end
191
+ import ".depends.mf"
192
+
193
+ CLEAN.include 'ext/*.o'
194
+ CLOBBER.include SO_FILE, 'doc', '.yardoc', '.depends.mf', 'ext/config.h'
195
+
data/ext/error.hh ADDED
@@ -0,0 +1,50 @@
1
+ /* HornetsEye - Computer Vision with Ruby
2
+ Copyright (C) 2006, 2007, 2008, 2009, 2010 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #ifndef ERROR_HH
17
+ #define ERROR_HH
18
+
19
+ #include <exception>
20
+ #include <sstream>
21
+
22
+ class Error: public std::exception
23
+ {
24
+ public:
25
+ Error(void) {}
26
+ Error( Error &e ): std::exception( e )
27
+ { m_message << e.m_message.str(); }
28
+ virtual ~Error(void) throw() {}
29
+ template< typename T >
30
+ std::ostream &operator<<( const T &t )
31
+ { m_message << t; return m_message; }
32
+ std::ostream &operator<<( std::ostream& (*__pf)(std::ostream&) )
33
+ { (*__pf)( m_message ); return m_message; }
34
+ virtual const char* what(void) const throw() {
35
+ temp = m_message.str();
36
+ return temp.c_str();
37
+ }
38
+ protected:
39
+ std::ostringstream m_message;
40
+ mutable std::string temp;
41
+ };
42
+
43
+ #define ERRORMACRO( condition, class, params, message ) \
44
+ if ( !( condition ) ) { \
45
+ class _e params; \
46
+ _e << message; \
47
+ throw _e; \
48
+ };
49
+
50
+ #endif
data/ext/frame.cc ADDED
@@ -0,0 +1,81 @@
1
+ /* HornetsEye - Computer Vision with Ruby
2
+ Copyright (C) 2006, 2007, 2008, 2009, 2010 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #include "frame.hh"
17
+
18
+ using namespace std;
19
+
20
+ Frame::Frame( const string &typecode, int width, int height, char *data ):
21
+ m_frame( Qnil )
22
+ {
23
+ VALUE mModule = rb_define_module( "Hornetseye" );
24
+ VALUE cMalloc = rb_define_class_under( mModule, "Malloc", rb_cObject );
25
+ VALUE cFrame = rb_define_class_under( mModule, "Frame", rb_cObject );
26
+ VALUE rbSize = INT2NUM( storageSize( typecode, width, height ) );
27
+ VALUE rbMemory;
28
+ if ( data != NULL ) {
29
+ rbMemory = Data_Wrap_Struct( cMalloc, 0, 0, (void *)data );
30
+ rb_ivar_set( rbMemory, rb_intern( "@size" ), rbSize );
31
+ } else
32
+ rbMemory = rb_funcall( cMalloc, rb_intern( "new" ), 1, rbSize );
33
+ m_frame = rb_funcall( cFrame, rb_intern( "import" ), 4,
34
+ rb_const_get( mModule, rb_intern( typecode.c_str() ) ),
35
+ INT2NUM( width ), INT2NUM( height ), rbMemory );
36
+ }
37
+
38
+ string Frame::typecode(void)
39
+ {
40
+ VALUE rbString = rb_funcall( rb_funcall( m_frame, rb_intern( "typecode" ), 0 ),
41
+ rb_intern( "to_s" ), 0 );
42
+ return StringValuePtr( rbString );
43
+ }
44
+
45
+ int Frame::width(void)
46
+ {
47
+ return NUM2INT( rb_funcall( m_frame, rb_intern( "width" ), 0 ) );
48
+ }
49
+
50
+ int Frame::height(void)
51
+ {
52
+ return NUM2INT( rb_funcall( m_frame, rb_intern( "height" ), 0 ) );
53
+ }
54
+
55
+ char *Frame::data(void)
56
+ {
57
+ VALUE rbMemory = rb_funcall( m_frame, rb_intern( "memory" ), 0 );
58
+ char *ptr;
59
+ Data_Get_Struct( rbMemory, char, ptr );
60
+ return ptr;
61
+ }
62
+
63
+ bool Frame::rgb(void)
64
+ {
65
+ return rb_funcall( m_frame, rb_intern( "rgb?" ), 0 ) != Qfalse;
66
+ }
67
+
68
+ void Frame::markRubyMember(void)
69
+ {
70
+ rb_gc_mark( m_frame );
71
+ }
72
+
73
+ int Frame::storageSize( const std::string &typecode, int width, int height )
74
+ {
75
+ VALUE mModule = rb_define_module( "Hornetseye" );
76
+ VALUE cFrame = rb_define_class_under( mModule, "Frame", rb_cObject );
77
+ return NUM2INT( rb_funcall( cFrame, rb_intern( "storage_size" ), 3,
78
+ rb_const_get( mModule, rb_intern( typecode.c_str() ) ),
79
+ INT2NUM( width ), INT2NUM( height ) ) );
80
+ }
81
+
data/ext/frame.hh ADDED
@@ -0,0 +1,43 @@
1
+ /* HornetsEye - Computer Vision with Ruby
2
+ Copyright (C) 2006, 2007, 2008, 2009, 2010 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #ifndef FRAME_HH
17
+ #define FRAME_HH
18
+
19
+ #include <boost/smart_ptr.hpp>
20
+ #include "rubyinc.hh"
21
+ #include <string>
22
+
23
+ class Frame
24
+ {
25
+ public:
26
+ Frame( const std::string &typecode, int width, int height, char *data = NULL );
27
+ Frame( VALUE rbFrame ): m_frame( rbFrame ) {}
28
+ virtual ~Frame(void) {}
29
+ std::string typecode(void);
30
+ int width(void);
31
+ int height(void);
32
+ char *data(void);
33
+ bool rgb(void);
34
+ VALUE rubyObject(void) { return m_frame; }
35
+ void markRubyMember(void);
36
+ static int storageSize( const std::string &typecode, int width, int height );
37
+ protected:
38
+ VALUE m_frame;
39
+ };
40
+
41
+ typedef boost::shared_ptr< Frame > FramePtr;
42
+
43
+ #endif
data/ext/init.cc ADDED
@@ -0,0 +1,42 @@
1
+ /* HornetsEye - Computer Vision with Ruby
2
+ Copyright (C) 2006, 2007, 2008, 2009, 2010 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #include "rubyinc.hh"
17
+ #include "openexrinput.hh"
18
+ #include "openexroutput.hh"
19
+
20
+ #ifdef WIN32
21
+ #define DLLEXPORT __declspec(dllexport)
22
+ #define DLLLOCAL
23
+ #else
24
+ #define DLLEXPORT __attribute__ ((visibility("default")))
25
+ #define DLLLOCAL __attribute__ ((visibility("hidden")))
26
+ #endif
27
+
28
+ extern "C" DLLEXPORT void Init_hornetseye_opencv(void);
29
+
30
+ extern "C" {
31
+
32
+ void Init_hornetseye_openexr(void)
33
+ {
34
+ rb_require( "multiarray" );
35
+ rb_require( "hornetseye_frame" );
36
+ VALUE mHornetseye = rb_define_module( "Hornetseye" );
37
+ OpenEXRInput::registerRubyClass( mHornetseye );
38
+ OpenEXROutput::registerRubyClass( mHornetseye );
39
+ rb_require( "hornetseye_openexr_ext.rb" );
40
+ }
41
+
42
+ }
@@ -0,0 +1,245 @@
1
+ /* HornetsEye - Computer Vision with Ruby
2
+ Copyright (C) 2006, 2007 Jan Wedekind
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+ #ifdef HAVE_CONFIG_H
17
+ #include "config.h"
18
+ #endif
19
+ #include <OpenEXR/IexThrowErrnoExc.h>
20
+ #include <OpenEXR/ImfChannelList.h>
21
+ #include <OpenEXR/ImfInputFile.h>
22
+ #include <OpenEXR/ImfRgbaFile.h>
23
+ #include <OpenEXR/ImfIO.h>
24
+ #include <cerrno>
25
+ #include "rubytools.hh"
26
+ #if (RUBYVM_VERSION >= 10901)
27
+ #include <ruby/io.h>
28
+ #else
29
+ #include <rubyio.h>
30
+ #endif
31
+ #include <unistd.h>
32
+ #include "openexrinput.hh"
33
+
34
+ using namespace boost;
35
+ using namespace Imf;
36
+ using namespace Imath;
37
+ using namespace std;
38
+
39
+ VALUE OpenEXRInput::cRubyClass = Qnil;
40
+
41
+ class ImfIStreamWrapper: public Imf::IStream
42
+ {
43
+ public:
44
+ ImfIStreamWrapper( int fd, const char fileName[] ):
45
+ IStream( fileName ), m_fd(fd) {}
46
+ virtual bool read( char c[], int n );
47
+ virtual Int64 tellg(void);
48
+ virtual void seekg( Int64 pos );
49
+ virtual void clear(void);
50
+ protected:
51
+ int m_fd;
52
+ };
53
+
54
+ bool ImfIStreamWrapper::read( char c[], int n )
55
+ {
56
+ if ( n != ::read( m_fd, c, n ) ) {
57
+ if ( errno )
58
+ Iex::throwErrnoExc();
59
+ else
60
+ throw Iex::InputExc( "Unexpected end of file." );
61
+ }
62
+ return true;
63
+ }
64
+
65
+ Int64 ImfIStreamWrapper::tellg(void)
66
+ {
67
+ return lseek( m_fd, 0, SEEK_CUR );
68
+ }
69
+
70
+ void ImfIStreamWrapper::seekg( Int64 pos )
71
+ {
72
+ lseek( m_fd, pos, SEEK_SET );
73
+ }
74
+
75
+ void ImfIStreamWrapper::clear(void)
76
+ {
77
+ errno = 0;
78
+ }
79
+
80
+ OpenEXRInput::OpenEXRInput( int fd ):
81
+ m_fd(fd), m_status(true)
82
+ {
83
+ assert( m_fd != 0 );
84
+ }
85
+
86
+ FramePtr OpenEXRInput::read(void) throw (Error)
87
+ {
88
+ FramePtr frame;
89
+ try {
90
+ ERRORMACRO( m_status, Error, , "OpenEXR-image was read already." );
91
+ ImfIStreamWrapper fileWrapper( m_fd, "OpenEXR input file" );
92
+ Int64 pos = fileWrapper.tellg();
93
+ InputFile imfFile( fileWrapper );
94
+ Box2i box( imfFile.header().dataWindow() );
95
+ const ChannelList &channels = imfFile.header().channels();
96
+ string typecode;
97
+ if ( channels.findChannel( "Y" ) ) {
98
+ if ( channels.findChannel( "RY" ) && channels.findChannel( "BY" ) )
99
+ typecode = "YCA";
100
+ else
101
+ typecode = "SFLOAT";
102
+ } else
103
+ if ( channels.findChannel( "R" ) && channels.findChannel( "G" ) &&
104
+ channels.findChannel( "B" ) )
105
+ typecode = "SFLOATRGB";
106
+ else {
107
+ ERRORMACRO( false, Error, ,
108
+ "Unknown combination of channels in EXR file" );
109
+ };
110
+ int
111
+ width = box.max.x - box.min.x + 1,
112
+ height = box.max.y - box.min.y + 1;
113
+ #ifndef NDEBUG
114
+ cerr << "OpenEXRInput of " << width << 'x' << height << "-image" << endl;
115
+ #endif
116
+ FrameBuffer frameBuffer;
117
+ if ( typecode == "SFLOAT" ) {
118
+ frame = FramePtr( new Frame( "SFLOAT", width, height ) );
119
+ float *offset = (float *)frame->data() - box.min.x - box.min.y * width;
120
+ frameBuffer.insert( "Y",
121
+ Slice( Imf::FLOAT,
122
+ (char *)offset,
123
+ sizeof(float),
124
+ sizeof(float) * width, 1, 1, 0.0 ) );
125
+ imfFile.setFrameBuffer( frameBuffer );
126
+ imfFile.readPixels( box.min.y, box.max.y );
127
+ } else if ( typecode == "SFLOATRGB" ) {
128
+ frame = FramePtr( new Frame( "SFLOATRGB", width, height ) );
129
+ float *offset = (float *)frame->data() -
130
+ 3 * ( box.min.x + box.min.y * width );
131
+ frameBuffer.insert( "R",
132
+ Slice( Imf::FLOAT,
133
+ (char *)offset,
134
+ 3 * sizeof(float),
135
+ 3 * width * sizeof(float), 1, 1, 0.0 ) );
136
+ frameBuffer.insert( "G",
137
+ Slice( Imf::FLOAT,
138
+ (char *)( offset + 1 ),
139
+ 3 * sizeof(float),
140
+ 3 * width * sizeof(float), 1, 1, 0.0 ) );
141
+ frameBuffer.insert( "B",
142
+ Slice( Imf::FLOAT,
143
+ (char *)( offset + 2 ),
144
+ 3 * sizeof(float),
145
+ 3 * width * sizeof(float), 1, 1, 0.0 ) );
146
+ imfFile.setFrameBuffer( frameBuffer );
147
+ imfFile.readPixels( box.min.y, box.max.y );
148
+ } else {
149
+ assert( typecode == "YCA" );
150
+ // This colourspace is hard to convert (see ImfRgbaFile.cpp) and
151
+ // requires correlation with a filter. OpenEXR is used for doing this.
152
+ fileWrapper.seekg( pos );
153
+ RgbaInputFile imgRgbaFile( fileWrapper );
154
+ int size = width * height;
155
+ shared_array< Rgba > buffer( new Rgba[ size ] );
156
+ imgRgbaFile.setFrameBuffer( buffer.get() -
157
+ box.min.x - box.min.y * width, 1, width );
158
+ imgRgbaFile.readPixels( box.min.y, box.max.y );
159
+ frame = FramePtr( new Frame( "SFLOATRGB", width, height ) );
160
+ const Rgba *p = buffer.get();
161
+ float *q = (float *)frame->data();
162
+ for ( int i=0; i<size; i++ ) {
163
+ q[0] = p->r;
164
+ q[1] = p->g;
165
+ q[2] = p->b;
166
+ p++;
167
+ q += 3;
168
+ };
169
+ };
170
+ m_status = false;
171
+ } catch ( Iex::BaseExc &e ) {
172
+ ERRORMACRO( false, Error, ,e.what() );
173
+ };
174
+ return frame;
175
+ }
176
+
177
+ string OpenEXRInput::inspect(void) const
178
+ {
179
+ return string( "OpenEXRInput-object" );
180
+ }
181
+
182
+ VALUE OpenEXRInput::registerRubyClass( VALUE module )
183
+ {
184
+ cRubyClass = rb_define_class_under( module, "OpenEXRInput", rb_cObject );
185
+ rb_define_singleton_method( cRubyClass, "new",
186
+ RUBY_METHOD_FUNC( wrapNew ), 1 );
187
+ rb_define_method( cRubyClass, "inspect",
188
+ RUBY_METHOD_FUNC( wrapInspect ), 0 );
189
+ rb_define_method( cRubyClass, "read", RUBY_METHOD_FUNC( wrapRead ), 0 );
190
+ rb_define_method( cRubyClass, "status?", RUBY_METHOD_FUNC( wrapStatus ), 0 );
191
+ return cRubyClass;
192
+ }
193
+
194
+ void OpenEXRInput::deleteRubyObject( void *ptr )
195
+ {
196
+ delete (OpenEXRInputPtr *)ptr;
197
+ }
198
+
199
+ VALUE OpenEXRInput::wrapNew( VALUE rbClass, VALUE rbFile )
200
+ {
201
+ VALUE retVal = Qnil;
202
+ try {
203
+ rb_check_type( rbFile, T_FILE );
204
+ #if (RUBYVM_VERSION >= 10901)
205
+ int fd = RFILE( rbFile )->fptr->fd;
206
+ #else
207
+ FILE *f = RFILE( rbFile )->fptr->f;
208
+ int fd = fileno( f );
209
+ #endif
210
+ ERRORMACRO( fd != 0, Error, , "File descriptor is zero" );
211
+ OpenEXRInputPtr ptr( new OpenEXRInput( fd ) );
212
+ retVal = Data_Wrap_Struct( rbClass, 0, deleteRubyObject,
213
+ new OpenEXRInputPtr( ptr ) );
214
+ } catch ( std::exception &e ) {
215
+ rb_raise( rb_eRuntimeError, "%s", e.what() );
216
+ };
217
+ return retVal;
218
+ }
219
+
220
+ VALUE OpenEXRInput::wrapInspect( VALUE rbSelf )
221
+ {
222
+ OpenEXRInputPtr *self; Data_Get_Struct( rbSelf, OpenEXRInputPtr, self );
223
+ string retVal( (*self)->inspect() );
224
+ return rb_str_new( retVal.c_str(), retVal.length() );
225
+ }
226
+
227
+ VALUE OpenEXRInput::wrapRead( VALUE rbSelf )
228
+ {
229
+ VALUE retVal = Qnil;
230
+ try {
231
+ OpenEXRInputPtr *self; Data_Get_Struct( rbSelf, OpenEXRInputPtr, self );
232
+ FramePtr frame( (*self)->read() );
233
+ retVal = frame->rubyObject();
234
+ } catch ( std::exception &e ) {
235
+ rb_raise( rb_eRuntimeError, "%s", e.what() );
236
+ };
237
+ return retVal;
238
+ }
239
+
240
+ VALUE OpenEXRInput::wrapStatus( VALUE rbSelf )
241
+ {
242
+ OpenEXRInputPtr *self; Data_Get_Struct( rbSelf, OpenEXRInputPtr, self );
243
+ return (*self)->status() ? Qtrue : Qfalse;
244
+ }
245
+