-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharray_types.h
241 lines (151 loc) · 7.09 KB
/
array_types.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
/*
Copyright (C) 2014 Jerome Revaud
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 3 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, see <http://www.gnu.org/licenses/>
*/
#ifndef ___ARRAY_TYPES_H___
#define ___ARRAY_TYPES_H___
typedef unsigned char UBYTE;
typedef unsigned int UINT;
/************************
* 1D Array
Equivalences:
C/Python/numpy: array.shape = (tx,)
array[x] := array->pixels[x]
Matlab/Fortran: [1, tx] = size(array)
array(x, 1) := array->pixels[x-1]
*/
#define DEFINE_ARRAY(type) \
typedef struct { \
type* pixels; \
int tx; \
} type##_array;
DEFINE_ARRAY(UBYTE)
DEFINE_ARRAY(int)
DEFINE_ARRAY(UINT)
DEFINE_ARRAY(float)
#define ASSERT_ARRAY_ZEROS(arr) {int size=arr->tx; assert((arr->pixels[0]==0 && arr->pixels[size/2]==0 && arr->pixels[size-1]==0) || !"error: matrix " #arr "is supposed to be zeros");}
/************************
* 2D Image
Equivalences:
C/Python/numpy: array.shape = (ty, tx)
array[y, x] := array->pixels[x + y*tx]
Matlab/Fortran: [tx, ty] = size(array)
array(x, y) := array->pixels[(x-1) + (y-1)*tx]
*/
#define DEFINE_IMG(type) \
typedef struct { \
type* pixels;\
int tx,ty;\
} type##_image;
DEFINE_IMG(UBYTE)
DEFINE_IMG(int)
DEFINE_IMG(UINT)
DEFINE_IMG(float)
#define ASSERT_SAME_SIZE ASSERT_SAME_IMG_SIZE
#define ASSERT_IMG_SIZE ASSERT_SAME_IMG_SIZE
#define ASSERT_SAME_IMG_SIZE(im1,im2) if(im1 && im2) assert(im1->tx==im2->tx && im1->ty==im2->ty);
#define ASSERT_IMAGE_ZEROS
#define ASSERT_IMG_ZEROS(img) {int size=img->tx*img->ty; assert((img->pixels[0]==0 && img->pixels[size/2]==0 && img->pixels[size-1]==0) || !"error: matrix " #img "is supposed to be zeros");}
#define IMG_SIZE(img) (long((img)->tx)*(img)->ty)
/************************
* 3D Image = Cube (Z coordinates are contiguous)
Equivalences:
C/Python/numpy: array.shape = (ty, tx, tz)
array[y, x, z] := array->pixels[z + x*tz + y*tx*tz]
Matlab/Fortran: [tz, tx, ty] = size(array)
array(z, x, y) := array->pixels[(z-1) + (x-1)*tz + (y-1)*tx*tz]
*/
#define DEFINE_CUBE(type) \
typedef struct { \
type* pixels; \
int tx,ty,tz; \
} type##_cube;
DEFINE_CUBE(UBYTE)
DEFINE_CUBE(short)
DEFINE_CUBE(int)
DEFINE_CUBE(UINT)
DEFINE_CUBE(float)
#define ASSERT_SAME_CUBE_SIZE(im1, im2) \
if((im1) && (im2)) assert((im1)->tx==(im2)->tx && (im1)->ty==(im2)->ty && (im1)->tz==(im2)->tz);
#define ASSERT_CUBE_ZEROS(img) {int size=img->tx*img->ty*img->tz; assert((img->pixels[0]==0 && img->pixels[size/2]==0 && img->pixels[size-1]==0) || !"error: matrix " #img "is supposed to be zeros");}
#define CUBE_SIZE(cube) (long((cube)->tx)*(cube)->ty*(cube)->tz)
/************************
* 3D Image = concatenation of XY layers
Equivalences:
C/Python/numpy: array.shape = (tz, ty, tx)
array[z, y, x] := array->pixels[x + y*tx + z*tx*ty]
Matlab/Fortran: [tx, ty, tz] = size(array)
array(x, y, z) := array->pixels[(x-1) + (y-1)*tx + (z-1)*tx*ty]
*/
#define DEFINE_LAYERS(type) \
typedef struct { \
type* pixels; \
int tx,ty,tz; \
} type##_layers; \
DEFINE_LAYERS(UBYTE)
DEFINE_LAYERS(int)
DEFINE_LAYERS(UINT)
DEFINE_LAYERS(float)
#define ASSERT_SAME_LAYERS_SIZE(im1,im2) ASSERT_SAME_CUBE_SIZE(im1,im2)
#define ASSERT_LAYERS_ZEROS ASSERT_CUBE_ZEROS
#define LAYERS_SIZE(layers) CUBE_SIZE(layers)
/*****************
creation, reshaping macros
*/
#define empty_array(type,tx) ((type##_array){NEWA(type,long(tx)),tx})
#define empty_image(type,tx,ty) ((type##_image){NEWA(type,long(tx)*(ty)),tx,ty})
#define empty_cube(type,tx,ty,tz) ((type##_cube ){NEWA(type,long(tx)*(ty)*long(tz)),tx,ty,tz})
#define empty_layers(type,tx,ty,tz) ((type##_layers){NEWA(type,long(tx)*(ty)*(tz)),tx,ty,tz})
#define zeros_array(type,tx) ((type##_array){NEWAC(type,long(tx)),tx})
#define zeros_image(type,tx,ty) ((type##_image){NEWAC(type,long(tx)*(ty)),tx,ty})
#define zeros_cube(type,tx,ty,tz) ((type##_cube ){NEWAC(type,long(tx)*(ty)*(tz)),tx,ty,tz})
#define zeros_layers(type,tx,ty,tz) ((type##_layers){NEWAC(type,long(tx)*(ty)*(tz)),tx,ty,tz})
#define array_like(type,l) ((type##_array){NEWA(type,long((l)->tx)),(l)->tx})
#define image_like(type,l) ((type##_image){NEWA(type,long((l)->tx)*(l)->ty),(l)->tx,(l)->ty})
#define cube_like(type,l) ((type##_cube ){NEWA(type,long((l)->tx)*(l)->ty*(l)->tz),(l)->tx,(l)->ty,(l)->tz})
#define layers_like(type,l) ((type##_layers){NEWA(type,long((l)->tx)*(l)->ty*(l)->tz),(l)->tx,(l)->ty,(l)->tz})
#define reshape_xy(type, arr) ((type##_array){(arr)->pixels, (arr)->tx*(arr)->ty})
#define reshape_xyz(type, arr) ((type##_array){(arr)->pixels, (arr)->tx*(arr)->ty*(arr)->tz})
#define reshape_xy_z(type, arr) ((type##_image){(arr)->pixels, (arr)->tx*(arr)->ty, (arr)->tz})
#define reshape_z_xy(type, arr) ((type##_image){(arr)->pixels, (arr)->tz, (arr)->tx*(arr)->ty})
#define reshape_x_yz(type, arr) ((type##_image){(arr)->pixels, (arr)->tx, (arr)->ty*(arr)->tz})
#define free_image(img) if(img){free(img->pixels); free(img); img=NULL;}
#define free_cube(cube) free_image(cube)
#define free_layers(cube) free_cube(cube)
// debugging only
#include <stdio.h>
inline long hash_arr(char* ptr, int nb, bool show) {
long res = 0;
if(show) printf("hashing [");
for(int i=0; i<nb; i++) {
res = 1000003*res + ((UBYTE*)ptr)[i];
if(show) printf("%d, ",((UBYTE*)ptr)[i]);
res = (res>>17) | (res<<47);
}
if(show) printf("]\n");
return res;
}
#define H(arr,val) printf("hash(" #arr ") = %ld\n",val);
#define hash_array(arr) H(arr,hash_arr((char*)(arr)->pixels,(arr)->tx*sizeof(*(arr)->pixels),0))
#define hash_image(arr) H(arr,hash_arr((char*)(arr)->pixels,(arr)->tx*(arr)->ty*sizeof(*(arr)->pixels),0))
#define hash_cube(arr) H(arr,hash_arr((char*)(arr)->pixels,(arr)->tx*(arr)->ty*(arr)->tz*sizeof(*(arr)->pixels),0))
#define hash_layers(arr) hash_cube(arr)
inline void save_raw(const char* fname, int* shape, int ndim, char* ptr, int size) {
FILE* f = fopen(fname, "w");
fwrite( &ndim, sizeof(int), 1, f);
fwrite( shape, sizeof(int), ndim, f);
fwrite( ptr, sizeof(*ptr), size, f);
fclose(f);
}
#define save_cube(fname,cube) {int sh[3] = {(cube)->ty, (cube)->tx, (cube)->tz}; save_raw(fname, sh, 3, (char*)(cube)->pixels, sizeof(*(cube)->pixels)*CUBE_SIZE(cube));}
#define save_layers(fname,layers) {int sh[3] = {(layers)->tz, (layers)->ty, (layers)->tx}; save_raw(fname, sh, 3, (char*)(layers)->pixels, sizeof(*(layers)->pixels)*LAYERS_SIZE(layers));}
#endif