File: iofits.c

package info (click to toggle)
qfits 6.2.0-8
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 2,976 kB
  • ctags: 1,019
  • sloc: ansic: 11,631; sh: 8,330; makefile: 131
file content (1347 lines) | stat: -rw-r--r-- 43,141 bytes parent folder | download | duplicates (5)
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
/* $Id: iofits.c,v 1.4 2006/02/17 10:26:07 yjung Exp $
 *
 * This file is part of the ESO QFITS Library
 * Copyright (C) 2001-2004 European Southern Observatory
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Author: yjung $
 * $Date: 2006/02/17 10:26:07 $
 * $Revision: 1.4 $
 * $Name: qfits-6_2_0 $
 */

/*-----------------------------------------------------------------------------
                                   Includes
 -----------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <string.h>
#include <ctype.h>

/*-----------------------------------------------------------------------------
                                   Define
 -----------------------------------------------------------------------------*/

#define NM_SIZ            512
#define FITS_BLSZ        2880
#define ONE_MEGABYTE    (1024 * 1024)

#ifndef SEEK_SET
#define SEEK_SET    0
#endif

#define ENDIAN_UNKNOWN      -1
#define ENDIAN_MOTOROLA      0
#define ENDIAN_INTEL         1

/*-----------------------------------------------------------------------------
                                   Macros
 -----------------------------------------------------------------------------*/

#define ENDIAN_NESS(f)          ((f)&1)
#define IEEE_FLOAT_COMPAT(f)    ((f)&2)
#define IEEE_DOUBLE_COMPAT(f)   ((f)&4)

/*-----------------------------------------------------------------------------
                                   typedefs
 -----------------------------------------------------------------------------*/

typedef short            short16 ;
typedef unsigned short    ushort16 ;
typedef int                long32 ;
typedef unsigned char    byte ;

/*-----------------------------------------------------------------------------
                               Function prototypes
 -----------------------------------------------------------------------------*/

static int bits_per_byte(void);
static int convert_fits_pixel_depth(char*, char*, int) ;
static int get_FITS_header_size(char*);
static int filesize(char*) ;
static int get_bitpix(char*) ;
static int set_bitpix(char*, int) ;
static int get_npix(char*) ;
static int write_pixbuf(char*, char*, int, int, int) ;
static int test_machine_formats(void) ;
static void swap_bytes(void*, int);
static void check_all_types(int) ;

/*-----------------------------------------------------------------------------
                               Global variables
 -----------------------------------------------------------------------------*/

static unsigned compat_flags ;

/*-----------------------------------------------------------------------------
                                  Main
 -----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
    int        ptype ;

    if (argc!=4) {
        printf("\n\n") ;
        printf("use: %s <in> <out> <8|16|32|-32|-64>\n", argv[0]) ;
        printf("\n") ;
        printf("\t<in> is a valid FITS file in the current directory\n") ;
        printf("\t<out> is the name of the output file\n") ;
        printf("\t<8|16|32|-32|-64> is the output file pixel type\n") ;
        printf("\n\n") ;
        return 0 ;
    }
    ptype = (int)atoi(argv[3]) ;
    check_all_types(ptype) ;
    if (!strcmp(argv[1], argv[2])) {
        fprintf(stderr, "cannot convert a file to itself\n") ;
        fprintf(stderr, "specify another name for the output\n") ;
        return -1 ;
    }
    return convert_fits_pixel_depth(argv[1], argv[2], ptype) ;
}

/*-----------------------------------------------------------------------------
                                  Functions code
 -----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/**
  @brief    convert FITS files from one pixel depth to another
  @param    name_in     input file name
  @param    name_out    output file name    
  @param    ptype_out   Output pixel type
  @return   int 0 if Ok, anything else otherwise
  Heavy use of mmap() to speed up the process
 */
