]> git.saurik.com Git - wxWidgets.git/commitdiff
Added support for writing a long comment to a GIF image.
authorDimitri Schoolwerth <dimitri.schoolwerth@gmail.com>
Fri, 28 Jan 2011 08:10:01 +0000 (08:10 +0000)
committerDimitri Schoolwerth <dimitri.schoolwerth@gmail.com>
Fri, 28 Jan 2011 08:10:01 +0000 (08:10 +0000)
Instead of limiting a comment to a maximum length of 255 bytes write multiple comment data blocks (each a Pascal string) followed by the block terminator.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66802 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/common/imaggif.cpp

index 75e8f73def579c2d4300051fb1f4f3572c396299..b33ac24dab6f46568a8637660588fc715951423d 100644 (file)
@@ -667,12 +667,7 @@ bool wxGIFHandler_WriteHeader(wxOutputStream *stream, int width, int height,
        ok = ok && wxGIFHandler_WriteLoop(stream);
     }
 
-    if ( !comment.empty() )
-    {
-       ok = ok && wxGIFHandler_WriteComment(stream, comment);
-    }
-
-    return ok;
+    return ok && wxGIFHandler_WriteComment(stream, comment);
 }
 
 bool wxGIFHandler_WriteRect(wxOutputStream *stream, int width, int height)
@@ -739,18 +734,44 @@ bool wxGIFHandler_WriteControl(wxOutputStream *stream,
 
 bool wxGIFHandler_WriteComment(wxOutputStream *stream, const wxString& comment)
 {
-    wxUint8 buf[3];
-    wxCharBuffer text(comment.mb_str());
-    size_t len = strlen(text.data());
-    len = wxMin(len, 255);
+    if ( comment.empty() )
+    {
+        return true;
+    }
 
+    // Write comment header.
+    wxUint8 buf[2];
     buf[0] = GIF_MARKER_EXT;
     buf[1] = GIF_MARKER_EXT_COMMENT;
-    buf[2] = (wxUint8)len;
+    if ( !wxGIFHandler_Write(stream, buf, sizeof(buf)) )
+    {
+        return false;
+    }
 
-    return wxGIFHandler_Write(stream, buf, sizeof(buf))
-        && wxGIFHandler_Write(stream, text.data(), len)
-        && wxGIFHandler_WriteZero(stream);
+    /*
+    If comment is longer than 255 bytes write it in blocks of maximum 255
+    bytes each.
+    */
+    wxCharBuffer text( comment.mb_str() );
+
+    size_t pos = 0, fullLength = text.length();
+
+    do
+    {
+        size_t blockLength = wxMin(fullLength - pos, 255);
+
+        if ( !wxGIFHandler_WriteByte(stream, (wxUint8) blockLength)
+            || !wxGIFHandler_Write(stream, &text.data()[pos], blockLength) )
+        {
+            return false;
+        }
+
+        pos += blockLength;
+    }while (pos < fullLength);
+
+
+    // Write comment footer.
+    return wxGIFHandler_WriteZero(stream);
 }
 
 bool wxGIFHandler_WriteLoop(wxOutputStream *stream)