Cleanup: pass EqualityOptions as const&
[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::shared_ptr;
47 using std::string;
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         float z_position,
60         Time fade_up_time,
61         Time fade_down_time
62         )
63         : Subtitle(in, out, h_position, h_align, v_position, v_align, z_position, fade_up_time, fade_down_time)
64         , _png_image (png_image)
65         , _id (make_uuid ())
66 {
67
68 }
69
70
71 SubtitleImage::SubtitleImage (
72         ArrayData png_image,
73         string id,
74         Time in,
75         Time out,
76         float h_position,
77         HAlign h_align,
78         float v_position,
79         VAlign v_align,
80         float z_position,
81         Time fade_up_time,
82         Time fade_down_time
83         )
84         : Subtitle(in, out, h_position, h_align, v_position, v_align, z_position, fade_up_time, fade_down_time)
85         , _png_image (png_image)
86         , _id (id)
87 {
88
89 }
90
91
92 void
93 SubtitleImage::read_png_file (boost::filesystem::path file)
94 {
95         _file = file;
96         _png_image = ArrayData (file);
97 }
98
99
100 void
101 SubtitleImage::write_png_file (boost::filesystem::path file) const
102 {
103         _file = file;
104         png_image().write (file);
105 }
106
107
108 bool
109 dcp::operator== (SubtitleImage const & a, SubtitleImage const & b)
110 {
111         return (
112                 a.png_image() == b.png_image() &&
113                 a.id() == b.id() &&
114                 a.in() == b.in() &&
115                 a.out() == b.out() &&
116                 a.h_position() == b.h_position() &&
117                 a.h_align() == b.h_align() &&
118                 a.v_position() == b.v_position() &&
119                 a.v_align() == b.v_align() &&
120                 a.z_position() == b.z_position() &&
121                 a.fade_up_time() == b.fade_up_time() &&
122                 a.fade_down_time() == b.fade_down_time()
123                 );
124 }
125
126
127 bool
128 dcp::operator!= (SubtitleImage const & a, SubtitleImage const & b)
129 {
130         return !(a == b);
131 }
132
133
134 bool
135 SubtitleImage::equals(shared_ptr<const Subtitle> other_sub, EqualityOptions const& options, NoteHandler note) const
136 {
137         if (!Subtitle::equals(other_sub, options, note)) {
138                 return false;
139         }
140
141         auto other = dynamic_pointer_cast<const SubtitleImage>(other_sub);
142         if (!other) {
143                 note(NoteType::ERROR, "Subtitle types differ: string vs image");
144                 return false;
145         }
146
147         if (png_image() != other->png_image()) {
148                 note (NoteType::ERROR, "subtitle image PNG data differs");
149                 if (options.export_differing_subtitles) {
150                         string const base = "dcpdiff_subtitle_";
151                         if (boost::filesystem::exists(base + "A.png")) {
152                                 note (NoteType::ERROR, "could not export subtitle as " + base + "A.png already exists");
153                         } else {
154                                 png_image().write(base + "A.png");
155                         }
156                         if (boost::filesystem::exists(base + "B.png")) {
157                                 note (NoteType::ERROR, "could not export subtitle as " + base + "B.png already exists");
158                         } else {
159                                 other->png_image().write(base + "B.png");
160                         }
161                 }
162                 return false;
163         }
164
165         return true;
166 }
167
168
169 ostream&
170 dcp::operator<< (ostream& s, SubtitleImage const & sub)
171 {
172         s << "\n[IMAGE] from " << sub.in() << " to " << sub.out() << ";\n"
173           << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
174           << "v pos " << sub.v_position() << ", valign " << ((int) sub.v_align())
175           << ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align())
176           << ", zpos " << sub.z_position() << "\n";
177
178         return s;
179 }
180