| Bsoft 2.0.5 Bernard's software package

rwSPE.cpp
Go to the documentation of this file.
1
9 #include "rwSPE.h"
10 #include "file_util.h"
11 #include "utilities.h"
12
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15
16 extern char* month[];
17
33 int readSPE(Bimage* p, int readdata, int img_select)
34 {
35 ifstream* fimg = new ifstream(p->file_name());
36 if ( fimg->fail() ) return -1;
37
38 if ( verbose & VERB_DEBUG )
39 cout << "DEBUG readSPE: header size = " << sizeof(SPEhead) << endl;
40
41 SPEhead* header = new SPEhead;
42
43 fimg->read((char *)header, SPESIZE);
44 if ( fimg->fail() ) return -2;
45
46 // Determine byte order and swap bytes if from little-endian machine
47 int sb = 0;
48 if ( header->datatype > 127 ) {
49 if ( verbose & VERB_PROCESS )
50 cerr << "Warning: Swapping header byte order" << endl;
51 sb = 1;
52 swapbytes((unsigned char *)&header->datatype, 2);
53 swapbytes((unsigned char *)&header->xdim, 2);
54 swapbytes((unsigned char *)&header->ydim, 2);
55 swapbytes((unsigned char *)&header->NumFrames, 2);
56 swapbytes((unsigned char *)&header->MaxIntensity, 4);
57 swapbytes((unsigned char *)&header->MinIntensity, 4);
58 }
59
60 p->label(header->Comments);
61
62 tm* t = p->get_localtime();
63 p->set_time(t);
64
65 if ( verbose & VERB_DEBUG )
66 cout << "DEBUG readSPE: xdim=" << header->xdim << " ydim=" << header->ydim << " NumFrames=" << header->NumFrames << endl;
67
68 // Map the parameters
69 long nimg(header->NumFrames);
70 p->size(header->xdim, header->ydim, 1);
71 p->channels(1);
72
73 if ( img_select > -1 ) {
74 if ( img_select >= nimg ) img_select = nimg - 1;
75 nimg = 1;
76 }
77
78 p->images(nimg);
79
80 switch ( header->datatype ) {
81 case 0: p->data_type(Float); break;
82 case 1: p->data_type(Integer); break;
83 case 2: p->data_type(Short); break;
84 case 3: p->data_type(UShort); break;
85 case 4: p->data_type(SCharacter); break;
86 case 5: p->data_type(Double); break;
87 case 6: p->data_type(SCharacter); break;
88 case 7: p->data_type(UCharacter); break;
89 default: p->data_type(UShort); break;
90 }
91
92 p->maximum(header->MaxIntensity);
93 p->minimum(header->MinIntensity);
94
95 p->data_offset(SPESIZE);
96
97 delete header;
98
99 long readsize = p->alloc_size();
100
101 if ( readdata ) {
102 p->data_alloc();
103 fread_large(p->data_pointer(), readsize, p->data_offset(), fimg);
104 if ( sb ) swapbytes(readsize, p->data_pointer(), p->data_type_size());
105 }
106
107 fimg->close();
108 delete fimg;
109
110 return 0;
111 }
112
119 int writeSPE(Bimage* p)
120 {
121 p->color_to_simple();
122 if ( p->data_type() != UCharacter && p->data_type() != UShort &&
123 p->data_type() != Short && p->data_type() != Float ) p->change_type(Float);
124
125 SPEhead* header = new SPEhead;
126 memset(header, 0, sizeof(SPEhead));
127
128 strncpy(header->Comments, p->label().c_str(), 400);
129
130 // Map the parameters
131 tm* t = p->get_localtime();
132 snprintf(header->date, 10, "%02d%3s%04d", t->tm_mday, month[t->tm_mon], t->tm_year);
133 header->xdim = p->sizeX();
134 header->ydim = p->sizeY();
135 header->NumFrames = p->images();
136 switch ( p->data_type() ) {
137 case UCharacter: header->datatype = 7; break;
138 case SCharacter: header->datatype = 6; break;
139 case UShort: header->datatype = 3; break;
140 case Short: header->datatype = 2; break;
141 case Integer: header->datatype = 1; break;
142 case Float: header->datatype = 0; break;
143 case Double: header->datatype = 5; break;
144 default: header->datatype = 0; break;
145 }
146 p->data_offset(SPESIZE);
147
148 header->MaxIntensity = p->maximum();
149 header->MinIntensity = p->minimum();
150
151 long datasize = p->alloc_size();
152
153 ofstream fimg(p->file_name());
154 if ( fimg.fail() ) return -1;
155
156 fimg.write((char *)header, p->data_offset());
157
158 fimg.write((char *)p->data_pointer(), datasize);
159
160 fimg.close();
161
162 delete header;
163
164 return 0;
165 }
166
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
SPEhead::Comments
char Comments[400]
Definition: rwSPE.h:24
SPEhead::datatype
short datatype
Definition: rwSPE.h:22
SPEhead::xdim
short xdim
Definition: rwSPE.h:20
month
char * month[]
Definition: rwimg.cpp:55
SPEhead::date
char date[10]
Definition: rwSPE.h:18
SPEhead
Definition: rwSPE.h:16
verbose
int verbose
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.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Integer
Definition: utilities.h:154
SCharacter
Definition: utilities.h:150
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
SPEhead::MinIntensity
float MinIntensity
Definition: rwSPE.h:30
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
SPEhead::MaxIntensity
float MaxIntensity
Definition: rwSPE.h:29
Float
Definition: utilities.h:157
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
Bimage::maximum
double maximum()
Definition: Bimage.h:535
Double
Definition: utilities.h:158
writeSPE
int writeSPE(Bimage *p)
Writing a SPE image file format.
Definition: rwSPE.cpp:119
Bimage::label
void label(string s)
Definition: Bimage.h:235
Bimage::set_time
void set_time(time_t t)
Definition: Bimage.h:249
sb
int sb(0)
readSPE
int readSPE(Bimage *p, int readdata, int img_select)
Reading a SPE image file format.
Definition: rwSPE.cpp:33
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
file_util.h
Header file for file checking functions.
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
UCharacter
Definition: utilities.h:149
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
fread_large
int fread_large(unsigned char *aptr, size_t pagesize, size_t offset, ifstream *fimg)
Reads blocks of memeory no larger than 1Gb.
Definition: file_util.cpp:347
Bimage::alloc_size
long alloc_size() const
Definition: Bimage.h:252
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
Short
Definition: utilities.h:152
UShort
Definition: utilities.h:151
rwSPE.h
Header file for reading and writing SPE files.
Bimage::images
long images()
Definition: Bimage.h:491
SPEhead::ydim
short ydim
Definition: rwSPE.h:26
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bimage::minimum
double minimum()
Definition: Bimage.h:534
SPESIZE
#define SPESIZE
Definition: rwSPE.h:13
SPEhead::NumFrames
short NumFrames
Definition: rwSPE.h:28