-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexmap.c
218 lines (189 loc) · 6.69 KB
/
indexmap.c
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
/** @file indexmap.c
*/
#include "CommandLineInterface/CLIcore.h"
#include "COREMOD_memory/COREMOD_memory.h"
// ==========================================
// Forward declaration(s)
// ==========================================
imageID image_basic_indexmap(const char *__restrict ID_index_name,
const char *__restrict ID_values_name,
const char *__restrict IDout_name);
// ==========================================
// Command line interface wrapper function(s)
// ==========================================
static errno_t image_basic_indexmap_cli()
{
if(CLI_checkarg(1, 4) + CLI_checkarg(2, 4) + CLI_checkarg(3, 3) == 0)
{
image_basic_indexmap(data.cmdargtoken[1].val.string,
data.cmdargtoken[2].val.string,
data.cmdargtoken[3].val.string);
return CLICMD_SUCCESS;
}
else
{
return CLICMD_INVALID_ARG;
}
}
// ==========================================
// Register CLI command(s)
// ==========================================
errno_t __attribute__((cold)) indexmap_addCLIcmd()
{
RegisterCLIcommand("imindexmap",
__FILE__,
image_basic_indexmap_cli,
"map input values to output image unsing index map",
"imindexmap <indexmap> <values> <output>",
"imindexmap imap imval outmap",
"long image_basic_indexmap(char *ID_index_name, char "
"*ID_values_name, char *IDout_name)");
return RETURN_SUCCESS;
}
imageID image_basic_indexmap(const char *__restrict ID_index_name,
const char *__restrict ID_values_name,
const char *__restrict IDout_name)
{
imageID IDindex, IDvalues;
imageID IDout;
long xsize, ysize, xysize;
long val_xsize, val_ysize, val_xysize;
uint8_t datatype;
uint8_t val_datatype;
long ii, i;
IDindex = image_ID(ID_index_name);
IDvalues = image_ID(ID_values_name);
xsize = data.image[IDindex].md[0].size[0];
ysize = data.image[IDindex].md[0].size[1];
xysize = xsize * ysize;
datatype = data.image[IDindex].md[0].datatype;
val_xsize = data.image[IDvalues].md[0].size[0];
val_ysize = data.image[IDvalues].md[0].size[1];
val_xysize = val_xsize * val_ysize;
val_datatype = data.image[IDindex].md[0].datatype;
create_2Dimage_ID(IDout_name, xsize, ysize, &IDout);
if(val_datatype == _DATATYPE_FLOAT)
{
for(ii = 0; ii < xysize; ii++)
{
i = (long)(data.image[IDindex].array.F[ii] + 0.1);
if((i > -1) && (i < val_xysize))
{
data.image[IDout].array.F[ii] = data.image[IDvalues].array.F[i];
}
}
}
else
{
float *arrayf = (float *) malloc(sizeof(float) * val_xysize);
if(arrayf == NULL)
{
PRINT_ERROR("malloc returns NULL pointer");
abort();
}
for(i = 0; i < val_xysize; i++)
{
arrayf[i] = (float) data.image[IDvalues].array.D[i];
}
switch(datatype)
{
case _DATATYPE_DOUBLE:
for(ii = 0; ii < xysize; ii++)
{
i = (long)(data.image[IDindex].array.D[ii] + 0.1);
if((i > -1) && (i < val_xysize))
{
data.image[IDout].array.F[ii] = arrayf[i];
}
}
break;
case _DATATYPE_UINT8:
for(ii = 0; ii < xysize; ii++)
{
i = (long) data.image[IDindex].array.UI8[ii];
if((i > -1) && (i < val_xysize))
{
data.image[IDout].array.F[ii] = arrayf[i];
}
}
break;
case _DATATYPE_INT8:
for(ii = 0; ii < xysize; ii++)
{
i = (long) data.image[IDindex].array.SI8[ii];
if((i > -1) && (i < val_xysize))
{
data.image[IDout].array.F[ii] = arrayf[i];
}
}
break;
case _DATATYPE_UINT16:
for(ii = 0; ii < xysize; ii++)
{
i = (long) data.image[IDindex].array.UI16[ii];
if((i > -1) && (i < val_xysize))
{
data.image[IDout].array.F[ii] = arrayf[i];
}
}
break;
case _DATATYPE_INT16:
for(ii = 0; ii < xysize; ii++)
{
i = (long) data.image[IDindex].array.SI16[ii];
if((i > -1) && (i < val_xysize))
{
data.image[IDout].array.F[ii] = arrayf[i];
}
}
break;
case _DATATYPE_UINT32:
for(ii = 0; ii < xysize; ii++)
{
i = (long) data.image[IDindex].array.UI32[ii];
if((i > -1) && (i < val_xysize))
{
data.image[IDout].array.F[ii] = arrayf[i];
}
}
break;
case _DATATYPE_INT32:
for(ii = 0; ii < xysize; ii++)
{
i = (long) data.image[IDindex].array.SI32[ii];
if((i > -1) && (i < val_xysize))
{
data.image[IDout].array.F[ii] = arrayf[i];
}
}
break;
case _DATATYPE_UINT64:
for(ii = 0; ii < xysize; ii++)
{
i = (long) data.image[IDindex].array.UI64[ii];
if((i > -1) && (i < val_xysize))
{
data.image[IDout].array.F[ii] = arrayf[i];
}
}
break;
case _DATATYPE_INT64:
for(ii = 0; ii < xysize; ii++)
{
i = (long) data.image[IDindex].array.SI64[ii];
if((i > -1) && (i < val_xysize))
{
data.image[IDout].array.F[ii] = arrayf[i];
}
}
break;
default:
printf("ERROR: datatype not supported\n");
free(arrayf);
return EXIT_FAILURE;
break;
}
free(arrayf);
}
return (IDout);
}