| Bsoft 2.0.5 Bernard's software package

rwDI.cpp
Go to the documentation of this file.
1
9 #include "rwDI.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
42 int readDI(Bimage* p, int readdata, int img_select)
43 {
44 ifstream* fimg = new ifstream(p->file_name());
45 if ( fimg->fail() ) return -1;
46
47 // extract the image attributes from the file header
48 int end_parse = 0;
49 int dataLength = 0;
50 long offset(0), x, y, n(0);
51 char aline[MAXLINELEN], *var;
52
53 // while ( end_parse == 0 && fimg->getline(aline, MAXLINELEN) != NULL ) {
54 while ( end_parse == 0 ) {
55 fimg->getline(aline, MAXLINELEN);
56 if ( !fimg->good() ) end_parse = 1;
57 if ( ( var = strstr(aline,"image list") ) != NULL )
58 n++; // Image
59 if ( ( var = strstr(aline,"Data offset") ) != NULL && offset < 1 )
60 sscanf(var,"Data offset: %ld",&offset); // Offset
61 if ( ( var = strstr(aline,"Data length") ) != NULL )
62 sscanf(var,"Data length: %d",&dataLength); // Length
63 if ( ( var = strstr(aline,"Samps/line") ) != NULL )
64 sscanf(var,"Samps/line: %ld",&x); // x
65 if ( ( var = strstr(aline,"Number of lines") ) != NULL )
66 sscanf(var,"Number of lines: %ld",&y); // y
67 if ( ( var = strstr(aline,"list end") ) != NULL )
68 end_parse = 1; // End
69 if ( aline[0] != '\\' )
70 end_parse = 1; // End
71 }
72
73 p->size(x, y, 1);
74 p->data_offset(offset);
75
76 if ( y < 2 ) p->sizeY(dataLength/(x*n*sizeof(short)));
77
78 long i, imgstart(0), imgend(n-1);
79
80 if ( img_select > -1 ) {
81 if ( img_select >= n ) img_select = n - 1;
82 p->images(1);
83 imgstart = imgend = img_select;
84 } else {
85 p->images(n);
86 img_select = -1;
87 }
88
89 p->channels(1);
90 p->data_type(Short);
91 p->minimum(-256*256);
92 p->maximum(256*256-1);
93
94 long image_size = p->channels()*p->sizeX()*p->sizeY()*p->sizeZ()*p->data_type_size();
95 unsigned char* data = NULL;
96
97 offset = p->data_offset() + imgstart*image_size;
98
99 if ( readdata ) {
100 p->data_alloc();
101 for ( i=imgstart, data=p->data_pointer(); i<=imgend; i++, data+=image_size, offset+=image_size )
102 fread_large(data, image_size, offset, fimg);
103 }
104
105 fimg->close();
106 delete fimg;
107
108 return 0;
109 }
110
rwDI.h
Header file for reading (only) Digital Instruments files.
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.
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
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
readDI
int readDI(Bimage *p, int readdata, int img_select)
Reading a Digital Instruments image file format. A two-image 2D image format used in Digital Instrume...
Definition: rwDI.cpp:42
Bimage::maximum
double maximum()
Definition: Bimage.h:535
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
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
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::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Short
Definition: utilities.h:152
Bimage::images
long images()
Definition: Bimage.h:491
verbose
int verbose
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