/*----------------------------------------------------------------------------*/
static int convert_fits_pixel_depth(
        char    *    name_in,
        char    *    name_out,
        int            ptype_out)
{
    int            fd_in,
                fd_out ;
    char    *    buf_in ;
    char    *    buf_out ;
    int            fsize_in,
                fsize_out,
                header_size,
                padd_size ;
    int            ptype_in ;
    int            bpp_out ;
    int            npix ;
    char    *    zero ;
    int            yet_to_dump ;
    int            buf_dump ;
    
    /* Open input file and get pixel depth and number of pixels */
    fsize_in = filesize(name_in) ;
    header_size = get_FITS_header_size(name_in) ;
    if ((fd_in = open(name_in, O_RDONLY)) == -1) {
        fprintf(stderr, "cannot open file %s: aborting\n", name_in) ;
        return -1 ;
    }
    buf_in = (char*)mmap(0, fsize_in, PROT_READ, MAP_SHARED, fd_in, 0) ;
    if (buf_in == (char*)-1) {
        perror("mmap") ;
        fprintf(stderr, "cannot mmap file: %s\n", name_in) ;
        close(fd_in) ;
        return -1 ;
    }
    ptype_in = get_bitpix(buf_in) ;
    if (ptype_in == 0) {
        close(fd_in) ; munmap(buf_in, fsize_in) ;
        return -1 ;
    }
    if (ptype_in == -32) {
        if (!IEEE_FLOAT_COMPAT(compat_flags)) {
            fprintf(stderr,
                "this machine does not support IEEE floating point values\n");
            fprintf(stderr,
                "cannot convert from -32 type\n");
            exit(-1) ;
        }
    }
    if (ptype_in == -64) {
        if (!IEEE_DOUBLE_COMPAT(compat_flags)) {
            fprintf(stderr,
                "this machine does not support IEEE double values\n");
            fprintf(stderr,
                "cannot convert from -64 type\n");
            exit(-1) ;
        }
    }

    /* Compute the size of the output file: header size+pixel area+padding */
    npix = get_npix(buf_in) ;
    if (npix < 1) {
        close(fd_in) ;
        munmap(buf_in, fsize_in) ;
        return -1 ;
    }
    bpp_out = ptype_out / 8 ;
    if (bpp_out < 0) {
        bpp_out = - bpp_out ;
    }
    fsize_out = header_size + npix * bpp_out ;
    if ((fsize_out % FITS_BLSZ) == 0) {
        padd_size = 0 ;
    } else {
        padd_size = FITS_BLSZ - fsize_out % FITS_BLSZ ;
    }
    fsize_out += padd_size ;

    /* Now create the output file and fill it with zeros, then mmap it. */
    /* The permissions are rw-rw-r-- by default. */
    if ((fd_out=creat(name_out,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH))==-1){
        perror("creat") ;
        fprintf(stderr, "cannot create file %s: aborting\n", name_out) ;
        close(fd_in) ; munmap(buf_in, fsize_in) ;
        return -1 ;
    }
    zero = calloc(ONE_MEGABYTE, 1) ; 
    yet_to_dump = fsize_out ;
    if (fsize_out < ONE_MEGABYTE) {
        buf_dump = fsize_out ;
    } else {
        buf_dump = ONE_MEGABYTE ;
    }
    while (yet_to_dump>0) {
        if (write(fd_out, zero, buf_dump) != buf_dump) {
            perror("write") ;
            fprintf(stderr, "error writing to the output file: aborting\n") ;
            close(fd_in) ; close(fd_out) ; munmap(buf_in, fsize_in) ;
            free(zero) ;
            return -1 ;
        }
        yet_to_dump -= buf_dump ;
        if (yet_to_dump > ONE_MEGABYTE) {
            buf_dump = ONE_MEGABYTE ;
        } else {
            buf_dump = yet_to_dump ;
        }
    }
    free(zero) ;
    close(fd_out) ;
    if ((fd_out = open(name_out, O_RDWR)) == -1) {
        perror("open") ;
        fprintf(stderr, "cannot reopen file %s for writing\n", name_out);
        close(fd_in) ; munmap(buf_in, fsize_in) ;
        return -1 ;
    }

    buf_out = (char*)mmap(0,
                         fsize_out,
                         PROT_READ | PROT_WRITE,
                         MAP_SHARED,
                         fd_out,
                         0) ;
    if (buf_out == (char*)-1) {
        perror("mmap") ;
        fprintf(stderr, "cannot mmap file: %s\n", name_out) ;
        munmap(buf_in, fsize_in) ; close(fd_in) ; close(fd_out) ;
        return -1 ;
    }

    /* Copy FITS header from input to output, modify BITPIX */
    memcpy(buf_out, buf_in, header_size) ;
    if (set_bitpix(buf_out, ptype_out)!=0) {
        fprintf(stderr, "error writing BITPIX in output: aborting\n");
        munmap(buf_in, fsize_in) ; munmap(buf_out, fsize_out) ;
        close(fd_in) ; close(fd_out) ;
        return -1 ;
    }

    /* Now write pixels */
    write_pixbuf(buf_in + header_size,
                 buf_out + header_size,
                 npix,
                 ptype_in,
                 ptype_out) ;

    /* Blank-pad the output file if needed */
    if (padd_size) {
        if (lseek(fd_out, fsize_out-padd_size, SEEK_SET)==-1) {
            perror("lseek");
            fprintf(stderr, "error seeking output file: aborting\n");
        } else {
            zero = calloc(padd_size, 1) ;
            if (write(fd_out, zero, 1)==-1) {
                perror("write") ;
                fprintf(stderr, "error writing into output file: aborting\n");
            }
            free(zero) ;
        }
    }

    /* Close, unmap, goodbye */
    close(fd_in) ;
    close(fd_out) ;
    munmap(buf_in, fsize_in) ;
    munmap(buf_out, fsize_out) ;
    return 0 ;
}

/*----------------------------------------------------------------------------*/
/**
  @brief    compute the size (in bytes) of a FITS header
  @param    name    FITS file name
  @return   int
  Should always be a multiple of 2880. This implementation assumes only that 80
  characters are found per line.
 */
