hornetseye-frame 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'rake/packagetask'
6
6
  require 'rbconfig'
7
7
 
8
8
  PKG_NAME = 'hornetseye-frame'
9
- PKG_VERSION = '0.1.0'
9
+ PKG_VERSION = '0.2.0'
10
10
  CXX = ENV[ 'CXX' ] || 'g++'
11
11
  STRIP = ENV[ 'STRIP' ] || 'strip'
12
12
  RB_FILES = FileList[ 'lib/**/*.rb' ]
@@ -113,7 +113,7 @@ begin
113
113
  s.extra_rdoc_files = []
114
114
  s.rdoc_options = %w{--no-private}
115
115
  s.add_dependency %<malloc>, [ '~> 1.1' ]
116
- s.add_dependency %<multiarray>, [ '~> 0.5' ]
116
+ s.add_dependency %<multiarray>, [ '~> 0.6' ]
117
117
  s.add_development_dependency %q{rake}
118
118
  end
119
119
  GEM_SOURCE = "#{PKG_NAME}-#{PKG_VERSION}.gem"
@@ -135,7 +135,7 @@ begin
135
135
  s.extra_rdoc_files = []
136
136
  s.rdoc_options = %w{--no-private}
137
137
  s.add_dependency %q<malloc>, [ '~> 1.1' ]
138
- s.add_dependency %<multiarray>, [ '~> 0.5' ]
138
+ s.add_dependency %<multiarray>, [ '~> 0.6' ]
139
139
  end
140
140
  GEM_BINARY = "#{PKG_NAME}-#{PKG_VERSION}-#{$BINSPEC.platform}.gem"
141
141
  desc "Build the gem file #{GEM_SOURCE}"
@@ -166,8 +166,11 @@ rule '.o' => '.cc' do |t|
166
166
  sh "#{CXX} #{$CXXFLAGS} -c -o #{t.name} #{t.source}"
167
167
  end
168
168
 
169
- # file 'ext/error.o' => [ 'ext/error.cc', 'ext/error.hh' ]
170
- # file 'ext/malloc.o' => [ 'ext/malloc.cc', 'ext/malloc.hh', 'ext/error.hh' ]
169
+ file 'ext/frame.o' => [ 'ext/frame.cc', 'ext/frame.hh' ]
170
+ file 'ext/colourspace.o' => [ 'ext/colourspace.cc', 'ext/colourspace.hh',
171
+ 'ext/frame.hh', 'ext/error.hh' ]
172
+ file 'ext/init.o' => [ 'ext/init.cc', 'ext/frame.hh', 'ext/colourspace.hh',
173
+ 'ext/error.hh' ]
171
174
 
172
175
  CLEAN.include 'ext/*.o'
173
176
  CLOBBER.include SO_FILE, 'doc', '.yardoc'
