-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataArray.h
More file actions
165 lines (136 loc) · 7.96 KB
/
DataArray.h
File metadata and controls
165 lines (136 loc) · 7.96 KB
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
#ifndef __DataArray_H
#define __DataArray_H
/*
* Copyright (c) 2010 Stephen Williams (steve@icarus.com)
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
# include <stdint.h>
# include <complex>
# include <vector>
# include <string>
# include <fitsio.h>
/*
* The DataArray is an abstract class that represents objects that can
* be interpreted as an N-dimensional data array.
*/
class DataArray {
public:
// These are image pixel types that the DataArray
// supports. Most have matching cfitsio definitions and a way
// to represent them in a FITS IMAGE HDU. There are a few that
// don't match anything, they are only used internally.
enum type_t {
// CFITSIO Equivilent FITS declaration
DT_VOID = 0,
DT_INT8 = SBYTE_IMG, // BITPIX= 8, BZERO = -128
DT_UINT8 = BYTE_IMG, // BITPIX= 8, BZERO = 0
DT_INT16 = SHORT_IMG, // BITPIX= 16, BZERO = 0
DT_UINT16 = USHORT_IMG, // BITPIX= 16, BZERO = 32768
DT_INT32 = LONG_IMG, // BITPIX= 32, BZERO = 0
DT_UINT32 = ULONG_IMG, // BITPIX= 32, BZERO = 2147483648
DT_INT64 = LONGLONG_IMG,// BITPIX= 64, BZERO = 0
DT_UINT64 = 65, // BITPIX= 64, BZERO = <very large>
DT_FLOAT32 = FLOAT_IMG, // BITPIX=-32
DT_FLOAT64 = DOUBLE_IMG, // BITPIX=-64
DT_DOUBLE = -65, // (Native double)
DT_COMPLEX = -66 // (Native double complex)
};
static type_t type_from_string(const std::string&str);
public:
DataArray() { }
virtual ~DataArray() =0;
// For classes that support it, this method sets the shape of
// the image. This should return false for read-only images,
// and it should return false if the type/dimensions can't be
// set on the particular instance.
virtual bool reconfig(const std::vector<long>&axes, DataArray::type_t type);
virtual std::vector<long> get_axes(void) const;
virtual type_t get_type(void) const { return DT_VOID; }
template <class T>
int set_line(const std::vector<long>&addr, long wid, const T*data,
const uint8_t*alpha =0);
template <class T>
int get_line(const std::vector<long>&addr, long wid, T*data,
int&has_alpha, uint8_t*alpha =0);
virtual int set_line_raw(const std::vector<long>&addr, long wid,
type_t type, const void*data,
const uint8_t*alpha =0);
virtual int get_line_raw(const std::vector<long>&addr, long wid,
type_t type, void*data,
int&has_alpha, uint8_t*alpha);
public:
// These are some convenient N-dimensional address
// manipulation functions.
static size_t get_pixel_count(const std::vector<long>&axes);
static std::vector<long> zero_addr(size_t axes);
// Increment the addr at the given axis within the ref axes
// dimensions. If the axis wraps, the next axis is
// incremented, and so on. If the address overflows, it wraps
// to pixel 0 and this function returns false.
static bool incr(std::vector<long>&addr, const std::vector<long>&ref, size_t axis);
static std::vector<long> add(const std::vector<long>&a, const std::vector<long>&b);
};
template <> inline int DataArray::set_line<int8_t>(const std::vector<long>&addr, long wid, const int8_t*data, const uint8_t*alpha)
{ return set_line_raw(addr, wid, DT_INT8, data, alpha); }
template <> inline int DataArray::set_line<int16_t>(const std::vector<long>&addr, long wid, const int16_t*data, const uint8_t*alpha)
{ return set_line_raw(addr, wid, DT_INT16, data, alpha); }
template <> inline int DataArray::set_line<int32_t>(const std::vector<long>&addr, long wid, const int32_t*data, const uint8_t*alpha)
{ return set_line_raw(addr, wid, DT_INT32, data, alpha); }
template <> inline int DataArray::set_line<int64_t>(const std::vector<long>&addr, long wid, const int64_t*data, const uint8_t*alpha)
{ return set_line_raw(addr, wid, DT_INT64, data, alpha); }
template <> inline int DataArray::set_line<uint8_t>(const std::vector<long>&addr, long wid, const uint8_t*data, const uint8_t*alpha)
{ return set_line_raw(addr, wid, DT_UINT8, data, alpha); }
template <> inline int DataArray::set_line<uint16_t>(const std::vector<long>&addr, long wid, const uint16_t*data, const uint8_t*alpha)
{ return set_line_raw(addr, wid, DT_UINT16, data, alpha); }
template <> inline int DataArray::set_line<uint32_t>(const std::vector<long>&addr, long wid, const uint32_t*data, const uint8_t*alpha)
{ return set_line_raw(addr, wid, DT_UINT32, data, alpha); }
template <> inline int DataArray::set_line<uint64_t>(const std::vector<long>&addr, long wid, const uint64_t*data, const uint8_t*alpha)
{ return set_line_raw(addr, wid, DT_UINT64, data, alpha); }
template <> inline int DataArray::set_line<double>(const std::vector<long>&addr, long wid, const double*data, const uint8_t*alpha)
{ return set_line_raw(addr, wid, DT_DOUBLE, data, alpha); }
template <> inline int DataArray::set_line< std::complex<double> >(const std::vector<long>&addr, long wid, const std::complex<double>*data, const uint8_t*alpha)
{ return set_line_raw(addr, wid, DT_COMPLEX, data, alpha); }
template <> inline int DataArray::get_line<int8_t>(const std::vector<long>&addr, long wid, int8_t*data, int&has_alpha, uint8_t*alpha)
{ return get_line_raw(addr, wid, DT_INT8, data, has_alpha, alpha); }
template <> inline int DataArray::get_line<int16_t>(const std::vector<long>&addr, long wid, int16_t*data, int&has_alpha, uint8_t*alpha)
{ return get_line_raw(addr, wid, DT_INT16, data, has_alpha, alpha); }
template <> inline int DataArray::get_line<int32_t>(const std::vector<long>&addr, long wid, int32_t*data, int&has_alpha, uint8_t*alpha)
{ return get_line_raw(addr, wid, DT_INT32, data, has_alpha, alpha); }
template <> inline int DataArray::get_line<int64_t>(const std::vector<long>&addr, long wid, int64_t*data, int&has_alpha, uint8_t*alpha)
{ return get_line_raw(addr, wid, DT_INT64, data, has_alpha, alpha); }
template <> inline int DataArray::get_line<uint8_t>(const std::vector<long>&addr, long wid, uint8_t*data, int&has_alpha, uint8_t*alpha)
{ return get_line_raw(addr, wid, DT_UINT8, data, has_alpha, alpha); }
template <> inline int DataArray::get_line<uint16_t>(const std::vector<long>&addr, long wid, uint16_t*data, int&has_alpha, uint8_t*alpha)
{ return get_line_raw(addr, wid, DT_UINT16, data, has_alpha, alpha); }
template <> inline int DataArray::get_line<uint32_t>(const std::vector<long>&addr, long wid, uint32_t*data, int&has_alpha, uint8_t*alpha)
{ return get_line_raw(addr, wid, DT_UINT32, data, has_alpha, alpha); }
template <> inline int DataArray::get_line<uint64_t>(const std::vector<long>&addr, long wid, uint64_t*data, int&has_alpha, uint8_t*alpha)
{ return get_line_raw(addr, wid, DT_UINT64, data, has_alpha, alpha); }
template <> inline int DataArray::get_line<double>(const std::vector<long>&addr, long wid, double*data, int&has_alpha, uint8_t*alpha)
{ return get_line_raw(addr, wid, DT_DOUBLE, data, has_alpha, alpha); }
class pixel_iterator {
public:
pixel_iterator(const std::vector<long>&ul, const std::vector<long>&lr);
~pixel_iterator();
void rewind() { addr_ = ul_; }
const std::vector<long>& value() const { return addr_; }
bool valid() const { return addr_.size() != 0; }
bool incr(size_t axis, long step =1);
private:
std::vector<long> ul_, addr_, lr_;
};
#endif