| Bsoft 2.0.5 Bernard's software package

rwIP.cpp
Go to the documentation of this file.
1
9 #include "rwIP.h"
10 #include "utilities.h"
11
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14
50 int readIP(Bimage* p, int readdata)
51 {
52 ifstream* fimg = new ifstream(p->file_name());
53 if ( fimg->fail() ) return -1;
54
55 // extract the image attributes from the file header
56 int bpp = 2, sb = 0;
57 long offset(IPSIZE), x, y;
58 char aline[MAXLINELEN] = "", *value;
59 Vector3<double> sampling(1,1,1);
60
61 if ( systype(0) < LittleIEEE ) sb = 1;
62
63 while ( !fimg->eof() && !strstr(aline, "COMMENT") ) {
64 fimg->getline(aline, MAXLINELEN);
65 value = strchr(aline, '=');
66 if ( value ) value += 1;
67 if ( strstr(aline, "HEADER") )
68 sscanf(value, "%ld", &offset); // Header size
69 if ( strstr(aline, "YPIXEL") )
70 sscanf(value, "%ld", &x); // x
71 if ( strstr(aline, "XPIXEL") )
72 sscanf(value, "%ld", &y); // y
73 if ( strstr(aline, "BYTE PER PIXEL") )
74 sscanf(value, "%d", &bpp); // bytes per pixel
75 if ( strstr(aline, "XRESOLUTION") )
76 sscanf(value, "%lf", &sampling[0]); // x unit
77 if ( strstr(aline, "YRESOLUTION") )
78 sscanf(value, "%lf", &sampling[1]); // y unit
79 }
80 p->data_offset(offset);
81
82 p->label(0);
83 while ( !fimg->eof() && strchr(aline, '\n') ) {
84 fimg->getline(aline, MAXLINELEN);
85 p->label(p->label() + aline);
86 }
87
88 p->size(x, y, 1);
89 p->images(1);
90 p->channels(1);
91 p->data_type(UShort);
92 if ( bpp > 2 ) p->data_type(Integer);
93 p->sampling(sampling);
94
95
96 if ( readdata ) {
97 p->data_alloc();
98 fimg->read((char *)p->data_pointer(), p->alloc_size());
99 if ( fimg->fail() ) return -3;
100 if ( sb ) swapbytes(p->alloc_size(), p->data_pointer(), p->data_type_size());
101 }
102
103 fimg->close();
104
105 delete fimg;
106
107 return 0;
108 }
109
116 int writeIP(Bimage* p)
117 {
118 if ( p->data_type() < Integer )
119 p->change_type(UShort);
120 else
121 p->change_type(Integer);
122
123 char* header = new char[IPSIZE];
124 memset(header, 0, IPSIZE);
125 p->data_offset(IPSIZE);
126
127 // int sb = 0;
128 // if ( systype(0) < LittleIEEE ) sb = 1;
129
130 snprintf(header, IPSIZE, "DITABIS micron Data File\r\n"
131 "CREATED = %s"
132 "HEADER = %ld\r\n"
133 "YPIXEL = %ld\r\n"
134 "XPIXEL = %ld\r\n"
135 "BYTE PER PIXEL = %ld\r\n"
136 "XRESOLUTION = %g\r\n"
137 "YRESOLUTION = %g\r\n"
138 "THUMB-NAIL-ZOOM = 10\r\n"
139 "MAGNIFICATION = 1\r\n"
140 "OFFSET = 0\r\n"
141 "OFFSET CORRECTION = NO\r\n"
142 // "GAIN = 10000\r\n"
143 // "LASER = 50\r\n"
144 // "PARAMS = Standard.set\r\n"
145 // "FORMAT = Standard.fmt\r\n"
146 // "CHANNEL = PMT LOWSENS\r\n"
147 // "MICRON-MARK = \r\n"
148 "COMMENT\r\n%s",
149 asctime(p->get_localtime()), p->data_offset(), p->sizeX(), p->sizeY(),
150 p->data_type_size(), p->sampling()[0], p->sampling()[1], p->label().c_str());
151
152 ofstream fimg(p->file_name());
153 if ( fimg.fail() ) return -1;
154
155 fimg.write((char *)header, p->data_offset());
156 if ( p->data_pointer() ) fimg.write((char *)p->data_pointer(), p->alloc_size());
157
158 fimg.close();
159
160 delete[] header;
161
162 return 0;
163 }
164
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
LittleIEEE
Definition: utilities.h:131
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1585
utilities.h
Header file for general utilities.
rwIP.h
Header file for reading and writing image plate reader files.
Integer
Definition: utilities.h:154
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:44
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
Bimage
General image parameter class.
Definition: Bimage.h:163
writeIP
int writeIP(Bimage *p)
Writing an image plate reader file format.
Definition: rwIP.cpp:116
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
Bimage::label
void label(string s)
Definition: Bimage.h:235
sb
int sb(0)
readIP
int readIP(Bimage *p, int readdata)
Reading an image plate reader file format.
Definition: rwIP.cpp:50
verbose
int verbose
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bimage::data_offset
long data_offset()
Definition: Bimage.h:265
Bimage::get_localtime
tm * get_localtime()
Definition: Bimage.h:245
Bimage::channels
long channels()
Definition: Bimage.h:371
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:504
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bimage::alloc_size
long alloc_size() const
Definition: Bimage.h:252
Vector3< double >
UShort
Definition: utilities.h:151
Bimage::images
long images()
Definition: Bimage.h:491
IPSIZE
#define IPSIZE
Definition: rwIP.h:13
systype
SysType systype(int show)
Finds the system type - mostly just for byte order.
Definition: utilities.cpp:144
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349