Supporters update.
[dcpomatic.git] / src / lib / image_content.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "compose.hpp"
23 #include "exceptions.h"
24 #include "film.h"
25 #include "frame_rate_change.h"
26 #include "image_content.h"
27 #include "image_examiner.h"
28 #include "image_filename_sorter.h"
29 #include "job.h"
30 #include "util.h"
31 #include "video_content.h"
32 #include <libcxml/cxml.h>
33 #include <dcp/filesystem.h>
34 #include <libxml++/libxml++.h>
35 #include <iostream>
36
37 #include "i18n.h"
38
39
40 using std::cout;
41 using std::list;
42 using std::make_shared;
43 using std::shared_ptr;
44 using std::string;
45 using std::vector;
46 using namespace dcpomatic;
47
48
49 ImageContent::ImageContent (boost::filesystem::path p)
50 {
51         video = make_shared<VideoContent>(this);
52
53         if (dcp::filesystem::is_regular_file(p) && valid_image_file(p)) {
54                 add_path (p);
55         } else {
56                 _path_to_scan = p;
57         }
58
59         set_default_colour_conversion ();
60 }
61
62
63 ImageContent::ImageContent (cxml::ConstNodePtr node, int version)
64         : Content (node)
65 {
66         video = VideoContent::from_xml (this, node, version, VideoRange::FULL);
67 }
68
69
70 string
71 ImageContent::summary () const
72 {
73         string s = path_summary () + " ";
74         /* Get the string() here so that the name does not have quotes around it */
75         if (still ()) {
76                 s += _("[still]");
77         } else {
78                 s += _("[moving images]");
79         }
80
81         return s;
82 }
83
84
85 string
86 ImageContent::technical_summary () const
87 {
88         string s = Content::technical_summary() + " - "
89                 + video->technical_summary() + " - ";
90
91         if (still ()) {
92                 s += _("still");
93         } else {
94                 s += _("moving");
95         }
96
97         return s;
98 }
99
100
101 void
102 ImageContent::as_xml (xmlpp::Node* node, bool with_paths) const
103 {
104         node->add_child("Type")->add_child_text ("Image");
105         Content::as_xml (node, with_paths);
106
107         if (video) {
108                 video->as_xml (node);
109         }
110 }
111
112
113 void
114 ImageContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
115 {
116         if (_path_to_scan) {
117                 job->sub (_("Scanning image files"));
118                 vector<boost::filesystem::path> paths;
119                 int n = 0;
120                 for (auto i: dcp::filesystem::directory_iterator(*_path_to_scan)) {
121                         if (dcp::filesystem::is_regular_file(i.path()) && valid_image_file(i.path())) {
122                                 paths.push_back (i.path());
123                         }
124                         ++n;
125                         if ((n % 1000) == 0) {
126                                 job->set_progress_unknown ();
127                         }
128                 }
129
130                 if (paths.empty()) {
131                         throw FileError (_("No valid image files were found in the folder."), *_path_to_scan);
132                 }
133
134                 sort (paths.begin(), paths.end(), ImageFilenameSorter());
135                 set_paths (paths);
136         }
137
138         Content::examine (film, job);
139
140         auto examiner = make_shared<ImageExaminer>(film, shared_from_this(), job);
141         video->take_from_examiner(film, examiner);
142         set_default_colour_conversion ();
143 }
144
145
146 DCPTime
147 ImageContent::full_length (shared_ptr<const Film> film) const
148 {
149         FrameRateChange const frc (film, shared_from_this());
150         return DCPTime::from_frames (llrint(video->length_after_3d_combine() * frc.factor()), film->video_frame_rate());
151 }
152
153
154 DCPTime
155 ImageContent::approximate_length () const
156 {
157         return DCPTime::from_frames (video->length_after_3d_combine(), 24);
158 }
159
160
161 string
162 ImageContent::identifier () const
163 {
164         char buffer[256];
165         snprintf (buffer, sizeof(buffer), "%s_%s_%" PRId64, Content::identifier().c_str(), video->identifier().c_str(), video->length());
166         return buffer;
167 }
168
169
170 bool
171 ImageContent::still () const
172 {
173         return number_of_paths() == 1;
174 }
175
176
177 void
178 ImageContent::set_default_colour_conversion ()
179 {
180         for (auto i: paths()) {
181                 if (valid_j2k_file (i)) {
182                         /* We default to no colour conversion if we have JPEG2000 files */
183                         video->unset_colour_conversion ();
184                         return;
185                 }
186         }
187
188         bool const s = still ();
189
190         boost::mutex::scoped_lock lm (_mutex);
191
192         if (s) {
193                 video->set_colour_conversion (PresetColourConversion::from_id ("srgb").conversion);
194         } else {
195                 video->set_colour_conversion (PresetColourConversion::from_id ("rec709").conversion);
196         }
197 }
198
199
200 void
201 ImageContent::add_properties (shared_ptr<const Film> film, list<UserProperty>& p) const
202 {
203         Content::add_properties (film, p);
204         video->add_properties (p);
205 }