Bsoft  2.1.0
Bernard's software package
btile.cpp
Go to the documentation of this file.
1 
9 #include "rwmg.h"
10 #include "mg_processing.h"
11 #include "rwimg.h"
12 #include "file_util.h"
13 #include "utilities.h"
14 #include "options.h"
15 #include "timer.h"
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
20 int img_tile_with_overlap(Bimage* p, Bstring outputfile, Vector3<int> size,
21  Vector3<int> overlap, int digits, int first_number, DataType nudatatype);
22 
23 // Usage assistance
24 const char* use[] = {
25 " ",
26 "Usage: btile [options] input.img/input.star output.img",
27 "------------------------------------------------------",
28 "Splits an image into overlapping tiles.",
29 "During tiling, the original size and tile origins are written into a text",
30 " file with the base of the output file name and the extension \".tiles\".",
31 "The program bpatch can reassemble the tiles using this text file.",
32 " ",
33 "Action:",
34 "-split Split into multiple images.",
35 " ",
36 "Parameters:",
37 "-verbose 7 Verbosity of output.",
38 "-datatype u Force writing of a new data type.",
39 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
40 "-size 120,150,70 Size of tiles (default 512,512,512).",
41 "-overlap 13,15,8 Overlap of tiles (default 0,0,0).",
42 "-first 5 Number given to the first file (default 0).",
43 "-digits 3 Number of digits inserted before the last period in the output file name (default 3).",
44 " ",
45 "Parameters for reconstructions:",
46 "-recid rec_1 Reconstruction ID to use.",
47 " ",
48 "Output:",
49 "-output output.star Output parameter file.",
50 " ",
51 NULL
52 };
53 
54 int main(int argc, char **argv)
55 {
56  // Initialize variables
57  int split(0); // Flag to split into individual images
58  DataType nudatatype(Unknown_Type); // Conversion to new type
59  Vector3<double> sampling; // Units for the three axes (A/pixel)
60  Vector3<long> size(512,512,512); // Tile size
61  Vector3<long> overlap; // Tile overlap
62  int first_number(0); // Number given to first file
63  int digits(3); // File number size
64  Bstring recid; // Reconstruction ID to tile
65  Bstring outfile; // Output micrograph parameter file
66 
67  int optind;
68  Boption* option = get_option_list(use, argc, argv, optind);
69  Boption* curropt;
70  for ( curropt = option; curropt; curropt = curropt->next ) {
71  if ( curropt->tag == "split" ) split = 1;
72  if ( curropt->tag == "datatype" )
73  nudatatype = curropt->datatype();
74  if ( curropt->tag == "sampling" )
75  sampling = curropt->scale();
76  if ( curropt->tag == "size" )
77  size = curropt->size();
78  if ( curropt->tag == "overlap" )
79  overlap = curropt->size();
80  if ( curropt->tag == "first" ) {
81  if ( ( first_number = curropt->value.integer() ) < 0 )
82  cerr << "-first: A number must be specified!" << endl;
83  if ( first_number < 0 ) first_number = 0;
84  }
85  if ( curropt->tag == "digits" )
86  if ( ( digits = curropt->value.integer() ) < 1 )
87  cerr << "-digits: A number of digits must be specified!" << endl;
88  if ( curropt->tag == "recid" ) {
89  recid = curropt->value;
90  if ( recid.length() < 1 )
91  cerr << "-recid: A reconstruction ID must be specified!" << endl;
92  }
93  if ( curropt->tag == "output" )
94  outfile = curropt->filename();
95  }
96  option_kill(option);
97 
98  double ti = timer_start();
99 
100  Bstring filename = argv[optind++];
101  Bproject* project = NULL;
102  Breconstruction* rec;
103 
104  if ( file_type(filename) == Micrograph ) {
105  project = read_project(filename);
106  project->select = 1;
107  for ( rec = project->rec; rec && rec->id != recid; rec = rec->next ) ;
108  if ( !rec ) {
109  cerr << "Error: Reconstruction " << recid << " not found!" << endl;
110  bexit(-1);
111  }
112  filename = rec->frec;
113  if ( sampling[0] > 0 ) rec->voxel_size = sampling;
114  }
115 
116  Bimage* p = read_img(filename, 1, -1);
117  if ( p == NULL ) bexit(-1);
118 
119  if ( optind >= argc ) bexit(0);
120 
121  if ( sampling.volume() > 0 ) p->sampling(sampling);
122 
123 
124  if ( argc > optind ) {
125  if ( split)
126  img_tile_with_overlap(p, argv[optind], size, overlap, digits, first_number, nudatatype);
127  else {
128  Vector3<long> start, step(size-overlap);
129  Bimage* ptile = p->extract_tiles(0, start, p->size(), size, step, 1);
130  ptile->change_type(nudatatype);
131  write_img(argv[optind], ptile, 0);
132  delete ptile;
133  }
134  }
135 
136  delete p;
137 
138  if ( project && outfile.length() ) {
139  write_project(outfile, project, 0, 0);
140  }
141 
143 
144  if ( verbose & VERB_TIME )
145  timer_report(ti);
146 
147  bexit(0);
148 }
149 
169  Vector3<int> overlap, int digits, int first_number, DataType nudatatype)
170 {
171  Bstring filename;
172  char format[16];
173  Bimage* pone = NULL;
174  Vector3<double> start;
175 
176  if ( digits < 2 ) digits = 2;
177 
178  snprintf(format, 16, "%%0%dd.", digits);
179 
180  if ( size[0] < 1 ) size[0] = 512;
181  if ( size[1] < 1 ) size[1] = 512;
182  if ( size[2] < 1 ) size[2] = 512;
183  if ( size[0] > p->sizeX() ) size[0] = p->sizeX();
184  if ( size[1] > p->sizeY() ) size[1] = p->sizeY();
185  if ( size[2] > p->sizeZ() ) size[2] = p->sizeZ();
186  if ( overlap[0] < 0 ) overlap[0] = 0;
187  if ( overlap[1] < 0 ) overlap[1] = 0;
188  if ( overlap[2] < 0 ) overlap[2] = 0;
189  if ( overlap[0] > p->sizeX() ) overlap[0] = p->sizeX()/2;
190  if ( overlap[1] > p->sizeY() ) overlap[1] = p->sizeY()/2;
191  if ( overlap[2] > p->sizeZ() ) overlap[2] = p->sizeZ()/2;
192 
193  int n, i = first_number;
194  Vector3<long> step = size - overlap;
195  Vector3<long> xsize;
196 
197  filename = outputfile.pre_rev('.') + ".tiles";
198  n = (p->sizeX()/step[0] + 1)*(p->sizeY()/step[1] + 1)*(p->sizeZ()/step[2] + 1);
199 
200  if ( verbose ) {
201  cout << "Splitting image " << p->file_name() << " into tiles:" << endl;
202  cout << "Tile size: " << size << endl;
203  cout << "Overlap: " << overlap << endl;
204  cout << "Number of tiles: " << n << endl << endl;
205  }
206 
207  ofstream fd(filename.c_str());
208  if ( fd.fail() ) return 0;
209 
210  fd << p->sizeX() << " " << p->sizeY() << " " << p->sizeZ() << endl;
211  fd << overlap[0] << " " << overlap[1] << " " << overlap[2] << endl;
212 
213  for ( n=0, start[2]=0; start[2]<p->sizeZ(); start[2]+=step[2] ) {
214  xsize[2] = p->sizeZ() - (long)start[2];
215  if ( xsize[2] > size[2] ) xsize[2] = size[2];
216  for ( start[1]=0; start[1]<p->sizeY(); start[1]+=step[1] ) {
217  xsize[1] = p->sizeY() - (long)start[1];
218  if ( xsize[1] > size[1] ) xsize[1] = size[1];
219  for ( start[0]=0; start[0]<p->sizeX(); start[0]+=step[0], i++ ) {
220  xsize[0] = p->sizeX() - (long)start[0];
221  if ( xsize[0] > size[0] ) xsize[0] = size[0];
222  pone = p->extract(0, start, xsize);
223  pone->label(p->label());
224  pone->change_type(nudatatype);
225  filename = outputfile.pre_rev('.') + Bstring(i, format) + outputfile.post_rev('.');
226  if ( verbose & VERB_LABEL )
227  cout << "File " << i << ": " << filename << endl;
228  write_img(filename, pone, 0);
229  delete pone;
230  fd << start[0] << " " << start[1] << " " << start[2] << endl;
231  n++;
232  }
233  }
234  }
235 
236  fd.close();
237 
238  if ( verbose & VERB_LABEL )
239  cout << "Number of tiles: " << n << endl << endl;
240 
241  return n;
242 }
Boption
A class for a command line input option.
Definition: options.h:26
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:429
Breconstruction::id
Bstring id
Definition: mg_processing.h:285
use
const char * use[]
Definition: btile.cpp:24
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
Bproject::select
int select
Definition: mg_processing.h:342
file_util.h
Header file for file checking functions.
Bstring
Definition: Bstring.h:29
Breconstruction::voxel_size
Vector3< double > voxel_size
Definition: mg_processing.h:295
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
Vector3< int >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
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
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
options.h
Header file for option handlers.
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
verbose
int verbose
project
Bproject * project
Definition: bshow.cpp:33
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
rwmg.h
Header file for reading and writing micrograph parameters.
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Micrograph
@ Micrograph
Definition: file_util.h:23
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.
Bimage::extract_tiles
Bimage * extract_tiles(long nn, vector< Vector3< long >> &coords, Vector3< long > tile_size)
Extracts a set of tiles at specified positions from an image into a new image.
Definition: Bimage_extract.cpp:331
Bproject
Definition: mg_processing.h:336
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:141
utilities.h
Header file for general utilities.
Boption::size
Vector3< long > size()
Definition: options.h:163
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
Breconstruction
Definition: mg_processing.h:283
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
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
mg_processing.h
Header file for micrograph processing.
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
img_tile_with_overlap
int img_tile_with_overlap(Bimage *p, Bstring outputfile, Vector3< int > size, Vector3< int > overlap, int digits, int first_number, DataType nudatatype)
Writes overlapping tiles into new images.
Definition: btile.cpp:168
main
int main(int argc, char **argv)
Definition: btile.cpp:54
Bimage::label
void label(string s)
Definition: Bimage.h:250
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:284