Bsoft  2.1.0
Bernard's software package
bmontage.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "img_combine.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: bmontage [options] input.img output.img",
22 "----------------------------------------------",
23 "Generates a montage from a 3D image or a set of 2D images",
24 " ",
25 "Actions:",
26 "-slices 3,4 Montage a 3D image in columns and rows (0,0: automatically calculated).",
27 "-images 3,4 Montage 2D images in columns and rows (0,0: automatically calculated).",
28 "-flip y Flip panel order on the x and/or y axis.",
29 " ",
30 "Parameters:",
31 "-verbose 7 Verbosity of output.",
32 "-datatype u Force writing of a new data type.",
33 "-first 12 First slice in 3D or first 2D image (default: 0).",
34 "-skip 2 Number of slices or images to skip (default: 0).",
35 "-pad 5 Padding to add around slices or images (default 0).",
36 "-fill 127 Fill value for padding (default average).",
37 " ",
38 NULL
39 };
40 
41 int main(int argc, char **argv)
42 {
43  // Initialize variables
44  DataType nudatatype(Unknown_Type); // Conversion to new type
45  int setmontage(0);
46  long first(0); // First slice or image in the montage
47  long skip(0); // Number slices or images to skip
48  long columns(0), rows(0); // Determine montage dimensions automatically
49  long pad(0); // Padding around images
50  int flip(0); // Flag to flip panels on: 1=x axis, 2=y axis
51  double fill(0); // Fill value for resizing
52  int fill_type(FILL_BACKGROUND); // Fill type for resizing
53 
54  int 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 == "slices" ) {
59  if ( curropt->values(columns, rows) < 2 )
60  cerr << "-slices: Columns and rows must be specified!" << endl;
61  else
62  setmontage = 1;
63  }
64  if ( curropt->tag == "images" ) {
65  if ( curropt->values(columns, rows) < 2 )
66  cerr << "-images: Columns and rows must be specified!" << endl;
67  else
68  setmontage = 2;
69  }
70  if ( curropt->tag == "flip" ) {
71  if ( curropt->value.contains("x") ) flip |= 1;
72  if ( curropt->value.contains("y") ) flip |= 2;
73  }
74  if ( curropt->tag == "datatype" )
75  nudatatype = curropt->datatype();
76  if ( curropt->tag == "first" )
77  if ( ( first = curropt->value.integer() ) < 1 )
78  cerr << "-first: A number must be specified!" << endl;
79  if ( curropt->tag == "skip" )
80  if ( ( skip = curropt->value.integer() ) < 1 )
81  cerr << "-skip: A number must be specified!" << endl;
82  if ( curropt->tag == "pad" )
83  if ( ( pad = curropt->value.integer() ) < 1 )
84  cerr << "-pad: A number of pixels must be specified!" << endl;
85  if ( curropt->tag == "fill" )
86  fill = curropt->fill(fill_type);
87  }
88  option_kill(option);
89 
90  double ti = timer_start();
91 
92  // Read all the file names
93  Bstring* file_list = NULL;
94  while ( optind < argc - 1 ) string_add(&file_list, argv[optind++]);
95  if ( !file_list ) {
96  cerr << "Error: No image files specified!" << endl;
97  bexit(-1);
98  }
99 
100  Vector3<long> nusize;
101  Vector3<long> translate = {pad, pad, 0};
102 
103  Bstring rawstring;
104  Bimage* p = img_catenate(file_list, rawstring, Unknown_Type,
105  nusize, 0, fill_type, fill, 0, 0);
106 
107  // Read image file
108 // int dataflag = 0;
109 // if ( optind < argc - 1 ) dataflag = 1;
110 // Bimage* p = read_img(argv[optind++], dataflag, -1);
111  if ( p == NULL ) {
112  cerr << "Error: No input file(s) read!" << endl;
113  bexit(-1);
114  }
115 
116  if ( !setmontage && first ) {
117  if ( p->sizeZ() > 1 ) setmontage = 1;
118  else setmontage = 2;
119  columns = rows = 1;
120  }
121 
122  if ( setmontage == 2 && skip == 0 ) skip = p->sizeZ() - 1;
123 
124  if ( fill_type == FILL_AVERAGE ) fill = p->average();
125  if ( fill_type == FILL_BACKGROUND ) {
126  if ( fabs(p->background(long(0))) < 1e-6 )
128  fill = p->background(long(0));
129  }
130 
131  Bimage* pm = NULL;
132 
133  p->background(fill);
134 
135  if ( pad ) {
136  nusize = {p->sizeX()+2*pad, p->sizeY()+2*pad, p->sizeZ()};
137  p->resize(nusize, translate, fill_type, fill);
138  }
139 
140  if ( setmontage )
141  pm = p->montage(first, columns, rows, skip, flip);
142 
143  if ( pm ) {
144  delete p;
145  p = pm;
146  }
147 
148  if ( optind < argc ) {
149  p->change_type(nudatatype);
150  write_img(argv[optind], p, 0);
151  }
152 
153  delete p;
154 
155  if ( verbose & VERB_TIME )
156  timer_report(ti);
157 
158  bexit(0);
159 }
160 
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bstring
Definition: Bstring.h:29
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< long >
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
use
const char * use[]
Definition: bmontage.cpp:19
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::background
double background(long nn)
Definition: Bimage.h:549
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
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
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
Boption::value
Bstring value
Definition: options.h:30
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.
verbose
int verbose
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bimage::montage
Bimage * montage(int first, int cols, int rows, int skip=0, int flipy=0)
Rearranges an image into a montage of 2D slices for display.
Definition: Bimage_montage.cpp:49
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
Boption::fill
double fill(int &fill_type)
Definition: options.h:138
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.
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
img_combine.h
Header file for combining two images in various ways.
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
main
int main(int argc, char **argv)
Definition: bmontage.cpp:41
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
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
img_catenate
Bimage * img_catenate(Bstring *file_list, Bstring &rawstring, DataType newdatatype, Vector3< long > nusize, int setZslices=0, int fill_type=0, double fill=0, double newavg=0, double newstd=0)
Catenates a list of images into a multi-image structure.
Definition: img_combine.cpp:163