@@ -0,0 +1,77 @@
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
+ extern "C" {
17
+ #include <libswscale/swscale.h>
18
+ }
19
+ #undef RSHIFT
20
+ #include "colourspace.hh"
21
+
22
+ using namespace std;
23
+
24
+ static enum PixelFormat stringToPixelFormat( const string &str ) throw (Error)
25
+ {
26
+ enum PixelFormat retVal = PIX_FMT_NONE;
27
+ if ( str == "YV12" )
28
+ retVal = PIX_FMT_YUV420P;
29
+ else if ( str == "UBYTERGB" )
30
+ retVal = PIX_FMT_RGB24;
31
+ else {
32
+ ERRORMACRO( false, Error, , "Unsupported colourspace \"" << str << "\"" );
33
+ };
34
+ return retVal;
35
+ }
36
+
37
+ FramePtr frameToType( const FramePtr in, const string &target ) throw (Error)
38
+ {
39
+ int
40
+ width = in->width(),
41
+ height = in->height();
42
+ FramePtr retVal( new Frame( target, width, height ) );
43
+ uint8_t *sourceData[3];
44
+ sourceData[0] = (uint8_t *)in->data();
45
+ sourceData[1] = (uint8_t *)in->data() + width * height * 5 / 4;
46
+ sourceData[2] = (uint8_t *)in->data() + width * height;
47
+ int sourceLineSize[3];
48
+ sourceLineSize[0] = width;
49
+ sourceLineSize[1] = width / 2;
50
+ sourceLineSize[2] = width / 2;
51
+ uint8_t *destData[1];
52
+ destData[0] = (uint8_t *)retVal->data();
53
+ int destLineSize[1];
54
+ destLineSize[0] = retVal->width() * 3;
55
+ SwsContext *swsContext = sws_getContext( width, height,
56
+ stringToPixelFormat( in->typecode() ),
57
+ width, height,
58
+ stringToPixelFormat( target ),
59
+ SWS_FAST_BILINEAR, 0, 0, 0 );
60
+ sws_scale( swsContext, sourceData, sourceLineSize, 0,
61
+ height, destData, destLineSize );
62
+ sws_freeContext( swsContext );
63
+ return retVal;
64
+ }
65
+
66
+ VALUE frameWrapToType( VALUE rbSelf, VALUE rbTarget )
67
+ {
68
+ VALUE rbRetVal = Qnil;
69
+ try {
70
+ FramePtr frame( new Frame( rbSelf ) );
71
+ VALUE rbString = rb_funcall( rbTarget, rb_intern( "to_s" ), 0 );
72
+ rbRetVal = frameToType( frame, StringValuePtr( rbString ) )->rubyObject();
73
+ } catch ( exception &e ) {
74
+ rb_raise( rb_eRuntimeError, "%s", e.what() );
75
+ };
76
+ return rbRetVal;
77
+ }
@@ -0,0 +1,29 @@
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 COLOURSPACE_HH
17
+ #define COLOURSPACE_HH
18
+
19
+ #include <ruby.h>
20
+ #include <string>
21
+ #include "error.hh"
22
+ #include "frame.hh"
23
+
24
+ FramePtr frameToType( const FramePtr in, const std::string &target ) throw (Error);
25
+
26
+ VALUE frameWrapToType( VALUE rbClass, VALUE rbTarget );
27
+
28
+ #endif
29
+
@@ -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
@@ -0,0 +1,64 @@
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 = rb_funcall( cFrame, rb_intern( "storage_size" ), 3,
27
+ rb_const_get( mModule, rb_intern( typecode.c_str() ) ),
28
+ INT2NUM( width ), INT2NUM( height ) );
29
+ VALUE rbMemory;
30
+ if ( data != NULL ) {
31
+ rbMemory = Data_Wrap_Struct( cMalloc, 0, 0, (void *)data );
32
+ rb_ivar_set( rbMemory, rb_intern( "@size" ), rbSize );
33
+ } else
34
+ rbMemory = rb_funcall( cMalloc, rb_intern( "new" ), 1, rbSize );
35
+ m_frame = rb_funcall( cFrame, rb_intern( "import" ), 4,
36
+ rb_const_get( mModule, rb_intern( typecode.c_str() ) ),
37
+ INT2NUM( width ), INT2NUM( height ), rbMemory );
38
+ }
39
+
40
+ string Frame::typecode(void)
41
+ {
42
+ VALUE rbString = rb_funcall( rb_funcall( m_frame, rb_intern( "typecode" ), 0 ),
43
+ rb_intern( "to_s" ), 0 );
44
+ return StringValuePtr( rbString );
45
+ }
46
+
47
+ int Frame::width(void)
48
+ {
49
+ return NUM2INT( rb_funcall( m_frame, rb_intern( "width" ), 0 ) );
50
+ }
51
+
52
+ int Frame::height(void)
53
+ {
54
+ return NUM2INT( rb_funcall( m_frame, rb_intern( "height" ), 0 ) );
55
+ }
56
+
57
+ char *Frame::data(void)
58
+ {
59
+ VALUE rbMemory = rb_funcall( m_frame, rb_intern( "memory" ), 0 );
60
+ char *ptr;
61
+ Data_Get_Struct( rbMemory, char, ptr );
62
+ return ptr;
63
+ }
64
+
@@ -0,0 +1,40 @@
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 <ruby.h>
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
+ VALUE rubyObject(void) { return m_frame; }
34
+ protected:
35
+ VALUE m_frame;
36
+ };
37
+
38
+ typedef boost::shared_ptr< Frame > FramePtr;
39
+
40
+ #endif
@@ -14,6 +14,7 @@
14
14
  You should have received a copy of the GNU General Public License
15
15
  along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
16
  #include <ruby.h>
17
+ #include "colourspace.hh"
17
18
 
18
19
  #ifdef WIN32
19
20
  #define DLLEXPORT __declspec(dllexport)
