Basic interop bitmap writing.
authorCarl Hetherington <cth@carlh.net>
Sat, 7 Jul 2018 23:19:42 +0000 (00:19 +0100)
committerCarl Hetherington <cth@carlh.net>
Sat, 7 Jul 2018 23:19:42 +0000 (00:19 +0100)
src/interop_subtitle_asset.cc
src/object.h
src/subtitle.h
src/subtitle_asset.cc
src/subtitle_asset_internal.cc
src/subtitle_asset_internal.h
src/subtitle_string.h
test/data/sub.png [new file with mode: 0644]
test/data/write_interop_subtitle_test3.xml [new file with mode: 0644]
test/write_subtitle_test.cc

index 7d3114c3915f3df09ab1cf38610256ad2d38a1cf..02e2d75b21b6edc0074b5ea56f742b5e45a3b924 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of libdcp.
 
 
 #include "interop_subtitle_asset.h"
 #include "interop_load_font_node.h"
+#include "subtitle_asset_internal.h"
 #include "xml.h"
 #include "raw_convert.h"
 #include "util.h"
 #include "font_asset.h"
 #include "dcp_assert.h"
+#include "subtitle_image.h"
 #include <libxml++/libxml++.h>
 #include <boost/foreach.hpp>
 #include <cmath>
@@ -63,7 +65,7 @@ InteropSubtitleAsset::InteropSubtitleAsset (boost::filesystem::path file)
        _reel_number = xml->string_child ("ReelNumber");
        _language = xml->string_child ("Language");
        _movie_title = xml->string_child ("MovieTitle");
-       _load_font_nodes = type_children<dcp::InteropLoadFontNode> (xml, "LoadFont");
+       _load_font_nodes = type_children<InteropLoadFontNode> (xml, "LoadFont");
 
        /* Now we need to drop down to xmlpp */
 
@@ -174,6 +176,16 @@ InteropSubtitleAsset::write (boost::filesystem::path p) const
 
        _file = p;
 
+       /* Image subtitles */
+       int n = 0;
+       BOOST_FOREACH (shared_ptr<dcp::Subtitle> i, _subtitles) {
+               shared_ptr<dcp::SubtitleImage> im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
+               if (im) {
+                       im->png_image().write (p.parent_path() / image_subtitle_file (n++));
+               }
+       }
+
+       /* Fonts */
        BOOST_FOREACH (shared_ptr<InteropLoadFontNode> i, _load_font_nodes) {
                boost::filesystem::path file = p.parent_path() / i->uri;
                FILE* f = fopen_boost (file, "wb");
index 508170510fc133055f7ee8761bf09ffaf26b7b38..b2d0ad35f995188b9c4e30b36ec7a1a17534f975 100644 (file)
@@ -43,6 +43,7 @@
 
 class write_interop_subtitle_test;
 class write_interop_subtitle_test2;
+class write_interop_subtitle_test3;
 class write_smpte_subtitle_test;
 class write_smpte_subtitle_test2;
 
@@ -66,6 +67,7 @@ public:
 protected:
        friend struct ::write_interop_subtitle_test;
        friend struct ::write_interop_subtitle_test2;
+       friend struct ::write_interop_subtitle_test3;
        friend struct ::write_smpte_subtitle_test;
        friend struct ::write_smpte_subtitle_test2;
 
index 8dba299de6b6aa2d8d9f4b9ccf14eaef79c2d955..ed63983ff7e225ebf1864568cfe545e1e2b12f42 100644 (file)
@@ -113,11 +113,11 @@ public:
                _v_position = p;
        }
 
-       void set_fade_up_time (dcp::Time t) {
+       void set_fade_up_time (Time t) {
                _fade_up_time = t;
        }
 
-       void set_fade_down_time (dcp::Time t) {
+       void set_fade_down_time (Time t) {
                _fade_down_time = t;
        }
 
index 3ecd16a79d165966101ea868aff187b9ca9983a0..f93ffa85ab61f6f5bb515a9e7249f7a1f76521ba 100644 (file)
@@ -549,7 +549,6 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* xml_root, int time_code_rate, S
                }
 
                shared_ptr<SubtitleString> is = dynamic_pointer_cast<SubtitleString>(i);
-
                if (is) {
                        if (!text ||
                            last_h_align != is->h_align() ||
@@ -571,7 +570,13 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* xml_root, int time_code_rate, S
                        text->children.push_back (shared_ptr<order::String> (new order::String (text, order::Font (is, standard), is->text())));
                }
 
-               /* XXX: image */
+               shared_ptr<SubtitleImage> ii = dynamic_pointer_cast<SubtitleImage>(i);
+               if (ii) {
+                       text.reset ();
+                       subtitle->children.push_back (
+                               shared_ptr<order::Image> (new order::Image (subtitle, ii->png_image(), ii->h_align(), ii->h_position(), ii->v_align(), ii->v_position()))
+                               );
+               }
        }
 
        /* Pull font changes as high up the hierarchy as we can */
@@ -584,6 +589,7 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* xml_root, int time_code_rate, S
        context.time_code_rate = time_code_rate;
        context.standard = standard;
        context.spot_number = 1;
+       context.image_number = 0;
 
        root->write_xml (xml_root, context);
 }
