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