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