libdcp
text_image.cc
Go to the documentation of this file.
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 
40 #include "equality_options.h"
41 #include "filesystem.h"
42 #include "text_image.h"
43 #include "util.h"
44 
45 
46 using std::dynamic_pointer_cast;
47 using std::ostream;
48 using std::shared_ptr;
49 using std::string;
50 using namespace dcp;
51 
52 
53 TextImage::TextImage(
54  ArrayData png_image,
55  Time in,
56  Time out,
57  float h_position,
58  HAlign h_align,
59  float v_position,
60  VAlign v_align,
61  float z_position,
62  Time fade_up_time,
63  Time fade_down_time
64  )
65  : Text(in, out, h_position, h_align, v_position, v_align, z_position, fade_up_time, fade_down_time)
66  , _png_image (png_image)
67  , _id (make_uuid ())
68 {
69 
70 }
71 
72 
73 TextImage::TextImage(
74  ArrayData png_image,
75  string id,
76  Time in,
77  Time out,
78  float h_position,
79  HAlign h_align,
80  float v_position,
81  VAlign v_align,
82  float z_position,
83  Time fade_up_time,
84  Time fade_down_time
85  )
86  : Text(in, out, h_position, h_align, v_position, v_align, z_position, fade_up_time, fade_down_time)
87  , _png_image (png_image)
88  , _id (id)
89 {
90 
91 }
92 
93 
94 void
95 TextImage::read_png_file(boost::filesystem::path file)
96 {
97  _file = file;
98  _png_image = ArrayData (file);
99 }
100 
101 
102 void
103 TextImage::write_png_file(boost::filesystem::path file) const
104 {
105  _file = file;
106  png_image().write (file);
107 }
108 
109 
110 bool
111 dcp::operator==(TextImage const & a, TextImage const & b)
112 {
113  return (
114  a.png_image() == b.png_image() &&
115  a.id() == b.id() &&
116  a.in() == b.in() &&
117  a.out() == b.out() &&
118  a.h_position() == b.h_position() &&
119  a.h_align() == b.h_align() &&
120  a.v_position() == b.v_position() &&
121  a.v_align() == b.v_align() &&
122  a.z_position() == b.z_position() &&
123  a.fade_up_time() == b.fade_up_time() &&
124  a.fade_down_time() == b.fade_down_time()
125  );
126 }
127 
128 
129 bool
130 dcp::operator!=(TextImage const & a, TextImage const & b)
131 {
132  return !(a == b);
133 }
134 
135 
136 bool
137 TextImage::equals(shared_ptr<const Text> other_sub, EqualityOptions const& options, NoteHandler note) const
138 {
139  if (!Text::equals(other_sub, options, note)) {
140  return false;
141  }
142 
143  auto other = dynamic_pointer_cast<const TextImage>(other_sub);
144  if (!other) {
145  note(NoteType::ERROR, "Text types differ: string vs image");
146  return false;
147  }
148 
149  if (png_image() != other->png_image()) {
150  note (NoteType::ERROR, "text image PNG data differs");
151  if (options.export_differing_texts) {
152  string const base = "dcpdiff_text_";
153  if (filesystem::exists(base + "A.png")) {
154  note (NoteType::ERROR, "could not export text as " + base + "A.png already exists");
155  } else {
156  png_image().write(base + "A.png");
157  }
158  if (filesystem::exists(base + "B.png")) {
159  note (NoteType::ERROR, "could not export text as " + base + "B.png already exists");
160  } else {
161  other->png_image().write(base + "B.png");
162  }
163  }
164  return false;
165  }
166 
167  return true;
168 }
169 
170 
171 ostream&
172 dcp::operator<<(ostream& s, TextImage const& text)
173 {
174  s << "\n[IMAGE] from " << text.in() << " to " << text.out() << ";\n"
175  << "fade up " << text.fade_up_time() << ", fade down " << text.fade_down_time() << ";\n"
176  << "v pos " << text.v_position() << ", valign " << ((int) text.v_align())
177  << ", hpos " << text.h_position() << ", halign " << ((int) text.h_align())
178  << ", zpos " << text.z_position() << "\n";
179 
180  return s;
181 }
182 
Class to hold an arbitrary block of data.
Definition: array_data.h:55
A class to describe what "equality" means for a particular test.
A bitmap subtitle or caption with all the associated attributes.
Definition: text_image.h:58
boost::optional< boost::filesystem::path > file() const
Definition: text_image.h:103
Definition: text.h:56
Time in() const
Definition: text.h:61
Time out() const
Definition: text.h:66
float v_position() const
Definition: text.h:82
A representation of time within a DCP.
Definition: dcp_time.h:73
Class to describe what equality means when calling Asset::equals().
Namespace for everything in libdcp.
Definition: array_data.h:50
HAlign
Definition: h_align.h:46
VAlign
Definition: v_align.h:46
TextImage class.
Utility methods and classes.