[Jahshaka-cvs] jah/jahwidgets/src/qt3/widgets scribbler.cpp, 1.18, 1.19 scribbler.h, 1.10, 1.11
Status: Beta
Brought to you by:
jahshaka
From: John B. <ok_...@us...> - 2007-04-05 16:21:45
|
Update of /cvsroot/jahshaka/jah/jahwidgets/src/qt3/widgets In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv31158/jahwidgets/src/qt3/widgets Modified Files: scribbler.cpp scribbler.h Log Message: Scribbler now supports qimage <-> il::image conversions. Index: scribbler.cpp =================================================================== RCS file: /cvsroot/jahshaka/jah/jahwidgets/src/qt3/widgets/scribbler.cpp,v retrieving revision 1.18 retrieving revision 1.19 diff -u -d -r1.18 -r1.19 --- scribbler.cpp 16 Jan 2007 10:47:47 -0000 1.18 +++ scribbler.cpp 5 Apr 2007 16:21:42 -0000 1.19 @@ -98,40 +98,6 @@ return valid; } -il::image_type_ptr qimage_to_image( const QImage& qimage ) -{ - typedef il::image< unsigned char, il::b8g8r8a8 > b8g8r8a8_image_type; - - if ( qimage.isNull() ) return il::image_type_ptr(); - - if ( qimage.depth( ) == 32 ) - { - int w = qimage.width( ); - int h = qimage.height( ); - uchar *bits = qimage.bits( ); - - // Optimal memcpy when applicable - il::image_type_ptr image( new il::image_type( b8g8r8a8_image_type( w, h, 1 ) ) ); - - w *= 4; - int stride = image->pitch( ); - uchar *ptr = image->data( ); - - while ( h-- ) - { - memcpy( ptr, bits, w ); - ptr += stride; - bits += w; - } - - return image; - } - else { - qWarning( "qimage_to_image: only supports 32bit images" ); - return il::image_type_ptr(); - } -} - // Scribber class: // // NB: This class only works in b8g8r8a8 - the pf_ is just provided as a courtesy, @@ -624,4 +590,99 @@ return std::string( str.ascii( ) ); } +il::image_type_ptr scribbler::qimage_to_image( const QImage& qimage ) +{ + typedef il::image< unsigned char, il::b8g8r8a8 > b8g8r8a8_image_type; + typedef il::image< unsigned char, il::a8r8g8b8 > a8r8g8b8_image_type; + + if ( qimage.isNull() ) return il::image_type_ptr(); + + if ( qimage.depth( ) == 32 ) + { + int w = qimage.width( ); + int h = qimage.height( ); + uchar *bits = qimage.bits( ); + + // Optimal memcpy when applicable + il::image_type_ptr image; + + if ( QImage::systemByteOrder() == QImage::BigEndian ) { + image.reset( new il::image_type( a8r8g8b8_image_type( w, h, 1 ) ) ); + } + else { + image.reset( new il::image_type( b8g8r8a8_image_type( w, h, 1 ) ) ); + } + + w *= 4; + int stride = image->pitch( ); + uchar *ptr = image->data( ); + + while ( h-- ) + { + memcpy( ptr, bits, w ); + ptr += stride; + bits += w; + } + + return image; + } + else { + qWarning( "qimage_to_image: only supports 32bit images" ); + return il::image_type_ptr(); + } +} + +QImage scribbler::image_to_qimage( il::image_type_ptr image ) +{ + if ( !image ) { + qWarning( "image_to_qimage passed NULL image_type_ptr" ); + return QImage(); + } + + //qDebug( "image_to_qimage pf: %s", WStringToQString::convert( image->pf() ).latin1() ); + //qDebug( "image_to_qimage: w %d h %d", image->width(), image->height() ); + + // Convert to correct format + il::image_type_ptr dst_img; + if ( QImage::systemByteOrder() == QImage::BigEndian ) { + // TODO: a8r8g8b8 is correct for the Mac, but for some reason causes image corruption... + //dst_img = il::convert( image, L"a8r8g8b8" ); + dst_img = il::convert( image, L"b8g8r8a8" ); + } + else { + dst_img = il::convert( image, L"b8g8r8a8" ); + } + + if ( !dst_img ) { + qWarning( "image_to_qimage unable to convert to QImage byte order" ); + return QImage(); + } + + /* + qDebug( "src: %s", WStringToQString::convert(image->pf()).latin1() ); + qDebug( "src data: %p", image->data() ); + qDebug( "dst data: %p", dst_img->data() ); + */ + + // Convert + int w = dst_img->width(); + int h = dst_img->height(); + QImage qimage( w, h, 32 ); + + int stride = dst_img->pitch( ); + uchar* ptr = dst_img->data( ); + uchar* bits = qimage.bits(); + + //qDebug( "qim data: %p", qimage.bits() ); + + while ( h-- ) + { + memcpy( bits, ptr, stride ); + ptr += stride; + bits += stride; + } + + return qimage; +} + } } Index: scribbler.h =================================================================== RCS file: /cvsroot/jahshaka/jah/jahwidgets/src/qt3/widgets/scribbler.h,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- scribbler.h 15 Sep 2006 16:16:42 -0000 1.10 +++ scribbler.h 5 Apr 2007 16:21:42 -0000 1.11 @@ -24,6 +24,7 @@ #include <openpluginlib/pl/utf8_utils.hpp> #include <qnamespace.h> // for alignment flags +#include <qimage.h> #include <stack> @@ -82,6 +83,10 @@ std::string timecode( int, double, bool ); + // Convenience methods for image <-> QImage conversion + static il::image_type_ptr qimage_to_image( const QImage& ); + static QImage image_to_qimage( il::image_type_ptr ); + private: TimecodeValueFormatter formatter_; QImage render_text_qimage( const pl::wstring& text, const pl::wstring& font_name, int pointSize, int alignment ) const; |