Bsoft  2.1.0
Bernard's software package
bimg.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: bimg [options] input.img output.img",
21 "------------------------------------------",
22 "Manipulates images in a few general ways.",
23 "Image format extensions supported:",
24 "BioRad (pic), Brix (brx), CCP4 (map), Digital Instruments (di)",
25 "EM (em), Goodford (pot), GRD (grd), Imagic (img), JPEG, (jpg), MFF (mff)",
26 "Image Magick (miff), MRC (mrc), PIC (bp), PIF (pif), Spider (spi)",
27 "Suprim (spm), TIFF (tif), RAW (raw)",
28 " ",
29 "Actions:",
30 "-delete 1,5-13,122 Delete the indicated sub-images (first image = 0).",
31 "-slices Interpret multiple 2D images as z-slices of a single 3D image",
32 "-images Interpret slices of a single 3D image as 2D images",
33 "-invert Invert density in the image.",
34 "-project Project along z axis (after reslicing).",
35 "-sum Sum images in a multi-image file.",
36 "-average Average images in a multi-image file.",
37 "-reslice -z+xy Reslice = switch axes (default xyz).",
38 "-size 10,50,8 New image size (pixels/voxels).",
39 "-translate -5,12,50 Translate (pixels, with wrapping if option -wrap is used).",
40 "-Wrap 10,50,8 Shrinking and wrapping an image (pixels).",
41 "-truncate -0.5,1.2 Truncate data to minimum and maximum.",
42 "-rescale -0.1,5.2 Rescale data to average and standard deviation (after -truncate).",
43 "-minmax -0.5,1.2 Rescale data to minimum and maximum (after -truncate).",
44 "-range -2.3,5.4 Fill with fill value outside this range.",
45 "-logarithm Calculate the logarithm of an image.",
46 "-Levels 8 Restrict to a number of levels.",
47 "-compression 2 Compression type: 5=LZW.",
48 " ",
49 "Parameters:",
50 "-verbose 7 Verbosity of output.",
51 "-datatype u Force writing of a new data type.",
52 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
53 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
54 "-select 1 Select an image (first image = 0; default: all images).",
55 "-fill 127 Fill value for resizing: average (default), background, or value.",
56 "-setbackground 0.5 Set background.",
57 "-wrap Turn wrapping on (default off).",
58 " ",
59 "Output:",
60 "-std stdev.mrc Standard deviation map.",
61 " ",
62 NULL
63 };
64 
65 int main(int argc, char **argv)
66 {
67  // Initialize variables
68  DataType nudatatype(Unknown_Type); // Conversion to new type
69  Vector3<double> sam; // Units for the three axes (A/pixel)
70  int setinvert(0); // Flag to invert density
71  int setreslice(0);
72  Bstring order("xyz"); // Reslice order
73  int setproject(0);
74  int setsum(0);
75  int setaverage(0);
76  int setrescale(0);
77  int setlogarithm(0);
78  double nuavg(0), nustd(0); // Rescaling to average and stdev
79  double numin(0), numax(0); // Rescaling to new min and max
80  double rangemin(0), rangemax(0); // Range to keep
81  int settruncate(0);
82  double cutmin(0), cutmax(0); // Truncation
83  int setshrinkwrap(0); // Shrinking and wrapping flag
84  int settranslate(0); // Translation flag
85  int setwrap(0); // Wrapping flag
86  Vector3<int> nusize; // New image size
87  Vector3<double> origin; // New image origin
88  int set_origin(0); // Flag to set origin
89  Vector3<int> shift; // Shift for integral translation
90  double fill(0); // Fill value for resizing
91  int fill_type(FILL_AVERAGE); // Fill type for resizing
92  int nlevels(0);
93  int setimg(-1); // Select all images
94  Bstring select_list; // List of sub-images to select
95  Bstring delete_list; // List of sub-images to delete
96  int setbackground(0); // Background
97  double nubackground(-1); // New background
98  int znswitch(0); // 0=not, 1=n2z, 2=z2n
99  Bstring stdfile; // Standard deviation output map
100 // int write_flags(0); // Flags for image properties during saving
101  int compression(0); // Output compression type
102 
103  int optind;
104  Boption* option = get_option_list(use, argc, argv, optind);
105  Boption* curropt;
106  for ( curropt = option; curropt; curropt = curropt->next ) {
107  if ( curropt->tag == "select" ) {
108  if ( curropt->value.contains(",") ||
109  curropt->value.contains("-") )
110  select_list = curropt->value;
111  else
112  setimg = curropt->value.integer();
113  }
114  if ( curropt->tag == "delete" ) {
115  delete_list = curropt->value;
116  if ( delete_list.length() < 1 )
117  cerr << "-delete: Image numbers must be specified!" << endl;
118  }
119  if ( curropt->tag == "datatype" )
120  nudatatype = curropt->datatype();
121  if ( curropt->tag == "sampling" )
122  sam = curropt->scale();
123  if ( curropt->tag == "invert" )
124  setinvert = 1;
125  if ( curropt->tag == "project" )
126  setproject = 1;
127  if ( curropt->tag == "average" )
128  setaverage = 1;
129  if ( curropt->tag == "sum" )
130  setsum = 1;
131  if ( curropt->tag == "wrap" )
132  setwrap = 1;
133  if ( curropt->tag == "Wrap" ) {
134  nusize = curropt->size();
135  setshrinkwrap = 1;
136  }
137  if ( curropt->tag == "fill" )
138  fill = curropt->fill(fill_type);
139  if ( curropt->tag == "reslice" ) {
140  if ( curropt->value.length() < 3 )
141  cerr << "-reslice: The order of x, y and z must be specified!" << endl;
142  else {
143  order = curropt->value;
144  setreslice = 1;
145  }
146  }
147  if ( curropt->tag == "rescale" ) {
148  if ( curropt->values(nuavg, nustd) < 2 )
149  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
150  else
151  setrescale = 1;
152  if ( nustd <= 0 ) setrescale = 0;
153  }
154  if ( curropt->tag == "minmax" ) {
155  if ( curropt->values(numin, numax) < 2 )
156  cerr << "-minmax: Both min and max must be specified!" << endl;
157  else
158  setrescale = 2;
159  if ( numin == numax ) setrescale = 0;
160  }
161  if ( curropt->tag == "range" )
162  if ( curropt->values(rangemin, rangemax) < 2 )
163  cerr << "-range: Both range min and max must be specified!" << endl;
164  if ( curropt->tag == "logarithm" )
165  setlogarithm = 1;
166  if ( curropt->tag == "truncate" ) {
167  if ( curropt->values(cutmin, cutmax) < 2 )
168  cerr << "-truncate: Both min and max must be specified!" << endl;
169  else
170  settruncate = 1;
171  if ( cutmin > cutmax ) swap(cutmin, cutmax);
172  if ( cutmin == cutmax ) settruncate = 0;
173  }
174  if ( curropt->tag == "size" )
175  nusize = curropt->size();
176  if ( curropt->tag == "origin" ) {
177  if ( curropt->value[0] == 'c' ) {
178  set_origin = 2;
179  } else {
180  origin = curropt->origin();
181  set_origin = 1;
182  }
183  }
184  if ( curropt->tag == "translate" ) {
185  shift = curropt->vector3();
186  if ( shift.length() ) settranslate = 1;
187  }
188  if ( curropt->tag == "Levels" ) nlevels = curropt->value.integer();
189  if ( curropt->tag == "compression" )
190  if ( ( compression = curropt->integer() ) < 1 )
191  cerr << "-compression: A number must be specified!" << endl;
192  if ( curropt->tag == "setbackground" ) {
193  nubackground = curropt->value.real();
194  setbackground = 1;
195  }
196  if ( curropt->tag == "slices" )
197  znswitch = 1;
198  if ( curropt->tag == "images" )
199  znswitch = 2;
200  if ( curropt->tag == "std" )
201  stdfile = curropt->filename();
202  }
203  option_kill(option);
204 
205  double ti = timer_start();
206 
207  // Read image file
208  int dataflag(0);
209  if ( optind < argc - 1 ) dataflag = 1;
210  Bimage* p = read_img(argv[optind++], dataflag, setimg);
211  if ( p == NULL ) bexit(-1);
212 
213  if ( znswitch == 1 )
214  if ( p->images_to_slices() < 0 ) bexit(-1);
215 
216  if ( znswitch == 2 )
217  if ( p->slices_to_images() < 0 ) bexit(-1);
218 
219  if ( select_list.length() )
220  p->select_images(select_list);
221 
222  if ( delete_list.length() )
223  p->delete_images(delete_list);
224 
225  if ( nudatatype == Unknown_Type )
226  nudatatype = p->data_type();
227  else if ( nudatatype > p->data_type() )
228  p->change_type(nudatatype);
229 
230  if ( setbackground ) p->background(nubackground);
231 
232  if ( fill_type == FILL_AVERAGE ) fill = p->average();
233  if ( fill_type == FILL_BACKGROUND ) {
234  if ( fabs(p->background(long(0))) < 1e-6 )
236  fill = p->background(long(0));
237  }
238 
239  if ( nusize.volume() > 0 ) {
240  if ( setshrinkwrap )
241  p->shrink_wrap(nusize, shift);
242  else {
243  if ( verbose & VERB_PROCESS ) {
244  if ( setwrap ) cout << "Resizing with wrapping:" << endl;
245  else cout << "Resizing:" << endl;
246  cout << "Shift: " << shift << endl;
247  cout << "New size: " << nusize << endl;
248  if ( fill_type != FILL_BACKGROUND )
249  cout << "Fill value: " << fill << endl;
250  cout << endl;
251  } else if ( verbose & VERB_LABEL )
252  cout << "Resizing" << endl << endl;
253 
254  if ( setwrap ) p->resize_wrap(nusize, shift);
255  else p->resize(nusize, shift, fill_type, fill);
256  }
257  } else if ( settranslate ) {
258  if ( setwrap ) p->shift_wrap(shift);
259  else p->shift(shift, fill_type, fill);
260  }
261 
262  if ( setinvert ) p->invert();
263 
264  if ( settruncate ) {
265  if ( fill_type == FILL_USER || fill_type == FILL_BACKGROUND )
266  p->truncate(cutmin, cutmax, fill, fill);
267  else p->truncate_to_min_max(cutmin, cutmax);
268  }
269 
270  if ( setrescale == 1 ) p->rescale_to_avg_std(nuavg, nustd);
271 
272  if ( setrescale == 2 ) p->rescale_to_min_max(numin, numax);
273 
274  if ( rangemin < rangemax ) {
275  if ( fill_type == FILL_AVERAGE ) p->truncate_to_avg(rangemin, rangemax);
276  else if ( fill_type == FILL_BACKGROUND ) p->truncate_to_background(rangemin, rangemax);
277  else p->truncate(rangemin, rangemax, fill, fill);
278  }
279 
280  if ( setlogarithm ) p->logarithm();
281 
282  if ( setreslice ) p->reslice(order);
283 
284  if ( nlevels ) p->limit_levels(nlevels);
285 
286  Bimage* p2 = NULL;
287  if ( setproject ) {
288  p2 = p->project('z', 1);
289  delete p;
290  p = p2;
291  }
292 
293  if ( setsum )
294  p->sum_images();
295 
296  if ( setaverage ) {
297  if ( stdfile.length() ) {
298  p2 = p->average_images(1);
299  delete p;
300  p = p2;
301  } else {
302  p->average_images();
303  }
304  }
305 
306  if ( sam.volume() > 0 ) p->sampling(sam);
307 
308  if ( set_origin ) {
309  if ( set_origin == 2 ) p->origin(p->default_origin());
310  else p->origin(origin);
311  }
312 
313  // Write an output file if a file name is given
314  if ( optind < argc ) {
315  p->change_type(nudatatype);
316  write_img(argv[optind], p, compression);
317  }
318 
319  // Write a standard deviation output file if a file name is given
320  p2 = p->next;
321  if ( stdfile.length() && p2 )
322  write_img(stdfile, p2, 0);
323 
324  delete p;
325  order = 0;
326 
327  if ( verbose & VERB_TIME )
328  timer_report(ti);
329 
330  bexit(0);
331 }
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::shrink_wrap
int shrink_wrap(Vector3< long > nusize, Vector3< long > translate)
Shrinks an image to a new size with wrapping of the excluded edges.
Definition: Bimage_resize.cpp:432
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bimage::truncate
int truncate(double minim, double maxim, double setmin, double setmax)
Truncates image data to a given minimum and maximum.
Definition: Bimage_rescale.cpp:277
Bimage::shift
int shift(Vector3< double > vec, int fill_type=0, double fill=0)
Shifts an image.
Definition: Bimage_transform.cpp:589
Bimage::reslice
void reslice(const char *order)
Definition: Bimage.h:717
Bstring
Definition: Bstring.h:29
Bimage::next
Bimage * next
Definition: Bimage.h:179
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Bimage::truncate_to_background
int truncate_to_background(double minim, double maxim)
Sets voxels in image data exceeding a given minimum and maximum to the image background.
Definition: Bimage_rescale.cpp:367
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:592
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
verbose
int verbose
Bimage::background
double background(long nn)
Definition: Bimage.h:549
FILL_USER
#define FILL_USER
Definition: utilities.h:176
Bstring::real
double real()
Definition: Bstring.cpp:553
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
options.h
Header file for option handlers.
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Bimage::shift_wrap
int shift_wrap(Vector3< double > vec)
Shifts an image.
Definition: Bimage_transform.cpp:641
Boption::origin
Vector3< double > origin()
Definition: options.h:180
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::delete_images
long delete_images(Bstring list, int retain=0)
Retains or deletes sub-images from a multi-image structure.
Definition: Bimage.cpp:2156
Bimage::images_to_slices
int images_to_slices()
Changes the 2D images to slices in a 3D image.
Definition: Bimage.cpp:2115
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
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
Bimage::project
Bimage * project(char axis, int flags=1)
Projects a 3D image to a 2D image down one of the three major axes.
Definition: Bimage_project.cpp:31
Bimage::resize_wrap
int resize_wrap(Vector3< long > nusize, Vector3< long > translate)
Resizes without interpolation or rescaling.
Definition: Bimage_resize.cpp:202
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
Bimage::truncate_to_avg
int truncate_to_avg(double minim, double maxim)
Sets voxels in image data exceeding a given minimum and maximum to the average.
Definition: Bimage_rescale.cpp:349
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1371
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bimage::sum_images
void sum_images()
Adds all sub-images.
Definition: Bimage.cpp:3306
Bimage::truncate_to_min_max
int truncate_to_min_max(double minim, double maxim)
Truncates image data to a given minimum and maximum.
Definition: Bimage_rescale.cpp:331
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
Boption::size
Vector3< long > size()
Definition: options.h:163
Boption::vector3
Vector3< double > vector3()
Definition: options.h:148
Boption::fill
double fill(int &fill_type)
Definition: options.h:138
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::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
main
int main(int argc, char **argv)
Definition: bimg.cpp:65
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.
Bimage::rescale_to_min_max
int rescale_to_min_max(double numin, double numax)
Rescales the image data to a given minimum and maximum.
Definition: Bimage_rescale.cpp:100
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
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3111
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
Bimage::average_images
void average_images()
Definition: Bimage.h:724
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Bimage::limit_levels
int limit_levels(int nlevels)
Converts a full gray scale image to a limited level image.
Definition: Bimage_rescale.cpp:426
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
Vector3::length
double length()
Definition: Vector3.h:222
use
const char * use[]
Definition: bimg.cpp:18
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bimage::logarithm
void logarithm()
Calculates the logarithm of the image data.
Definition: Bimage_rescale.cpp:728
Bimage::resize
int resize(Vector3< long > nusize, Vector3< long > translate, int fill_type=0, double fill=0)
Resizes without interpolation or rescaling.
Definition: Bimage_resize.cpp:28
Bimage::select_images
long select_images(Bstring list)
Definition: Bimage.cpp:2187
Boption::integer
long integer()
Definition: options.h:100
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