Cleanup: pass EqualityOptions as const&
[libdcp.git] / src / subtitle_image.cc
index e1f123bc916933f92ceaea1797847fbfbdf71923..f8f8b570293f2f893c6e1603df06847a839c3270 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
 
     This file is part of libdcp.
 
     files in the program, then also delete it here.
 */
 
+
+/** @file  src/subtitle_image.cc
+ *  @brief SubtitleImage class
+ */
+
+
 #include "subtitle_image.h"
 #include "util.h"
 
+
+using std::dynamic_pointer_cast;
 using std::ostream;
+using std::shared_ptr;
 using std::string;
 using namespace dcp;
 
+
 SubtitleImage::SubtitleImage (
-       Data png_image,
+       ArrayData png_image,
        Time in,
        Time out,
        float h_position,
        HAlign h_align,
        float v_position,
        VAlign v_align,
+       float z_position,
        Time fade_up_time,
        Time fade_down_time
        )
-       : Subtitle (in, out, h_position, h_align, v_position, v_align, fade_up_time, fade_down_time)
+       : Subtitle(in, out, h_position, h_align, v_position, v_align, z_position, fade_up_time, fade_down_time)
        , _png_image (png_image)
        , _id (make_uuid ())
 {
 
 }
 
+
 SubtitleImage::SubtitleImage (
-       Data png_image,
+       ArrayData png_image,
        string id,
        Time in,
        Time out,
@@ -65,23 +77,26 @@ SubtitleImage::SubtitleImage (
        HAlign h_align,
        float v_position,
        VAlign v_align,
+       float z_position,
        Time fade_up_time,
        Time fade_down_time
        )
-       : Subtitle (in, out, h_position, h_align, v_position, v_align, fade_up_time, fade_down_time)
+       : Subtitle(in, out, h_position, h_align, v_position, v_align, z_position, fade_up_time, fade_down_time)
        , _png_image (png_image)
        , _id (id)
 {
 
 }
 
+
 void
 SubtitleImage::read_png_file (boost::filesystem::path file)
 {
        _file = file;
-       _png_image = Data (file);
+       _png_image = ArrayData (file);
 }
 
+
 void
 SubtitleImage::write_png_file (boost::filesystem::path file) const
 {
@@ -89,6 +104,7 @@ SubtitleImage::write_png_file (boost::filesystem::path file) const
        png_image().write (file);
 }
 
+
 bool
 dcp::operator== (SubtitleImage const & a, SubtitleImage const & b)
 {
@@ -101,24 +117,64 @@ dcp::operator== (SubtitleImage const & a, SubtitleImage const & b)
                a.h_align() == b.h_align() &&
                a.v_position() == b.v_position() &&
                a.v_align() == b.v_align() &&
+               a.z_position() == b.z_position() &&
                a.fade_up_time() == b.fade_up_time() &&
                a.fade_down_time() == b.fade_down_time()
                );
 }
 
+
 bool
 dcp::operator!= (SubtitleImage const & a, SubtitleImage const & b)
 {
        return !(a == b);
 }
 
+
+bool
+SubtitleImage::equals(shared_ptr<const Subtitle> other_sub, EqualityOptions const& options, NoteHandler note) const
+{
+       if (!Subtitle::equals(other_sub, options, note)) {
+               return false;
+       }
+
+       auto other = dynamic_pointer_cast<const SubtitleImage>(other_sub);
+       if (!other) {
+               note(NoteType::ERROR, "Subtitle types differ: string vs image");
+               return false;
+       }
+
+       if (png_image() != other->png_image()) {
+               note (NoteType::ERROR, "subtitle image PNG data differs");
+               if (options.export_differing_subtitles) {
+                       string const base = "dcpdiff_subtitle_";
+                       if (boost::filesystem::exists(base + "A.png")) {
+                               note (NoteType::ERROR, "could not export subtitle as " + base + "A.png already exists");
+                       } else {
+                               png_image().write(base + "A.png");
+                       }
+                       if (boost::filesystem::exists(base + "B.png")) {
+                               note (NoteType::ERROR, "could not export subtitle as " + base + "B.png already exists");
+                       } else {
+                               other->png_image().write(base + "B.png");
+                       }
+               }
+               return false;
+       }
+
+       return true;
+}
+
+
 ostream&
 dcp::operator<< (ostream& s, SubtitleImage const & sub)
 {
        s << "\n[IMAGE] from " << sub.in() << " to " << sub.out() << ";\n"
          << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
          << "v pos " << sub.v_position() << ", valign " << ((int) sub.v_align())
-         << ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align()) << "\n";
+         << ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align())
+         << ", zpos " << sub.z_position() << "\n";
 
        return s;
 }
+