lavc/sunrastenc: consider cases with linesize < 0
authorStefano Sabatini <stefasab@gmail.com>
Sun, 18 Mar 2012 15:55:53 +0000 (16:55 +0100)
committerStefano Sabatini <stefasab@gmail.com>
Mon, 19 Mar 2012 18:01:40 +0000 (19:01 +0100)
Make sunrast_image_write_image() deal with cases when linesize is < 0.
Fix trac ticket #1077.

libavcodec/sunrastenc.c

index a9b4749..6aae353 100644 (file)
@@ -56,7 +56,7 @@ static void sunrast_image_write_image(AVCodecContext *avctx,
 {
     SUNRASTContext *s = avctx->priv_data;
     const uint8_t *ptr;
 {
     SUNRASTContext *s = avctx->priv_data;
     const uint8_t *ptr;
-    int len, alen, x;
+    int len, alen, x, y;
 
     if (s->maplength) {     // palettized
         PutByteContext pb_r, pb_g;
 
     if (s->maplength) {     // palettized
         PutByteContext pb_r, pb_g;
@@ -83,30 +83,30 @@ static void sunrast_image_write_image(AVCodecContext *avctx,
      if (s->type == RT_BYTE_ENCODED) {
         uint8_t value, value2;
         int run;
      if (s->type == RT_BYTE_ENCODED) {
         uint8_t value, value2;
         int run;
-        const uint8_t *end = pixels + avctx->height * linesize;
 
 
+        y = 0;
         ptr = pixels;
 
         ptr = pixels;
 
-#define GET_VALUE ptr >= end ? 0 : x >= len ? ptr[len-1] : ptr[x]
+#define GET_VALUE y >= avctx->height ? 0 : x >= len ? ptr[len-1] : ptr[x]
 
 
-        x = 0;
+        x = 0, y = 0;
         value2 = GET_VALUE;
         value2 = GET_VALUE;
-        while (ptr < end) {
+        while (y < avctx->height) {
             run = 1;
             value = value2;
             x++;
             if (x >= alen) {
                 x = 0;
             run = 1;
             value = value2;
             x++;
             if (x >= alen) {
                 x = 0;
-                ptr += linesize;
+                ptr += linesize, y++;
             }
 
             value2 = GET_VALUE;
             }
 
             value2 = GET_VALUE;
-            while (value2 == value && run < 256 && ptr < end) {
+            while (value2 == value && run < 256 && y < avctx->height) {
                 x++;
                 run++;
                 if (x >= alen) {
                     x = 0;
                 x++;
                 run++;
                 if (x >= alen) {
                     x = 0;
-                    ptr += linesize;
+                    ptr += linesize, y++;
                 }
                 value2 = GET_VALUE;
             }
                 }
                 value2 = GET_VALUE;
             }
@@ -125,7 +125,6 @@ static void sunrast_image_write_image(AVCodecContext *avctx,
         // update data length for header
         s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
     } else {
         // update data length for header
         s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
     } else {
-        int y;
         for (y = 0; y < avctx->height; y++) {
             bytestream2_put_buffer(&s->p, ptr, len);
             if (len < alen)
         for (y = 0; y < avctx->height; y++) {
             bytestream2_put_buffer(&s->p, ptr, len);
             if (len < alen)