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