File: binary_io.cpp

package info (click to toggle)
ghemical 1.01-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,984 kB
  • ctags: 19,443
  • sloc: ansic: 69,073; cpp: 60,583; fortran: 35,324; sh: 5,419; makefile: 506; perl: 91
file content (674 lines) | stat: -rw-r--r-- 23,629 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/**********************************************************************
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
***********************************************************************/

//THIS
#include "binary_io.h"

using namespace std;

namespace OpenBabel {

//test byte ordering
static int SSINT = 0x00000001;
static unsigned char *SSTPTR = (unsigned char*)&SSINT;
bool SSwabInt = (SSTPTR[0]!=0);
static const int TMPSIZE = sizeof(double);

/******************** reading generic binary data *************************/
unsigned int OB_io_read_binary(char* ccc, char *x, unsigned int size, unsigned int count)
{
    if (x == NULL || size == 0)
    	return 0;

    if (size == 1)
    {
    	memcpy(x, ccc, count);
    	return count;
    }
    else
    {
    	unsigned int sz = size * count;
    	if (!SSwabInt)
    		memcpy(x, ccc, sz);
    	else
    	{
    		unsigned int i,j,k;
    		unsigned char c, tmp[TMPSIZE];

    		for ( i = 0 ; i < sz ; i += size )
    		{
    			memcpy(tmp, &ccc[i], size);
    			for (j = 0, k = size - 1 ; j < k ; j++, k--) {
    				c = tmp[j]; tmp[j] = tmp[k]; tmp[k] = c;
    			}
    			memcpy(&x[i], tmp, size);
    		}
    	}
    	return sz;
    }
}
unsigned int OB_io_read_binary(istream& ifs, char *x, unsigned int size, unsigned int count)
{
    if (x == NULL || size == 0)
    	return 0;

    if (size == 1)
    {
    	ifs.read(x, count);
    	return count;
    }
    else
    {
    	unsigned int sz = size * count;
    	if (!SSwabInt)
    		ifs.read(x, sz);
    	else
    	{
    		unsigned int i,j,k;
    		unsigned char c, tmp[TMPSIZE];

    		for ( i = 0 ; i < count ; i++ )
    		{
    			ifs.read((char*)&tmp[0], size);
    			for (j = 0, k = size - 1 ; j < k ; j++, k--) {
    				c = tmp[j]; tmp[j] = tmp[k]; tmp[k] = c;
    			}
    			memcpy(&x[i*size], tmp, size);
    		}
    	}
    	return sz;
    }
}

/******************** writing generic binary data *************************/
unsigned int OB_io_write_binary(char *ccc, const char *x, unsigned int size, unsigned int count)
{
    if (x == NULL || size == 0)
    	return 0;

    if (size == 1)
    {
    	memcpy(ccc, x, count);
    	return count;
    }
    else
    {
    	unsigned int sz = size * count;
    	if (!SSwabInt)
    		memcpy(ccc, x, sz);
    	else
    	{
    		unsigned i,j,k;
    		unsigned char c, tmp[TMPSIZE];

    		for (i = 0 ; i < sz ; i += size)
    		{
    			memcpy(tmp, &x[i], size);
    			for (j = 0, k = size - 1 ; j < k ; j++, k-- ) {
    				c = tmp[j]; tmp[j] = tmp[k]; tmp[k] = c;
    			}
    			memcpy(&ccc[i], tmp, size);
    		}
    	}
    	return sz;
    }
}
unsigned int OB_io_write_binary(ostream &ofs, const char *x, unsigned int size, unsigned int count)
{
    if (x == NULL || size == 0)
    	return 0;

    if (size == 1)
    {
    	ofs.write(x, count);
    	return count;
    }
    else
    {
    	unsigned int sz = size * count;
    	if (!SSwabInt)
    		ofs.write(x, sz);
    	else
    	{
    		unsigned i,j,k;
    		unsigned char c, tmp[TMPSIZE];

    		for (i = 0 ; i < sz ; i += size)
    		{
    			memcpy(tmp, &x[i], size);
    			for (j = 0, k = size - 1 ; j < k ; j++, k-- ) {
    				c = tmp[j]; tmp[j] = tmp[k]; tmp[k] = c;
    			}
    			ofs.write((char*)&tmp[0], size);
    		}
    	}
    	return sz;
    }
}

/**********************reading STL strings****************************/

unsigned int OB_io_read_binary(char* ccc, string& str)
{
    char *buffer;
    unsigned int i = 0, idx = 0;

    idx    += OB_io_read_binary(ccc, (char*) &i, sizeof(unsigned int), 1);
    buffer  = new char[i+1];
    idx    += OB_io_read_binary(&ccc[idx], buffer, sizeof(char), i);

    buffer[i] = '\0';
    str       = buffer;

	delete [] buffer;

    return idx;
}
unsigned int OB_io_read_binary(istream& ifs, string& str)
{
    char *buffer;
    unsigned int i = 0, idx = 0;

    idx    += OB_io_read_binary(ifs, (char*) &i, sizeof(unsigned int), 1);
    buffer  = new char[i+1];
    idx    += OB_io_read_binary(ifs, buffer, sizeof(char), i);

    buffer[i] = '\0';
    str       = buffer;

	delete [] buffer;

    return idx;
}

/******************** writing STL strings *************************/

unsigned int OB_io_write_binary(char* ccc, const string& str)
{
    unsigned int idx = 0, size = str.size();

    idx += OB_io_write_binary(ccc, (char*) &size, sizeof(unsigned int), 1);
    idx += OB_io_write_binary(&ccc[idx], str.c_str(), sizeof(char), size);

    return idx;
}
unsigned int OB_io_write_binary(ostream& ofs, const string& str)
{
    unsigned int idx = 0, size = str.size();

    idx += OB_io_write_binary(ofs, (char*) &size, sizeof(unsigned int), 1);
    idx += OB_io_write_binary(ofs, str.c_str(), sizeof(char), size);

    return idx;
}

/******************* writing compressed data *************************/
/*!
**\fn OB_io_write_binary_compressed(char*ccc, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
**\brief Writes an unsigned interger array out in a platform independed
**compressed binary format.
**\param ccc A character array that the binary is written to
**\param x An unsigned integer array to be written to ccc.
**\param NumBits The number of bits that will be used to store
**each unsigned integer.  The largest value in x must be less than
**2^NumBits.
**\param NumInts The number of integers in x
**\return The number of bytes written to ccc.  This value should
**always be 1+(NumBits*NumInts)/8 (integer division).
*/
unsigned int OB_io_write_binary_compressed(char*ccc, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
  {
    //If there are no values or x is NULL do nothing
    if (!NumInts || x==NULL) return 0;
 
    //If 32 or more bits are specified simply write uncompressed
    if (NumBits >=32) return OB_io_write_binary(ccc,(const char*) x, sizeof(unsigned int), NumInts);

    bool swap = !SSwabInt; 
    unsigned int bitarraysize = 1+(NumBits*NumInts)/8;
    unsigned char *cc = (unsigned char*) ccc;
    unsigned int i,j;
    for (i=0 ; i<bitarraysize ; i++) cc[i] = 0;
    unsigned char mask1[8],mask2[8];
    mask2[7] = 127;
    mask2[6] = 63;
    mask2[5] = 31;
    mask2[4] = 15;
    mask2[3] = 7;
    mask2[2] = 3;
    mask2[1] = 1;
    mask2[0] = 0;
    for (i=0 ; i<8 ; i++) mask1[i] = ~mask2[i];
    unsigned int bitshift=0;
    unsigned int bytes=0;
    unsigned int value;
    unsigned char *cptr = (unsigned char*)&value;
    for (i=0 ; i<NumInts ; i++) {
        if (swap) {
            cptr[0] = ((unsigned char*)(&x[i]))[3]; 
            cptr[1] = ((unsigned char*)(&x[i]))[2]; 
            cptr[2] = ((unsigned char*)(&x[i]))[1]; 
            cptr[3] = ((unsigned char*)(&x[i]))[0]; 
          }
        else      {value = x[i];}
        for (j=0 ; j<NumBits/8 ; j++) {
            cc[bytes+0+j] |= (cptr[j]<<(bitshift  ))&mask1[bitshift];
            cc[bytes+1+j] |= (cptr[j]>>(8-bitshift))&mask2[bitshift];
          }
        cc[bytes+0+j] |= (cptr[j]<<(bitshift  ))&mask1[bitshift];
        if ((bitshift + NumBits%8) > 7) cc[bytes+1+j] |= (cptr[j]>>(8-bitshift))&mask2[bitshift];
        bitshift += NumBits;
        while (bitshift > 7) {bitshift-=8; bytes++;}
      }
    return bitarraysize;
  }
/*!
**\fn OB_io_write_binary_compressed(ostream& ostr, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
**\brief Writes an unsigned interger array out in a platform independed
**compressed binary format.
**\param ostr An output stream that the binary is written to
**\param x An unsigned integer array to be written to ostr.
**\param NumBits The number of bits that will be used to store
**each unsigned integer.  The largest value in x must be less than
**2^NumBits.
**\param NumInts The number of integers in x
**\return The number of bytes written to ostr.  This value should
**always be 1+(NumBits*NumInts)/8 (integer division).
*/
unsigned int OB_io_write_binary_compressed(ostream& ostr, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
  {
    //If there are no values or x is NULL do nothing
    if (!NumInts || x==NULL) return 0;
 
    //If 32 or more bits are specified simply write uncompressed
    if (NumBits >=32) return OB_io_write_binary(ostr,(const char*) x, sizeof(unsigned int), NumInts);

    unsigned int bitarraysize = 1+(NumBits*NumInts)/8;
    char *cc = new char [bitarraysize];
    unsigned int idx = OB_io_write_binary_compressed(cc,x,NumBits,NumInts);
    ostr.write(cc,bitarraysize);
    delete [] cc;
    return idx;
  }

/*!
**\fn OB_io_write_binary_compressed(char*ccc, float *x, unsigned int NumBits, unsigned int Nfloats)
**\brief Writes a float array out in a platform independed
**compressed binary format.
**\param ccc A character array that the binary is written to
**\param x A float array to be written to ccc.
**\param NumBits The number of bits that will be used to store
**each unsigned integer.  The lost of resolution of the float
**values depends directly on how many bits are used.
**\param Nfloats The number of floats in x.
**\return The number of bytes written to ccc.  This value should
**always be 9+(NumBits*NumInts)/8 (integer division).
**\par Compression Info
**There is a loss of resolution of the floats using this routine.
**The greater the value of NumBits and the smaller the range of values
**in x, the smaller the loss of resolution.  The function 
**OB_io_util_calc_NumBits(float *x, unsigned int N, float res)
**will calculate how many bits are required for a given resolution and
**set of floats.
*/
unsigned int OB_io_write_binary_compressed(char*ccc, float *x, unsigned int NumBits, unsigned int Nfloats)
  {
    unsigned int idx=0;

    //If there are no values of x is NULL do nothing
    if (!Nfloats || x==NULL) return 0;

    //If 32 or more bits are specified simply write uncompressed
    if (NumBits >=32) return OB_io_write_binary(&ccc[idx],(const char*) x, sizeof(unsigned int), Nfloats);



    //Write max and min
    unsigned int i;
    float max = x[0];
    float min = x[0];
    for (i=0 ; i<Nfloats ; i++) {
        if (x[i] > max) max = x[i];
        if (x[i] < min) min = x[i];
      }
    idx += OB_io_write_binary(&ccc[idx],(const char*)&max,sizeof(float),1);
    idx += OB_io_write_binary(&ccc[idx],(const char*)&min,sizeof(float),1);

    //Integerize values and write out 
    unsigned int maxint = (unsigned int) 0; 
    for (i=0 ; i<NumBits ; i++) maxint |= ((unsigned int)1)<<i;
    unsigned int *ix = new unsigned int [Nfloats]; 
    if (max!=min) {
        for (i=0 ; i<Nfloats ; i++) 
            ix[i] = (unsigned int) (((float)maxint*((x[i]-min)/(max-min))) + 0.499999999f);
      }
    else for (i=0 ; i<Nfloats ; i++) ix[i] = (unsigned int) 0;
    idx += OB_io_write_binary_compressed(&ccc[idx],ix,NumBits,Nfloats);
    delete [] ix;  

    return idx;
  }
/*!
**\fn OB_io_write_binary_compressed(ostream& ostr, float *x, unsigned int NumBits, unsigned int Nfloats)
**\brief Writes a float array out in a platform independed
**compressed binary format.
**\param ostr A character array that the binary is written to
**\param x A float array to be written to ostr.
**\param NumBits The number of bits that will be used to store
**each unsigned integer.  The lost of resolution of the float
**values depends directly on how many bits are used.
**\param Nfloats The number of floats in x.
**\return The number of bytes written to ccc.  This value should
**always be 9+(NumBits*NumInts)/8 (integer division).
**\par Compression Info
**There is a loss of resolution of the floats using this routine.
**The greater the value of NumBits and the smaller the range of values
**in x, the smaller the loss of resolution.  The function
**OB_io_util_calc_NumBits(float *x, unsigned int N, float res)
**will calculate how many bits are required for a given resolution and
**set of floats.
*/
unsigned int OB_io_write_binary_compressed(ostream& ostr, float *x, unsigned int NumBits, unsigned int Nfloats)
  {
    unsigned int idx=0;

    //If there are no values of x is NULL do nothing
    if (!Nfloats || x==NULL) return 0;

    //If 32 or more bits are specified simply write uncompressed
    if (NumBits >=32) return OB_io_write_binary(ostr,(const char*) x, sizeof(unsigned int), Nfloats);

    //Write max and min
    unsigned int i;
    float max = x[0];
    float min = x[0];
    for (i=0 ; i<Nfloats ; i++) {
        if (x[i] > max) max = x[i];
        if (x[i] < min) min = x[i];
      }
    idx += OB_io_write_binary(ostr,(const char*)&max,sizeof(float),1);
    idx += OB_io_write_binary(ostr,(const char*)&min,sizeof(float),1);
 
    //Integerize values and write out
    unsigned int maxint = (unsigned int) 0;
    for (i=0 ; i<NumBits ; i++) maxint |= ((unsigned int)1)<<i;
    unsigned int *ix = new unsigned int [Nfloats];
    if (max!=min) {
        for (i=0 ; i<Nfloats ; i++)
            ix[i] = (unsigned int) (((float)maxint*((x[i]-min)/(max-min))) + 0.499999999f);
      }
    else for (i=0 ; i<Nfloats ; i++) ix[i] = (unsigned int) 0;
    idx += OB_io_write_binary_compressed(ostr,ix,NumBits,Nfloats);
    delete [] ix;


    return idx;
  }

/******************* reading compressed data *************************/
/*!
**\fn OB_io_read_binary_compressed(char*ccc, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
**\brief Reads compressed binary data that was written with either
**OB_io_write_binary_compressed(char*ccc, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
**or
**OB_io_write_binary_compressed(ostream& ostr, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
**\param ccc A character array the binary is being read from
**\param x An array of unsigned ints the binary data will be extracted to
**\param NumBits The number of bits used to store each unsigned integer
**\param NumInts The number of integers stored in ccc
**\return The number of bytes read from ccc.  This value should
**always be 1+(NumBits*NumInts)/8 (integer division).
*/
unsigned int OB_io_read_binary_compressed(char*ccc, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
  {
    //If there are no values or x is NULL do nothing
    if (!NumInts || x==NULL) return 0;
 
    //If 32 or more bits are specified simply read uncompressed
    if (NumBits >=32) return OB_io_read_binary(ccc,(char*) x, sizeof(unsigned int), NumInts);

    //Read the values
    bool swap = !SSwabInt;
    unsigned int bitarraysize = 1+(NumBits*NumInts)/8;
    unsigned char *cc = (unsigned char*) ccc;
    unsigned int i,j;
    unsigned int mask = (unsigned int) 0;
    for (i=0 ; i<NumBits ; i++) mask |= ((unsigned int) 1) << i;
    unsigned char mask1[8],mask2[8];
    mask1[0] = 255;
    mask1[1] = 127;
    mask1[2] = 63;
    mask1[3] = 31;
    mask1[4] = 15;
    mask1[5] = 7;
    mask1[6] = 3;
    mask1[7] = 1;
    for (i=0 ; i<8 ; i++) mask2[i] = ~mask1[i];
    unsigned int bitshift=0;
    unsigned int bytes=0;
    unsigned int value;
    unsigned char *cptr = (unsigned char*)&value;
    for (i=0 ; i<NumInts ; i++) {
        value = (unsigned int) 0;
        for (j=0 ; j<NumBits/8 ; j++) {
            cptr[j]  = (cc[bytes+0+j]>>(  bitshift))&mask1[bitshift];
            cptr[j] |= (cc[bytes+1+j]<<(8-bitshift))&mask2[bitshift];
          }
        cptr[j]  = (cc[bytes+0+j]>>(  bitshift))&mask1[bitshift];
        if ((bitshift + NumBits%8) > 7) cptr[j] |= (cc[bytes+1+j]<<(8-bitshift))&mask2[bitshift];
 
        bitshift += NumBits;
        while (bitshift > 7) {bitshift-=8; bytes++;}
        if (swap) {
            ((unsigned char*)(&x[i]))[3] = cptr[0]; 
            ((unsigned char*)(&x[i]))[2] = cptr[1]; 
            ((unsigned char*)(&x[i]))[1] = cptr[2]; 
            ((unsigned char*)(&x[i]))[0] = cptr[3]; 
          }
        else x[i] = value;
        x[i] &= mask;
      }

    return bitarraysize;
  }

/*!
**\fn OB_io_read_binary_compressed(istream& istr, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
**\brief Reads compressed binary data that was written with either
**OB_io_write_binary_compressed(char*ccc, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
**or
**OB_io_write_binary_compressed(ostream& ostr, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
**\param istr An input file stream the binary is being read from
**\param x An array of unsigned ints the binary data will be extracted to
**\param NumBits The number of bits used to store each unsigned integer
**\param NumInts The number of integers stored in istr 
**\return The number of bytes read from istr.  This value should
**always be 1+(NumBits*NumInts)/8 (integer division).
*/
unsigned int OB_io_read_binary_compressed(istream& istr, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
  {
    //If there are no values or x is NULL do nothing
    if (!NumInts || x==NULL) return 0;
 
    //If 32 or more bits are specified simply read uncompressed
    if (NumBits >=32) return OB_io_read_binary(istr,(char*) x, sizeof(unsigned int), NumInts);

    unsigned int bitarraysize = 1+(NumBits*NumInts)/8;
    char *cc = new char [bitarraysize];
    istr.read(cc,bitarraysize);
    unsigned int idx = OB_io_read_binary_compressed(cc,x,NumBits,NumInts);
    delete [] cc;
    return idx;
  }

/*!
**\fn OB_io_read_binary_compressed(char*ccc, float *x, unsigned int NumBits, unsigned int Nfloats)
**\brief Reads compressed binary data that was written with either
**OB_io_write_binary_compressed(char*ccc, float *x, unsigned int NumBits, unsigned int NumInts)
**or
**OB_io_write_binary_compressed(ostream& ostr, float *x, unsigned int NumBits, unsigned int NumInts)
**\param ccc A character array the binary is being read from
**\param x An array of floats the binary data will be extracted to
**\param NumBits The number of bits used to store each float
**\param Nfloats The number of floats to be read 
**\return The number of bytes read from ccc.  This value should
**always be 1+(NumBits*NumInts)/8 (integer division).
*/
unsigned int OB_io_read_binary_compressed(char*ccc, float *x, unsigned int NumBits, unsigned int Nfloats)
  {
    unsigned int idx=0;

    //If there are no values or x is NULL do nothing
    if (!Nfloats || x==NULL) return 0;
 
    //If 32 or more bits are specified simply write uncompressed
    if (NumBits >=32) return OB_io_read_binary(&ccc[idx],(char*) x, sizeof(unsigned int), Nfloats);

    //Read max and min
    float max;
    float min;
    idx += OB_io_read_binary(&ccc[idx],(char*)&max,sizeof(float),1);
    idx += OB_io_read_binary(&ccc[idx],(char*)&min,sizeof(float),1);
 
    //Read in values
    unsigned int i;
    unsigned int maxint = (unsigned int) 0;
    for (i=0 ; i<NumBits ; i++) maxint |= ((unsigned int)1)<<i;
    unsigned int *ix = new unsigned int [Nfloats];
    idx += OB_io_read_binary_compressed(&ccc[idx],ix,NumBits,Nfloats);
    for (i=0 ; i<Nfloats ; i++) x[i] = (max-min)*((float)ix[i]/(float)maxint)+min;
    delete [] ix;

    return idx;
  }

/*!
**\fn OB_io_read_binary_compressed(istream& istr, float *x, unsigned int NumBits, unsigned int Nfloats)
**\brief Reads compressed binary data that was written with either
**OB_io_write_binary_compressed(char*ccc, float *x, unsigned int NumBits, unsigned int NumInts)
**or
**OB_io_write_binary_compressed(ostream& ostr, float *x, unsigned int NumBits, unsigned int NumInts)
**\param istr An input stream  the binary is being read from
**\param x An array of floats the binary data will be extracted to
**\param NumBits The number of bits used to store each float
**\param Nfloats The number of float values to be read
**\return The number of bytes read from istr.  This value should
**always be 1+(NumBits*NumInts)/8 (integer division).
*/
unsigned int OB_io_read_binary_compressed(istream& istr, float *x, unsigned int NumBits, unsigned int Nfloats)
  {
    unsigned int idx=0;

    //If there are no values or x is NULL do nothing
    if (!Nfloats || x==NULL) return 0;
 
    //If 32 or more bits are specified simply write uncompressed
    if (NumBits >=32) return OB_io_read_binary(istr,(char*) x, sizeof(unsigned int), Nfloats);

    //Read max and min
    float max;
    float min;
    idx += OB_io_read_binary(istr,(char*)&max,sizeof(float),1);
    idx += OB_io_read_binary(istr,(char*)&min,sizeof(float),1);
 
    //Read in values
    unsigned int i;
    unsigned int maxint = (unsigned int) 0;
    for (i=0 ; i<NumBits ; i++) maxint |= ((unsigned int)1)<<i;
    unsigned int *ix = new unsigned int [Nfloats];
    idx += OB_io_read_binary_compressed(istr,ix,NumBits,Nfloats);
    for (i=0 ; i<Nfloats ; i++) x[i] = (max-min)*((float)ix[i]/(float)maxint)+min;
    delete [] ix;

    return idx;
  }


/*!
**\fn OB_io_util_calc_NumBits(unsigned int *x, unsigned int N)
**\param x an unsigned integer array
**\paran N The number of values in the unsigned integer array x.
**\return The number of bits per value required to store the
**unsigned integer values in x with the 
**OB_io_write_binary_compressed(char*ccc, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
**and
**OB_io_write_binary_compressed(ostream& ostr, unsigned int *x, unsigned int NumBits, unsigned int NumInts)
** functions.
*/
unsigned int OB_io_util_calc_NumBits(unsigned int *x, unsigned int N)
  {
    if (!N) return 0;
    unsigned int i;
    unsigned int max=0;
    for (i=0 ; i<N ; i++) if (x[i] > max) max = x[i];
    unsigned int mmax=(unsigned int) 0;
    for (i=0 ; i<32 && max > mmax ; i++) mmax |= ((unsigned int)1)<<i;
    return i;
  }

/*!
**\fn OB_io_util_calc_NumBits(float *x, unsigned int N, float res)
**\param x A float array
**\param N The number of values in the float array x.
**\param The resolution desired.
**\returns The number of bits per value required to store float
**the float values in x at a resolution of res (or better) with the
**OB_io_write_binary_compressed(char*ccc, float *x, unsigned int NumBits, unsigned int Nfloats)
**or
**OB_io_write_binary_compressed(istream& istr, float *x, unsigned int NumBits, unsigned int Nfloats)
**functions.
*/
unsigned int OB_io_util_calc_NumBits(float *x, unsigned int N, float res)
  {
    if (!N) return 0;

    //Find max and min
    unsigned int i;
    float max = x[0];
    float min = x[0];
    for (i=0 ; i<N ; i++) {
        if (x[i] > max) max = x[i];
        if (x[i] < min) min = x[i];
      }

    unsigned int imax = (unsigned int) ((max-min)/res+0.5f);
    unsigned int mmax=(unsigned int) 0;
    for (i=0 ; i<32 && imax > mmax ; i++) mmax |= ((unsigned int)1)<<i;
    return i;
  }


/******************* Writing OBBinaryIO objects ************************/
ostream& operator<<(ostream& ostr, const OBBinaryIO& obj)
  {
    unsigned int size = obj.BinarySize();
    OB_io_write_binary(ostr,(const char*)&size,sizeof(unsigned int),1);
    obj.WriteBinary(ostr);
    return ostr;
  }
istream& operator>>(istream& istr, OBBinaryIO& obj)
  {
    unsigned int size;
    OB_io_read_binary(istr,(char*)&size,sizeof(unsigned int),1);
    unsigned int size2;
    size2 = obj.ReadBinary(istr);
    if (size != size2) {
        cerr << "WARNING operator>>(istream& istr, OBBinaryIO& obj) " << endl;
        cerr << "Record size (" << size << ") is not equal to number of bytes read (" << size2 << ")" << endl;
      }
    return istr;
  }


}// End namespace OpenBabel