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