tricky.cc |
| Suppose we have a lot of 3D images in raw format and vol format. We want to rotate each image of angle pi/4 around the X-axis. It would be smart if the program could know that an image is already rotated. |
#include <vol.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
int main( int argc, char **argv ) {
for (int i = 1; i < argc; ++i ) {
int len = strlen( argv[i] );
Vol v;
|
|
if (len < 4 || (strcmp( argv[i] + len - 4, ".raw" ) != 0
&& strcmp( argv[i] + len - 4, ".vol" )) != 0 ) {
fprintf( stderr, "Cannot find format of %s\n, skipping ...", argv[i] );
continue;
} else if (strcmp( argv[i] + len - 4, ".raw" ) == 0) {
v = Vol( argv[i], 10, 10, 10, 0 ); (1)
} else {
v = Vol( argv[i] ); (2)
}
|
|
if (!v.isOK()) {
fprintf( stderr, "Cannot load %s, skipping ...\n", argv[i] );
continue;
}
if (v.getHeaderValue( "Rotated" ) != NULL ) {
int date;
int errcode = v.getHeaderValueAsInt( "Rotation-Date", &date );
|
|
[user@host ~]$ volheader example.vol Version: 2 X: 41 Y: 41 Z: 41 Voxel-Size: 1 Alpha-Color: 0 Int-Endian: DCBA Voxel-Endian: A Res-X: 1.000000 Res-Y: 1.000000 Res-Z: 1.000000 . [user@host ~]$ |
|
Rotated: yes Rotation-Date: 1043078350 |
|
fprintf( stderr, "The file %s is already a rotated file (%s), skipping ....\n", argv[i],
(errcode ? "no date found" : ctime( (time_t*)&date )) );
} else {
char *newname = new char[ len - 4 + strlen(".rot.vol") + 1 ];
if (newname == NULL) {
fprintf( stderr, "cannot allocate memory .. skipping.\n" );
} else {
v.rotate( M_PI/4, 0, 0 );
|
|
strcpy( newname, argv[i] );
strcpy( newname + len - 4, ".rot.vol" );
v.setHeaderValue( "Rotated", "yes" );
v.setHeaderValue( "Rotation-Date", (int)time(NULL) );
v.dumpVol( newname );
delete []newname;
}
}
}
return 0;
}
|
|
![]() |
![]() |
![]() |
![]() |
| ... |