Merge branch '1.0' into 1.0-vob
[dcpomatic.git] / src / lib / image_content.cc
1 /*
2     Copyright (C) 2013 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                 sort (_paths.begin(), _paths.end());
49         }
50 }
51
52
53 ImageContent::ImageContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
54         : Content (f, node)
55         , VideoContent (f, node)
56 {
57         
58 }
59
60 string
61 ImageContent::summary () const
62 {
63         /* Get the string() here so that the name does not have quotes around it */
64         if (still ()) {
65                 return String::compose (_("%1 [still]"), path().filename().string());
66         }
67
68         return String::compose (_("%1 [moving images]"), path().filename().string());
69 }
70
71 string
72 ImageContent::technical_summary () const
73 {
74         string s = Content::technical_summary() + " - "
75                 + VideoContent::technical_summary() + " - ";
76
77         if (still ()) {
78                 s += _("still");
79         } else {
80                 s += _("moving");
81         }
82
83         return s;
84 }
85
86 void
87 ImageContent::as_xml (xmlpp::Node* node) const
88 {
89         node->add_child("Type")->add_child_text ("Image");
90         Content::as_xml (node);
91         VideoContent::as_xml (node);
92 }
93
94 void
95 ImageContent::examine (shared_ptr<Job> job)
96 {
97         job->sub (_("Computing digest"));
98         Content::examine (job);
99
100         shared_ptr<const Film> film = _film.lock ();
101         assert (film);
102         
103         shared_ptr<ImageExaminer> examiner (new ImageExaminer (film, shared_from_this(), job));
104
105         take_from_video_examiner (examiner);
106         set_video_length (examiner->video_length ());
107 }
108
109 void
110 ImageContent::set_video_length (VideoContent::Frame len)
111 {
112         {
113                 boost::mutex::scoped_lock lm (_mutex);
114                 _video_length = len;
115         }
116
117         signal_changed (ContentProperty::LENGTH);
118 }
119
120 Time
121 ImageContent::full_length () const
122 {
123         shared_ptr<const Film> film = _film.lock ();
124         assert (film);
125         
126         FrameRateConversion frc (video_frame_rate(), film->video_frame_rate ());
127         return video_length() * frc.factor() * TIME_HZ / video_frame_rate();
128 }
129
130 string
131 ImageContent::identifier () const
132 {
133         stringstream s;
134         s << VideoContent::identifier ();
135         s << "_" << video_length();
136         return s.str ();
137 }
138
139 bool
140 ImageContent::still () const
141 {
142         return number_of_paths() == 1;
143 }