VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvviewerwindow_ansi.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 // gvviewerwindow_ansi.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gvviewerwindow_ansi.h"
23 #include "gstr.h"
24 #include "genvironment.h"
25 #include "grcolourspace.h"
26 #include "grscaler.h"
27 #include "gassert.h"
28 #include <exception>
29 
30 namespace
31 {
32  void output( const char * p )
33  {
34  std::cout << p << std::flush ;
35  }
36  int env( const std::string & name )
37  {
38  std::string s = G::Environment::get( name , "" ) ;
39  if( s.empty() )
40  throw std::runtime_error( "no environment variable '"+name+"'; try \"export "+name+"\"" ) ;
41  if( !G::Str::isUInt(s) )
42  throw std::runtime_error( "invalid value of envionment variable '"+name+"'" ) ;
43  int result = static_cast<int>( G::Str::toUInt(s) ) ;
44  if( result <= 0 )
45  throw std::runtime_error( "invalid value of envionment variable '"+name+"'" ) ;
46  return result ;
47  }
48 }
49 
51  m_handler(handler) ,
52  m_config(config) ,
53  m_mask(dx,dy,m_config.m_mask_file)
54 {
55  output( "\033[2J\033[1;1H" ) ;
56 }
57 
59 {
60 }
61 
63 {
64 }
65 
66 void Gv::ViewerWindowAnsi::display( int data_dx , int data_dy , int data_channels , const char * data_p , size_t data_n )
67 {
68  output( "\033[2J\033[1;1H" ) ;
69  int dx = this->dx() ;
70  int dy = this->dy() ;
71 
72  std::string line ;
73  line.reserve( dx + 2 ) ;
74  int x = -1 ;
75  int y = -1 ;
76  for( Gr::Scaler y_scaler(dy,data_dy) ; !!y_scaler ; ++y_scaler )
77  {
78  int display_y = y_scaler.first() ;
79  if( display_y == y )
80  continue ;
81  y = display_y ;
82 
83  if( !line.empty() )
84  std::cout << line << std::flush ;
85 
86  line.clear() ;
87  for( Gr::Scaler x_scaler(dx,data_dx) ; !!x_scaler ; ++x_scaler )
88  {
89  int display_x = x_scaler.first() ;
90  if( display_x == x )
91  continue ;
92  x = display_x ;
93 
94  int data_x = x_scaler.second() ;
95  int data_y = y_scaler.second() ;
96 
97  int data_offset = data_y * data_dx + data_x ;
98  if( data_channels != 1 )
99  data_offset *= data_channels ;
100  const unsigned char * p = reinterpret_cast<const unsigned char *>(data_p+data_offset) ;
101 
102  unsigned int r = *p++ ;
103  unsigned int g = data_channels > 1 ? *p++ : r ;
104  unsigned int b = data_channels > 2 ? *p++ : r ;
105 
106  unsigned int luma = Gr::ColourSpace::yuv_int(Gr::ColourSpace::triple<unsigned char>(r,g,b)).y() ;
107  static const char * luma_map = " .,:;|(){}%%$@@##" ;
108 
109  bool masked = m_mask.masked(data_x,data_y) ;
110  char c = masked ? '_' : luma_map[(luma/16U)%16U] ;
111 
112  if( y == 0 && x < int(m_config.m_title.length()) )
113  line.append( 1U , m_config.m_title.at(x) ) ;
114  else
115  line.append( 1U , c ) ;
116  }
117  }
118  if( !line.empty() )
119  line.erase( line.begin() + (line.size()-1U) ) ;
120  std::cout << line << std::flush ;
121 }
122 
124 {
125  return env( "COLUMNS" ) ;
126 }
127 
129 {
130  return env( "LINES" ) ;
131 }
132 
133 /// \file gvviewerwindow_ansi.cpp
static std::string get(const std::string &name, const std::string &default_)
Returns the environment variable value or the given default.
virtual ~ViewerWindowAnsi()
Destructor.
virtual void display(int, int, int, const char *, size_t) override
Override from ViewerWindow.
A class that allows for iterating over one integer range while accessing values from another...
Definition: grscaler.h:89
ViewerWindowAnsi(ViewerWindowHandler &, ViewerWindowConfig, int dx, int dy)
Constructor.
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:450
virtual int dx() const override
Override from ViewerWindow.
virtual int dy() const override
Override from ViewerWindow.
A configuration structure for Gv::ViewerWindow.
virtual void init() override
Override from ViewerWindow.
triple< unsigned char > yuv_int(triple< unsigned char > rgb) g__noexcept
A fast conversion from rgb to yuv.
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception...
Definition: gstr.cpp:266
A callback interface for Gv::ViewerWindow.