forked from mivp/s2volsurf
/
xrw2xrw.c
169 lines (148 loc) · 4.29 KB
/
xrw2xrw.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
/* xrw2xrw.c
*
* Copyright 2012 David G. Barnes
*
* This file is part of S2VOLSURF.
*
* S2VOLSURF 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.
*
* S2VOLSURF 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 S2VOLSURF. If not, see <http://www.gnu.org/licenses/>.
*
* We would appreciate it if research outcomes using S2VOLSURF would
* provide the following acknowledgement:
*
* "Three-dimensional visualisation was conducted with the S2PLOT
* progamming library"
*
* and a reference to
*
* D.G.Barnes, C.J.Fluke, P.D.Bourke & O.T.Parry, 2006, Publications
* of the Astronomical Society of Australia, 23(2), 82-93.
*
* $Id$
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <png.h>
#include "libxrw.c"
void usage(char *exename) {
fprintf(stderr, "usage: %s [options] -f xrwfilename -o xrwfilename\n", exename);
fprintf(stderr, "-t t1 t2 t3\t\t set edge trim (centred) along each axis (0 0 0) pre-stride\n");
fprintf(stderr, "-s s1 s2 s3\t\t set data averaging along each axis (1 1 1)\n");
fprintf(stderr, "-2 \t\t force dimensions to power-of-two\n");
fprintf(stderr, "-1G \t\t ensure image has fewer than 1G pixels\n");
}
int main(int argc, char *argv[]) {
#define FNAMELEN 400
char ifname[FNAMELEN+1];
int haveifname = 0;
char ofname[FNAMELEN+1];
int haveofname = 0;
int trim[3] = {0,0,0};
int stride[3] = {1,1,1}; //{3,3,1};
int pow2 = 0;
int max1g = 0;
if (argc < 5) {
usage(argv[0]);
return -1;
}
int ic = 1;
while (ic < argc) {
if (!strcmp(argv[ic], "-f")) {
strncpy(ifname, argv[++ic], FNAMELEN);
haveifname = 1;
} else if (!strcmp(argv[ic], "-o")) {
strncpy(ofname, argv[++ic], FNAMELEN);
haveofname = 1;
} else if (!strcmp(argv[ic], "-t")) {
trim[0] = atoi(argv[++ic]);
trim[1] = atoi(argv[++ic]);
trim[2] = atoi(argv[++ic]);
if (trim[0] < 0 || trim[1] < 1 || trim[2] < 1) {
fprintf(stderr, "trim must be at least 0 in each direction!\n");
exit(-1);
}
} else if (!strcmp(argv[ic], "-s")) {
stride[0] = atoi(argv[++ic]);
stride[1] = atoi(argv[++ic]);
stride[2] = atoi(argv[++ic]);
if (stride[0] < 1 || stride[1] < 1 || stride[2] < 1) {
fprintf(stderr, "stride must be at least 1 in each direction!\n");
exit(-1);
}
} else if (!strcmp(argv[ic], "-2")) {
pow2 = 1;
} else if (!strcmp(argv[ic], "-1G")) {
max1g = 1;
}
ic++;
}
if (!haveifname || !haveofname) {
usage(argv[0]);
return -1;
}
XRAW_STRUCT *xr = loadXraw(ifname);
if (!xr) {
fprintf(stderr, "Failed to open or read '%s'.\n", argv[1]);
return -1;
}
showXraw(xr);
fprintf(stdout, "- - - - - - - - - - - - - - - - - - - - - - - - -\n");
if (trim[0] + trim[1] + trim[2] > 1) {
XRAW_STRUCT *xrt = trimXraw(xr, trim);
if (xrt) {
deleteXraw(xr);
xr = xrt;
}
}
if (max1g) {
while (((long)xr->nx / (long)stride[0] *
(long)xr->ny / (long)stride[1] *
(long)xr->nz / (long)stride[2]) >
(long)1024 * (long)1024 * (long)1024) {
int maxidx = 0;
int maxn = xr->nx;
if (xr->ny/stride[1] > xr->nx/stride[0]) {
maxidx = 1;
maxn = xr->ny;
}
if (xr->nz/stride[2] > maxn/stride[maxidx]) {
maxidx = 2;
}
stride[maxidx] += 1;
}
}
fprintf(stderr, "converting...\n");
VOL_STRUCT *vol = Xraw2Xvol(xr, stride);
if (!vol) {
fprintf(stderr, "Failed to parse data volume.\n");
return -1;
}
showXvol(vol);
if (pow2) {
fprintf(stderr, "converting to pow2-format...\n");
VOL_STRUCT *vol2 = makePow2Xvol(vol);
vol = vol2;
}
fprintf(stderr, "converting...\n");
XRAW_STRUCT *xro = Xvol2Xraw(vol);
sprintf(xro->filename, ofname);
showXraw(xro);
fprintf(stderr, "writing...\n");
saveXraw(xro);
return 0;
}