Support for conversions of things that have multiple representations.
authorCarl Hetherington <cth@carlh.net>
Wed, 5 Feb 2014 16:35:34 +0000 (16:35 +0000)
committerCarl Hetherington <cth@carlh.net>
Wed, 5 Feb 2014 16:35:34 +0000 (16:35 +0000)
src/convert_time.cc [new file with mode: 0644]
src/convert_time.h [new file with mode: 0644]
src/frame_time.h
src/metric_time.cc
src/metric_time.h
src/subtitle.cc
src/subtitle.h
src/wscript
test/time_test.cc [new file with mode: 0644]
test/wscript

diff --git a/src/convert_time.cc b/src/convert_time.cc
new file mode 100644 (file)
index 0000000..95cad64
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include "metric_time.h"
+#include "frame_time.h"
+#include "convert_time.h"
+
+using namespace sub;
+
+FrameTime
+sub::metric_to_frame (MetricTime t, float frames_per_second)
+{
+       return FrameTime (t.hours(), t.minutes(), t.seconds(), t.milliseconds() * frames_per_second / 1000);
+}
+
+MetricTime
+sub::frame_to_metric (FrameTime t, float frames_per_second)
+{
+       return MetricTime (t.hours(), t.minutes(), t.seconds(), t.frames() * 1000 / frames_per_second);
+}
diff --git a/src/convert_time.h b/src/convert_time.h
new file mode 100644 (file)
index 0000000..92ec737
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include "metric_time.h"
+#include "frame_time.h"
+
+namespace sub {
+
+FrameTime metric_to_frame (MetricTime, float frames_per_second);
+MetricTime frame_to_metric (FrameTime, float frames_per_second);
+
+}
index 045b8336a16b46904c470c0fdffe4e12b21c6e87..9443676ba9a45ca87bcdedde9342af256c52e6f4 100644 (file)
@@ -41,6 +41,22 @@ public:
                , _frames (f)
        {}
 
+       int hours () const {
+               return _hours;
+       }
+       
+       int minutes () const {
+               return _minutes;
+       }
+       
+       int seconds () const {
+               return _seconds;
+       }
+       
+       int frames () const {
+               return _frames;
+       }
+
        std::string timecode () const;
 
 private:
index 3fc78741723f970e74d62a99e1e29a83a130ce08..d505a9776be79f33278a2b50ba86b44787810695 100644 (file)
@@ -20,6 +20,7 @@
 #include "metric_time.h"
 #include "compose.hpp"
 #include <iostream>
+#include <cmath>
 
 using std::ostream;
 using std::string;
@@ -31,6 +32,30 @@ MetricTime::MetricTime (int h, int m, int s, int ms)
 
 }
 
+int
+MetricTime::hours () const
+{
+       return floor (_milliseconds / (3600 * 1000));
+}
+
+int
+MetricTime::minutes () const
+{
+       return int64_t (floor (_milliseconds / (60 * 1000))) % 60;
+}
+
+int
+MetricTime::seconds () const
+{
+       return int64_t (floor (_milliseconds / (1000))) % 3600;
+}
+
+int
+MetricTime::milliseconds () const
+{
+       return _milliseconds % (3600 * 1000);
+}
+
 bool
 sub::operator== (MetricTime const & a, MetricTime const & b)
 {
index 5a351766ea108bb5f8bf744936431ab8f6cda5d9..4cea7f793418125733787e0c2b0eeb2fe32205ff 100644 (file)
@@ -33,6 +33,11 @@ public:
        {}
 
        MetricTime (int h, int m, int s, int ms);
+
+       int hours () const;
+       int minutes () const;
+       int seconds () const;
+       int milliseconds () const;
                          
 private:
        friend bool operator== (MetricTime const & a, MetricTime const & b);
index d637ed969183ba597a6f5e4b5f65bfd9118079a2..26c4fd921470862e8aff6b701d2847a92b482bf8 100644 (file)
@@ -18,7 +18,9 @@
 */
 
 #include "subtitle.h"
+#include "convert_time.h"
 
+using std::list;
 using namespace sub;
 
 bool
@@ -34,3 +36,33 @@ sub::operator< (Subtitle const & a, Subtitle const & b)
 
        assert (false);
 }