index c525256d525d5a51a91bdb46c376c3142534e1f7..13bdf6ab1de5a8d3803414401a86e756b68bbbf6 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "subtitle_asset_internal.h"
 #include "subtitle_string.h"
+#include "compose.hpp"
 #include <cmath>
 
 using std::string;
@@ -145,38 +146,36 @@ order::Part::write_xml (xmlpp::Element* parent, order::Context& context) const
        }
 }
 
-xmlpp::Element*
-order::Text::as_xml (xmlpp::Element* parent, Context& context) const
+static void
+position_align (xmlpp::Element* e, order::Context& context, HAlign h_align, float h_position, VAlign v_align, float v_position)
 {
-       xmlpp::Element* e = parent->add_child ("Text", context.xmlns());
-
-       if (_h_align != HALIGN_CENTER) {
+       if (h_align != HALIGN_CENTER) {
                if (context.standard == SMPTE) {
-                       e->set_attribute ("Halign", halign_to_string (_h_align));
+                       e->set_attribute ("Halign", halign_to_string (h_align));
                } else {
-                       e->set_attribute ("HAlign", halign_to_string (_h_align));
+                       e->set_attribute ("HAlign", halign_to_string (h_align));
                }
        }
 
-       if (fabs(_h_position) > ALIGN_EPSILON) {
+       if (fabs(h_position) > ALIGN_EPSILON) {
                if (context.standard == SMPTE) {
-                       e->set_attribute ("Hposition", raw_convert<string> (_h_position * 100, 6));
+                       e->set_attribute ("Hposition", raw_convert<string> (h_position * 100, 6));
                } else {
-                       e->set_attribute ("HPosition", raw_convert<string> (_h_position * 100, 6));
+                       e->set_attribute ("HPosition", raw_convert<string> (h_position * 100, 6));
                }
        }
 
        if (context.standard == SMPTE) {
-               e->set_attribute ("Valign", valign_to_string (_v_align));
+               e->set_attribute ("Valign", valign_to_string (v_align));
        } else {
-               e->set_attribute ("VAlign", valign_to_string (_v_align));
+               e->set_attribute ("VAlign", valign_to_string (v_align));
        }
 
-       if (fabs(_v_position) > ALIGN_EPSILON) {
+       if (fabs(v_position) > ALIGN_EPSILON) {
                if (context.standard == SMPTE) {
-                       e->set_attribute ("Vposition", raw_convert<string> (_v_position * 100, 6));
+                       e->set_attribute ("Vposition", raw_convert<string> (v_position * 100, 6));
                } else {
-                       e->set_attribute ("VPosition", raw_convert<string> (_v_position * 100, 6));
+                       e->set_attribute ("VPosition", raw_convert<string> (v_position * 100, 6));
                }
        } else {
                if (context.standard == SMPTE) {
@@ -185,6 +184,14 @@ order::Text::as_xml (xmlpp::Element* parent, Context& context) const
                        e->set_attribute ("VPosition", "0");
                }
        }
+}
+
+xmlpp::Element*
+order::Text::as_xml (xmlpp::Element* parent, Context& context) const
+{
+       xmlpp::Element* e = parent->add_child ("Text", context.xmlns());
+
+       position_align (e, context, _h_align, _h_position, _v_align, _v_position);
 
        /* Interop only supports "horizontal" or "vertical" for direction, so only write this
           for SMPTE.
@@ -224,3 +231,20 @@ order::Font::clear ()
 {
        _values.clear ();
 }
+
+xmlpp::Element *
+order::Image::as_xml (xmlpp::Element* parent, Context& context) const
+{
+       xmlpp::Element* e = parent->add_child ("Image", context.xmlns());
+
+       position_align (e, context, _h_align, _h_position, _v_align, _v_position);
+       e->add_child_text (image_subtitle_file (context.image_number++));
+
+       return e;
+}
+
+string
+dcp::image_subtitle_file (int n)
+{
+       return String::compose ("sub_%1.png", n);
+}
index fad65a91f4d0ccaaea1debc23f95d1013323cecb..e2b1602e475fc255ac0ca82ec98b72663355f715 100644 (file)
@@ -37,6 +37,7 @@
 #include "raw_convert.h"
 #include "types.h"
 #include "dcp_time.h"
+#include "data.h"
 #include <libxml++/libxml++.h>
 #include <boost/foreach.hpp>
 
@@ -59,6 +60,7 @@ struct Context
        int time_code_rate;
        Standard standard;
        int spot_number;
+       int image_number;
 };
 
 class Font
@@ -163,7 +165,32 @@ private:
        Time _fade_down;
 };
 
+class Image : public Part
+{
+public:
+       Image (boost::shared_ptr<Part> parent, Data png_data, HAlign h_align, float h_position, VAlign v_align, float v_position)
+               : Part (parent)
+               , _png_data (png_data)
+               , _h_align (h_align)
+               , _h_position (h_position)
+               , _v_align (v_align)
+               , _v_position (v_position)
+       {}
+
+       xmlpp::Element* as_xml (xmlpp::Element* parent, Context& context) const;
+
+private:
+       Data _png_data;
+       HAlign _h_align;
+       float _h_position;
+       VAlign _v_align;
+       float _v_position;
+};
+
 }
+
+std::string image_subtitle_file (int n);
+
 }
 
 #endif
index 08e4da5dfe05df69b8ec54816fb8ab169fcba7ea..c274152b3bd992bc835fd4a32376b71a6f766e0a 100644 (file)
@@ -137,7 +137,7 @@ public:
                _text = t;
        }
 
-       void set_colour (dcp::Colour c) {
+       void set_colour (Colour c) {
                _colour = c;
        }
 
@@ -145,7 +145,7 @@ public:
                _effect = e;
        }
 
-       void set_effect_colour (dcp::Colour c) {
+       void set_effect_colour (Colour c) {
                _effect_colour = c;
        }
 
diff --git a/test/data/sub.png b/test/data/sub.png
new file mode 100644 (file)
index 0000000..89b53cf
Binary files /dev/null and b/test/data/sub.png differ
diff --git a/test/data/write_interop_subtitle_test3.xml b/test/data/write_interop_subtitle_test3.xml
new file mode 100644 (file)
index 0000000..e92685b
--- /dev/null
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<DCSubtitle Version="1.0"><SubtitleID>a6c58cff-3e1e-4b38-acec-a42224475ef6</SubtitleID><MovieTitle>Test</MovieTitle><ReelNumber>1</ReelNumber><Language>EN</Language><Subtitle SpotNumber="1" TimeIn="00:04:09:229" TimeOut="00:04:11:229" FadeUpTime="0" FadeDownTime="0"><Image VAlign="top" VPosition="80">sub_0.png</Image></Subtitle></DCSubtitle>
index c8f8fc57de10188a93d9d2acf0253f51f9b187c4..dec815c079810945661c4e4c7cfbad7605e3057e 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2015-2016 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2015-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of libdcp.
 
 #include "interop_subtitle_asset.h"
 #include "smpte_subtitle_asset.h"
 #include "subtitle_string.h"
+#include "subtitle_image.h"
 #include "subtitle_asset_internal.h"
 #include "test.h"
+#include "util.h"
 #include <boost/test/unit_test.hpp>
 
 using std::list;
@@ -307,6 +309,43 @@ BOOST_AUTO_TEST_CASE (write_interop_subtitle_test2)
                );
 }
 
+/* Write some subtitle content as Interop XML using bitmaps and check that it is right */
+BOOST_AUTO_TEST_CASE (write_interop_subtitle_test3)
+{
+       dcp::InteropSubtitleAsset c;
+       c.set_reel_number ("1");
+       c.set_language ("EN");
+       c.set_movie_title ("Test");
+
+       c.add (
+               shared_ptr<dcp::Subtitle> (
+                       new dcp::SubtitleImage (
+                               dcp::Data ("test/data/sub.png"),
+                               dcp::Time (0, 4,  9, 22, 24),
+                               dcp::Time (0, 4, 11, 22, 24),
+                               0,
+                               dcp::HALIGN_CENTER,
+                               0.8,
+                               dcp::VALIGN_TOP,
+                               dcp::Time (0, 0, 0, 0, 24),
+                               dcp::Time (0, 0, 0, 0, 24)
+                               )
+                       )
+               );
+
+       c._id = "a6c58cff-3e1e-4b38-acec-a42224475ef6";
+
+       boost::filesystem::create_directories ("build/test/write_interop_subtitle_test3");
+       c.write ("build/test/write_interop_subtitle_test3/subs.xml");
+
+       check_xml (
+               dcp::file_to_string("test/data/write_interop_subtitle_test3.xml"),
+               dcp::file_to_string("build/test/write_interop_subtitle_test3/subs.xml"),
+               list<string>()
+               );
+       check_file ("build/test/write_interop_subtitle_test3/sub_0.png", "test/data/sub.png");
+}
+
 /* Write some subtitle content as SMPTE XML and check that it is right */
 BOOST_AUTO_TEST_CASE (write_smpte_subtitle_test)
 {