Bsoft  2.1.0
Bernard's software package
rwBCR.cpp
Go to the documentation of this file.
1 
9 #include "rwBCR.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 
48 int readBCR(Bimage* p, int readdata)
49 {
50  ifstream fimg;
51  fimg.open(p->file_name());
52  if ( fimg.fail() ) return -1;
53 
54  p->images(1);
55  p->channels(1);
56  p->sizeZ(1);
57 
58  long count(0), hsize(BCRSIZE);
59  char aline[MAXLINELEN];
60  Bstring s(" "), tag, val;
61 
62  while ( !fimg.eof() && count < hsize && s.length() ) {
63  fimg.getline(aline, MAXLINELEN);
64  s = aline;
65  count += s.length();
66 // cout << count << tab << s << endl;
67  if ( s.contains("=") ) {
68  tag = s.pre('=').no_space();
69  val = s.post('=').no_space();
70  if ( tag == "fileformat" ) {
71  if ( val.contains("bcrstm") )
72  p->data_type(UShort);
73  else
74  p->data_type(Float);
75  } else if ( tag == "headersize" ) {
76  hsize = val.integer();
77  } else if ( tag == "xpixels" ) {
78  p->sizeX(val.integer());
79  } else if ( tag == "ypixels" ) {
80  p->sizeY(val.integer());
81  }
82  }
83  }
84  s = 0;
85  fimg.clear();
86 
87 // cout << "Count: " << count << endl;
88 
89  p->data_offset(hsize);
90 
91  if ( readdata) {
92  fimg.seekg(p->data_offset(), ios_base::beg);
93 // cout << "offset: " << p->data_offset() << endl;
94  p->data_alloc();
95 // cout << "alloc_size: " << p->alloc_size() << endl;
96  fimg.read((char *)p->data_pointer(), p->alloc_size());
97  if ( fimg.fail() ) {
98  cerr << "Error: No data read!" << endl;
99  cerr << fimg.rdstate() << endl;
100  if (fimg.bad()) {
101  cerr << "I/O error while reading\n";
102  } else if (fimg.eof()) {
103  cerr << "End of file reached successfully\n";
104  } else if (fimg.fail()) {
105  cerr << "Non-integer data encountered\n";
106  }
107  return -1;
108  }
109  }
110 
111  fimg.close();
112 
113  return 0;
114 }
115 
122 {
123  ofstream fimg;
124  fimg.open(p->file_name());
125  if ( fimg.fail() ) return -1;
126 
127  Bstring header("fileformat = ");
128 
129  if ( p->data_type() == Float ) header += "bcrf\n";
130  else header += "bcrstm\n";
131  header += "headersize = " + Bstring(BCRSIZE, "%d\n");
132  header += "xpixels = " + Bstring(p->sizeX(), "%d\n");
133  header += "ypixels = " + Bstring(p->sizeY(), "%d\n");
134 
135 // cout << "header length: " << header.length() << endl;
136 // cout << header << "---" << endl;
137 // cout << BCRSIZE - header.length() << endl;
138 
139  header += Bstring(' ', BCRSIZE - header.length());
140 // cout << "header length: " << header.length() << endl;
141 
142  fimg.write((char *)header.c_str(), BCRSIZE);
143 
144  header = 0;
145 
146  fimg.write((char *)p->data_pointer(), p->alloc_size());
147 
148  fimg.close();
149 
150  return 0;
151 }
152 
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:45
writeBCR
int writeBCR(Bimage *p)
Writing a BCR-STM image format.
Definition: rwBCR.cpp:121
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:402
rwBCR.h
Header file for reading and writing AFM BCR-STM files.
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
readBCR
int readBCR(Bimage *p, int readdata)
Reading a BCR-STM image format.
Definition: rwBCR.cpp:48
Bimage::images
long images()
Definition: Bimage.h:504
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Bimage::alloc_size
long alloc_size() const
Definition: Bimage.h:267
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:389
Bimage::data_offset
long data_offset()
Definition: Bimage.h:288
UShort
@ UShort
Definition: utilities.h:155
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
verbose
int verbose
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.
Bstring::length
long length() const
Definition: Bstring.cpp:263
BCRSIZE
#define BCRSIZE
Definition: rwBCR.h:13
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
Bstring::no_space
Bstring no_space()
Definition: Bstring.cpp:340