+
+void
+sub::convert_font_sizes (list<Subtitle>& subs, int screen_height_in_points)
+{
+       for (list<Subtitle>::iterator i = subs.begin(); i != subs.end(); ++i) {
+               if (i->font_size.proportional) {
+                       i->font_size.points = i->font_size.proportional.get() * screen_height_in_points;
+               } else {
+                       i->font_size.proportional = float (i->font_size.points.get()) / screen_height_in_points;
+               }
+       }
+}
+
+void
+sub::convert_times (list<Subtitle>& subs, float frames_per_second)
+{
+       for (list<Subtitle>::iterator i = subs.begin(); i != subs.end(); ++i) {
+               if (i->from.frame) {
+                       i->from.metric = frame_to_metric (i->from.frame.get(), frames_per_second);
+               } else {
+                       i->from.frame = metric_to_frame (i->from.metric.get(), frames_per_second);
+               }
+
+               if (i->to.frame) {
+                       i->to.metric = frame_to_metric (i->to.frame.get(), frames_per_second);
+               } else {
+                       i->to.frame = metric_to_frame (i->to.metric.get(), frames_per_second);
+               }
+       }
+}
index b60b7f7adca2e8b98abacfc87bb06b1dc679b0ca..889f405d2d3e397d17214e3c80d4c63f14be7333 100644 (file)
@@ -27,6 +27,7 @@
 #include "effect.h"
 #include <boost/optional.hpp>
 #include <string>
+#include <list>
 
 namespace sub {
 
@@ -50,6 +51,8 @@ public:
                boost::optional<float> proportional;
                /** in points */
                boost::optional<int> points;
+
+               void convert ();
        } font_size;
 
        /** vertical position of the baseline of the text */
@@ -87,6 +90,9 @@ public:
 
 bool operator< (Subtitle const & a, Subtitle const & b);       
 
+void convert_font_sizes (std::list<Subtitle> &, int screen_height_in_points);
+void convert_times (std::list<Subtitle> &, float frames_per_second);
+       
 }
 
 #endif
index 11c7ac5eaf4f1d8a89adefe69022db956a2b3b24..7e91edb6d924a98e67d74d5aad3d7160a614fa61 100644 (file)
@@ -12,6 +12,7 @@ def build(bld):
     obj.export_includes = ['.']
     obj.source = """
                  colour.cc
+                 convert_time.cc
                  dcp_reader.cc
                  effect.cc
                  frame_time.cc
@@ -25,6 +26,7 @@ def build(bld):
 
     headers = """
               colour.h
+              convert_time.h
               dcp_reader.h
               effect.h
               frame_time.h
diff --git a/test/time_test.cc b/test/time_test.cc
new file mode 100644 (file)
index 0000000..a42fc9d
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include <boost/test/unit_test.hpp>
+#include "metric_time.h"
+#include "frame_time.h"
+#include "convert_time.h"
+
+/* Check time conversions */
+BOOST_AUTO_TEST_CASE (time_test)
+{
+       BOOST_CHECK_EQUAL (metric_to_frame (sub::MetricTime (3, 5, 7, 40), 25), sub::FrameTime (3, 5, 7, 1));
+       BOOST_CHECK_EQUAL (frame_to_metric (sub::FrameTime  (3, 5, 7, 1), 25), sub::MetricTime (3, 5, 7, 40));
+       BOOST_CHECK_EQUAL (metric_to_frame (sub::MetricTime (3, 5, 7, 120), 25), sub::FrameTime (3, 5, 7, 3));
+       BOOST_CHECK_EQUAL (frame_to_metric (sub::FrameTime  (3, 5, 7, 3), 25), sub::MetricTime (3, 5, 7, 120));
+}
+
index 2954352b1224f6c1b9096b29617f016787f1b676..96a7428d43bfc8ee5dea2d850974a96dd68b6d74 100644 (file)
@@ -19,6 +19,7 @@ def build(bld):
                  dcp_reader_test.cc
                  stl_reader_test.cc
                  stl_writer_test.cc
+                 time_test.cc
                  test.cc
                  """
     obj.target = 'tests'