-
Notifications
You must be signed in to change notification settings - Fork 0
/
catimg.c
162 lines (127 loc) · 3.39 KB
/
catimg.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
/**
* Program which concatenates a series of images into a single volume.
*
* Author: Paul McCarthy <pauld.mccarthy@gmail.com>
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include "io/analyze75.h"
#include "util/startup.h"
#include "util/suffix.h"
#include "util/filesize.h"
/**
* Creates a new file which is the concatenation of the given input files.
*
* \return 0 on success, non-0 on failure.
*/
static uint8_t _concat(
char *output, /**< name of output file */
char **inputs, /**< names of input files */
uint16_t ninputs /**< number of input files */
);
/**
* Creates a header for the new concatenated image.
*/
static void _mk_hdr(
dsr_t *hdr, /**< place to put new header */
dsr_t *input_hdrs, /**< input headers */
uint16_t ninputs, /**< number of input headers */
float dimsz /**< size of new dimension */
);
int main (int argc, char *argv[]) {
char *output;
char **inputs;
dsr_t newhdr;
dsr_t *hdrs;
uint16_t i;
uint16_t ninputs;
float dimsz;
hdrs = NULL;
startup("catimg", argc, argv, NULL, NULL);
if (argc < 5) {
printf("usage: catimg output dimsz input input [input ...]\n");
goto fail;
}
ninputs = argc-3;
output = argv[1];
dimsz = atof(argv[2]);
inputs = argv+3;
hdrs = malloc(ninputs*sizeof(dsr_t));
if (hdrs == NULL) goto fail;
for (i = 0; i < ninputs; i++) {
if (analyze_load_hdr(inputs[i], hdrs+i)) {
printf("Couldn't load image %s\n", inputs[i]);
goto fail;
}
}
if (analyze_hdr_compat(ninputs, hdrs, 0)) {
printf("Headers aren't compatible\n");
goto fail;
}
_mk_hdr(&newhdr, hdrs, ninputs, dimsz);
if (_concat(output, inputs, ninputs)) {
printf("Cat failed, simple as that\n");
goto fail;
}
if (analyze_write_hdr(output, &newhdr)) {
printf("Write failed to %s\n", output);
goto fail;
}
free(hdrs);
return 0;
fail:
printf("Cat failed\n");
if (hdrs != NULL) free(hdrs);
return 1;
}
uint8_t _concat(char *filename, char **inputs, uint16_t ninputs) {
uint16_t i;
int size;
uint8_t *buf;
FILE *outf;
FILE *inf;
char *infname;
buf = NULL;
outf = NULL;
inf = NULL;
filename = set_suffix(filename, "img");
if (filename == NULL) goto fail;
outf = fopen(filename, "wb");
if (outf == NULL) goto fail;
for (i = 0; i < ninputs; i++, inf = NULL, buf = NULL) {
infname = set_suffix(inputs[i], "img");
if (infname == NULL) goto fail;
inf = fopen(infname, "rb");
if (inf == NULL) goto fail;
size = filesize(inf);
buf = malloc(size);
if (buf == NULL) goto fail;
if (fread( buf, 1, size, inf) != size) goto fail;
if (fwrite(buf, 1, size, outf) != size) goto fail;
fclose(inf);
free(buf);
free(infname);
infname = NULL;
inf = NULL;
}
fclose(outf);
free(filename);
return 0;
fail:
if (outf != NULL) fclose(outf);
if (inf != NULL) fclose(inf);
if (filename != NULL) free(filename);
if (buf != NULL) free(buf);
return 1;
}
void _mk_hdr(dsr_t *hdr, dsr_t *input_hdrs, uint16_t ninputs, float dimsz) {
uint8_t ndims;
memcpy(hdr, input_hdrs, sizeof(dsr_t));
ndims = analyze_num_dims(hdr);
hdr->dime.dim [0] = ndims+1;
hdr->dime.dim [ndims+1] = ninputs;
hdr->dime.pixdim[ndims+1] = dimsz;
}