-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.h
416 lines (331 loc) · 9.55 KB
/
image.h
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**************************************************************************/
/* Copyright 2009 Tim Day */
/* */
/* This file is part of Fracplanet */
/* */
/* Fracplanet 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 3 of the License, or */
/* (at your option) any later version. */
/* */
/* Fracplanet 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 Fracplanet. If not, see <http://www.gnu.org/licenses/>. */
/**************************************************************************/
/*! \file
\brief Interface (and implementation) for templated Image class.
*/
#ifndef _image_h_
#define _image_h_
#include "rgb.h"
class Progress;
//! Class to support specialisation for particular pixel formats
template <typename T> class PixelTraits
{
public:
typedef T ComputeType;
typedef T ScalarType;
static ScalarType scalar(const T& v) {return v;}
};
template<> class PixelTraits<uchar>
{
public:
typedef float ComputeType;
typedef uchar ScalarType;
static ScalarType scalar(const uchar& v) {return v;}
};
template<> class PixelTraits<ushort>
{
public:
typedef float ComputeType;
typedef ushort ScalarType;
static ScalarType scalar(const ushort& v) {return v;}
};
template<> class PixelTraits<ByteRGBA>
{
public:
typedef FloatRGBA ComputeType;
typedef float ScalarType;
static ScalarType scalar(const ByteRGBA& v) {return (static_cast<float>(v.r)+static_cast<float>(v.g)+static_cast<float>(v.b))/3.0f;}
};
//! Class for 2D raster images of a specified type.
/*! Assumes explicit instantiation.
NB The base type just refers to storage allocated elsewhere.
Used to be gratuitously implemented using boost multiarray type,
but that was too inefficient
This is still pretty bad byt gcc 4.1 seems to do a very nice job with -O2.
*/
template <typename T> class Raster
{
public:
typedef typename PixelTraits<T>::ComputeType ComputeType;
typedef typename PixelTraits<T>::ScalarType ScalarType;
static const ScalarType scalar(const T& v) {return PixelTraits<T>::scalar(v);}
Raster(uint w,uint h,uint p,T* d)
:_width(w)
,_height(h)
,_pitch(p)
,_data(d)
,_row_end(row_range(h),p)
,_const_row_end(row_range(h),p)
{}
virtual ~Raster()
{}
uint width() const
{
return _width;
}
uint height() const
{
return _height;
}
uint pitch() const
{
return _pitch;
}
bool contiguous() const
{
return (_width==_pitch);
}
uint contiguous_size() const
{
assert(contiguous());
return _width*_height;
}
T* contiguous_begin()
{
assert(contiguous());
return _data;
}
const T* contiguous_begin() const
{
assert(contiguous());
return _data;
}
T* contiguous_end()
{
assert(contiguous());
return _data+contiguous_size();
}
const T* contiguous_end() const
{
assert(contiguous());
return _data+contiguous_size();
}
T* row(uint r)
{
return _data+r*_pitch;
}
const T* row(uint r) const
{
return _data+r*_pitch;
}
boost::iterator_range<T*> row_range(uint r)
{
T*const it=row(r);
return boost::iterator_range<T*>(it,it+_width);
}
boost::iterator_range<const T*> row_range(uint r) const
{
const T*const it=row(r);
return boost::iterator_range<const T*>(it,it+_width);
}
class RowIterator : public std::iterator<std::forward_iterator_tag, boost::iterator_range<T*> >
{
public:
RowIterator(const boost::iterator_range<T*>& row, uint p)
:_row(row)
,_pitch(p)
{}
~RowIterator()
{}
RowIterator& operator=(const RowIterator& it)
{
_row=it._row;
assert(_pitch==it._pitch);
return (*this);
}
bool operator==(const RowIterator& it) const
{
return _row.begin()==it._row.begin();
}
bool operator!=(const RowIterator& it) const
{
return _row.begin()!=it._row.begin();
}
RowIterator& operator++()
{
_row=boost::iterator_range<T*>
(
_row.begin()+_pitch,
_row.end()+_pitch
);
return (*this);
}
RowIterator operator++(int)
{
RowIterator tmp(*this);
_row=boost::iterator_range<T*>
(
_row.begin()+_pitch,
_row.end()+_pitch
);
return tmp;
}
boost::iterator_range<T*>& operator*()
{
return _row;
}
boost::iterator_range<T*>* operator->()
{
return &_row;
}
private:
boost::iterator_range<T*> _row;
const uint _pitch;
};
RowIterator row_begin()
{
return RowIterator(row_range(0),_pitch);
}
RowIterator row_end()
{
return _row_end;
}
class ConstRowIterator : public std::iterator<std::forward_iterator_tag, boost::iterator_range<const T*> >
{
public:
ConstRowIterator(const boost::iterator_range<const T*>& row, uint p)
:_row(row)
,_pitch(p)
{}
~ConstRowIterator()
{}
ConstRowIterator& operator=(const ConstRowIterator& it)
{
_row=it._row;
assert(_pitch==it._pitch);
return (*this);
}
bool operator==(const ConstRowIterator& it) const
{
return _row.begin()==it._row.begin();
}
bool operator!=(const ConstRowIterator& it) const
{
return _row.begin()!=it._row.begin();
}
ConstRowIterator& operator++()
{
_row=boost::iterator_range<const T*>
(
_row.begin()+_pitch,
_row.end()+_pitch
);
return (*this);
}
ConstRowIterator operator++(int)
{
ConstRowIterator tmp(*this);
_row=boost::iterator_range<const T*>
(
_row.begin()+_pitch,
_row.end()+_pitch
);
return tmp;
}
boost::iterator_range<const T*>& operator*()
{
return _row;
}
boost::iterator_range<const T*>* operator->()
{
return &_row;
}
private:
boost::iterator_range<const T*> _row;
const uint _pitch;
};
ConstRowIterator row_begin() const
{
return ConstRowIterator(row_range(0),_pitch);
}
ConstRowIterator row_end() const
{
return _const_row_end;
}
//! Clear the image to a constant value.
void fill(const T& v);
const ScalarType maximum_scalar_pixel_value() const;
//! Fill a line segment on the given half-open range [x0,x1), interpolating between the two given values.
void scan(uint y,float x0,const ComputeType& v0,float x1,const ComputeType& v1);
//! Variant scan, interpolates between two values then process them through function before
template <typename V> void scan(uint y,float x0,const V& v0,float x1,const V& v1,const boost::function<ComputeType (const V&)>& fn);
bool write_ppmfile(const std::string&,Progress*) const;
bool write_pgmfile(const std::string&,Progress*) const;
private:
const uint _width;
const uint _height;
const uint _pitch;
T*const _data;
const RowIterator _row_end;
const ConstRowIterator _const_row_end;
};
/*! Scan x0 to x1 into image.
Pixel centres are at 0.5 , so x0=0.75 goes to pixel 1.
Rightmost pixel is at width()-0.5.
*/
template <typename T> inline void Raster<T>::scan(uint y,float x0,const ComputeType& v0,float x1,const ComputeType& v1)
{
assert(x0<=x1);
if (x1<0.5f || width()-0.5f<x0) return; // Early out for spans off edges
const int ix_min=static_cast<int>(std::max(0.0f ,ceilf(x0-0.5f)));
const int ix_max=static_cast<int>(std::min(width()-0.5f,floorf(x1-0.5f)));
const ComputeType kv((v1-v0)/(x1-x0));
T*const row_ptr=row(y);
for (int ix=ix_min;ix<=ix_max;ix++)
{
const ComputeType v(v0+kv*(ix+0.5f-x0));
row_ptr[ix]=static_cast<T>(v);
}
}
template <typename T> template <typename V> inline void Raster<T>::scan(uint y,float x0,const V& v0,float x1,const V& v1,const boost::function<ComputeType (const V&)>& fn)
{
assert(x0<=x1);
if (x1<0.5f || width()-0.5f<x0) return; // Early out for spans off edges
const int ix_min=static_cast<int>(std::max(0.0f ,ceilf(x0-0.5f)));
const int ix_max=static_cast<int>(std::min(width()-0.5f,floorf(x1-0.5f)));
const V kv((v1-v0)/(x1-x0));
T*const row_ptr=row(y);
for (int ix=ix_min;ix<=ix_max;ix++)
{
const ComputeType v(fn(v0+kv*(ix+0.5f-x0)));
row_ptr[ix]=static_cast<T>(v);
}
}
template <typename T> class ImageStorage
{
public:
ImageStorage(int n)
:_storage(new T[n])
{}
~ImageStorage()
{}
protected:
boost::scoped_array<T> _storage;
};
template <typename T> class Image : private ImageStorage<T>, public Raster<T>, public boost::noncopyable
{
public:
Image(uint w,uint h)
:ImageStorage<T>(w*h)
,Raster<T>(w,h,w,ImageStorage<T>::_storage.get())
{}
~Image()
{}
};
#endif