| 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 |
#if HAVE_CONFIG_H |
| 64 |
#include "config.h" |
| 65 |
#endif |
| 66 |
|
| 67 |
#include <stdlib.h> |
| 68 |
#include <stdio.h> |
| 69 |
#include <string.h> |
| 70 |
#include <ctype.h> |
| 71 |
#include <math.h> |
| 72 |
#include <float.h> |
| 73 |
#include <minc.h> |
| 74 |
#include <ParseArgv.h> |
| 75 |
#include <time_stamp.h> |
| 76 |
#include <voxel_loop.h> |
| 77 |
|
| 78 |
#ifndef TRUE |
| 79 |
# define TRUE 1 |
| 80 |
# define FALSE 0 |
| 81 |
#endif |
| 82 |
|
| 83 |
#define DEFAULT_RANGE DBL_MAX |
| 84 |
#define NCOPTS_DEFAULT NC_VERBOSE | NC_FATAL |
| 85 |
#define INVALID_DATA (-DBL_MAX) |
| 86 |
|
| 87 |
|
| 88 |
typedef enum |
| 89 |
{CONV_DEFAULT, CONV_AVERAGE, CONV_MAGNITUDE, CONV_GREY, CONV_LINEAR} |
| 90 |
Conversion_Type; |
| 91 |
|
| 92 |
|
| 93 |
typedef struct { |
| 94 |
int numvalues; |
| 95 |
double *values; |
| 96 |
} Double_Array; |
| 97 |
|
| 98 |
|
| 99 |
typedef struct { |
| 100 |
Conversion_Type conversion_type; |
| 101 |
int num_coefficients; |
| 102 |
double *linear_coefficients; |
| 103 |
} Program_Data; |
| 104 |
|
| 105 |
|
| 106 |
static void do_makescalar(void *caller_data, long num_voxels, |
| 107 |
int input_num_buffers, int input_vector_length, |
| 108 |
double *input_data[], |
| 109 |
int output_num_buffers, int output_vector_length, |
| 110 |
double *output_data[], Loop_Info *loop_info); |
| 111 |
static int get_double_list(char *dst, char *key, char *nextarg); |
| 112 |
static long get_vector_length(int mincid); |
| 113 |
|
| 114 |
|
| 115 |
static int clobber = FALSE; |
| 116 |
#if MINC2 |
| 117 |
static int v2format = FALSE; |
| 118 |
#endif |
| 119 |
static int verbose = TRUE; |
| 120 |
static nc_type datatype = MI_ORIGINAL_TYPE; |
| 121 |
static int is_signed = FALSE; |
| 122 |
static double valid_range[2] = {0.0, 0.0}; |
| 123 |
static int buffer_size = 10 * 1024; |
| 124 |
static Conversion_Type conversion_type = CONV_DEFAULT; |
| 125 |
static Double_Array linear_coefficients = {0, NULL}; |
| 126 |
|
| 127 |
|
| 128 |
static ArgvInfo argTable[] = { |
| 129 |
#if MINC2 |
| 130 |
{"-2", ARGV_CONSTANT, (char *) TRUE, (char *) &v2format, |
| 131 |
"Produce a MINC 2.0 format output file."}, |
| 132 |
#endif |
| 133 |
{"-clobber", ARGV_CONSTANT, (char *) TRUE, (char *) &clobber, |
| 134 |
"Overwrite existing file."}, |
| 135 |
{"-noclobber", ARGV_CONSTANT, (char *) FALSE, (char *) &clobber, |
| 136 |
"Don't overwrite existing file (default)."}, |
| 137 |
{"-verbose", ARGV_CONSTANT, (char *) TRUE, (char *) &verbose, |
| 138 |
"Print out log messages (default)."}, |
| 139 |
{"-quiet", ARGV_CONSTANT, (char *) FALSE, (char *) &verbose, |
| 140 |
"Do not print out log messages."}, |
| 141 |
{"-buffer_size", ARGV_INT, (char *) 1, (char *) &buffer_size, |
| 142 |
"Set the internal buffer size (in kb)."}, |
| 143 |
{"-filetype", ARGV_CONSTANT, (char *) MI_ORIGINAL_TYPE, (char *) &datatype, |
| 144 |
"Use data type of first file (default)."}, |
| 145 |
{"-byte", ARGV_CONSTANT, (char *) NC_BYTE, (char *) &datatype, |
| 146 |
"Write out byte data."}, |
| 147 |
{"-short", ARGV_CONSTANT, (char *) NC_SHORT, (char *) &datatype, |
| 148 |
"Write out short integer data."}, |
| 149 |
{"-int", ARGV_CONSTANT, (char *) NC_INT, (char *) &datatype, |
| 150 |
"Write out 32-bit integer data."}, |
| 151 |
{"-long", ARGV_CONSTANT, (char *) NC_INT, (char *) &datatype, |
| 152 |
"Superseded by -int."}, |
| 153 |
{"-float", ARGV_CONSTANT, (char *) NC_FLOAT, (char *) &datatype, |
| 154 |
"Write out single-precision floating-point data."}, |
| 155 |
{"-double", ARGV_CONSTANT, (char *) NC_DOUBLE, (char *) &datatype, |
| 156 |
"Write out double-precision floating-point data."}, |
| 157 |
{"-signed", ARGV_CONSTANT, (char *) TRUE, (char *) &is_signed, |
| 158 |
"Write signed integer data."}, |
| 159 |
{"-unsigned", ARGV_CONSTANT, (char *) FALSE, (char *) &is_signed, |
| 160 |
"Write unsigned integer data (default if type specified)."}, |
| 161 |
{"-valid_range", ARGV_FLOAT, (char *) 2, (char *) valid_range, |
| 162 |
"Valid range for output data."}, |
| 163 |
{"-magnitude", ARGV_CONSTANT, (char *) CONV_MAGNITUDE, |
| 164 |
(char *) &conversion_type, |
| 165 |
"Compute magnitude of vectors (default)."}, |
| 166 |
{"-average", ARGV_CONSTANT, (char *) CONV_AVERAGE, |
| 167 |
(char *) &conversion_type, |
| 168 |
"Average components of vectors."}, |
| 169 |
{"-rgbtogrey", ARGV_CONSTANT, (char *) CONV_GREY, (char *) &conversion_type, |
| 170 |
"Convert RGB to greyscale."}, |
| 171 |
{"-rgbtogray", ARGV_CONSTANT, (char *) CONV_GREY, (char *) &conversion_type, |
| 172 |
"Synonym for rgbtogrey."}, |
| 173 |
{"-linear", ARGV_FUNC, (char *) get_double_list, |
| 174 |
(char *) &linear_coefficients, |
| 175 |
"Specify comma-separated list of coefficients for linear combination."}, |
| 176 |
{NULL, ARGV_END, NULL, NULL, NULL} |
| 177 |
}; |
| 178 |
|
| 179 |
static const char str_wrong_dimension_order[] = { |
| 180 |
"Your input file contains an image with a vector dimension, but the\n" |
| 181 |
"vector dimension isn't the last (i.e. fastest-varying) dimension in\n" |
| 182 |
"the image. Please restructure the file using mincreshape before\n" |
| 183 |
"attempting to use mincmakescalar.\n" |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
int main(int argc, char *argv[]) |
| 189 |
{ |
| 190 |
char *infile, *outfile; |
| 191 |
char *arg_string; |
| 192 |
int inmincid; |
| 193 |
int input_vector_length; |
| 194 |
int ivalue; |
| 195 |
Loop_Options *loop_options; |
| 196 |
Program_Data program_data; |
| 197 |
|
| 198 |
|
| 199 |
arg_string = time_stamp(argc, argv); |
| 200 |
|
| 201 |
|
| 202 |
if (ParseArgv(&argc, argv, argTable, 0) || (argc != 3)) { |
| 203 |
(void) fprintf(stderr, "\nUsage: %s [options] <in.mnc> <out.mnc>\n", |
| 204 |
argv[0]); |
| 205 |
(void) fprintf(stderr, " %s -help\n\n", |
| 206 |
argv[0]); |
| 207 |
exit(EXIT_FAILURE); |
| 208 |
} |
| 209 |
infile = argv[1]; |
| 210 |
outfile = argv[2]; |
| 211 |
|
| 212 |
|
| 213 |
if ((conversion_type != CONV_DEFAULT) && |
| 214 |
(linear_coefficients.numvalues > 0)) { |
| 215 |
(void) fprintf(stderr, |
| 216 |
"Do not specify -linear with other conversion options\n"); |
| 217 |
exit(EXIT_FAILURE); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
if (conversion_type == CONV_DEFAULT) conversion_type = CONV_MAGNITUDE; |
| 222 |
program_data.conversion_type = conversion_type; |
| 223 |
program_data.num_coefficients = 0; |
| 224 |
program_data.linear_coefficients = NULL; |
| 225 |
|
| 226 |
|
| 227 |
if (linear_coefficients.numvalues > 0) { |
| 228 |
conversion_type = CONV_LINEAR; |
| 229 |
program_data.conversion_type = conversion_type; |
| 230 |
program_data.num_coefficients = linear_coefficients.numvalues; |
| 231 |
program_data.linear_coefficients = |
| 232 |
malloc(linear_coefficients.numvalues * |
| 233 |
sizeof(*program_data.linear_coefficients)); |
| 234 |
for (ivalue=0; ivalue < linear_coefficients.numvalues; ivalue++) { |
| 235 |
program_data.linear_coefficients[ivalue] = |
| 236 |
linear_coefficients.values[ivalue]; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
inmincid = miopen(infile, NC_NOWRITE); |
| 242 |
input_vector_length = get_vector_length(inmincid); |
| 243 |
if (input_vector_length < 0) { |
| 244 |
fprintf(stderr, str_wrong_dimension_order); |
| 245 |
exit(EXIT_FAILURE); |
| 246 |
} |
| 247 |
if (input_vector_length < 1) input_vector_length = 1; |
| 248 |
|
| 249 |
|
| 250 |
if ((conversion_type == CONV_GREY) && |
| 251 |
(input_vector_length != 3) && (input_vector_length > 1)) { |
| 252 |
(void) fprintf(stderr, "Input file does not contain RGB data\n"); |
| 253 |
exit(EXIT_FAILURE); |
| 254 |
} |
| 255 |
if ((conversion_type == CONV_LINEAR) && |
| 256 |
(input_vector_length != program_data.num_coefficients) && |
| 257 |
(input_vector_length > 1)) { |
| 258 |
(void) fprintf(stderr, |
| 259 |
"Input vector length does not match number of linear coefficients\n"); |
| 260 |
exit(EXIT_FAILURE); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
loop_options = create_loop_options(); |
| 265 |
set_loop_clobber(loop_options, clobber); |
| 266 |
set_loop_verbose(loop_options, verbose); |
| 267 |
#if MINC2 |
| 268 |
set_loop_v2format(loop_options, v2format); |
| 269 |
#endif |
| 270 |
set_loop_datatype(loop_options, datatype, is_signed, |
| 271 |
valid_range[0], valid_range[1]); |
| 272 |
set_loop_output_vector_size(loop_options, 1); |
| 273 |
set_loop_buffer_size(loop_options, (long) buffer_size * 1024); |
| 274 |
set_loop_first_input_mincid(loop_options, inmincid); |
| 275 |
|
| 276 |
|
| 277 |
voxel_loop(1, &infile, 1, &outfile, arg_string, loop_options, |
| 278 |
do_makescalar, (void *) &program_data); |
| 279 |
|
| 280 |
|
| 281 |
if (program_data.linear_coefficients != NULL) { |
| 282 |
free(program_data.linear_coefficients); |
| 283 |
} |
| 284 |
if (linear_coefficients.values != NULL) { |
| 285 |
free(linear_coefficients.values); |
| 286 |
} |
| 287 |
|
| 288 |
exit(EXIT_SUCCESS); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
static void do_makescalar(void *caller_data, long num_voxels, |
| 313 |
int input_num_buffers, int input_vector_length, |
| 314 |
double *input_data[], |
| 315 |
int output_num_buffers, int output_vector_length, |
| 316 |
double *output_data[], Loop_Info *loop_info) |
| 317 |
|
| 318 |
{ |
| 319 |
Program_Data *program_data; |
| 320 |
long ivoxel; |
| 321 |
double *input_vector; |
| 322 |
double value, result; |
| 323 |
int ivalue; |
| 324 |
static double grey_coefficients[3] = {0.299, 0.587, 0.114}; |
| 325 |
|
| 326 |
|
| 327 |
program_data = (Program_Data *) caller_data; |
| 328 |
|
| 329 |
|
| 330 |
if ((input_num_buffers != 1) || (output_num_buffers != 1) || |
| 331 |
(output_vector_length != 1)) { |
| 332 |
(void) fprintf(stderr, "Bad internal values.\n"); |
| 333 |
exit(EXIT_FAILURE); |
| 334 |
} |
| 335 |
if ((program_data->conversion_type == CONV_GREY) && |
| 336 |
(input_vector_length != 3)) { |
| 337 |
(void) fprintf(stderr, |
| 338 |
"Input must have three components for -grey conversion.\n"); |
| 339 |
exit(EXIT_FAILURE); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
for (ivoxel=0; ivoxel < num_voxels; ivoxel++) { |
| 344 |
|
| 345 |
|
| 346 |
if (input_vector_length <= 1) { |
| 347 |
output_data[0][ivoxel] = input_data[0][ivoxel]; |
| 348 |
continue; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
input_vector = &input_data[0][ivoxel*input_vector_length]; |
| 353 |
|
| 354 |
|
| 355 |
result = 0.0; |
| 356 |
for (ivalue=0; ivalue < input_vector_length; ivalue++) { |
| 357 |
|
| 358 |
|
| 359 |
value = input_vector[ivalue]; |
| 360 |
if (value == INVALID_DATA) { |
| 361 |
result = INVALID_DATA; |
| 362 |
break; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
switch (program_data->conversion_type) { |
| 367 |
case CONV_AVERAGE: |
| 368 |
result += value; |
| 369 |
break; |
| 370 |
case CONV_MAGNITUDE: |
| 371 |
result += value * value; |
| 372 |
break; |
| 373 |
case CONV_GREY: |
| 374 |
result += grey_coefficients[ivalue] * value; |
| 375 |
break; |
| 376 |
case CONV_LINEAR: |
| 377 |
result += program_data->linear_coefficients[ivalue] * value; |
| 378 |
break; |
| 379 |
default: |
| 380 |
break; |
| 381 |
} |
| 382 |
|
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
if (result != INVALID_DATA) { |
| 387 |
switch (program_data->conversion_type) { |
| 388 |
case CONV_AVERAGE: |
| 389 |
result /= (double) input_vector_length; |
| 390 |
break; |
| 391 |
case CONV_MAGNITUDE: |
| 392 |
result = sqrt(result); |
| 393 |
break; |
| 394 |
default: |
| 395 |
break; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
output_data[0][ivoxel] = result; |
| 401 |
} |
| 402 |
|
| 403 |
return; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
static int get_double_list(char *dst, char *key, char *nextarg) |
| 421 |
{ |
| 422 |
#define VECTOR_SEPARATOR ',' |
| 423 |
|
| 424 |
int num_elements; |
| 425 |
int num_alloc; |
| 426 |
double *double_list; |
| 427 |
double dvalue; |
| 428 |
char *cur, *end, *prev; |
| 429 |
Double_Array *double_array; |
| 430 |
|
| 431 |
|
| 432 |
if (nextarg == NULL) { |
| 433 |
(void) fprintf(stderr, |
| 434 |
"\"%s\" option requires an additional argument\n", |
| 435 |
key); |
| 436 |
exit(EXIT_FAILURE); |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
double_array = (Double_Array *) dst; |
| 441 |
|
| 442 |
|
| 443 |
end = nextarg + strlen(nextarg); |
| 444 |
cur = nextarg; |
| 445 |
while (isspace(*cur)) cur++; |
| 446 |
num_elements = 0; |
| 447 |
num_alloc = 0; |
| 448 |
double_list = NULL; |
| 449 |
|
| 450 |
|
| 451 |
while (cur!=end) { |
| 452 |
|
| 453 |
|
| 454 |
prev = cur; |
| 455 |
dvalue = strtod(prev, &cur); |
| 456 |
if (cur == prev) { |
| 457 |
(void) fprintf(stderr, |
| 458 |
"expected vector of doubles for \"%s\", but got \"%s\"\n", |
| 459 |
key, nextarg); |
| 460 |
exit(EXIT_FAILURE); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
num_elements++; |
| 465 |
if (num_elements > num_alloc) { |
| 466 |
num_alloc += 20; |
| 467 |
if (double_list == NULL) { |
| 468 |
double_list = |
| 469 |
malloc(num_alloc * sizeof(*double_list)); |
| 470 |
} |
| 471 |
else { |
| 472 |
double_list = |
| 473 |
realloc(double_list, num_alloc * sizeof(*double_list)); |
| 474 |
} |
| 475 |
} |
| 476 |
double_list[num_elements-1] = dvalue; |
| 477 |
|
| 478 |
|
| 479 |
while (isspace(*cur)) cur++; |
| 480 |
|
| 481 |
|
| 482 |
if (*cur == VECTOR_SEPARATOR) cur++; |
| 483 |
|
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
double_array->numvalues = num_elements; |
| 488 |
if (double_array->values != NULL) { |
| 489 |
free(double_array->values); |
| 490 |
} |
| 491 |
double_array->values = double_list; |
| 492 |
|
| 493 |
return TRUE; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
static long get_vector_length(int mincid) |
| 509 |
{ |
| 510 |
int imgid; |
| 511 |
int ndims; |
| 512 |
int dim[MAX_VAR_DIMS]; |
| 513 |
char dimname[MAX_NC_NAME]; |
| 514 |
long vector_length; |
| 515 |
int i; |
| 516 |
|
| 517 |
|
| 518 |
imgid = ncvarid(mincid, MIimage); |
| 519 |
|
| 520 |
|
| 521 |
(void) ncvarinq(mincid, imgid, NULL, NULL, &ndims, dim, NULL); |
| 522 |
|
| 523 |
|
| 524 |
(void) ncdiminq(mincid, dim[ndims-1], dimname, &vector_length); |
| 525 |
if ((strcmp(dimname, MIvector_dimension) != 0) || (ndims <= 2)) { |
| 526 |
vector_length = 0; |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
for (i = 0; i < ndims; i++) { |
| 532 |
ncdiminq(mincid, dim[i], dimname, NULL); |
| 533 |
if (!strcmp(dimname, MIvector_dimension)) { |
| 534 |
vector_length = -1; |
| 535 |
break; |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
return vector_length; |
| 541 |
} |
| 542 |
|