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