Merge master.
[dcpomatic.git] / src / lib / image_content.cc
1 /*
2     Copyright (C) 2013-2014 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 <libcxml/cxml.h>
21 #include "image_content.h"
22 #include "image_examiner.h"
23 #include "config.h"
24 #include "compose.hpp"
25 #include "film.h"
26 #include "job.h"
27
28 #include "i18n.h"
29
30 using std::string;
31 using std::cout;
32 using std::stringstream;
33 using boost::shared_ptr;
34
35 ImageContent::ImageContent (shared_ptr<const Film> f, boost::filesystem::path p)
36         : Content (f)
37         , VideoContent (f)
38 {
39         if (boost::filesystem::is_regular_file (p)) {
40                 _paths.push_back (p);
41         } else {
42                 for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
43                         if (boost::filesystem::is_regular_file (i->path()) && valid_image_file (i->path())) {
44                                 _paths.push_back (i->path ());
45                         }
46                 }
47
48                 if (_paths.empty()) {
49                         throw FileError (_("No valid image files were found in the folder."), p);
50                 }
51                                 
52                 sort (_paths.begin(), _paths.end());
53         }
54 }
55
56
57 ImageContent::ImageContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version)
58         : Content (f, node)
59         , VideoContent (f, node, version)
60 {
61         
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                 + VideoContent::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) const
95 {
96         node->add_child("Type")->add_child_text ("Image");
97         Content::as_xml (node);
98         VideoContent::as_xml (node);
99 }
100
101 void
102 ImageContent::examine (shared_ptr<Job> job)
103 {
104         job->sub (_("Computing digest"));
105         Content::examine (job);
106
107         shared_ptr<const Film> film = _film.lock ();
108         assert (film);
109         
110         shared_ptr<ImageExaminer> examiner (new ImageExaminer (film, shared_from_this(), job));
111
112         take_from_video_examiner (examiner);
113         set_video_length (examiner->video_length ());
114 }
115
116 void
117 ImageContent::set_video_length (ContentTime len)
118 {
119         {
120                 boost::mutex::scoped_lock lm (_mutex);
121                 _video_length = len;
122         }
123
124         signal_changed (ContentProperty::LENGTH);
125 }
126
127 DCPTime
128 ImageContent::full_length () const
129 {
130         shared_ptr<const Film> film = _film.lock ();
131         assert (film);
132         return DCPTime (video_length_after_3d_combine(), FrameRateChange (video_frame_rate(), film->video_frame_rate()));
133 }
134
135 string
136 ImageContent::identifier () const
137 {
138         stringstream s;
139         s << VideoContent::identifier ();
140         s << "_" << video_length().get();
141         return s.str ();
142 }
143
144 bool
145 ImageContent::still () const
146 {
147         return number_of_paths() == 1;
148 }
149
150 void
151 ImageContent::set_video_frame_rate (float r)
152 {
153         {
154                 boost::mutex::scoped_lock lm (_mutex);
155                 if (_video_frame_rate == r) {
156                         return;
157                 }
158                 
159                 _video_frame_rate = r;
160         }
161         
162         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
163 }
164