VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvoverlay.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 3 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, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // gvoverlay.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gvoverlay.h"
23 #include "grglyph.h"
24 #include "grcolourspace.h"
25 #include "gdatetime.h"
26 #include "gstr.h"
27 #include "gdate.h"
28 #include "gtime.h"
29 #include <sstream>
30 
31 namespace
32 {
33  void make_dim( bool monochrome , unsigned char & r , unsigned char & g , unsigned char & b )
34  {
35  unsigned int y = monochrome ? r : Gr::ColourSpace::y_int(r,g,b) ;
36  unsigned int u = monochrome ? 0 : Gr::ColourSpace::u_int(r,g,b) ;
37  unsigned int v = monochrome ? 0 : Gr::ColourSpace::v_int(r,g,b) ;
38  y /= 2 ;
39  r = Gr::ColourSpace::r_int( y , u , v ) ;
40  g = Gr::ColourSpace::g_int( y , u , v ) ;
41  b = Gr::ColourSpace::b_int( y , u , v ) ;
42  }
43 }
44 
45 void Gv::Overlay::operator()( int offset_x , int offset_y , bool on )
46 {
47  int x = m_x0 + offset_x ;
48  int y = m_y0 + offset_y ;
49  if( x >= 0 && y >= 0 && x < m_data.dx() && y < m_data.dy() )
50  {
51  unsigned char r = m_data.r( x , y ) ;
52  unsigned char g = m_data.g( x , y ) ;
53  unsigned char b = m_data.b( x , y ) ;
54 
55  if( on )
56  r = g = b = 255U ;
57  else
58  make_dim( false , r , g , b ) ;
59 
60  m_data.rgb( x , y , r , g , b ) ;
61  }
62 }
63 
65  m_data(data)
66 {
67 }
68 
69 void Gv::Overlay::write( int x0 , int y0 , const std::string & format , const Gv::Timezone & tz )
70 {
71  std::string text = format ;
72 
74  G::Str::replaceAll( text , "%t" , timestamp(t,tz) ) ;
75  G::Str::replaceAll( text , "%z" , zone(tz) ) ;
76  G::Str::replaceAll( text , "%D" , date(t,tz) ) ;
77  G::Str::replaceAll( text , "%T" , time(t,tz) ) ;
78 
79  m_x0 = x0 ;
80  m_y0 = y0 ;
81  Gr::Glyph::output( text , *this ) ;
82 
83  int w = static_cast<int>(text.length()) * 9 ;
84  for( int x = 0 ; x < (w+1) ; x++ )
85  {
86  operator()( x , -1 , false ) ;
87  operator()( x , +8 , false ) ;
88  }
89  for( int y = -1 ; y < 8 ; y++ )
90  {
91  operator()( w , y , false ) ;
92  }
93 }
94 
95 void Gv::Overlay::timestamp( int x0 , int y0 , const Gv::Timezone & tz )
96 {
97  write( x0 , y0 , "%t" , tz ) ;
98 }
99 
100 std::string Gv::Overlay::timestamp( G::EpochTime t , const Gv::Timezone & tz )
101 {
102  return datetime(t,tz) + " " + zone(tz) ;
103 }
104 
105 std::string Gv::Overlay::zone( const Gv::Timezone & tz )
106 {
107  return tz.zero() ? std::string("UTC") : tz.str() ;
108 }
109 
110 std::string Gv::Overlay::date( G::EpochTime t , const Gv::Timezone & tz )
111 {
112  G::DateTime::BrokenDownTime tm = G::DateTime::utc( t ) ;
113  return G::Date(tm).string(G::Date::yyyy_mm_dd_slash) ;
114 }
115 
116 std::string Gv::Overlay::time( G::EpochTime t , const Gv::Timezone & tz )
117 {
118  G::DateTime::BrokenDownTime tm = G::DateTime::utc( t ) ;
119  return G::Time(tm).hhmmss(":") ;
120 }
121 
122 std::string Gv::Overlay::datetime( G::EpochTime t , const Gv::Timezone & tz )
123 {
124  return date(t,tz) + " " + time(t,tz) ;
125 }
126 
127 /// \file gvoverlay.cpp
static BrokenDownTime utc(EpochTime epoch_time)
Converts from epoch time to UTC broken-down-time.
Definition: gdatetime.cpp:123
std::string hhmmss(const char *sep=nullptr) const
Returns the hhmmss string.
Definition: gtime.cpp:80
int dx() const
Returns the width.
Definition: grimagedata.h:329
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
A holder for image data, having eight bits per sample and one or three channels.
Definition: grimagedata.h:46
A date (dd/mm/yyyy) class.
Definition: gdate.h:39
unsigned char v_int(unsigned char r, unsigned char g, unsigned char b)
A fast conversion from rgb to v.
unsigned char r(int x, int y) const
Returns the R-value for a point.
Definition: grimagedata.h:390
unsigned char b_int(unsigned char y, unsigned char u, unsigned char) g__noexcept
A fast conversion from yuv to b.
std::time_t seconds() const
Returns the offset as a signed number of seconds.
Definition: gvtimezone.cpp:63
A representation of a timezone.
Definition: gvtimezone.h:35
A simple time-of-day (hh/mm/ss) class.
Definition: gtime.h:38
unsigned char u_int(unsigned char r, unsigned char g, unsigned char b)
A fast conversion from rgb to u.
unsigned char g(int x, int y) const
Returns the G-value for a point. Returns the R-value if there is only one channel.
Definition: grimagedata.h:397
bool zero() const
Returns true for utc.
Definition: gvtimezone.cpp:58
static EpochTime now()
Returns the current epoch time.
std::string string(Format format=yyyy_mm_dd_slash) const
Returns a string representation of the date.
Definition: gdate.cpp:89
Overlay(Gr::ImageData &out)
Constructor.
Definition: gvoverlay.cpp:64
unsigned char g_int(unsigned char y, unsigned char u, unsigned char v) g__noexcept
A fast conversion from yuv to g.
unsigned char b(int x, int y) const
Returns the B-value for a point. Returns the R-value if there is only one channel.
Definition: grimagedata.h:404
static unsigned int replaceAll(std::string &s, const std::string &from, const std::string &to)
Does a global replace on string 's', replacing all occurances of sub-string 'from' with 'to'...
Definition: gstr.cpp:157
void write(int x, int y, const std::string &format, const Gv::Timezone &)
Writes the string at the given position, with text-substitution for "%t".
Definition: gvoverlay.cpp:69
unsigned char r_int(unsigned char y, unsigned char, unsigned char v) g__noexcept
A fast conversion from yuv to r.
unsigned char y_int(unsigned char r, unsigned char g, unsigned char b)
A fast conversion from rgb to y.
unsigned int rgb(int x, int y) const
Returns the colour of a pixel as rgb values packed into one integer.
Definition: grimagedata.h:411
void timestamp(int x, int y, const Gv::Timezone &)
Writes a timestamp at the given position.
Definition: gvoverlay.cpp:95
static void output(const std::string &s, Tout &out_functor)
Calls an (x,y,bool) functor for all the glyph points corresponding to the given line of text...
Definition: grglyph.h:67
int dy() const
Returns the height.
Definition: grimagedata.h:335