File: image_data.hpp

package info (click to toggle)
gamera 1%3A3.4.2%2Bgit20160808.1725654-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,312 kB
  • ctags: 24,991
  • sloc: xml: 122,324; ansic: 52,869; cpp: 50,664; python: 35,034; makefile: 118; sh: 101
file content (228 lines) | stat: -rw-r--r-- 6,148 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 *
 * Copyright (C) 2001-2005 Ichiro Fujinaga, Michael Droettboom, and Karl MacMillan
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
  The ImageData class is dense storage for Gamera matrices. This class is used
  rather than a standard vector so that we can control the iterator type - the
  Vigra iterators assume that the iterator type is T* and some std::vectors
  don't use that as the iterator type.
*/

#ifndef kwm11162001_image_data_hpp
#define kwm11162001_image_data_hpp

#include "dimensions.hpp"

#include <cstddef>
#include <cmath>
#include <iostream>
#include <algorithm>

namespace Gamera {

  class ImageDataBase {
  public:

    ImageDataBase(const Dim& dim, const Point& offset) {
      m_size = (dim.nrows() * dim.ncols());
      m_stride = dim.ncols();
      m_page_offset_x = offset.x();
      m_page_offset_y = offset.y();
      m_user_data = 0;
    }

    ImageDataBase(const Dim& dim) {
      m_size = (dim.nrows() * dim.ncols());
      m_stride = dim.ncols();
      m_page_offset_x = 0;
      m_page_offset_y = 0;
      m_user_data = 0;
    }

    ImageDataBase(const Size& size, const Point& offset) {
      m_size = (size.height() + 1) * (size.width() + 1);
      m_stride = size.width() + 1;
      m_page_offset_x = offset.x();
      m_page_offset_y = offset.y();
      m_user_data = 0;
    }

    ImageDataBase(const Size& size) {
      m_size = (size.height() + 1) * (size.width() + 1);
      m_stride = size.width() + 1;
      m_page_offset_x = 0;
      m_page_offset_y = 0;
      m_user_data = 0;
    }

    ImageDataBase(const Rect& rect) {
      if (rect.nrows() < 1 || rect.ncols() < 1)
	throw std::range_error("nrows and ncols must be >= 1.");
      m_size = rect.nrows() * rect.ncols();
      m_stride = rect.ncols();
      m_page_offset_x = rect.ul_x();
      m_page_offset_y = rect.ul_y();
      m_user_data = 0;
    }

    virtual ~ImageDataBase() {
    }

    /*
      Various information about dimensions.
    */
    size_t stride() const { return m_stride; }
    size_t ncols() const { return m_stride; }
    size_t nrows() const { return size() / m_stride; }
    Dim dim() const { return Dim(m_stride, size() / m_stride); }
    size_t page_offset_x() const { return m_page_offset_x; }
    size_t page_offset_y() const { return m_page_offset_y; }
    Point offset() const { return Point(m_page_offset_x, m_page_offset_y); }
    size_t size() const { return m_size; }
    virtual size_t bytes() const = 0;
    virtual double mbytes() const = 0;

    /*
      Setting dimensions
    */
    void page_offset_x(size_t x) { m_page_offset_x = x; }
    void page_offset_y(size_t y) { m_page_offset_y = y; }
    virtual void nrows(size_t nrows) {
      do_resize(nrows * m_stride);
    }
    virtual void ncols(size_t ncols) {
      m_stride = ncols;
      do_resize((m_size / m_stride) * m_stride);
    }
    virtual void dimensions(size_t rows, size_t cols) = 0;
    virtual void dim(const Dim& dim) = 0;
  public:
    void* m_user_data;
  protected:
    virtual void do_resize(size_t size) = 0;
    size_t m_size;
    size_t m_stride;
    size_t m_page_offset_x;
    size_t m_page_offset_y;
  };

  template<class T>
  class ImageData : public ImageDataBase {
  public:
    /*
      Standard typedefs
    */
    typedef T value_type;
    typedef T& reference;
    typedef T* pointer;
    typedef int difference_type;
    typedef T* iterator;
    typedef const T* const_iterator;

    ImageData(const Dim& dim, const Point& offset) : 
      ImageDataBase(dim, offset) {
      m_data = 0;
      create_data();
    }

    ImageData(const Dim& dim) : 
      ImageDataBase(dim) {
      m_data = 0;
      create_data();
    }

    ImageData(const Size& size, const Point& offset) :
      ImageDataBase(size, offset) { 
      m_data = 0;
      create_data(); 
    }

    ImageData(const Size& size) : 
      ImageDataBase(size) { 
      m_data = 0;
      create_data();
    }

    ImageData(const Rect& rect) : 
      ImageDataBase(rect) { 
      m_data = 0;
      create_data();
    }

    /*
      Destructor
    */
    virtual ~ImageData() {
      if (m_data != 0) {
	delete[] m_data;
      }
    }
    
    virtual size_t bytes() const { return m_size * sizeof(T); }
    virtual double mbytes() const { return (m_size * sizeof(T)) / 1048576.0; }
    virtual void dimensions(size_t rows, size_t cols) {
      m_stride = cols; do_resize(rows * cols); }
    virtual void dim(const Dim& dim) {
      m_stride = dim.ncols(); do_resize(dim.nrows() * dim.ncols()); }
    virtual Dim dim() const {
      return Dim(m_stride, size() / m_stride);      
    }

    /*
      Iterators
    */
    iterator begin() { return m_data; }
    iterator end() { return m_data + m_size; }
    const_iterator begin() const { return m_data; }
    const_iterator end() const { return m_data + m_size; }

    /*
      Operators
    */
    T& operator[](size_t n) { return m_data[n]; }
  protected:
    virtual void do_resize(size_t size) {
      if (size > 0) {
	size_t smallest = std::min(m_size, size);
	m_size = size;
	T* new_data = new T[m_size];
	for (size_t i = 0; i < smallest; ++i)
	  new_data[i] = m_data[i];
	if (m_data)
	  delete[] m_data;
	m_data = new_data;
      } else {
	if (m_data)
	  delete[] m_data;
	m_data = 0;
	m_size = 0;
      }
    }
  private:
    void create_data() {
      if (m_size > 0)
	m_data = new T[m_size];
      std::fill(m_data, m_data + m_size, pixel_traits<T>::default_value());
    }

    T* m_data;
  };
}

#endif