VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvmask.h
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 /// \file gvmask.h
19 ///
20 
21 #ifndef GV_MASK__H
22 #define GV_MASK__H
23 
24 #include "gdef.h"
25 #include "gtime.h"
26 #include "grimagedata.h"
27 #include "grpnm.h"
28 #include <string>
29 #include <vector>
30 #include <istream>
31 
32 namespace Gv
33 {
34  class Mask ;
35 }
36 
37 /// \class Gv::Mask
38 /// Implements a binary mask over an image that can be edited by mouse actions,
39 /// and that can be stored on disk.
40 ///
41 class Gv::Mask
42 {
43 public:
44  Mask( int dx , int dy , const std::string & file = std::string() , bool create = false ) ;
45  ///< Constructor, optionally reading the mask from an existing
46  ///< file. If the file is a different size from the parameters
47  ///< then its contents are resampled to fit.
48 
49  bool empty() const ;
50  ///< Returns true if empty.
51 
52  void write( const G::Path & filename ) const ;
53  ///< Writes to file.
54 
55  bool update() ;
56  ///< Updates the mask from the file. Returns true if updated.
57 
58  void scale( int factor ) ;
59  ///< Scales down.
60 
61  void up( int x , int y , bool shift , bool control ) ;
62  ///< Called on mouse-up. Commits any down()/move() edits.
63 
64  void move( int x , int y ) ;
65  ///< Called on mouse-move. Modifies the current edit.
66 
67  void down( int x , int y , bool shift , bool control ) ;
68  ///< Called on mouse-down. Starts an edit.
69 
70  bool masked( int x , int y ) const ;
71  ///< Returns true if the pixel is masked.
72 
73  bool masked( size_t ) const ;
74  ///< Optimised overload, ignoring the current edit.
75 
76  G::EpochTime time() const ;
77  ///< Returns the timestamp on the mask file at
78  ///< construction, not affected by any calls
79  ///< to write().
80 
81 private:
82  Mask( const Mask & ) ;
83  void operator=( const Mask & ) ;
84  int clip_x( int ) const ;
85  int clip_y( int ) const ;
86  bool fill( std::vector<bool>& , int , int , int , int , bool ) ;
87  void sample( const Gr::ImageData & image_data , const Gr::PnmInfo & info ) ;
88  void findOrCreate( bool ) ;
89  void open( std::ifstream & file ) ;
90  Gr::PnmInfo read( std::ifstream & file , Gr::ImageData & image_data ) ;
91 
92 private:
93  int m_dx ;
94  int m_dy ;
95  G::Path m_path ;
96  bool m_empty ;
97  std::vector<bool> m_map ;
98  std::vector<bool> m_map_current ;
99  bool m_down ;
100  int m_down_x ;
101  int m_down_y ;
102  bool m_down_shift ;
103  int m_move_x ;
104  int m_move_y ;
105  G::EpochTime m_file_time ;
106 } ;
107 
108 inline
109 bool Gv::Mask::masked( size_t offset ) const
110 {
111  return m_map[offset] ;
112 }
113 
114 inline
115 bool Gv::Mask::masked( int x , int y ) const
116 {
117  const size_t offset = y * m_dx + x ;
118  if( m_down && m_down_shift )
119  return m_map[offset] && !m_map_current[offset] ;
120  else
121  return m_map[offset] || m_map_current[offset] ;
122 }
123 
124 #endif
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
void scale(int factor)
Scales down.
Implements a binary mask over an image that can be edited by mouse actions, and that can be stored on...
Definition: gvmask.h:41
void up(int x, int y, bool shift, bool control)
Called on mouse-up. Commits any down()/move() edits.
Definition: gvmask.cpp:205
void move(int x, int y)
Called on mouse-move. Modifies the current edit.
Definition: gvmask.cpp:179
bool empty() const
Returns true if empty.
Definition: gvmask.cpp:124
G::EpochTime time() const
Returns the timestamp on the mask file at construction, not affected by any calls to write()...
Definition: gvmask.cpp:129
bool update()
Updates the mask from the file. Returns true if updated.
Definition: gvmask.cpp:213
void write(const G::Path &filename) const
Writes to file.
Definition: gvmask.cpp:134
Mask(int dx, int dy, const std::string &file=std::string(), bool create=false)
Constructor, optionally reading the mask from an existing file.
Definition: gvmask.cpp:37
void down(int x, int y, bool shift, bool control)
Called on mouse-down. Starts an edit.
Definition: gvmask.cpp:171
A structure holding portable-anymap metadata.
Definition: grpnm.h:42
bool masked(int x, int y) const
Returns true if the pixel is masked.
Definition: gvmask.h:115
A Path object represents a file system path.
Definition: gpath.h:72