/*----------------------------------------------------------------------------*/
static int get_FITS_header_size(char * name)
{
    FILE    *   in ;
    char        line[81] ;
    int         found = 0 ;
    int         count ;
    int            hs ;

    if ((in = fopen(name, "r")) == NULL) {
        fprintf(stderr, "cannot open %s: aborting\n", name) ;
        return 0 ;
    }
    count = 0 ;
    while (!found) {
        if (fread(line, 1, 80, in)!=80) break ;
        count ++ ;
        if (!strncmp(line, "END ", 4)) found = 1 ;
    }
    fclose(in);

    if (!found) return 0 ;
    /* The size is the cards nb x 80, rounded to the nextmultiple of 2880 */
    hs = count * 80 ;
    if ((hs % FITS_BLSZ) != 0) hs = (1+(hs/FITS_BLSZ)) * FITS_BLSZ ;
    return hs ;
}

/*----------------------------------------------------------------------------*/
/**
  @brief    how many bytes can I read from this file
  @param    filename    Name of file
  @return   size of the file in bytes
  Strongly non portable. Only on Unix systems! 
 */
/*----------------------------------------------------------------------------*/
static int filesize(char *filename)
{
    int        size ;
    struct stat    fileinfo ;

    /* POSIX compliant  */
    if (stat(filename, &fileinfo) != 0) size = (int)0 ;
    else size = (int)fileinfo.st_size ;
    return size ;
}

/*----------------------------------------------------------------------------*/
/**
  @brief    gets the value of BITPIX from a FITS header
  @param    buf     allocated char buffer containing the whole FITS header
  @return   int 8 16 32 -32 or -64 or 0 if cannot find it
 */
/*----------------------------------------------------------------------------*/
static int get_bitpix(char * buf)
{
    int        bitpix ;
    char *    where ;

    where = strstr(buf, "BITPIX") ;
    if (where == NULL) {
        fprintf(stderr, "cannot find BITPIX in header: aborting\n") ;
        return 0 ;
    }
    sscanf(where, "%*[^=] = %d", &bitpix) ;
    /* Check the returned value makes sense */
    if ((bitpix != 8) &&
        (bitpix != 16) &&
        (bitpix != 32) &&
        (bitpix != -32) &&
        (bitpix != -64)) {
        bitpix = 0 ;
    }
    return bitpix ;
}

/*----------------------------------------------------------------------------*/
/**
  @brief    overwrites the value of BITPIX in a FITS header
  @param    buf     allocated char buffer containing the whole FITS header
  @param    ptype   pixel type to write
  @return   int 0 if Ok, anything else otherwise
 */
/*----------------------------------------------------------------------------*/
static int set_bitpix(char * buf, int ptype)
{
    char *     where ;
    char    replace[81] ;

    where = strstr(buf, "BITPIX") ;
    if (where == NULL) {
        fprintf(stderr, "cannot find BITPIX in header: aborting\n") ;
        return -1 ;
    }
    sprintf(replace,
            "BITPIX  =                  % 3d / Bits per pixel                                 ",
            ptype) ;
    memcpy(where, replace, 80) ;
    return 0 ;
}

/*----------------------------------------------------------------------------*/
/**
  @brief    retrieves how many pixels in a FITS file from the header
  @param    buf     allocated char buffer containing the whole FITS header
  @return   int # of pixels in the file
  Does not support extensions!
 */
/*----------------------------------------------------------------------------*/
static int get_npix(char * buf)
{
    int        naxes ;
    int        npix ;
    int        naxis ;
    char  * where ;
    char    lookfor[80] ;
    int        i ;

    where = strstr(buf, "NAXIS") ;
    if (where == NULL) {
        fprintf(stderr, "cannot find NAXIS in header: aborting\n") ;
        return 0 ;
    }
    sscanf(where, "%*[^=] = %d", &naxes) ;
    if ((naxes<1) || (naxes>999)) {
        fprintf(stderr, "illegal value for %s: %d\n", lookfor, naxes) ;
        return 0 ;
    }
    npix = 1 ;
    for (i=1 ; i<=naxes ; i++) {
        sprintf(lookfor, "NAXIS%d", i) ;
        where = strstr(buf, lookfor) ;
        if (where == NULL) {
            fprintf(stderr, "cannot find %s in header: aborting\n",
                    lookfor) ;
            return 0 ;
        }
        sscanf(where, "%*[^=] = %d", &naxis) ;
        if (naxis<1) {
            fprintf(stderr, "error: found %s=%d\n", lookfor, naxis);
            return 0 ;
        }
        npix *= naxis ;
    }
    return npix ;
}

/*----------------------------------------------------------------------------*/
/**
  @brief    transfers a pixel buffer from one area to another
  @param    bi      allocated char buffer containing the whole in FITS header
  @param    bo      allocated char buffer containing the whole out FITS header
  @param    npix    number of pixels in input area 
  @param    ptype_in    input pixel type
  @param    ptype_out   output pixel type
  @return   int 0 if Ok, anything else otherwise
  Optimized code, very limited readability!!!
  Pixel format conversion on the fly if needed.
 */
