Bsoft  2.1.0
Bernard's software package
rwkernel.cpp
Go to the documentation of this file.
1 
9 #include "rwkernel.h"
10 #include "utilities.h"
11 #include <fstream>
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
22 {
23  string thisname;
24 
25  if ( access(p->file_name().c_str(), R_OK) == 0 )
26  thisname = p->file_name();
27  else
28  thisname = parameter_file_path(p->file_name());
29 
30  if ( verbose & VERB_DEBUG )
31  cout << "DEBUG readKernel: filename=" << thisname << endl;
32 
33  ifstream fkernel(thisname);
34  if ( fkernel.fail() ) {
35  error_show(p->file_name(), __FILE__, __LINE__);
36  return -1;
37  }
38 
39  p->file_name(thisname);
40  p->data_type(Float);
41 
42  int i;
43  Vector3<long> kernelsize;
44 
45  for ( i=0; i<3 && !fkernel.eof(); i++ )
46  fkernel >> kernelsize[i];
47 
48  if ( kernelsize.volume() < 1 ) {
49  cerr << "Error: The kernel size is wrong!" << endl;
50  return -1;
51  }
52 
53  p->size(kernelsize);
54  p->images(1);
55  p->channels(1);
56 
57  if ( verbose & VERB_DEBUG )
58  cout << "DEBUG readKernel: kernel size=" << p->size() << endl;
59 
60  p->origin(p->sizeX()/2, p->sizeY()/2, p->sizeZ()/2);
61 
62  long nkernel = p->sizeX()*p->sizeY()*p->sizeZ();
63  float* kernel = (float *) p->data_alloc();
64 
65  for ( i=0; i<nkernel && !fkernel.eof(); i++ )
66  fkernel >> kernel[i];
67 
68  fkernel.close();
69 
70  if ( i < nkernel ) {
71  cerr << "Error: The kernel does not contain all the required values!" << endl;
72  return -1;
73  }
74 
75  if ( verbose & VERB_DEBUG ) {
76  for ( i=0; i<nkernel; i++ )
77  cout << kernel[i] << " ";
78  cout << endl;
79  }
80 
81  return 0;
82 }
83 
90 {
91  p->change_type(Float);
92 
93  ofstream fkernel(p->file_name().c_str());
94  if ( fkernel.fail() ) {
95  error_show(p->file_name().c_str(), __FILE__, __LINE__);
96  return -1;
97  }
98 
99  int i, x, y, z;
100 
101  fkernel << p->sizeX() << " " << p->sizeY() << " " << p->sizeZ() << endl;
102  for ( i=z=0; z<p->sizeZ(); z++ ) {
103  for ( y=0; y<p->sizeY(); y++ ) {
104  for ( x=0; x<p->sizeX(); x++, i++ )
105  fkernel << (*p)[i] << " ";
106  fkernel << endl;
107  }
108  }
109  fkernel << endl;
110 
111  fkernel.close();
112 
113  if ( verbose & VERB_DEBUG ) {
114  cout << "DEBUG write_kernel_file: kernel size=" << p->size() << endl;
115  for ( i=0; i<p->sizeX()*p->sizeY()*p->sizeZ(); i++ )
116  cout << (*p)[i] << " ";
117  cout << endl;
118  }
119 
120  return 0;
121 }
122 
verbose
int verbose
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
rwkernel.h
Header file for functions to read and write kernel files.
Vector3< long >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
parameter_file_path
Bstring parameter_file_path(Bstring filename)
Finds the parameter file path.
Definition: Bstring.cpp:795
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bimage::images
long images()
Definition: Bimage.h:504
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
writeKernel
int writeKernel(Bimage *p)
Writes a text kernel file.
Definition: rwkernel.cpp:89
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
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
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
readKernel
int readKernel(Bimage *p)
Reads a text kernel file.
Definition: rwkernel.cpp:21
kernel
Definition: kernlut.h:14