Add an explicit exception for J2K decompression failures.
[libdcp.git] / src / interop_subtitle_asset.cc
index 7d3114c3915f3df09ab1cf38610256ad2d38a1cf..d250b752e9d1f0aa10d00a1398237073d8ab853c 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 "compose.hpp"
+#include "subtitle_image.h"
 #include <libxml++/libxml++.h>
 #include <boost/foreach.hpp>
+#include <boost/weak_ptr.hpp>
 #include <cmath>
 #include <cstdio>
 
@@ -63,7 +67,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 */
 
@@ -75,6 +79,13 @@ InteropSubtitleAsset::InteropSubtitleAsset (boost::filesystem::path file)
                        parse_subtitles (e, ps, optional<int>(), INTEROP);
                }
        }
+
+       BOOST_FOREACH (shared_ptr<Subtitle> i, _subtitles) {
+               shared_ptr<SubtitleImage> si = dynamic_pointer_cast<SubtitleImage>(i);
+               if (si) {
+                       si->read_png_file (file.parent_path() / String::compose("%1.png", si->id()));
+               }
+       }
 }
 
 InteropSubtitleAsset::InteropSubtitleAsset ()
@@ -174,6 +185,15 @@ InteropSubtitleAsset::write (boost::filesystem::path p) const
 
        _file = p;
 
+       /* Image subtitles */
+       BOOST_FOREACH (shared_ptr<dcp::Subtitle> i, _subtitles) {
+               shared_ptr<dcp::SubtitleImage> im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
+               if (im) {
+                       im->write_png_file(p.parent_path() / String::compose("%1.png", im->id()));
+               }
+       }
+
+       /* 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");
@@ -229,3 +249,31 @@ InteropSubtitleAsset::add_font_assets (list<shared_ptr<Asset> >& assets)
                assets.push_back (shared_ptr<FontAsset> (new FontAsset (i.uuid, i.file.get ())));
        }
 }
+
+void
+InteropSubtitleAsset::write_to_assetmap (xmlpp::Node* node, boost::filesystem::path root) const
+{
+       Asset::write_to_assetmap (node, root);
+
+       BOOST_FOREACH (shared_ptr<dcp::Subtitle> i, _subtitles) {
+               shared_ptr<dcp::SubtitleImage> im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
+               if (im) {
+                       DCP_ASSERT (im->file());
+                       write_file_to_assetmap (node, root, im->file().get(), im->id());
+               }
+       }
+}
+
+void
+InteropSubtitleAsset::add_to_pkl (shared_ptr<PKL> pkl, boost::filesystem::path root) const
+{
+       Asset::add_to_pkl (pkl, root);
+
+       BOOST_FOREACH (shared_ptr<dcp::Subtitle> i, _subtitles) {
+               shared_ptr<dcp::SubtitleImage> im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
+               if (im) {
+                       Data png_image = im->png_image ();
+                       pkl->add_asset (im->id(), optional<string>(), make_digest(png_image), png_image.size(), "image/png");
+               }
+       }
+}