/*----------------------------------------------------------------------------*/
static int write_pixbuf(
        char    *        bi,
        char    *        bo,
        int                npix,
        int                ptype_in,
        int                ptype_out)
{
    int                    bpp ;
    register int         i ;
    register char *        ip ;
    register char *        op ;
    double                d ;
    float                f ;
    char                a2[2] ;
    char                a4[4] ;
    char                a8[8] ;

    /*
     * Trivial case of identical copies
     *    8 to  8
     *   16 to 16
     *   32 to 32
     *  -32 to-32
     *  -64 to-64
     */

    if (ptype_in == ptype_out) {
        /* How many bytes per pixel? */
        bpp = ptype_in / 8 ; if (bpp < 0) bpp = - bpp ;
        /* Copy the buffer as it is and return */
        memcpy(bo, bi, npix * bpp) ; 
        return 0 ;
    }

    /* Use registers to boost speed */
    ip = bi ;
    op = bo ;

    /* ---------- 8 bit to something  */

    /*
     * 8 to 16
     * one byte in input becomes the LSB in the 2 output bytes
     * we transfer it directly in the LSB, this is independent from
     * machine endian-ness.
     */

    if ((ptype_in == 8) && (ptype_out == 16)) {
        for (i=0 ; i<npix ; i++) {
            op++ ;
            *op++ |= *ip++ ;
        }
        return 0 ;
    }

    /*
     * 8 to 32
     * One byte in input becomes the LSB in the 4 output bytes
     * we transfer it directly in the LSB, this is independent from
     * machine endian-ness.
     */

    if ((ptype_in == 8) && (ptype_out == 32)) {
        for (i=0 ; i<npix ; i++) {
            op++ ;
            op++ ;
            op++ ;
            *op++ |= *ip++ ;
        }
        return 0 ;
    }

    /*
     * 8 to -32
     * Conversion achieved through cast of the input byte into a float
     * this means we use the local float type, need to know endian-ness
     */

    if ((ptype_in == 8) && (ptype_out == -32)) {
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                f = (float)(*(byte*)ip) ;
                memcpy(op, &f, 4) ;
                ip ++ ;
                op+=4 ;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                f = (float)(*(byte*)ip) ;
                swap_bytes(&f, sizeof(float)) ;
                memcpy(op, &f, 4) ;
                ip ++ ;
                op+=4 ;
            }
        }
        return 0 ;
    }

    /*
     * 8 to -64
     * Conversion achieved through cast of the input byte into a double
     * this means we use the local float type, need to know endian-ness
     */
    if ((ptype_in == 8) && (ptype_out == -64)) {
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                d = (double)(*(byte*)ip) ;
                memcpy(op, &d, 8) ;
                ip++ ;
                op+=8 ;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                d = (double)(*(byte*)ip) ;
                swap_bytes(&d, sizeof(double)) ;
                memcpy(op, &d, 8) ;
                ip++ ;
                op+=8 ;
            }
        }
        return 0 ;
    }

    /* ---------- 16 bit to something */

    /*
     * 16 to 8
     * We only keep the 8 LSBits. If the MSB is set, it means the input
     * number is negative (FITS 16 bit is signed), so we clip to 0.  If
     * the MSB is cleared but any other bit in the above 8 is set, it
     * means the input is greater than 0xff, so we clip to 0xff.
     */

    if ((ptype_in == 16) && (ptype_out == 8)) {
        fprintf(stderr, "warning: loss of precision in output\n") ;
        for (i=0 ; i<npix ; i++) {
            if (!*ip) {
                ip++ ;
                *op++ = *ip++ ;
            } else {
                if (*ip & 0x80) {
                    *op++ = 0 ;
                    ip ++ ;
                    ip ++ ;
                } else {
                    *op++ = (byte)0xff ;
                    ip ++ ;
                    ip ++ ;
                }
            }
        }
        return 0 ;
    }

    /*
     * 16 to 32
     * We only need to transfer the 16 input bits as LSB for the output
     * 32 bits. No need to know local endian-ness.
     */
    if ((ptype_in == 16) && (ptype_out == 32)) {
        for (i=0 ; i<npix ; i++) {
            op++ ;
            op++ ;
            *op++ = *ip++ ;
            *op++ = *ip++ ;
        }
        return 0 ;
    }

    /*
     * 16 to -32
     * Conversion to IEEE float is done through a cast of the input
     * number. Need to know local endian-ness.
     */
    if ((ptype_in == 16) && (ptype_out == -32)) {
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                /* signed short to float conversion */
                f = (float)(*(short16*)ip) ;
                memcpy(op, &f, 4) ;
                ip ++ ;
                ip ++ ;
                op+=4 ;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                /* swap to get a local signed short */
                memcpy(a2, ip, 2) ;    
                swap_bytes(a2, 2) ;
                /* signed short to float conversion */
                f = (float)(*(short16*)&a2[0]) ;
                /* swap to write as Motorola data */
                swap_bytes(&f, sizeof(float)) ;
                memcpy(op, &f, 4) ;
                ip ++ ;
                ip ++ ;
                op+=4 ;
            }
        }
        return 0 ;
    }

    /*
     * 16 to -64
     * Conversion to IEEE double is done through a cast of the input
     * number. Need to know local endian-ness.
     */
    if ((ptype_in == 16) && (ptype_out == -64)) {
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                /* signed short to double conversion */
                d = (double)(*(short16*)ip) ;
                memcpy(op, &d, 8) ;
                ip++ ;
                ip++ ;
                op+=8 ;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                /* swap to get a local signed short */
                memcpy(a2, ip, 2) ;
                swap_bytes(a2, 2) ;
                /* signed short to double conversion */
                d = (double)(*(short16*)&a2[0]) ;
                /* swap back to Motorola format */
                swap_bytes(&d, sizeof(double)) ;
                memcpy(op, &d, 8) ;
                ip++ ;
                ip++ ;
                op+=8 ;
            }
        }
        return 0 ;
    }

    /* ---------- 32 bit to something */

    /*
     * 32 to 8
     * Loss of the 24 upper bits: as for 16->8 we try to find if the
     * input is negative by looking at the MSB and in that case clip to
     * zero. If the MSB is cleared but any other of the upper 24 bits is
     * set, the number is greater than 0xff and is clipped to 0xff.
     */
    if ((ptype_in == 32) && (ptype_out == 8)) {
        fprintf(stderr, "warning: loss of precision in output\n") ;
        for (i=0 ; i<npix ; i++) {
            /*
             * if first bit is set, the number is negative on 32 bits,
             * should be clipped to 0 on 8 bits
             */
            if (*ip & 0x80) {
                *op++ = 0 ;
            } else if ((byte)*ip |
                       (byte)*(ip+1) |
                       (byte)*(ip+2)) {
            /*
             * if any bit is set in the first 24 bits, the number is
             * greater than 0xff, should be clipped to 0xff
             */
                *op++ = (byte)0xff ;
            } else {
                *op++ = *(ip+3) ;
            }
            ip += 4 ;
        }
        return 0 ;
    }

    /*
     * 32 to 16
     * Loss of the 16 upper bits.
     * We convert the input to a local signed long to see if it
     * overflows a signed short. If it does, it is clipped.
     */
    if ((ptype_in == 32) && (ptype_out == 16)) {
        fprintf(stderr, "warning: loss of precision in output\n") ;
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                if ((*(long32*)ip) > 32767) {
                    *op++ = (byte)0x7f ;
                    *op++ = (byte)0xff ;
                    ip += 4 ;
                } else if ((*(long32*)ip) < -32768) {
                    *op++ = (byte)0x80 ;
                    *op++ = (byte)0x00 ;
                    ip += 4 ;
                } else {
                    ip+=2 ;
                    *op++ = *ip++ ;
                    *op++ = *ip++ ;
                }
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                /* construct a local signed long to check its value */
                memcpy(a4, ip, 4) ;
                swap_bytes(a4, 4) ;
                if ((*(long32*)&a4[0]) > 32767) {
                    *op++ = (byte)0x7f ;
                    *op++ = (byte)0xff ;
                    ip += 4 ;
                } else if ((*(long32*)&a4[0]) < -32768) {
                    *op++ = (byte)0x80 ;
                    *op++ = (byte)0x00 ;
                    ip += 4 ;
                } else {
                    ip+=2 ;
                    *op++ = *ip++ ;
                    *op++ = *ip++ ;
                }
            }
        }
        return 0 ;
    }

    /*
     * 32 to -32
     * We cast the input to a local float, then output it to disk.
     */
    if ((ptype_in == 32) && (ptype_out == -32)) {
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                /* conversion through cast to local float */
                f = (float)(*(long32*)ip) ;
                memcpy(op, &f, 4) ;
                ip+=4 ;
                op+=4 ;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                /* conversion through swap and cast to a local float */
                memcpy(a4, ip, 4) ;
                swap_bytes(a4, 4) ;
                f = (float)(*(long32*)&a4[0]) ;
                /* construct a motorola float in the output */
                swap_bytes(&f, sizeof(float)) ;
                memcpy(op, &f, 4) ;
                ip+=4 ;
                op+=4 ;
            }
        }
        return 0 ;
    }

    /*
     * 32 to -64
     * Conversion through cast to a local double, then output to disk.
     */
    if ((ptype_in == 32) && (ptype_out == -64)) {
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                d = (double)(*(long32*)ip) ;
                memcpy(op, &d, 8) ;
                ip+=4 ;
                op+=8 ;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                /* build a local double though swap and cast */
                memcpy(a4, ip, 4) ;
                swap_bytes(a4, 4) ;
                d = (double)(*(long32*)&a4[0]) ;
                /* construct a Motorola double for output */
                swap_bytes(&d, sizeof(double)) ;
                memcpy(op, &d, 8) ;
                ip+=4 ;
                op+=8 ;
            }
        }
        return 0 ;
    }

    /* ---------- -32 to something */

    /*
     * -32 to 8
     * Construct the input float through cast (need to know endian-ness).
     * Clip it to [0..255], round the value to the smaller integer.
     */
    if ((ptype_in == -32) && (ptype_out == 8)) {
        fprintf(stderr, "warning: loss of precision in output\n") ;
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                f = *((float*)ip) ;
                if (f>255.0) {
                    *op++ = (byte)0xff ;
                } else if (f<0.0) {
                    *op++ = (byte)0x00 ;
                } else {
                    *op++ = (byte)(f+0.5) ;
                }
                ip += 4;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                memcpy(a4, ip, 4) ;
                swap_bytes(a4, 4) ;
                f = *((float*)&a4[0]) ;
                if (f>255.0) {
                    *op++ = (byte)0xff ;
                } else if (f<0.0) {
                    *op++ = (byte)0x00 ;
                } else {
                    *op++ = (byte)(f+0.5) ;
                }
                ip += 4;
            }
        }

        return 0 ;
    }

    /*
     * -32 to 16
     * Construct the input float through cast (need to know endian-ness)
     * Clip it to [-32768..32767], round the value to the smaller
     * integer.
     */
    if ((ptype_in == -32) && (ptype_out == 16)) {
        fprintf(stderr, "warning: loss of precision in output\n") ;
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                f = *((float*)ip) ;
                if (f>32767.0) {
                    *op++ = (byte)0x7f ;
                    *op++ = (byte)0xff ;
                } else if (f<-32768.0) {
                    *op++ = (byte)0x80 ;
                    *op++ = 0x00 ;
                } else {
                    *op++ = (short16)f >> 8 ;
                    *op++ = (short16)f & (byte)0xff ;
                }
                ip+=4 ;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                memcpy(a4, ip, 4) ;
                swap_bytes(a4, 4) ;
                f = *((float*)&a4[0]) ;
                if (f>32767.0) {
                    *op++ = (byte)0x7f ;
                    *op++ = (byte)0xff ;
                } else if (f<-32768.0) {
                    *op++ = (byte)0x80 ;
                    *op++ = (byte)0x00 ;
                } else {
                    *op++ = (short16)f >> 8 ;
                    *op++ = (short16)f & (byte)0xff ;
                }
                ip+=4 ;
            }
        }
        return 0 ;
    }

    /*
     * -32 to 32
     * Construct the input float through cast (need to know endian-ness)
     * Clip it to [-2147483648 .. 2147483647], then round to the smaller
     * integer.
     */
    if ((ptype_in == -32) && (ptype_out == 32)) {
        fprintf(stderr, "warning: probable loss of precision in output\n") ;
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                f = *((float*)ip) ;
                if (f>2147483647.0) {
                    *op++ = (byte)0x7f ;
                    *op++ = (byte)0xff ;
                    *op++ = (byte)0xff ;
                    *op++ = (byte)0xff ;
                } else if (f<-2147483648.0) {
                    *op++ = (byte)0x80 ;
                    *op++ = (byte)0x00 ;
                    *op++ = (byte)0x00 ;
                    *op++ = (byte)0x00 ;
                } else {
                    *op++ = (long32)f >> 24 ;
                    *op++ = ((long32)f >> 16) & 0xff ;
                    *op++ = ((long32)f >> 8)  & 0xff ;
                    *op++ = (long32)f & 0xff ;
                }
                ip+=4 ;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                memcpy(a4, ip, 4) ;
                swap_bytes(a4, 4) ;
                f = *((float*)&a4[0]) ;
                if (f>2147483647.0) {
                    *op++ = (byte)0x7f ;
                    *op++ = (byte)0xff ;
                    *op++ = (byte)0xff ;
                    *op++ = (byte)0xff ;
                } else if (f<-2147483648.0) {
                    *op++ = (byte)0x80 ;
                    *op++ = (byte)0x00 ;
                    *op++ = (byte)0x00 ;
                    *op++ = (byte)0x00 ;
                } else {
                    *op++ = (long32)f >> 24 ;
                    *op++ = ((long32)f >> 16) & 0xff ;
                    *op++ = ((long32)f >> 8)  & 0xff ;
                    *op++ = (long32)f & 0xff ;
                }
                ip+=4 ;
            }
        }
        return 0 ;
    }

    /*
     * -32 to -64
     * Conversion achieved through cast. Need to know endian-ness.
     */
    if ((ptype_in == -32) && (ptype_out == -64)) {
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                d = (double)(*(float*)ip) ;
                memcpy(op, &d, 8) ;
                ip+=4 ;
                op+=8 ;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                memcpy(a4, ip, 4) ;
                swap_bytes(a4, 4) ;
                d = (double)(*(float*)&a4[0]) ;
                swap_bytes(&d, sizeof(double)) ;
                memcpy(op, &d, 8) ;
                ip+=4 ;
                op+=8 ;
            }
        }
        return 0 ;
    }

    /* ---------- -64 to something */

    /*
     * -64 to 8
     * Heavy loss of input precision!
     * The input double is constructed through swap and cast. It is then
     * clipped to [0..255] and rounded to the smaller integer.
     */
    if ((ptype_in == -64) && (ptype_out == 8)) {
        fprintf(stderr, "warning: loss of precision in output\n") ;
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                d = *((double*)ip) ;
                if (d>255.0) {
                    *op++ = (byte)0xff ;
                } else if (d<0.0) {
                    *op++ = (byte)0x00 ;
                } else {
                    *op++ = (byte)(d+0.5) ;
                }
                ip+=8 ;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                memcpy(a8, ip, 8) ;
                swap_bytes(a8, 8) ;
                d = *((double*)&a8[0]) ;
                if (d>255.0) {
                    *op++ = (byte)0xff ;
                } else if (d<0.0) {
                    *op++ = (byte)0x00 ;
                } else {
                    *op++ = (byte)(d+0.5) ;
                }
                ip+=8 ;
            }
        }
        return 0 ;
    }

    /*
     * -64 to 16
     * Heavy loss of input precision!
     * The input double is constructed through swap and cast. It is then
     * clipped to [-32768..32767] and rounded to the smaller integer.
     */
    if ((ptype_in == -64) && (ptype_out == 16)) {
        fprintf(stderr, "warning: loss of precision in output\n") ;
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                d = *((double*)ip) ;
                if (d>32767.0) {
                    *op++ = (byte)0x7f ;
                    *op++ = (byte)0xff ;
                } else if (d<-32768.0) {
                    *op++ = (byte)0x80 ;
                    *op++ = (byte)0x00 ;
                } else {
                    *op++ = (short16)d >> 8 ;
                    *op++ = (short16)d & (byte)0xff ;
                }
                ip+=8 ;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                memcpy(a8, ip, 8) ;
                swap_bytes(a8, 8) ;
                d = *((double*)&a8[0]) ;
                if (d>32767.0) {
                    *op++ = (byte)0x7f ;
                    *op++ = (byte)0xff ;
                } else if (d<-32768.0) {
                    *op++ = (byte)0x80 ;
                    *op++ = (byte)0x00 ;
                } else {
                    *op++ = (short16)d >> 8 ;
                    *op++ = (short16)d & (byte)0xff ;
                }
                ip+=8 ;
            }
        }
        return 0 ;
    }

    /*
     * -64 to 32
     * Heavy loss of input precision!
     * The input double is constructed through swap and cast. It is then
     * clipped to [-2147483648..2147483647] and rounded to the smaller
     * integer.
     */
    if ((ptype_in == -64) && (ptype_out == 32)) {
        fprintf(stderr, "warning: loss of precision in output\n") ;
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                d = *((double*)ip) ;
                if (d>2147483647.0) {
                    *op++ = (byte)0x7f ;
                    *op++ = (byte)0xff ;
                    *op++ = (byte)0xff ;
                    *op++ = (byte)0xff ;
                } else if (d<-2147483648.0) {
                    *op++ = (byte)0x80 ;
                    *op++ = (byte)0x00 ;
                    *op++ = (byte)0x00 ;
                    *op++ = (byte)0x00 ;
                } else {
                    *op++ = (long32)d >> 24 ;
                    *op++ = ((long32)d >> 16) & (byte)0xff ;
                    *op++ = ((long32)d >> 8)  & (byte)0xff ;
                    *op++ = (long32)d & (byte)0xff ;
                }
                ip+=8 ;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                memcpy(a8, ip, 8) ;
                swap_bytes(a8, 8) ;
                d = *((double*)&a8[0]) ;
                if (d>2147483647.0) {
                    *op++ = (byte)0x7f ;
                    *op++ = (byte)0xff ;
                    *op++ = (byte)0xff ;
                    *op++ = (byte)0xff ;
                } else if (d<-2147483648.0) {
                    *op++ = (byte)0x80 ;
                    *op++ = (byte)0x00 ;
                    *op++ = (byte)0x00 ;
                    *op++ = (byte)0x00 ;
                } else {
                    *op++ = (long32)d >> 24 ;
                    *op++ = ((long32)d >> 16) & 0xff ;
                    *op++ = ((long32)d >> 8)  & 0xff ;
                    *op++ = (long32)d & 0xff ;
                }
                ip+=8 ;
            }
        }
        return 0 ;
    }

    /*
     * -64 to -32
     * Heavy loss of input precision!
     * The input double is constructed through swap and cast. It is then
     * simply cast to a float using the local algorithm for this
     * conversion.
     */
    if ((ptype_in == -64) && (ptype_out == -32)) {
        fprintf(stderr, "warning: loss of precision in output\n") ;
        if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) {
            for (i=0 ; i<npix ; i++) {
                f = (float)(*(double*)ip) ;
                memcpy(op, &f, 4) ;
                ip+=8 ;
                op+=4 ;
            }
        } else {
            for (i=0 ; i<npix ; i++) {
                memcpy(a8, ip, 8) ;
                swap_bytes(a8, 8) ;
                f = (float)(*(double*)&a8[0]) ;
                swap_bytes(&f, sizeof(float)) ;
                memcpy(op, &f, 4) ;
                ip+=8 ;
                op+=4 ;
            }
        }
        return 0 ;
    }
    return 0 ;
}

