Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / image_content.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "image_content.h"
21 #include "image_examiner.h"
22 #include "compose.hpp"
23 #include "film.h"
24 #include "job.h"
25 #include "frame_rate_change.h"
26 #include "exceptions.h"
27 #include "safe_stringstream.h"
28 #include <libcxml/cxml.h>
29 #include <libxml++/libxml++.h>
30 #include <boost/foreach.hpp>
31 #include <iostream>
32
33 #include "i18n.h"
34
35 #include "image_filename_sorter.cc"
36
37 using std::string;
38 using std::cout;
39 using boost::shared_ptr;
40
41 ImageContent::ImageContent (shared_ptr<const Film> film, boost::filesystem::path p)
42         : Content (film)
43         , VideoContent (film)
44 {
45         if (boost::filesystem::is_regular_file (p) && valid_image_file (p)) {
46                 _paths.push_back (p);
47         } else {
48                 for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
49                         if (boost::filesystem::is_regular_file (i->path()) && valid_image_file (i->path())) {
50                                 _paths.push_back (i->path ());
51                         }
52                 }
53
54                 if (_paths.empty()) {
55                         throw FileError (_("No valid image files were found in the folder."), p);
56                 }
57
58                 sort (_paths.begin(), _paths.end(), ImageFilenameSorter ());
59         }
60 }
61
62
63 ImageContent::ImageContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
64         : Content (film, node)
65         , VideoContent (film, node, version)
66 {
67
68 }
69
70 string
71 ImageContent::summary () const
72 {
73         string s = path_summary () + " ";
74         /* Get the string() here so that the name does not have quotes around it */
75         if (still ()) {
76                 s += _("[still]");
77         } else {
78                 s += _("[moving images]");
79         }
80
81         return s;
82 }
83
84 string
85 ImageContent::technical_summary () const
86 {
87         string s = Content::technical_summary() + " - "
88                 + VideoContent::technical_summary() + " - ";
89
90         if (still ()) {
91                 s += _("still");
92         } else {
93                 s += _("moving");
94         }
95
96         return s;
97 }
98
99 void
100 ImageContent::as_xml (xmlpp::Node* node) const
101 {
102         node->add_child("Type")->add_child_text ("Image");
103         Content::as_xml (node);
104         VideoContent::as_xml (node);
105 }
106
107 void
108 ImageContent::examine (shared_ptr<Job> job)
109 {
110         Content::examine (job);
111
112         shared_ptr<const Film> film = _film.lock ();
113         DCPOMATIC_ASSERT (film);
114
115         shared_ptr<ImageExaminer> examiner (new ImageExaminer (film, shared_from_this(), job));
116         take_from_video_examiner (examiner);
117 }
118
119 void
120 ImageContent::set_video_length (Frame len)
121 {
122         {
123                 boost::mutex::scoped_lock lm (_mutex);
124                 _video_length = len;
125         }
126
127         signal_changed (ContentProperty::LENGTH);
128 }
129
130 DCPTime
131 ImageContent::full_length () const
132 {
133         shared_ptr<const Film> film = _film.lock ();
134         DCPOMATIC_ASSERT (film);
135         FrameRateChange const frc (video_frame_rate(), film->video_frame_rate());
136         return DCPTime::from_frames (llrint (video_length_after_3d_combine() * frc.factor ()), film->video_frame_rate ());
137 }
138
139 string
140 ImageContent::identifier () const
141 {
142         SafeStringStream s;
143         s << VideoContent::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_video_frame_rate (float r)
156 {
157         {
158                 boost::mutex::scoped_lock lm (_mutex);
159                 if (_video_frame_rate == r) {
160                         return;
161                 }
162
163                 _video_frame_rate = r;
164         }
165
166         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
167 }
168
169 void
170 ImageContent::set_default_colour_conversion ()
171 {
172         BOOST_FOREACH (boost::filesystem::path i, _paths) {
173                 if (valid_j2k_file (i)) {
174                         /* We default to no colour conversion if we have JPEG2000 files */
175                         unset_colour_conversion ();
176                         return;
177                 }
178         }
179
180         bool const s = still ();
181
182         boost::mutex::scoped_lock lm (_mutex);
183
184         if (s) {
185                 _colour_conversion = PresetColourConversion::from_id ("srgb").conversion;
186         } else {
187                 _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
188         }
189 }