Bsoft  2.1.0
Bernard's software package
rwPIC.cpp
Go to the documentation of this file.
1 
9 #include "rwPIC.h"
10 #include "utilities.h"
11 
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14 
27 int readPIC(Bimage *p, int readdata)
28 {
29  ifstream* fimg = new ifstream(p->file_name());
30  if ( fimg->fail() ) return -1;
31 
32  short titlen, nbr, nbc, sb = 0, pad;
33  char title[128];
34 
35  if ( verbose & VERB_DEBUG )
36  cout << "DEBUG readPIC: Reading PIC file = " << p->file_name() << endl;
37 
38  // Read and swap bytes if from different-endian machine
39  fimg->read( (char *)&pad, sizeof(short) ); if ( fimg->fail() ) return -2;
40  fimg->read( (char *)&titlen, sizeof(short) ); if ( fimg->fail() ) return -2;
41  if ( titlen > 256 ) {
42  sb = 1;
43  swapbytes((unsigned char *)&titlen, sizeof(short));
44  }
45  fimg->read( (char *)&pad, sizeof(short) ); if ( fimg->fail() ) return -2;
46  if ( !pad ) fimg->read( (char *)&pad, sizeof(short) ); if ( fimg->fail() ) return -2;
47  if ( titlen > 59 ) titlen = 59;
48  if ( titlen > 0 ) {
49  fimg->read( title, 2*titlen );
50  if ( fimg->fail() ) return -2;
51  title[2*titlen+1] = 0;
52  }
53  fimg->read( (char *)&pad, sizeof(short) ); if ( fimg->fail() ) return -2;
54  if ( !pad ) fimg->read( (char *)&pad, sizeof(short) ); if ( fimg->fail() ) return -2;
55  fimg->read( (char *)&nbr, sizeof(short) ); if ( fimg->fail() ) return -2;
56  fimg->read( (char *)&pad, sizeof(short) ); if ( fimg->fail() ) return -2;
57  if ( !pad ) fimg->read( (char *)&pad, sizeof(short) ); if ( fimg->fail() ) return -2;
58  fimg->read( (char *)&nbc, sizeof(short) ); if ( fimg->fail() ) return -2;
59 
60  if ( sb ) {
61  swapbytes((unsigned char *)&nbr, sizeof(short));
62  swapbytes((unsigned char *)&nbc, sizeof(short));
63  }
64 
65  if ( verbose & VERB_DEBUG ) {
66  cout << "DEBUG readPIC: sb=" << sb << endl;
67  cout << "DEBUG readPIC: title: " << title << " (length = " << 2*titlen << ")" << endl;
68  cout << "DEBUG readPIC: size=" << nbc << "x" << nbr << endl;
69  cout << "DEBUG readPIC: offset=" << 2*titlen + 8*sizeof(short) << endl;
70  }
71 
72  // Map the parameters
73  p->size(nbc, nbr, 1);
74  p->images(1);
75  p->channels(1);
77 // if ( p->file_name().contains(".bq") || p->file_name().contains(".BQ") )
78  if ( p->file_name().find(".bq") != string::npos || p->file_name().find(".BQ") != string::npos )
79  p->data_type(Float);
80  p->label(title);
81  p->data_offset(2*titlen + 8*sizeof(short));
82 
83 
84  long datatypesize = p->data_type_size();
85  long datasize = p->alloc_size();
86  long linesize = p->sizeX()*datatypesize;
87  long i, y, y2, pagesize;
88 // char buf[128];
89  char* data = NULL;
90 
91  // At least some PIC files are written with a variable page size
92  // with a maximum of 2042, padded with a short in between
93  if ( readdata ) {
94  data = (char *) p->data_alloc();
95  for ( y=0; y<p->sizeY(); y++ ) {
96  for ( i=1, y2=0; y2<p->sizeX(); y2+=2042, i++ ) {
97  pagesize = 2042;
98  if ( pagesize > linesize - y2 ) pagesize = linesize - y2;
99  fimg->read( (char *)&pad, sizeof(short) );
100  if ( fimg->fail() ) return -3;
101  if ( !pad ) fimg->read( (char *)&pad, sizeof(short) );
102  if ( fimg->fail() ) return -3;
103  fimg->read( data+y*linesize+y2, pagesize );
104  if ( fimg->fail() ) return -3;
105  }
106  }
107  }
108 
109  fimg->close();
110  delete fimg;
111 
112  if ( p->data_pointer() && p->data_type() == Float ) {
113  if ( verbose > 0 )
114  cout << "Warning: BQ files may be VMS G_float, not supported in Bsoft" << endl;
115  for ( y=0; y<datasize; y+=4 )
116  vax2ieee(p->data_pointer()+y, 1-sb);
117  }
118 
119  if ( verbose & VERB_DEBUG )
120  cout << "DEBUG readPIC: PIC file " << p->file_name() << " read" << endl;
121 
122  return 0;
123 }
124 
132 {
134  p->color_to_simple();
135 
136  short titlen, nbr, nbc, pad = 3;
137  char title[120];
138  memset(title, ' ', 120);
139 
140  // Map the parameters
141  strncpy(title, p->label().c_str(), 120);
142  titlen = strlen(title)/2 + 1;
143  nbc = p->sizeX();
144  nbr = p->sizeY();
145 
146  long datatypesize = p->data_type_size();
147  long linesize = p->sizeX()*datatypesize;
148 
149  ofstream fimg(p->file_name());
150  if ( fimg.fail() ) return -1;
151 
152  fimg.write( (char *)&pad, sizeof(short) );
153  fimg.write( (char *)&titlen, sizeof(short) );
154  fimg.write( (char *)&pad, sizeof(short) );
155  fimg.write( title, 2*titlen );
156  fimg.write( (char *)&pad, sizeof(short) );
157  fimg.write( (char *)&nbr, sizeof(short) );
158  fimg.write( (char *)&pad, sizeof(short) );
159  fimg.write( (char *)&nbc, sizeof(short) );
160 
161  // At least some PIC files are written with a variable page size
162  // with a maximum of 2042, padded with a short in between
163  long y, y2, pagesize;
164  char* data = (char *) p->data_pointer();
165 
166  pad = 3;
167  for ( y=0; y<p->sizeY(); y++ ) {
168  for ( y2=0; y2<p->sizeX(); y2+=2042 ) {
169  if ( p->sizeX() > 2042 ) pad = y2 + 1;
170  pagesize = 2042;
171  if ( pagesize > linesize - y2 ) pagesize = linesize - y2;
172  fimg.write( (char *)&pad, sizeof(short) );
173  fimg.write( data+y*linesize+y2, pagesize );
174  }
175  }
176 
177  fimg.close();
178 
179  return 0;
180 }
181 
vax2ieee
void vax2ieee(unsigned char *v, int sb)
Converts VAX floating point format to IEEE floating point format.
Definition: utilities.cpp:512
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
UCharacter
@ UCharacter
Definition: utilities.h:153
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
sb
int sb(0)
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1607
Bimage::images
long images()
Definition: Bimage.h:504
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Bimage::alloc_size
long alloc_size() const
Definition: Bimage.h:267
verbose
int verbose
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:468
Bimage::data_offset
long data_offset()
Definition: Bimage.h:288
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
writePIC
int writePIC(Bimage *p)
Writing a PIC image file format(s). A 2D image format used in electron microscopy.
Definition: rwPIC.cpp:131
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
rwPIC.h
Header file for reading and writing PIC's BP and BQ files.
readPIC
int readPIC(Bimage *p, int readdata)
Reading a PIC image file format(s). A 2D image format used in electron microscopy....
Definition: rwPIC.cpp:27
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
Bimage::label
void label(string s)
Definition: Bimage.h:250