| Bsoft 2.0.5 Bernard's software package

bsplit.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "Bstring.h"
11 #include "utilities.h"
12 #include "options.h"
13 #include "timer.h"
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
18 // Usage assistance
19 const char* use[] = {
20 " ",
21 "Usage: bsplit [options] input.img output.img",
22 "--------------------------------------------",
23 "Splits multi-image files of the following formats:",
24 "Digital Instruments (di), Imagic (img), Image Magick (miff), PIF (pif)",
25 "SPIDER (spi), TIFF (tif), RAW (raw).",
26 "Files of single image 3D image formats can be split with the -images option.",
27 " ",
28 "Actions:",
29 "-images Interpret slices of a single 3D image as 2D images.",
30 "-invert Invert density in the image.","-reslice -z+xy Reslice = switch axes (default xyz).",
31 "-select 1,5-13,122 Sub-images to select (first image = 0, default all).",
32 " ",
33 "Parameters:",
34 "-verbose 7 Verbosity of output.",
35 "-datatype u Force writing of a new data type.",
36 "-first 5 Number given to the first file (default 0).",
37 "-digits 3 Number of digits inserted before the last period in the output file name.",
38 " ",
39 NULL
40 };
41
42 int main(int argc, char **argv)
43 {
44 // Initialize variables
45 DataType nudatatype(Unknown_Type); // Conversion to new type
46 int setZslices(0); // Interpret 3D slices as separate 2D images
47 int setinvert(0); // Flag to invert density
48 int setreslice(0);
49 Bstring order("xyz");
50 int first_number(0); // Number given to first file
51 int digits(3); // File number size
52 Bstring select_list; // List of sub-images to select, default all
53
54 int i, optind;
55 Boption* option = get_option_list(use, argc, argv, optind);
56 Boption* curropt;
57 for ( curropt = option; curropt; curropt = curropt->next ) {
58 if ( curropt->tag == "images" )
59 setZslices = 1;
60 if ( curropt->tag == "invert" )
61 setinvert = 1;
62 if ( curropt->tag == "reslice" ) {
63 if ( curropt->value.length() < 3 )
64 cerr << "-reslice: The order of x, y and z must be specified!" << endl;
65 else {
66 order = curropt->value;
67 setreslice = 1;
68 }
69 }
70 if ( curropt->tag == "select" ) {
71 select_list = curropt->value;
72 if ( select_list.length() < 1 )
73 cerr << "-select: Image numbers must be specified!" << endl;
74 }
75 if ( curropt->tag == "datatype" )
76 nudatatype = curropt->datatype();
77 if ( curropt->tag == "first" )
78 if ( ( first_number = curropt->value.integer() ) < 0 )
79 cerr << "-first: A number must be specified!" << endl;
80 if ( curropt->tag == "digits" )
81 if ( ( digits = curropt->value.integer() ) < 1 )
82 cerr << "-digits: A number of digits must be specified!" << endl;
83 }
84 option_kill(option);
85
86 double ti = timer_start();
87
88 // Read image file
89 Bstring inputfile = argv[optind++];
90 Bimage* p = read_img(inputfile, 1, -1);
91 if ( p == NULL ) {
92 cerr << "Error: No input file read!" << endl;
93 bexit(-1);
94 }
95
96 if ( optind >= argc ) bexit(0);
97
98 if ( verbose & VERB_LABEL )
99 cout << "Splitting " << inputfile << " into " << p->images() << " files" << endl;
100
101 Bstring filename;
102 Bstring outputfile = argv[optind];
103
104 if ( digits < 0.44*log(1.0*p->images()) + 1 ) digits = (int) (0.44*log(1.0*p->images()) + 1);
105
106 if ( setZslices )
107 if ( p->slices_to_images() < 0 ) bexit(-1);
108
109 if ( setinvert ) p->invert();
110
111 int* imgnum = new int[p->images()];
112 select_numbers(select_list, p->images(), imgnum);
113
114 Bimage* pone = NULL;
115 char format[32];
116 snprintf(format, 32, "_%%0%dd.", digits);
117 p->label(get_cmd_line(argc, argv));
118 for ( i=0; i<p->images(); i++ ) if ( imgnum[i] ) {
119 pone = p->extract(i);
120 pone->image[0] = p->image[i];
121 pone->label(p->label());
122 pone->change_type(nudatatype);
123 if ( setreslice ) pone->reslice(order);
124 filename = outputfile.pre_rev('.') + Bstring(i+first_number, format) + outputfile.post_rev('.');
125 if ( verbose )
126 cout << "Image " << i << ": " << filename << endl;
127 write_img(filename, pone, 0);
128 delete pone;
129 }
130
131 if ( verbose & VERB_PROCESS )
132 cout << endl;
133
134 delete[] imgnum;
135 delete p;
136 order = 0;
137
138 if ( verbose & VERB_TIME )
139 timer_report(ti);
140
141 bexit(0);
142 }
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2052
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3108
utilities.h
Header file for general utilities.
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
Bstring::length
long length() const
Definition: Bstring.cpp:263
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
timer.h
Utilities for timing functions.
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
select_numbers
int select_numbers(Bstring &string, int n, int *numsel)
Converts a string with a selection specification into an integer array.
Definition: utilities.cpp:328
verbose
int verbose
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:428
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
Bstring.h
Header file for the string class.
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bstring::integer
long integer()
Definition: Bstring.cpp:539
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
Bimage::images
long images()
Definition: Bimage.h:491
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:413
use
const char * use[]
Definition: bsplit.cpp:19
main
int main(int argc, char **argv)
Definition: bsplit.cpp:42
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:106
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
Bimage::image
Bsub_image * image
Definition: Bimage.h:188