@@ -30,6 +31,8 @@ extern "C" {
30
31
  void Init_hornetseye_frame(void)
31
32
  {
32
33
  VALUE rbHornetseye = rb_define_module( "Hornetseye" );
34
+ VALUE cFrame = rb_define_class_under( rbHornetseye, "Frame_", rb_cObject );
35
+ rb_define_method( cFrame, "to_type", RUBY_METHOD_FUNC( frameWrapToType ), 1 );
33
36
  rb_require( "hornetseye_frame_ext.rb" );
34
37
  }
35
38
 
@@ -50,7 +50,7 @@ module Hornetseye
50
50
  BGR = FourCC 'R', 'G', 'B', ' '
51
51
  UYVY = FourCC 'U', 'Y', 'V', 'Y'
52
52
  YUY2 = FourCC 'Y', 'U', 'Y', '2'
53
- I420 = FourCC 'I', '4', '2', '0'
53
+ # I420 = FourCC 'I', '4', '2', '0'
54
54
  YV12 = FourCC 'Y', 'V', '1', '2'
55
55
  MJPG = FourCC 'M', 'J', 'P', 'G'
56
56
 
@@ -0,0 +1,121 @@
1
+ # hornetseye-frame - Colourspace conversions and compression
2
+ # Copyright (C) 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
+
17
+ # Namespace of Hornetseye computer vision library
18
+ module Hornetseye
19
+
20
+ class Frame_
21
+
22
+ class << self
23
+
24
+ attr_accessor :typecode, :width, :height
25
+
26
+ def inspect
27
+ to_s
28
+ end
29
+
30
+ def to_s
31
+ "Frame(#{typecode},#{@width},#{@height})"
32
+ end
33
+
34
+ def shape
35
+ [ @width, @height ]
36
+ end
37
+
38
+ def storage_size
39
+ case typecode
40
+ when BGR
41
+ width * height * 3
42
+ when UYVY
43
+ width * height * 2
44
+ when YUY2
45
+ widtha = ( width + 3 ) & ~0x3
46
+ widtha * height * 2
47
+ when YV12
48
+ width * height * 3 / 2
49
+ when MJPG
50
+ width * height * 2
51
+ else
52
+ raise "Memory size of #{inspect} is not known"
53
+ end
54
+ end
55
+
56
+ end
57
+
58
+ attr_reader :memory
59
+
60
+ def initialize( value = nil )
61
+ @memory = value || Malloc.new( self.class.storage_size )
62
+ end
63
+
64
+ def inspect
65
+ "#{self.class.inspect}(#{ "0x%08x" % @memory.object_id })"
66
+ end
67
+
68
+ def typecode
69
+ self.class.typecode
70
+ end
71
+
72
+ def shape
73
+ self.class.shape
74
+ end
75
+
76
+ def width
77
+ self.class.width
78
+ end
79
+
80
+ def height
81
+ self.class.height
82
+ end
83
+
84
+ end
85
+
86
+ def Frame( typecode, width, height )
87
+ if typecode.is_a? FourCC
88
+ retval = Class.new Frame_
89
+ retval.typecode = typecode
90
+ retval.width = width
91
+ retval.height = height
92
+ retval
93
+ else
94
+ Hornetseye::MultiArray typecode, width, height
95
+ end
96
+ end
97
+
98
+ module_function :Frame
99
+
100
+ class Frame
101
+
102
+ class << self
103
+
104
+ def new( typecode, width, height )
105
+ Hornetseye::Frame( typecode, width, height ).new
106
+ end
107
+
108
+ def import( typecode, width, height, memory )
109
+ Hornetseye::Frame( typecode, width, height ).new memory
110
+ end
111
+
112
+ def storage_size( typecode, width, height )
113
+ Hornetseye::Frame( typecode, width, height ).storage_size
114
+ end
115
+
116
+ end
117
+
118
+ end
119
+
120
+ end
121
+
@@ -14,6 +14,8 @@
14
14
  # You should have received a copy of the GNU General Public License
15
15
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
+ require 'malloc'
17
18
  require 'multiarray'
18
19
  require 'hornetseye-frame/fourcc'
20
+ require 'hornetseye-frame/frame'
19
21
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jan Wedekind
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-24 00:00:00 +01:00
17
+ date: 2010-09-30 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -41,8 +41,8 @@ dependencies:
41
41
  - !ruby/object:Gem::Version
42
42
  segments:
43
43
  - 0
44
- - 5
45
- version: "0.5"
44
+ - 6
45
+ version: "0.6"
46
46
  type: :runtime
47
47
  version_requirements: *id002
48
48
  - !ruby/object:Gem::Dependency
@@ -72,8 +72,14 @@ files:
72
72
  - COPYING
73
73
  - .document
74
74
  - lib/hornetseye-frame/fourcc.rb
75
+ - lib/hornetseye-frame/frame.rb
75
76
  - lib/hornetseye_frame_ext.rb
76
77
  - ext/init.cc
78
+ - ext/frame.cc
79
+ - ext/colourspace.cc
80
+ - ext/frame.hh
81
+ - ext/colourspace.hh
82
+ - ext/error.hh
77
83
  has_rdoc: yard
78
84
  homepage: http://wedesoft.github.com/hornetseye-frame/
79
85
  licenses: []