Bsoft  2.1.0
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  vector<int> imgnum = select_numbers(select_list, p->images());
112 
113  Bimage* pone = NULL;
114  char format[32];
115  snprintf(format, 32, "_%%0%dd.", digits);
116  for ( i=0; i<p->images(); i++ ) if ( imgnum[i] ) {
117  pone = p->extract(i);
118  pone->image[0] = p->image[i];
119  pone->label(p->label());
120  pone->change_type(nudatatype);
121  if ( setreslice ) pone->reslice(order);
122  filename = outputfile.pre_rev('.') + Bstring(i+first_number, format) + outputfile.post_rev('.');
123  if ( verbose )
124  cout << "Image " << i << ": " << filename << endl;
125  write_img(filename, pone, 0);
126  delete pone;
127  }
128 
129  if ( verbose & VERB_PROCESS )
130  cout << endl;
131 
132  delete p;
133  order = 0;
134 
135  if ( verbose & VERB_TIME )
136  timer_report(ti);
137 
138  bexit(0);
139 }
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::reslice
void reslice(const char *order)
Definition: Bimage.h:717
Bstring
Definition: Bstring.h:29
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
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
options.h
Header file for option handlers.
verbose
int verbose
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::images
long images()
Definition: Bimage.h:504
Boption::tag
Bstring tag
Definition: options.h:29
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
main
int main(int argc, char **argv)
Definition: bsplit.cpp:42
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2074
Boption::value
Bstring value
Definition: options.h:30
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bstring.h
Header file for the string class.
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
rwimg.h
Header file for 2D and 3D image I/O.
use
const char * use[]
Definition: bsplit.cpp:19
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3111
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
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:335
Bimage::label
void label(string s)
Definition: Bimage.h:250