| Bsoft 2.0.5 Bernard's software package

rwGOODFORD.cpp
Go to the documentation of this file.
1
9 #include "rwGOODFORD.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
29 int readGOODFORD(Bimage *p, int readdata)
30 {
31 ifstream* fimg = new ifstream(p->file_name());
32 if ( fimg->fail() ) return -1;
33
34 GOODFORDhead* header = new GOODFORDhead;
35
36 fimg->read((char *)header, GOODFORDSIZE);
37 if ( fimg->fail() ) return -2;
38
39 // Determine byte order and swap bytes if from little-endian machine
40 unsigned char* b = (unsigned char *) header;
41 int i, sb = 0;
42 if ( ( abs( header->grdflg ) > SWAPTRIG ) || ( abs(header->km) > SWAPTRIG ) ) {
43 if ( verbose & VERB_PROCESS )
44 cerr << "Warning: Swapping header byte order for 4-byte types" << endl;
45 sb = 1;
46 int extent = GOODFORDSIZE; // Swap whole header
47 swapbytes(b, 4);
48 for ( i=76; i<extent; i+=4 ) swapbytes(b+i, 4);
49 }
50 /*
51 // Convert VAX floating point types if necessary
52 if ( header->h > 100 || header->h < 0.01 ) {
53 if ( verbose & VERB_PROCESS )
54 cerr << "Warning: Converting VAX floating point" << endl;
55 vax = 1;
56 vax2ieee(b+76, sb);
57 for ( i=116; i<132; i+=4 ) // All floating point parameters
58 vax2ieee(b+i, sb);
59 }
60 */
61 // Map the parameters
62 p->size(header->im, header->jm, header->km);
63 p->page_size(header->im, header->jm, 1);
64 p->images(1);
65 p->channels(1);
66 switch ( header->grdflg ) {
67 case 1: p->data_type(Float); break;
68 default: p->data_type(Float); break;
69 }
70 p->data_offset(GOODFORDSIZE + GFSLICESIZE + sizeof(int));
71 p->sampling(header->h, header->h, header->h);
72 p->label(header->title);
73
74 // Allocating the single sub-image and setting its origin
75 p->origin(-header->ox, -header->oy, -header->oz);
76
77 long pad = GFSLICESIZE + 2*sizeof(int);
78 long pagesize = p->sizeX()*p->sizeY()*p->data_type_size();
79 long offset = p->data_offset();
80 unsigned char* data = NULL;
81
82 if ( readdata ) {
83 p->data_alloc();
84 for ( i=0, data=p->data_pointer(); i<p->sizeZ(); i++, data+=pagesize, offset+=pagesize+pad )
85 fread_large(data, pagesize, offset, fimg);
86 if ( sb ) swapbytes(p->alloc_size(), p->data_pointer(), p->data_type_size());
87 }
88
89 fimg->close();
90 delete fimg;
91
92 delete header;
93
94 return 0;
95 }
96
103 int writeGOODFORD(Bimage *p)
104 {
105 p->color_to_simple();
106
107 p->change_type(Float);
108
109 GOODFORDhead* header = new GOODFORDhead;
110 memset(header, 0, sizeof(GOODFORDhead));
111
112 GFslice_head* slice_header = new GFslice_head;
113 memset(slice_header ,0, sizeof(GFslice_head));
114
115 // Map the parameters
116 memset(header->title, ' ', 72);
117 strncpy(header->title, p->label().c_str(), 72);
118 header->im = slice_header->im = p->sizeX();
119 header->jm = slice_header->jm = p->sizeY();
120 header->km = p->sizeZ();
121 switch ( p->data_type() ) {
122 case Float: header->grdflg = 1; break;
123 default: header->grdflg = 1; break;
124 }
125 p->data_offset(GOODFORDSIZE + GFSLICESIZE + sizeof(int));
126 header->ox = -p->image->origin()[0];
127 header->oy = -p->image->origin()[1];
128 header->oz = -p->image->origin()[2];
129 header->h = p->sampling()[0];
130 header->pad1 = header->pad8 = 160;
131 header->pad4 = header->pad6 = p->sizeZ();
132 header->pad5 = 1;
133 slice_header->pad1 = slice_header->pad2 = 12;
134
135 long i;
136 int pagesize = p->sizeX()*p->sizeY()*p->data_type_size();
137
138 ofstream fimg(p->file_name().c_str());
139 if ( fimg.fail() ) return -1;
140
141 unsigned char* data = p->data_pointer();
142
143 fimg.write((char *)header, GOODFORDSIZE);
144
145 for ( i=0; i<p->sizeZ(); i++, data += pagesize ) {
146 slice_header->k = i + 1;
147 fimg.write((char *)slice_header, GFSLICESIZE);
148 fimg.write((char *)&pagesize, sizeof(int));
149 fimg.write((char *)data, pagesize);
150 fimg.write((char *)&pagesize, sizeof(int));
151 }
152
153 fimg.close();
154
155 delete header;
156 delete slice_header;
157
158 return 0;
159 }
160
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
SWAPTRIG
#define SWAPTRIG
Definition: rwimg.h:16
GOODFORDhead::km
int km
Definition: rwGOODFORD.h:28
rwGOODFORD.h
Header file for reading and writing Peter Goodford's GRID files.
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
GFslice_head::pad1
int pad1
Definition: rwGOODFORD.h:38
GFSLICESIZE
#define GFSLICESIZE
Definition: rwGOODFORD.h:14
writeGOODFORD
int writeGOODFORD(Bimage *p)
Writing Peter Goodford's GRID map image file format. A 3D image format used for electrostatic potenti...
Definition: rwGOODFORD.cpp:103
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.
GOODFORDhead::jm
int jm
Definition: rwGOODFORD.h:27
GOODFORDhead::pad4
int pad4
Definition: rwGOODFORD.h:23
GFslice_head::im
int im
Definition: rwGOODFORD.h:40
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
GOODFORDhead::oy
float oy
Definition: rwGOODFORD.h:31
GOODFORDhead::title
char title[72]
Definition: rwGOODFORD.h:18
Float
Definition: utilities.h:157
GOODFORDhead::pad6
int pad6
Definition: rwGOODFORD.h:25
GFslice_head
Definition: rwGOODFORD.h:37
Bimage
General image parameter class.
Definition: Bimage.h:163
GOODFORDhead::ox
float ox
Definition: rwGOODFORD.h:30
GOODFORDhead::grdflg
int grdflg
Definition: rwGOODFORD.h:21
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
GOODFORDhead::pad8
int pad8
Definition: rwGOODFORD.h:34
GOODFORDhead::pad5
int pad5
Definition: rwGOODFORD.h:24
GOODFORDhead
Definition: rwGOODFORD.h:16
Bimage::label
void label(string s)
Definition: Bimage.h:235
GOODFORDhead::h
float h
Definition: rwGOODFORD.h:29
sb
int sb(0)
GFslice_head::jm
int jm
Definition: rwGOODFORD.h:41
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
GOODFORDhead::oz
float oz
Definition: rwGOODFORD.h:32
file_util.h
Header file for file checking functions.
Bimage::data_offset
long data_offset()
Definition: Bimage.h:265
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bimage::channels
long channels()
Definition: Bimage.h:371
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:504
readGOODFORD
int readGOODFORD(Bimage *p, int readdata)
Reading Peter Goodford's GRID map image file format. A 3D image format used for electrostatic potenti...
Definition: rwGOODFORD.cpp:29
GOODFORDSIZE
#define GOODFORDSIZE
Definition: rwGOODFORD.h:13
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
GFslice_head::k
int k
Definition: rwGOODFORD.h:39
GOODFORDhead::im
int im
Definition: rwGOODFORD.h:26
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
GOODFORDhead::pad1
int pad1
Definition: rwGOODFORD.h:17
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::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
verbose
int verbose
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
Bimage::page_size
Vector3< long > page_size()
Definition: Bimage.h:480
GFslice_head::pad2
int pad2
Definition: rwGOODFORD.h:42
Bimage::images
long images()
Definition: Bimage.h:491
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563