static int bits_per_byte(void)
{
    unsigned char c=1 ;
    int i=0 ;
    while (c) { c <<=1 ; i++ ; }
    return i ;
}

/*----------------------------------------------------------------------------*/
/**
  @brief    test out endian-ness and IEEE compatibility of the local machine
  @return   flag (1 unsigned int)
  bit 0 stands for endian-ness: 0=Motorola, 1=Intel
  bit 1 stands for IEEE float compatible (1=yes, 0=no)
  bit 2 stands for IEEE double compatible (1=yes, 0=no)
 */
/*----------------------------------------------------------------------------*/
static int test_machine_formats(void)
{
    ushort16  ps = 0xff ;
    int       endian = ENDIAN_UNKNOWN ;
    unsigned  flags = 0;
    float     f ;
    double    d ;
    byte     * b ;

    /* Test endian-ness for this machine */
    endian = ((*((char*)(&ps))) ? ENDIAN_INTEL : ENDIAN_MOTOROLA ) ;
    flags |= endian ;

    /* Test whether the internal float format is IEEE 32bit floating */
    if (sizeof(float)==4) {
        f = (float)2.5e-16 ;
        b = (byte*)(&f) ;
        if (endian == ENDIAN_INTEL) {
            swap_bytes(&f, sizeof(float)) ;
        }
        if ((b[0] == (byte)0x25) &&
            (b[1] == (byte)0x90) &&
            (b[2] == (byte)0x1d) &&
            (b[3] == (byte)0x7d)) {
            flags |= 2 ;
        }
    }

    if (sizeof(double)==8) {
        d = (double)3.14e32 ;
        b = (byte*)&d ;
        if (endian == ENDIAN_INTEL) {
            swap_bytes(&d, sizeof(double)) ;
        }
        if ((b[0] == (byte)0x46) &&
            (b[1] == (byte)0xae) &&
            (b[2] == (byte)0xf6) &&
            (b[3] == (byte)0x79) &&
            (b[4] == (byte)0x70) &&
            (b[5] == (byte)0xae) &&
            (b[6] == (byte)0xec) &&
            (b[7] == (byte)0xd7)) {
            flags |= 4 ;
        }
    }
    return flags ;
}

