| Bsoft 2.0.5 Bernard's software package

rwBIORAD.cpp
Go to the documentation of this file.
1
8 #include "rwBIORAD.h"
9 #include "file_util.h"
10 #include "utilities.h"
11
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14
15 int swap_header(BIORADhead* header)
16 {
17 unsigned char* b = (unsigned char *) header;
18 int i;
19 int extent = BIORADSIZE - 10; // exclude magnification from swapping
20
21 for ( i=0; i<extent; i+=2 ) swapbytes(b+i, 2);
22
23 swapbytes(b+extent, 4);
24
25 return 0;
26 }
27
40 int readBIORAD(Bimage* p, int readdata)
41 {
42 ifstream* fimg = new ifstream(p->file_name());
43 if ( fimg->fail() ) return -1;
44
45 BIORADhead* header = new BIORADhead;
46
47 fimg->read((char *)header, BIORADSIZE);
48 if ( fimg->fail() ) return -2;
49
50 // Determine byte order and swap bytes if from little-endian machine
51 int sb = 0;
52 if ( header->magic != 12345 ) {
53 if ( verbose & VERB_PROCESS )
54 cerr << "Warning: Swapping header byte order" << endl;
55 sb = 1;
56 swap_header(header);
57 }
58 if ( header->magic != 12345 ) {
59 cerr << "Error: The file " << p->file_name() << " is not a BioRad file!" << endl;
60 fimg->close();
61 delete fimg;
62 return -3;
63 }
64
65 // Map the parameters
66 p->size(header->nx, header->ny, header->nz);
67 p->images(1);
68 p->channels(1);
69 switch ( header->mode ) {
70 case 1: p->data_type(UCharacter); break;
71 case 0: p->data_type(UShort); break;
72 default: p->data_type(UCharacter); break;
73 }
74 p->minimum(header->black);
75 p->maximum(header->white);
76 p->data_offset(BIORADSIZE);
77
78
79 delete header;
80
81 if ( readdata ) {
82 p->data_alloc();
83 fread_large(p->data_pointer(), p->alloc_size(), p->data_offset(), fimg);
84 if ( sb ) swapbytes(p->alloc_size(), p->data_pointer(), p->data_type_size());
85 }
86
87 fimg->close();
88 delete fimg;
89
90 return 0;
91 }
92
99 int writeBIORAD(Bimage* p)
100 {
101 if ( p->data_type() != UCharacter && p->data_type() != UShort ) p->change_type(UCharacter);
102 p->color_to_simple();
103
104 BIORADhead* header = new BIORADhead;
105 memset(header, 0, sizeof(BIORADhead));
106
107 int sb = 0;
108 if ( systype(0) < LittleIEEE ) sb = 1;
109
110 // Map the parameters
111 header->nx = p->size()[0];
112 header->ny = p->size()[1];
113 header->nz = p->size()[2];
114 switch ( p->data_type() ) {
115 case UCharacter: header->mode = 1; break;
116 case UShort: header->mode = 0; break;
117 default: header->mode = 0; break;
118 }
119 header->black = (short) p->minimum();
120 header->white = (short) p->maximum();
121 header->magic = 12345; // Magic number for the BioRad format
122 p->data_offset(BIORADSIZE);
123
124 if ( sb ) {
125 swap_header(header);
126 swapbytes(p->alloc_size(), p->data_pointer(), p->data_type_size());
127 }
128
129 ofstream fimg(p->file_name());
130 if ( fimg.fail() ) return -1;
131
132 fimg.write((char *)header, p->data_offset());
133 if ( p->data_pointer() ) fimg.write((char *)p->data_pointer(), p->alloc_size());
134
135 fimg.close();
136
137 delete header;
138
139 return 0;
140 }
141
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
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
BIORADhead::mode
short mode
Definition: rwBIORAD.h:22
verbose
int verbose
BIORADhead
Definition: rwBIORAD.h:15
utilities.h
Header file for general utilities.
BIORADSIZE
#define BIORADSIZE
Definition: rwBIORAD.h:12
readBIORAD
int readBIORAD(Bimage *p, int readdata)
Reading a BioRad image file format.
Definition: rwBIORAD.cpp:40
writeBIORAD
int writeBIORAD(Bimage *p)
Writing a BioRad image file format.
Definition: rwBIORAD.cpp:99
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
rwBIORAD.h
Header file for reading and writing BioRad files.
BIORADhead::ny
short ny
Definition: rwBIORAD.h:17
swap_header
int swap_header(BIORADhead *header)
Definition: rwBIORAD.cpp:15
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
BIORADhead::black
short black
Definition: rwBIORAD.h:19
Bimage::maximum
double maximum()
Definition: Bimage.h:535
sb
int sb(0)
BIORADhead::white
short white
Definition: rwBIORAD.h:20
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::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
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
UShort
Definition: utilities.h:151
Bimage::images
long images()
Definition: Bimage.h:491
systype
SysType systype(int show)
Finds the system type - mostly just for byte order.
Definition: utilities.cpp:144
BIORADhead::magic
short magic
Definition: rwBIORAD.h:26
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bimage::minimum
double minimum()
Definition: Bimage.h:534
BIORADhead::nz
short nz
Definition: rwBIORAD.h:18
BIORADhead::nx
short nx
Definition: rwBIORAD.h:16