]> granicus.if.org Git - libass/commitdiff
Factor out bitmap buffer copy
authorGrigori Goronzy <greg@blackbox>
Tue, 21 Jul 2009 01:06:05 +0000 (03:06 +0200)
committerGrigori Goronzy <greg@blackbox>
Tue, 21 Jul 2009 01:09:13 +0000 (03:09 +0200)
For clarity, factor out bitmap copying into a function. Do not pad the
bitmap with stride in the last row; this can not by guaranteed anyway.
Add a comment about this peculiarity to the API documentation in ass.h
Idea by Evgeniy Stepanov.

libass/ass.h
libass/ass_render.c

index b92960a157d32b16fdaa8be6668161865bce043f..3c8c1f65c20e03f9433b32e6d71b2a99f2ae3144 100644 (file)
 /* Libass renderer object. Contents are private. */
 typedef struct ass_renderer_s ass_renderer_t;
 
-/* A linked list of images produced by ass renderer. */
+/*
+ * A linked list of images produced by an ass renderer.
+ *
+ * These images have to be rendered in-order for the correct screen
+ * composition.  The libass renderer clips these bitmaps to the frame size.
+ * w/h can be zero, in this case the bitmap should not be rendered at all.
+ * The last bitmap row is not guaranteed to be padded up to stride size,
+ * e.g. in the worst case a bitmap has the size stride * (h - 1) + w.
+ */
 typedef struct ass_image_s {
     int w, h;                   // Bitmap width/height
     int stride;                 // Bitmap stride
     unsigned char *bitmap;      // 1bpp stride*h alpha buffer
+                                // Note: the last row may not be padded to
+                                // bitmap stride!
     uint32_t color;             // Bitmap color and alpha, RGBA
     int dst_x, dst_y;           // Bitmap placement inside the video frame
 
index f6234828b310ef68ef12a5a95b61e123bbac5e9a..23c93fbaca1c1c2400fb2f55bea41892f8d15c8d 100644 (file)
@@ -610,6 +610,20 @@ static ass_image_t **render_glyph(ass_renderer_t *render_priv,
     return tail;
 }
 
+/**
+ * \brief Replace the bitmap buffer in ass_image_t with a copy
+ * \param img ass_image_t to operate on
+ * \return pointer to old bitmap buffer
+ */
+static unsigned char *clone_bitmap_buffer(ass_image_t *img)
+{
+    unsigned char *old_bitmap = img->bitmap;
+    int size = img->stride * (img->h - 1) + img->w;
+    img->bitmap = malloc(size);
+    memcpy(img->bitmap, old_bitmap, size);
+    return old_bitmap;
+}
+
 /**
  * \brief Calculate overlapping area of two consecutive bitmaps and in case they
  * overlap, composite them together
@@ -680,12 +694,8 @@ render_overlap(ass_renderer_t *render_priv, ass_image_t **last_tail,
         return;
     }
     // Allocate new bitmaps and copy over data
-    a = (*last_tail)->bitmap;
-    b = (*tail)->bitmap;
-    (*last_tail)->bitmap = malloc(as * ah);
-    (*tail)->bitmap = malloc(bs * bh);
-    memcpy((*last_tail)->bitmap, a, as * ah);
-    memcpy((*tail)->bitmap, b, bs * bh);
+    a = clone_bitmap_buffer(*last_tail);
+    b = clone_bitmap_buffer(*tail);
 
     // Composite overlapping area
     for (y = 0; y < h; y++)