/*----------------------------------------------------------------------------*/
/**
  @brief    swap bytes in a even-sized memory area
  @param    p   memory area
  @param    s   area size (should be even)
  The area pointed to by p is modified (byte swapped) as:
  AB       -> BA
  ABCD     -> DCBA
  ABCDEFGH -> HGFEDCBA
 */
/*----------------------------------------------------------------------------*/
static void swap_bytes(void * p, int s)
{
    byte tmp, *a, *b ;

    a = (byte*)p ;
    b = a + s ;

    while (a<b) {
        tmp = *a ;
        *a++ = *--b ;
        *b = tmp ;
    }
}

/*----------------------------------------------------------------------------*/
/**
  @brief    test out if the requested pixel type corresponds to a valid type
  @param    ptype   requested pixel type for output
  Also tests out the sizes of all basic types (short16, long32, byte) to 
  ensure they are the correct size. Also checks out that a float is IEEE 
  compatible and a double is what it pretends to be.
 */
/*----------------------------------------------------------------------------*/
static void check_all_types(int ptype)
{
    /* Check the requested pixel type is correct */
    if ((ptype!=8)&&(ptype!=16)&&(ptype!=32)&&(ptype!=-32)&&(ptype!=-64)) {
        fprintf(stderr, "illegal pixel type: %d\n", ptype) ;
        fprintf(stderr, "should be 8 16 32 -32 or -64\n") ;
        exit(-1) ;
    }

    /*
     * Check the following:
     * bitsize of byte is 8 bits
     * bitsize of short is 16 bits
     * bitsize of long is 32 bits
     */

    if (bits_per_byte() != 8) {
        fprintf(stderr, "this machine does not support 8-bit chars\n") ;
        fprintf(stderr, "cannot go any further -- sorry.\n") ;
        exit(-1) ;
    }

    if (sizeof(short16)!=2) {
        fprintf(stderr, "internal: wrong definition for short16\n") ;
        fprintf(stderr, "edit definition and recompile.\n") ;
        exit(-1) ;
    }
    
    if (sizeof(long32)!=4) {
        fprintf(stderr, "internal: wrong definition for long32\n") ;
        fprintf(stderr, "edit definition and recompile.\n") ;
        exit(-1) ;
    }
    
    /*
     * If -32 or -64 are requested in input check out that
     * the local CPU works with float and double in IEEE format,
     * otherwise, we cannot do much (arithmetic and bitwise
     * interpretations needed, not implemented here...).
     */

    compat_flags = test_machine_formats() ;
    if (ptype == -32) {
        if (!IEEE_FLOAT_COMPAT(compat_flags)) {
            fprintf(stderr,
                "this machine does not support IEEE floating point values\n");
            fprintf(stderr,
                "cannot convert from -32 type\n");
            exit(-1) ;
        }
    }
    if (ptype == -64) {
        if (!IEEE_DOUBLE_COMPAT(compat_flags)) {
            fprintf(stderr,
                "this machine does not support IEEE double values\n");
            fprintf(stderr,
                "cannot convert from -64 type\n");
            exit(-1) ;
        }
    }
    return ;
}