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