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