]> git.sesse.net Git - mlt/commitdiff
convert to and cache requested format in qimage
authorDan Dennedy <dan@dennedy.org>
Fri, 2 Mar 2012 08:10:37 +0000 (00:10 -0800)
committerDan Dennedy <dan@dennedy.org>
Fri, 2 Mar 2012 08:10:37 +0000 (00:10 -0800)
src/modules/qimage/producer_qimage.c
src/modules/qimage/qimage_wrapper.cpp
src/modules/qimage/qimage_wrapper.h

index 68f5faa094894283cade07db41510658438a90fa..950c07ab3ca98dbd3d2530ca8ba21b7668e67b29 100644 (file)
@@ -165,19 +165,21 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
        self->qimage = mlt_cache_item_data( self->qimage_cache, NULL );
        self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.image" );
        self->current_image = mlt_cache_item_data( self->image_cache, NULL );
-       refresh_image( self, frame, *width, *height );
+       self->alpha_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.alpha" );
+       self->current_alpha = mlt_cache_item_data( self->alpha_cache, NULL );
+       refresh_image( self, frame, *format, *width, *height );
 
        // Get width and height (may have changed during the refresh)
        *width = mlt_properties_get_int( properties, "width" );
        *height = mlt_properties_get_int( properties, "height" );
-       *format = self->has_alpha ? mlt_image_rgb24a : mlt_image_rgb24;
+       *format = self->format;
 
        // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
        // The fault is not in the design of mlt, but in the implementation of the qimage producer...
        if ( self->current_image )
        {
                // Clone the image and the alpha
-               int image_size = self->current_width * ( self->current_height + 1 ) * ( self->has_alpha ? 4 :3 );
+               int image_size = mlt_image_format_size( self->format, self->current_width, self->current_height, NULL );
                uint8_t *image_copy = mlt_pool_alloc( image_size );
                memcpy( image_copy, self->current_image, image_size );
                // Now update properties so we free the copy after
@@ -186,6 +188,13 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
                *buffer = image_copy;
                mlt_log_debug( MLT_PRODUCER_SERVICE( &self->parent ), "%dx%d (%s)\n",
                        self->current_width, self->current_height, mlt_image_format_name( *format ) );
+               // Clone the alpha channel
+               if ( self->current_alpha )
+               {
+                       image_copy = mlt_pool_alloc( self->current_width * self->current_height );
+                       memcpy( image_copy, self->current_alpha, self->current_width * self->current_height );
+                       mlt_frame_set_alpha( frame, image_copy, self->current_width * self->current_height, mlt_pool_release );
+               }
        }
        else
        {
@@ -195,6 +204,7 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
        // Release references and locks
        mlt_cache_item_close( self->qimage_cache );
        mlt_cache_item_close( self->image_cache );
+       mlt_cache_item_close( self->alpha_cache );
        mlt_service_unlock( MLT_PRODUCER_SERVICE( &self->parent ) );
 
        return error;
index f2cb722f19cd145cef4a94dd4d75bdf53ea1a637..4877af8eb3c883999ce2ba782ca58d919e88d1bc 100644 (file)
@@ -215,7 +215,7 @@ int refresh_qimage( producer_qimage self, mlt_frame frame )
        return image_idx;
 }
 
