Permalink
Browse files

Using libswscale

  • Loading branch information...
1 parent 8dcb93a commit ab1698151fcaf469100ac77e24d94ece6f47b849 @wedesoft committed Dec 21, 2010
Showing with 75 additions and 45 deletions.
  1. +44 −5 Rakefile
  2. +31 −40 ext/ximagepainter.cc
View
@@ -7,7 +7,7 @@ require 'rake/loaders/makefile'
require 'rbconfig'
PKG_NAME = 'hornetseye-xorg'
-PKG_VERSION = '0.5.0'
+PKG_VERSION = '0.5.1'
CFG = RbConfig::CONFIG
CXX = ENV[ 'CXX' ] || 'g++'
RB_FILES = FileList[ 'lib/**/*.rb' ]
@@ -27,7 +27,7 @@ EMAIL = %q{jan@wedesoft.de}
HOMEPAGE = %q{http://wedesoft.github.com/hornetseye-xorg/}
OBJ = CC_FILES.ext 'o'
-$CXXFLAGS = "-DNDEBUG #{CFG[ 'CPPFLAGS' ]} #{CFG[ 'CFLAGS' ]}"
+$CXXFLAGS = "-DNDEBUG -DHAVE_CONFIG_H -D__STDC_CONSTANT_MACROS #{CFG[ 'CPPFLAGS' ]} #{CFG[ 'CFLAGS' ]}"
if CFG[ 'rubyhdrdir' ]
$CXXFLAGS = "#{$CXXFLAGS} -I#{CFG[ 'rubyhdrdir' ]} " +
"-I#{CFG[ 'rubyhdrdir' ]}/#{CFG[ 'arch' ]}"
@@ -46,7 +46,7 @@ desc 'Compile Ruby extension (default)'
task :all => [ SO_FILE ]
file SO_FILE => OBJ do |t|
- sh "#{CXX} -shared -o #{t.name} #{OBJ} -lGLU -lGL -lX11 -lXv -lXpm #{$LIBRUBYARG}"
+ sh "#{CXX} -shared -o #{t.name} #{OBJ} -lswscale -lGLU -lGL -lX11 -lXv -lXpm #{$LIBRUBYARG}"
end
task :test => [ SO_FILE ]
@@ -73,6 +73,45 @@ task :uninstall do
end
end
+desc 'Create config.h'
+task :config_h => 'ext/config.h'
+
+def check_program
+ f_base_name = 'rakeconf'
+ begin
+ File.open( "#{f_base_name}.cc", 'w' ) { |f| yield f }
+ `#{CXX} -S #{$CXXFLAGS} -c -o #{f_base_name}.o #{f_base_name}.cc 2>&1 >> rake.log`
+ $?.exitstatus == 0
+ ensure
+ File.delete *Dir.glob( "#{f_base_name}.*" )
+ end
+end
+
+def check_c_header( name )
+ check_program do |c|
+ c.puts <<EOS
+extern "C" {
+ #include <#{name}>
+}
+int main(void) { return 0; }
+EOS
+ end
+end
+
+file 'ext/config.h' do |t|
+ s = "/* config.h. Generated from Rakefile by rake. */\n"
+ # need to compile with -D__STDC_CONSTANT_MACROS
+ if check_c_header 'libswscale/swscale.h'
+ s << "#define HAVE_LIBSWSCALE_INCDIR 1\n"
+ else
+ unless check_c_header 'ffmpeg/swscale.h'
+ raise 'Cannot find swscale.h header file'
+ end
+ s << "#undef HAVE_LIBSWSCALE_INCDIR\n"
+ end
+ File.open( t.name, 'w' ) { |f| f.puts s }
+end
+
Rake::TestTask.new do |t|
t.libs << 'ext'
t.test_files = TC_FILES
@@ -170,7 +209,7 @@ rule '.o' => '.cc' do |t|
sh "#{CXX} #{$CXXFLAGS} -c -o #{t.name} #{t.source}"
end
-file ".depends.mf" do |t|
+file ".depends.mf" => :config_h do |t|
sh "g++ -MM #{CC_FILES.join ' '} | " +
"sed -e :a -e N -e 's/\\n/\\$/g' -e ta | " +
"sed -e 's/ *\\\\\\$ */ /g' -e 's/\\$/\\n/g' | sed -e 's/^/ext\\//' > #{t.name}"
@@ -181,5 +220,5 @@ end
import ".depends.mf"
CLEAN.include 'ext/*.o'
-CLOBBER.include SO_FILE, 'doc', '.yardoc', '.depends.mf'
+CLOBBER.include SO_FILE, 'doc', '.yardoc', '.depends.mf', 'ext/config.h'
View
@@ -32,11 +32,21 @@
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
#include <boost/shared_array.hpp>
#include <errno.h>
#ifndef NDEBUG
#include <iostream>
#endif
+extern "C" {
+#ifndef HAVE_LIBSWSCALE_INCDIR
+ #include <ffmpeg/swscale.h>
+#else
+ #include <libswscale/swscale.h>
+#endif
+}
#include "ximagepainter.hh"
#include "rubytools.hh"
#include "x11window.hh"
@@ -63,61 +73,42 @@ void XImagePainter::paint( bool ) throw (Error)
FramePtr frame = m_imageSource->frame();
if ( frame ) {
- // Create scaled BGRA data.
- int
- newWidth = m_window->width(),
- newHeight = m_window->height();
boost::shared_array< unsigned char > array
- ( new unsigned char[ newWidth * newHeight * 4 ] );
- const char *p = frame->data();
- int
- fw = frame->width(),
- dx = ( frame->width() << 16 ) / newWidth,
- dy = ( frame->height() << 16 ) / newHeight,
- py = 0;
- unsigned char *d = array.get();
- for ( int y=0; y<newHeight; y++ ) {
- int t = fw * ( py >> 16 );
- const char *s = p + ( t << 1 ) + t;/* 3 * t */
- int
- ox = 0,
- px = 0;
- for ( int x=0; x<newWidth; x++ ) {
- px += dx;
- *d++ = s[2];
- *d++ = s[1];
- *d++ = s[0];
- *d++ = '\000';
- t = ( px - ox ) >> 16;
- ox += t << 16;
- s += ( t << 1 ) + t;// s += 3 * t
- }
- py += dy;
- };
-
- // Display the scaled BGRA data.
+ ( new unsigned char[ m_window->width() * m_window->height() * 4 ] );
+ SwsContext *swsContext =
+ sws_getContext( frame->width(), frame->height(), PIX_FMT_RGB24,
+ m_window->width(), m_window->height(), PIX_FMT_BGRA,
+ SWS_FAST_BILINEAR, 0, 0, 0 );
+ uint8_t *sourceData[4];
+ int sourceLineSize[4];
+ sourceData[0] = (uint8_t *)frame->data();
+ sourceLineSize[0] = 3 * frame->width();
+ uint8_t *destData[4];
+ int destLineSize[4];
+ destData[0] = (uint8_t *)array.get();
+ destLineSize[0] = 4 * m_window->width();
+ sws_scale( swsContext, sourceData, sourceLineSize, 0,
+ frame->height(), destData, destLineSize );
XImage *xImage = XCreateImage( m_display->get(), m_visualInfo.visual,
- 24, ZPixmap, 0,
- (char *)array.get(),
- newWidth, newHeight,
- 32, newWidth * 4 );
+ 24, ZPixmap, 0, (char *)array.get(),
+ m_window->width(), m_window->height(),
+ 32, m_window->width() * 4 );
ERRORMACRO( xImage != NULL, Error, ,
"Failed to create X11-image" );
xImage->byte_order = LSBFirst;
#ifndef NDEBUG
- cerr << "Window size is " << newWidth << 'x'
- << newHeight << endl;
+ cerr << "Window size is " << m_window->width() << 'x'
+ << m_window->height() << endl;
#endif
XPutImage( m_display->get(), m_window->get(), m_window->gc(),
- xImage, 0, 0, 0, 0, newWidth, newHeight );
+ xImage, 0, 0, 0, 0, m_window->width(), m_window->height() );
xImage->data = (char *)NULL;
XDestroyImage( xImage );
};
};
}
-
XVisualInfo *XImagePainter::visualInfo( X11DisplayPtr display )
throw (Error)
{

0 comments on commit ab16981

Please sign in to comment.