Better subtitle comparisons in tests.
[libdcp.git] / src / subtitle_image.cc
1 /*
2     Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 /** @file  src/subtitle_image.cc
36  *  @brief SubtitleImage class
37  */
38
39
40 #include "subtitle_image.h"
41 #include "util.h"
42
43
44 using std::dynamic_pointer_cast;
45 using std::ostream;
46 using std::string;
47 using std::shared_ptr;
48 using namespace dcp;
49
50
51 SubtitleImage::SubtitleImage (
52         ArrayData png_image,
53         Time in,
54         Time out,
55         float h_position,
56         HAlign h_align,
57         float v_position,
58         VAlign v_align,
59         Time fade_up_time,
60         Time fade_down_time
61         )
62         : Subtitle (in, out, h_position, h_align, v_position, v_align, fade_up_time, fade_down_time)
63         , _png_image (png_image)
64         , _id (make_uuid ())
65 {
66
67 }
68
69
70 SubtitleImage::SubtitleImage (
71         ArrayData png_image,
72         string id,
73         Time in,
74         Time out,
75         float h_position,
76         HAlign h_align,
77         float v_position,
78         VAlign v_align,
79         Time fade_up_time,
80         Time fade_down_time
81         )
82         : Subtitle (in, out, h_position, h_align, v_position, v_align, fade_up_time, fade_down_time)
83         , _png_image (png_image)
84         , _id (id)
85 {
86
87 }
88
89
90 void
91 SubtitleImage::read_png_file (boost::filesystem::path file)
92 {
93         _file = file;
94         _png_image = ArrayData (file);
95 }
96
97
98 void
99 SubtitleImage::write_png_file (boost::filesystem::path file) const
100 {
101         _file = file;
102         png_image().write (file);
103 }
104
105
106 bool
107 dcp::operator== (SubtitleImage const & a, SubtitleImage const & b)
108 {
109         return (
110                 a.png_image() == b.png_image() &&
111                 a.id() == b.id() &&
112                 a.in() == b.in() &&
113                 a.out() == b.out() &&
114                 a.h_position() == b.h_position() &&
115                 a.h_align() == b.h_align() &&
116                 a.v_position() == b.v_position() &&
117                 a.v_align() == b.v_align() &&
118                 a.fade_up_time() == b.fade_up_time() &&
119                 a.fade_down_time() == b.fade_down_time()
120                 );
121 }
122
123
124 bool
125 dcp::operator!= (SubtitleImage const & a, SubtitleImage const & b)
126 {
127         return !(a == b);
128 }
129
130
131 bool
132 SubtitleImage::equals(shared_ptr<const Subtitle> other_sub, EqualityOptions options, NoteHandler note) const
133 {
134         if (!Subtitle::equals(other_sub, options, note)) {
135                 return false;
136         }
137
138         auto other = dynamic_pointer_cast<const SubtitleImage>(other_sub);
139         if (!other) {
140                 note(NoteType::ERROR, "Subtitle types differ: string vs image");
141                 return false;
142         }
143
144         if (png_image() != other->png_image()) {
145                 note (NoteType::ERROR, "subtitle image PNG data differs");
146                 if (options.export_differing_subtitles) {
147                         string const base = "dcpdiff_subtitle_";
148                         if (boost::filesystem::exists(base + "A.png")) {
149                                 note (NoteType::ERROR, "could not export subtitle as " + base + "A.png already exists");
150                         } else {
151                                 png_image().write(base + "A.png");
152                         }
153                         if (boost::filesystem::exists(base + "B.png")) {
154                                 note (NoteType::ERROR, "could not export subtitle as " + base + "B.png already exists");
155                         } else {
156                                 other->png_image().write(base + "B.png");
157                         }
158                         options.export_differing_subtitles = false;
159                 }
160                 return false;
161         }
162
163         return true;
164 }
165
166
167 ostream&
168 dcp::operator<< (ostream& s, SubtitleImage const & sub)
169 {
170         s << "\n[IMAGE] from " << sub.in() << " to " << sub.out() << ";\n"
171           << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
172           << "v pos " << sub.v_position() << ", valign " << ((int) sub.v_align())
173           << ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align()) << "\n";
174
175         return s;
176 }
177