/* convert.c Converts cvl format pictures between VAX and Sun representations. Conversion of floating point pictures is machine specific so a define must be included in the command line to the compiler. -DVAX specifies a VAX as host. -DSUN specifies a Sun as host. If no definition is made an error will be reported during compilation. */ #include #define STDIN 0 #define STDOUT 1 #define TRUE 1 char *argv0; extern int errno; myputchar(ch) char ch; { int errval; if((errval = write(STDOUT,&ch,1)) != 1) errprnt("Unable to write character,errval = %d,errno = %d\n",errval,errno); } swap(cx,cy) register char *cx, *cy; { register char temp; temp = *cx; *cx = *cy; *cy = temp; } #ifdef SUN /* defined on cc command line */ shiftdouble(dbl) /* Vax 7 bit exponent, bias 128, to Sun */ char dbl[8]; /* 11 bit, bias 1023 */ { short k; unsigned *wd0, *wd1, sign, carry; for(k=0;k<7;k+=2) swap(&dbl[k],&dbl[k+1]); wd0 = (unsigned *)(&dbl[0]); wd1 = (unsigned *)(&dbl[4]); sign = *wd0 & 020000000000; /* pick off sign bit */ carry = *wd0 & 07; /* carry to word 1 */ *wd0 = *wd0 >> 3; if(sign) /* restore sign, remove old sign */ *wd0 = *wd0 ^ 022000000000; carry = carry << 29; *wd1 = *wd1 >> 3; *wd1 = *wd1 | carry; *wd0 += 006770000000; /* adjust exponent */ } #else #ifdef VAX shiftdouble(dbl) /* Sun 11 bit exponent, bias 1023, to Vax */ char dbl[8]; /* 7 bit, bias 128 */ { unsigned *wd0, *wd1, sign, carry; wd0 = (unsigned *)(&dbl[0]); wd1 = (unsigned *)(&dbl[4]); swap(&dbl[0],&dbl[3]); /* place bits into correct order */ swap(&dbl[1],&dbl[2]); /* for vax unsigned integer */ swap(&dbl[4],&dbl[7]); swap(&dbl[5],&dbl[6]); sign = *wd0 & 020000000000; /* pick off sign bit */ *wd0 -= 006770000000; /* adjust exponent */ *wd0 = *wd0 << 3; if(sign) /* restore sign bit */ *wd0 = *wd0 | 020000000000; carry = *wd1 & 034000000000; carry = carry >> 29; *wd1 = *wd1 << 3; *wd0 = *wd0 | carry; swap(&dbl[0],&dbl[2]); /* return to vax floating point format */ swap(&dbl[1],&dbl[3]); swap(&dbl[4],&dbl[6]); swap(&dbl[5],&dbl[7]); } #else #Syntax error. Do not delete. Use command line switch to indicate host! #endif #endif main(argc,argv) int argc; char **argv; { struct cvl_p header; short fltpic = 0; /* flag floating point picture */ short *fltptr; /* pointer to first two bytes of a float */ char buff[1024]; char *rowbuff; char *p; short shrt; char *spt; char *malloc(); short cols,rows,lcom,bp; int i,j; int psize; /* bytes per pixel */ argv0 = argv[0]; p = (char *)(&header); spt = (char *)(&shrt); /* Reading the header into p from STDIN */ if(read(STDIN,p,sizeof header) != sizeof header) errprnt("Unable to read image header\n"); i = 'TCIP' + 020000000000; /* magic tag value, just what ihead wants */ /* Writing 4 bytes from p to STDOUT (Standard output) */ if (write(STDOUT,&i,4) != 4) errprnt("Unable to write tag\n"); for(i=2;i<16;i++) { /* switching the format of the low-order and high-order bytes to make it compatible for the other machine */ spt[1] = p[i*2]; spt[0] = p[1+i*2]; /* writing out the swapped bits */ myputchar(spt[0]); myputchar(spt[1]); /* Loading in, the values of lcom,rows,cols, and bp. */ if(i == 2) lcom = shrt; if(i == 7) rows = shrt; if(i == 8) cols = shrt; if(i == 10) bp = shrt; if(i == 15 && shrt != 0) fltpic = TRUE; /* floating point picture */ } /* dump comments */ while(lcom >= 1024) { /* Reading in and writing out,1024 bits at a time */ if(read(STDIN,buff,1024) != 1024) errprnt("Unable to read comments\n"); if(write(STDOUT,buff,1024) != 1024) errprnt("Unable to write comments\n"); lcom -= 1024; } /* Reading in and writing out, the remaining bits in lcom */ if(lcom > 0) { if(read(STDIN,buff,lcom) != lcom) errprnt("Unable to read comments\n"); if(write(STDOUT,buff,lcom) != lcom) errprnt("Unable to write comments\n"); } psize = bp / 8; /* bytes per pixel, 0 for bit pictures */ rowbuff = malloc(psize * cols + 1); /* area for picture rows */ switch(psize){ case 0: cols /= 8; case 1: for(i=0;i