Fix parsing of SSA colour tags which have their leading zeros omitted. v1.6.27
authorCarl Hetherington <cth@carlh.net>
Wed, 10 Aug 2022 18:58:19 +0000 (20:58 +0200)
committerCarl Hetherington <cth@carlh.net>
Wed, 10 Aug 2022 18:58:36 +0000 (20:58 +0200)
src/ssa_reader.cc
test/ssa_reader_test.cc

index 863225112d2fab532e2271d939f82a15b908a8cf..07d592a1e1e1f704a543e15a2158858f760ce005 100644 (file)
@@ -25,6 +25,7 @@
 #include "compose.hpp"
 #include <boost/algorithm/string.hpp>
 #include <boost/bind/bind.hpp>
+#include <cstdlib>
 #include <iostream>
 #include <vector>
 
@@ -55,17 +56,24 @@ SSAReader::SSAReader (FILE* f)
 Colour
 h_colour (string s)
 {
-       /* There are both BGR and ABGR versions of these colours */
-       if ((s.length() != 8 && s.length() != 10) || s[0] != '&' || s[1] != 'H') {
+       if (s.empty() || s[0] != '&' || s[1] != 'H') {
                throw SSAError(String::compose("Badly formatted colour tag %1", s));
        }
-       int ir, ig, ib;
-       /* XXX: ignoring alpha channel here; note that 00 is opaque and FF is transparent */
-       int const off = s.length() == 10 ? 4 : 2;
-       if (sscanf(s.c_str() + off, "%2x%2x%2x", &ib, &ig, &ir) < 3) {
-               throw SSAError(String::compose("Badly formatted colour tag %1", s));
+
+       auto start = s.c_str();
+       auto const end = start + s.length();
+       while (start < end && (*start == '&' || *start == 'H')) {
+               ++start;
        }
-       return sub::Colour(ir / 255.0, ig / 255.0, ib / 255.0);
+
+       auto const colour = strtoll(start, nullptr, 16);
+
+       /* XXX: ignoring alpha channel here; note that 00 is opaque and FF is transparent */
+       return sub::Colour(
+               ((colour & 0x000000ff) >> 0) / 255.0,
+               ((colour & 0x0000ff00) >> 8) / 255.0,
+               ((colour & 0x00ff0000) >> 16) / 255.0
+               );
 }
 
 class Style
index 82d3a3330283a715df54fac2c399f1703faf7830..e6da6976ee0cb19e94093f6bf0a3b020aa1a2463 100644 (file)
@@ -650,21 +650,10 @@ BOOST_AUTO_TEST_CASE (ssa_reader_c)
 {
        test_c("&H00FFFF&", "ffff00");
        test_c("&H123456&", "563412");
+       test_c("&H0&", "000000");
+       test_c("&HFF&", "ff0000");
+       test_c("&HFF00&", "00ff00");
        test_c("&HFF0000&", "0000ff");
        test_c("&HFFFFFF&", "ffffff");
 }
 
-
-/** Test invalid \c */
-BOOST_AUTO_TEST_CASE (ssa_reader_c_bad)
-{
-       sub::RawSubtitle base;
-       BOOST_CHECK_THROW(
-               sub::SSAReader::parse_line(
-                       base,
-                       "{\\c&H0}Dieser Untertitel ist gelb",
-                       1920, 1080
-                       ),
-               sub::SSAError
-               );
-}