Basic support for colours in binary STL. v1.6.48
authorCarl Hetherington <cth@carlh.net>
Sun, 19 May 2024 19:23:37 +0000 (21:23 +0200)
committerCarl Hetherington <cth@carlh.net>
Sun, 19 May 2024 19:23:37 +0000 (21:23 +0200)
src/stl_binary_reader.cc
test/stl_binary_reader_test.cc

index 35091f083cfbcddebcbdcb12af77595a7289d35f..7b38065b1aac637325c39f12fc80f16e5b64a5bb 100644 (file)
@@ -248,14 +248,46 @@ void STLBinaryReader::read (shared_ptr<InputReader> reader)
                                        break;
                                }
 
-                               if (c >= 0x80 && c <= 0x83) {
-                                       /* Italic or underline control code */
+                               if (c <= 0x07 || (c >= 0x80 && c <= 0x83)) {
+                                       /* Colour, italic or underline control code */
                                        sub.text = utf_to_utf<char> (iso6937_to_utf16 (text.c_str()));
                                        _subs.push_back (sub);
                                        text.clear ();
                                }
 
                                switch (c) {
+                               case 0x0:
+                                       /* Black */
+                                       sub.colour = Colour(0, 0, 0);
+                                       break;
+                               case 0x1:
+                                       /* Red */
+                                       sub.colour = Colour(1, 0, 0);
+                                       break;
+                               case 0x2:
+                                       /* Lime */
+                                       sub.colour = Colour(0, 1, 0);
+                                       break;
+                               case 0x3:
+                                       /* Yellow */
+                                       sub.colour = Colour(1, 1, 0);
+                                       break;
+                               case 0x4:
+                                       /* Blue */
+                                       sub.colour = Colour(0, 0, 1);
+                                       break;
+                               case 0x5:
+                                       /* Magenta */
+                                       sub.colour = Colour(1, 0, 1);
+                                       break;
+                               case 0x6:
+                                       /* Cyan */
+                                       sub.colour = Colour(0, 1, 1);
+                                       break;
+                               case 0x7:
+                                       /* White */
+                                       sub.colour = Colour(1, 1, 1);
+                                       break;
                                case 0x80:
                                        italic = true;
                                        break;
index 6566c5f161e0d0ff2b77f672c95cf3c6bb55d5d6..a0469d009796d5e1f1847d0a9ccd24532c0a86bd 100644 (file)
@@ -17,6 +17,7 @@
 
 */
 
+
 #include "stl_binary_reader.h"
 #include "subtitle.h"
 #include "test.h"
 #include <boost/test/unit_test.hpp>
 #include <fstream>
 
+
 using std::ifstream;
+using std::make_shared;
 using std::ofstream;
 using std::shared_ptr;
-using std::make_shared;
+using std::vector;
+
 
 /* Test reading of a binary STL file */
 BOOST_AUTO_TEST_CASE (stl_binary_reader_test1)
@@ -94,3 +98,36 @@ BOOST_AUTO_TEST_CASE (stl_binary_reader_test3)
        }
 }
 
+
+BOOST_AUTO_TEST_CASE(stl_binary_reader_with_colour)
+{
+       auto path = private_test / "at.stl";
+       ifstream in(path.string().c_str());
+       auto reader = make_shared<sub::STLBinaryReader>(in);
+
+       vector<sub::Colour> reference = {
+               { 1, 1, 1 },
+               { 1, 0, 1 },
+               { 1, 1, 1 },
+               { 1, 0, 1 },
+               { 1, 1, 1 },
+               { 1, 1, 0 },
+               { 1, 1, 1 },
+               { 1, 1, 0 },
+               { 1, 1, 1 },
+               { 1, 1, 0 },
+               { 1, 1, 1 },
+               { 1, 1, 0 },
+               { 1, 1, 1 },
+               { 1, 1, 0 }
+       };
+
+       /* Check the first few lines */
+       auto subs = reader->subtitles();
+       BOOST_REQUIRE(subs.size() >= reference.size());
+
+       for (size_t i = 0; i < reference.size(); ++i) {
+               BOOST_CHECK(subs[i].colour == reference[i]);
+       }
+}
+