-void refresh_image( producer_qimage self, mlt_frame frame, int width, int height )
+void refresh_image( producer_qimage self, mlt_frame frame, mlt_image_format format, int width, int height )
 {
        // Obtain properties of frame and producer
        mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
@@ -229,7 +229,7 @@ void refresh_image( producer_qimage self, mlt_frame frame, int width, int height
                self->current_image = NULL;
 
        // If we have a qimage and need a new scaled image
-       if ( self->qimage && !self->current_image )
+       if ( self->qimage && ( !self->current_image || ( format != mlt_image_none  && format != self->format ) ) )
        {
                char *interps = mlt_properties_get( properties, "rescale.interp" );
                int interp = 0;
@@ -252,7 +252,7 @@ void refresh_image( producer_qimage self, mlt_frame frame, int width, int height
                }
                QImage scaled = interp == 0 ? qimage->scaled( QSize( width, height ) ) :
                        qimage->scaled( QSize(width, height), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
-               self->has_alpha = scaled.hasAlphaChannel();
+               int has_alpha = scaled.hasAlphaChannel();
 #endif
 
 #ifdef USE_QT3
@@ -267,9 +267,10 @@ void refresh_image( producer_qimage self, mlt_frame frame, int width, int height
                self->current_height = height;
 
                // Allocate/define image
-               int dst_stride = width * ( self->has_alpha ? 4 : 3 );
+               int dst_stride = width * ( has_alpha ? 4 : 3 );
                int image_size = dst_stride * ( height + 1 );
                self->current_image = ( uint8_t * )mlt_pool_alloc( image_size );
+               self->format = has_alpha ? mlt_image_rgb24a : mlt_image_rgb24;
 
                // Copy the image
                int y = self->current_height + 1;
@@ -283,16 +284,48 @@ void refresh_image( producer_qimage self, mlt_frame frame, int width, int height
                                *dst++ = qRed(*src);
                                *dst++ = qGreen(*src);
                                *dst++ = qBlue(*src);
-                               if ( self->has_alpha ) *dst++ = qAlpha(*src);
+                               if ( has_alpha ) *dst++ = qAlpha(*src);
                                ++src;
                        }
                }
 
+               // Convert image to requested format
+               if ( format != mlt_image_none && format != self->format )
+               {
+                       uint8_t *buffer = NULL;
+
+                       // First, set the image so it can be converted when we get it
+                       mlt_frame_set_image( frame, self->current_image, image_size, mlt_pool_release );
+                       mlt_properties_set_int( properties, "format", self->format );
+                       mlt_properties_set_int( properties, "width", width );
+                       mlt_properties_set_int( properties, "height", height );
+                       self->format = format;
+
+                       // get_image will do the format conversion
+                       mlt_frame_get_image( frame, &buffer, &format, &width, &height, 0 );
+
+                       // cache copies of the image and alpha buffers
+                       if ( buffer )
+                       {
+                               image_size = mlt_image_format_size( format, width, height, NULL );
+                               self->current_image = (uint8_t*) mlt_pool_alloc( image_size );
+                               memcpy( self->current_image, buffer, image_size );
+                       }
+                       if ( ( buffer = mlt_frame_get_alpha_mask( frame ) ) )
+                       {
+                               self->current_alpha = (uint8_t*) mlt_pool_alloc( width * height );
+                               memcpy( self->current_alpha, buffer, width * height );
+                       }
+               }
+
                // Update the cache
                mlt_cache_item_close( self->image_cache );
                mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "qimage.image", self->current_image, image_size, mlt_pool_release );
                self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.image" );
                self->image_idx = image_idx;
+               mlt_cache_item_close( self->alpha_cache );
+               mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha", self->current_alpha, width * height, mlt_pool_release );
+               self->alpha_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha" );
        }
 
        // Set width/height of frame
index 78e933c8d5fc0638b79863152c2d8e7ee05983d8..b14c29106804400488db65adb6dc918659c5d07d 100644 (file)
@@ -41,18 +41,20 @@ struct producer_qimage_s
        int image_idx;
        int qimage_idx;
        uint8_t *current_image;
-       int has_alpha;
+       uint8_t *current_alpha;
        int current_width;
        int current_height;
        mlt_cache_item image_cache;
+       mlt_cache_item alpha_cache;
        mlt_cache_item qimage_cache;
        void *qimage;
+       mlt_image_format format;
 };
 
 typedef struct producer_qimage_s *producer_qimage;
 
 extern int refresh_qimage( producer_qimage self, mlt_frame frame );
-extern void refresh_image( producer_qimage, mlt_frame, int width, int height );
+extern void refresh_image( producer_qimage, mlt_frame, mlt_image_format, int width, int height );
 extern void make_tempfile( producer_qimage, const char *xml );
 
 #ifdef USE_KDE