-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimtoASCII.c
149 lines (127 loc) · 3.84 KB
/
imtoASCII.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
/** @file imtoASCII.c
*/
#include "CommandLineInterface/CLIcore.h"
#include "COREMOD_memory/COREMOD_memory.h"
// ==========================================
// Forward declaration(s)
// ==========================================
errno_t IMAGE_FORMAT_im_to_ASCII(const char *__restrict IDname,
const char *__restrict foutname);
// ==========================================
// Command line interface wrapper function(s)
// ==========================================
static errno_t IMAGE_FORMAT_im_to_ASCII_cli()
{
if(CLI_checkarg(1, 4) + CLI_checkarg(2, 3) == 0)
{
IMAGE_FORMAT_im_to_ASCII(data.cmdargtoken[1].val.string,
data.cmdargtoken[2].val.string);
return RETURN_SUCCESS;
}
else
{
return RETURN_FAILURE;
}
}
// ==========================================
// Register CLI command(s)
// ==========================================
errno_t imtoASCII_addCLIcmd()
{
RegisterCLIcommand(
"im2ascii",
__FILE__,
IMAGE_FORMAT_im_to_ASCII_cli,
"convert image file to ASCII",
"<input image> <output ASCII file>",
"im2ascii im im.txt",
"int IMAGE_FORMAT_im_to_ASCII(const char *IDname, const char *fname)");
return RETURN_SUCCESS;
}
errno_t IMAGE_FORMAT_im_to_ASCII(const char *__restrict IDname,
const char *__restrict foutname)
{
long ii;
long k;
imageID ID;
FILE *fpout;
long naxis;
long *coord;
long npix;
ID = image_ID(IDname);
naxis = data.image[ID].md[0].naxis;
coord = (long *) malloc(sizeof(long) * naxis);
if(coord == NULL)
{
PRINT_ERROR("malloc returns NULL pointer");
abort();
}
npix = 1;
for(k = 0; k < naxis; k++)
{
npix *= data.image[ID].md[0].size[k];
coord[k] = 0;
}
printf("npix = %ld\n", npix);
fpout = fopen(foutname, "w");
for(ii = 0; ii < npix; ii++)
{
int kOK;
for(k = 0; k < naxis; k++)
{
fprintf(fpout, "%4ld ", coord[k]);
}
switch(data.image[ID].md[0].datatype)
{
case _DATATYPE_UINT8:
fprintf(fpout, " %5u\n", data.image[ID].array.UI8[ii]);
break;
case _DATATYPE_UINT16:
fprintf(fpout, " %5u\n", data.image[ID].array.UI16[ii]);
break;
case _DATATYPE_UINT32:
fprintf(fpout, " %u\n", data.image[ID].array.UI32[ii]);
break;
case _DATATYPE_UINT64:
fprintf(fpout, " %lu\n", data.image[ID].array.UI64[ii]);
break;
case _DATATYPE_INT8:
fprintf(fpout, " %5d\n", data.image[ID].array.SI8[ii]);
break;
case _DATATYPE_INT16:
fprintf(fpout, " %5d\n", data.image[ID].array.SI16[ii]);
break;
case _DATATYPE_INT32:
fprintf(fpout, " %5d\n", data.image[ID].array.SI32[ii]);
break;
case _DATATYPE_INT64:
fprintf(fpout, " %5ld\n", data.image[ID].array.SI64[ii]);
break;
case _DATATYPE_FLOAT:
fprintf(fpout, " %f\n", data.image[ID].array.F[ii]);
break;
case _DATATYPE_DOUBLE:
fprintf(fpout, " %lf\n", data.image[ID].array.D[ii]);
break;
}
coord[0]++;
k = 0;
kOK = 0;
while((kOK == 0) && (k < naxis))
{
if(coord[k] == data.image[ID].md[0].size[k])
{
coord[k] = 0;
coord[k + 1]++;
}
else
{
kOK = 1;
}
k++;
}
}
fclose(fpout);
free(coord);
return RETURN_SUCCESS;
}