| Bsoft 2.0.5 Bernard's software package

bconvert.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "utilities.h"
11 #include "options.h"
12 #include "timer.h"
13
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16
17 // Usage assistance
18 const char* use[] = {
19 " ",
20 "Usage: bconvert [options] input.images",
21 "--------------------------------------",
22 "Converts a list of images into a new format, using the original names and replacing the extension.",
23 "Any number of input images may be given and may include the wild card \"*\".",
24 "(However, VMS does not support Unix style usage of wild cards).",
25 " ",
26 "Actions:",
27 "-rescale -0.1,5.2 Rescale input images to average and standard deviation.",
28 "-extension zyx Change the file extension after writing.",
29 " ",
30 "Parameters:",
31 "-verbose 3 Verbosity of output.",
32 "-datatype u Force writing of a new data type.",
33 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
34 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
35 "-raw d=f#x=120,120,1 Formatting to reinterpret image files.",
36 " ",
37 "Output:",
38 "-output mrc Output format (default pif).",
39 " ",
40 NULL
41 };
42
43 int main(int argc, char **argv)
44 {
45 // Initialize variables
46 DataType nudatatype(Unknown_Type); // Conversion to new type
47 Vector3<double> sam; // Units for the three axes (A/pixel)
48 Vector3<double> origin; // New image origin
49 int set_origin(0); // Flag to set origin
50 Bstring nuformat("pif"); // New image file format
51 Bstring nuext; // New extension after writing
52 Bstring rawstring;
53 int setrescale(0);
54 double nuavg(0), nustd(0); // Rescale image
55
56 int optind;
57 Boption* option = get_option_list(use, argc, argv, optind);
58 Boption* curropt;
59 for ( curropt = option; curropt; curropt = curropt->next ) {
60 if ( curropt->tag == "rescale" ) {
61 if ( curropt->values(nuavg, nustd) < 2 )
62 cerr << "-rescale: Both averge and standard deviation must be specified!" << endl;
63 else
64 setrescale = 1;
65 if ( nustd <= 0 ) setrescale = 0;
66 }
67 if ( curropt->tag == "extension" ) nuext = curropt->value;
68 if ( curropt->tag == "datatype" )
69 nudatatype = curropt->datatype();
70 if ( curropt->tag == "sampling" )
71 sam = curropt->sampling();
72 if ( curropt->tag == "origin" ) {
73 if ( curropt->value[0] == 'c' ) {
74 set_origin = 2;
75 } else {
76 origin = curropt->origin();
77 set_origin = 1;
78 }
79 }
80 if ( curropt->tag == "raw" ) {
81 rawstring = curropt->value;
82 if ( rawstring.length() < 1 )
83 cerr << "-raw: A valid format reinterpretation string must be specified!" << endl;
84 }
85 if ( curropt->tag == "output" ) {
86 nuformat = curropt->value;
87 if ( nuformat.length() < 1 )
88 cerr << "-output: An output format must be specified!" << endl;
89 }
90 }
91 option_kill(option);
92
93 if ( verbose && argc < 3 ) {
94 cerr << "Error: No input files given!" << endl;
95 bexit(-1);
96 }
97
98 double ti = timer_start();
99
100 // Read the images and convert them
101 Bimage* p = NULL;
102 Bstring filename, nuname;
103
104 if ( nuformat[0] != '.' ) nuformat = "." + nuformat;
105 if ( nuext.length() && nuext[0] != '.' ) nuext = "." + nuext;
106 if ( rawstring.length() > 0 && rawstring[0] != '#' ) rawstring = "#" + rawstring;
107
108 while ( optind < argc ) {
109 filename = argv[optind++];
110 filename += rawstring;
111 p = read_img(filename, 1, -1);
112 if ( p != NULL ) {
113 if ( nudatatype == Unknown_Type )
114 nudatatype = p->data_type();
115 else if ( nudatatype > p->data_type() )
116 p->change_type(nudatatype);
117 if ( setrescale ) p->rescale_to_avg_std(nuavg, nustd);
118 p->statistics();
119 p->calculate_background();
120 if ( sam.volume() > 0 ) p->sampling(sam);
121 if ( set_origin ) {
122 if ( set_origin == 2 ) p->origin(p->default_origin());
123 else p->origin(origin);
124 }
125 p->label(get_cmd_line(argc, argv));
126 filename = p->file_name();
127 filename = filename.pre_rev('.') + nuformat;
128 p->change_type(nudatatype);
129 write_img(filename, p, 0);
130 delete p;
131 if ( nuext.length() ) {
132 nuname = filename.pre_rev('.') + nuext;
133 if ( rename(filename.c_str(), nuname.c_str()) )
134 cerr << "File " << filename << " not renamed to " << nuname << endl;
135 }
136 }
137 }
138
139 if ( verbose & VERB_TIME )
140 timer_report(ti);
141
142 bexit(0);
143 }
144
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
main
int main(int argc, char **argv)
Definition: bconvert.cpp:43
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
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
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
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::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
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
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:163
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
use
const char * use[]
Definition: bconvert.cpp:18
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:585
Vector3< double >
Boption::origin
Vector3< double > origin()
Definition: options.h:180
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:413
Vector3::volume
double volume()
Definition: Vector3.h:235
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
verbose
int verbose
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
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::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563