Merge master; fix destruction of Server; some test cleanups.
[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 "compose.hpp"
24 #include "film.h"
25 #include "job.h"
26 #include "frame_rate_change.h"
27 #include "exceptions.h"
28
29 #include "i18n.h"
30
31 using std::string;
32 using std::cout;
33 using std::stringstream;
34 using boost::shared_ptr;
35
36 ImageContent::ImageContent (shared_ptr<const Film> f, boost::filesystem::path p)
37         : Content (f)
38         , VideoContent (f)
39 {
40         if (boost::filesystem::is_regular_file (p)) {
41                 _paths.push_back (p);
42         } else {
43                 for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
44                         if (boost::filesystem::is_regular_file (i->path()) && valid_image_file (i->path())) {
45                                 _paths.push_back (i->path ());
46                         }
47                 }
48
49                 if (_paths.empty()) {
50                         throw FileError (_("No valid image files were found in the folder."), p);
51                 }
52                                 
53                 sort (_paths.begin(), _paths.end());
54         }
55 }
56
57
58 ImageContent::ImageContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version)
59         : Content (f, node)
60         , VideoContent (f, node, version)
61 {
62         
63 }
64
65 string
66 ImageContent::summary () const
67 {
68         string s = path_summary () + " ";
69         /* Get the string() here so that the name does not have quotes around it */
70         if (still ()) {
71                 s += _("[still]");
72         } else {
73                 s += _("[moving images]");
74         }
75
76         return s;
77 }
78
79 string
80 ImageContent::technical_summary () const
81 {
82         string s = Content::technical_summary() + " - "
83                 + VideoContent::technical_summary() + " - ";
84
85         if (still ()) {
86                 s += _("still");
87         } else {
88                 s += _("moving");
89         }
90
91         return s;
92 }
93
94 void
95 ImageContent::as_xml (xmlpp::Node* node) const
96 {
97         node->add_child("Type")->add_child_text ("Image");
98         Content::as_xml (node);
99         VideoContent::as_xml (node);
100 }
101
102 void
103 ImageContent::examine (shared_ptr<Job> job)
104 {
105         job->sub (_("Computing digest"));
106         Content::examine (job);
107
108         shared_ptr<const Film> film = _film.lock ();
109         assert (film);
110         
111         shared_ptr<ImageExaminer> examiner (new ImageExaminer (film, shared_from_this(), job));
112
113         take_from_video_examiner (examiner);
114         set_video_length (examiner->video_length ());
115 }
116
117 void
118 ImageContent::set_video_length (ContentTime len)
119 {
120         {
121                 boost::mutex::scoped_lock lm (_mutex);
122                 _video_length = len;
123         }
124
125         signal_changed (ContentProperty::LENGTH);
126 }
127
128 DCPTime
129 ImageContent::full_length () const
130 {
131         shared_ptr<const Film> film = _film.lock ();
132         assert (film);
133         return DCPTime (video_length_after_3d_combine(), FrameRateChange (video_frame_rate(), film->video_frame_rate()));
134 }
135
136 string
137 ImageContent::identifier () const
138 {
139         stringstream s;
140         s << VideoContent::identifier ();
141         s << "_" << video_length().get();
142         return s.str ();
143 }
144
145 bool
146 ImageContent::still () const
147 {
148         return number_of_paths() == 1;
149 }
150
151 void
152 ImageContent::set_video_frame_rate (float r)
153 {
154         {
155                 boost::mutex::scoped_lock lm (_mutex);
156                 if (_video_frame_rate == r) {
157                         return;
158                 }
159                 
160                 _video_frame_rate = r;
161         }
162         
163         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
164 }
165