std::shared_ptr
[libsub.git] / src / dcp_reader.cc
1 /*
2     Copyright (C) 2014-2018 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 "dcp_reader.h"
21 #include "compose.hpp"
22 #include "exceptions.h"
23 #include <dcp/subtitle_string.h>
24 #include <dcp/interop_subtitle_asset.h>
25 #include <dcp/smpte_subtitle_asset.h>
26 #include <boost/foreach.hpp>
27 #include <boost/filesystem.hpp>
28
29 using std::list;
30 using std::cout;
31 using std::string;
32 using std::exception;
33 using std::shared_ptr;
34 using boost::optional;
35 using std::dynamic_pointer_cast;
36 using namespace sub;
37
38 static Time
39 dcp_to_sub_time (dcp::Time t)
40 {
41         return Time::from_hms (t.h, t.m, t.s, t.e * 1000.0 / t.tcr);
42 }
43
44 static Colour
45 dcp_to_sub_colour (dcp::Colour c)
46 {
47         return Colour (c.r / 255.0, c.g / 255.0, c.b / 255.0);
48 }
49
50 DCPReader::DCPReader (boost::filesystem::path file)
51 {
52         shared_ptr<dcp::SubtitleAsset> sc;
53         string interop_error;
54         string smpte_error;
55
56         try {
57                 sc.reset (new dcp::InteropSubtitleAsset (file));
58         } catch (exception& e) {
59                 interop_error = e.what ();
60         }
61
62         if (!sc) {
63                 try {
64                         sc.reset (new dcp::SMPTESubtitleAsset (file));
65                 } catch (exception& e) {
66                         smpte_error = e.what();
67                 }
68         }
69
70         if (!sc) {
71                 throw DCPError (String::compose ("Could not read subtitles (%1 / %2)", interop_error, smpte_error));
72         }
73
74
75         BOOST_FOREACH (shared_ptr<dcp::Subtitle> i, sc->subtitles ()) {
76
77                 /* We don't deal with image subs */
78                 shared_ptr<dcp::SubtitleString> is = dynamic_pointer_cast<dcp::SubtitleString>(i);
79                 if (!is) {
80                         continue;
81                 }
82
83                 RawSubtitle rs;
84                 rs.text = is->text ();
85                 rs.font = is->font ();
86                 rs.font_size = FontSize::from_proportional (is->size() / (72.0 * 11.0));
87
88                 switch (is->effect ()) {
89                 case dcp::BORDER:
90                         rs.effect = BORDER;
91                         break;
92                 case dcp::SHADOW:
93                         rs.effect = SHADOW;
94                         break;
95                 default:
96                         break;
97                 }
98
99                 rs.effect_colour = dcp_to_sub_colour (is->effect_colour());
100
101                 rs.colour = dcp_to_sub_colour (is->colour());
102                 rs.bold = is->bold ();
103                 rs.italic = is->italic ();
104                 rs.underline = is->underline ();
105
106                 switch (is->h_align()) {
107                 case dcp::HALIGN_LEFT:
108                         rs.horizontal_position.reference = LEFT_OF_SCREEN;
109                         break;
110                 case dcp::HALIGN_CENTER:
111                         rs.horizontal_position.reference = HORIZONTAL_CENTRE_OF_SCREEN;
112                         break;
113                 case dcp::HALIGN_RIGHT:
114                         rs.horizontal_position.reference = RIGHT_OF_SCREEN;
115                         break;
116                 }
117
118                 rs.vertical_position.proportional = is->v_position();
119                 switch (is->v_align()) {
120                 case dcp::VALIGN_TOP:
121                         rs.vertical_position.reference = TOP_OF_SCREEN;
122                         break;
123                 case dcp::VALIGN_CENTER:
124                         rs.vertical_position.reference = VERTICAL_CENTRE_OF_SCREEN;
125                         break;
126                 case dcp::VALIGN_BOTTOM:
127                         rs.vertical_position.reference = BOTTOM_OF_SCREEN;
128                         break;
129                 }
130
131                 rs.from = dcp_to_sub_time (is->in ());
132                 rs.to = dcp_to_sub_time (is->out ());
133
134                 rs.fade_up = dcp_to_sub_time (is->fade_up_time ());
135                 rs.fade_down = dcp_to_sub_time (is->fade_down_time ());
136
137                 _subs.push_back (rs);
138         }
139 }