Bv2.1 7.2.3: Check that subtitle <StartTime> exists and is 0.
[libdcp.git] / src / subtitle_image.cc
1 /*
2     Copyright (C) 2018-2020 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 #include "subtitle_image.h"
35 #include "util.h"
36
37 using std::ostream;
38 using std::string;
39 using std::shared_ptr;
40 using namespace dcp;
41
42 SubtitleImage::SubtitleImage (
43         ArrayData png_image,
44         Time in,
45         Time out,
46         float h_position,
47         HAlign h_align,
48         float v_position,
49         VAlign v_align,
50         Time fade_up_time,
51         Time fade_down_time
52         )
53         : Subtitle (in, out, h_position, h_align, v_position, v_align, fade_up_time, fade_down_time)
54         , _png_image (png_image)
55         , _id (make_uuid ())
56 {
57
58 }
59
60 SubtitleImage::SubtitleImage (
61         ArrayData png_image,
62         string id,
63         Time in,
64         Time out,
65         float h_position,
66         HAlign h_align,
67         float v_position,
68         VAlign v_align,
69         Time fade_up_time,
70         Time fade_down_time
71         )
72         : Subtitle (in, out, h_position, h_align, v_position, v_align, fade_up_time, fade_down_time)
73         , _png_image (png_image)
74         , _id (id)
75 {
76
77 }
78
79 void
80 SubtitleImage::read_png_file (boost::filesystem::path file)
81 {
82         _file = file;
83         _png_image = ArrayData (file);
84 }
85
86 void
87 SubtitleImage::write_png_file (boost::filesystem::path file) const
88 {
89         _file = file;
90         png_image().write (file);
91 }
92
93 bool
94 dcp::operator== (SubtitleImage const & a, SubtitleImage const & b)
95 {
96         return (
97                 a.png_image() == b.png_image() &&
98                 a.id() == b.id() &&
99                 a.in() == b.in() &&
100                 a.out() == b.out() &&
101                 a.h_position() == b.h_position() &&
102                 a.h_align() == b.h_align() &&
103                 a.v_position() == b.v_position() &&
104                 a.v_align() == b.v_align() &&
105                 a.fade_up_time() == b.fade_up_time() &&
106                 a.fade_down_time() == b.fade_down_time()
107                 );
108 }
109
110 bool
111 dcp::operator!= (SubtitleImage const & a, SubtitleImage const & b)
112 {
113         return !(a == b);
114 }
115
116 bool
117 SubtitleImage::equals (shared_ptr<SubtitleImage> other, EqualityOptions options, NoteHandler note)
118 {
119         if (png_image() != other->png_image()) {
120                 note (DCP_ERROR, "subtitle image PNG data differs");
121                 if (options.export_differing_subtitles) {
122                         string const base = "dcpdiff_subtitle_";
123                         if (boost::filesystem::exists(base + "A.png")) {
124                                 note (DCP_ERROR, "could not export subtitle as " + base + "A.png already exists");
125                         } else {
126                                 png_image().write(base + "A.png");
127                         }
128                         if (boost::filesystem::exists(base + "B.png")) {
129                                 note (DCP_ERROR, "could not export subtitle as " + base + "B.png already exists");
130                         } else {
131                                 other->png_image().write(base + "B.png");
132                         }
133                         options.export_differing_subtitles = false;
134                 }
135                 return false;
136         }
137
138         if (in() != other->in()) {
139                 note (DCP_ERROR, "subtitle in times differ");
140                 return false;
141         }
142
143         if (out() != other->out()) {
144                 note (DCP_ERROR, "subtitle out times differ");
145                 return false;
146         }
147
148         if (h_position() != other->h_position()) {
149                 note (DCP_ERROR, "subtitle horizontal positions differ");
150                 return false;
151         }
152
153         if (h_align() != other->h_align()) {
154                 note (DCP_ERROR, "subtitle horizontal alignments differ");
155                 return false;
156         }
157
158         if (v_position() != other->v_position()) {
159                 note (DCP_ERROR, "subtitle vertical positions differ");
160                 return false;
161         }
162
163         if (v_align() != other->v_align()) {
164                 note (DCP_ERROR, "subtitle vertical alignments differ");
165                 return false;
166         }
167
168         if (fade_up_time() != other->fade_up_time()) {
169                 note (DCP_ERROR, "subtitle fade-up times differ");
170                 return false;
171         }
172
173         if (fade_down_time() != other->fade_down_time()) {
174                 note (DCP_ERROR, "subtitle fade-down times differ");
175                 return false;
176         }
177
178         return true;
179 }
180
181 ostream&
182 dcp::operator<< (ostream& s, SubtitleImage const & sub)
183 {
184         s << "\n[IMAGE] from " << sub.in() << " to " << sub.out() << ";\n"
185           << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
186           << "v pos " << sub.v_position() << ", valign " << ((int) sub.v_align())
187           << ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align()) << "\n";
188
189         return s;
190 }
191