#include #include #include /* #define DEBUG 1 */ /* * WROWB * subroutine takes character array BUF with 8 pixels per * byte & expands it to write a row of * either bit, byte, short, long, float, or double pixels * to the file descriptor FD using the parameters * found in the CVL header AREA. * Returns 1 on success, -1 on error. * Partial reads after retries are treated as errors. * Alters a zero value in the optional bits/band field of AREA. * The optional CVL header exponent bit fields are only used * to distinguish floating and integral values of the same size; * otherwise, if the bits per pixel fields match an allowable * input pixel type then that type is used with further checks. * cc -c -O wrowb.c * Written by Robert L. Kirby, U. of Md. CVL, August 4, 1983 * with modifications by Diane Donaldson and Chris Welsh. * * Modified 04/22/87 for switching on cv_bp rather than cv_bb, better error * handling/messages and diagnostics, Joe Sanjour */ char *malloc(); int wrowb (fd, buf, area) int fd; /* Input file descriptor */ char *buf; /* Buffer to receive converted values */ struct cvl_p *area; { int nbytes; /* Bytes per input row */ register int icnt; /* Data items in row */ register char *obp = buf; /* Output buffer pointer */ icnt = area->cv_cols; /* use thead() to check header for inconsistencies */ switch(thead(area,1)) { case 1: { fprintf(stderr,"wrowb: inconsistencies found in header, fixed\n"); break; } case -1: { fprintf(stderr,"wrowb: inconsistencies found in header, unfixable\n"); return(-1); } #ifdef DEBUG case 0: { fprintf(stderr,"wrowb: header okay\n"); break; } default: { fprintf(stderr,"wrowb: thead is being wierd\n"); break; } #endif DEBUG } /* end switch */ if ((nbytes = (area->cv_cols * area->cv_bp + BYTEBITS - 1)/BYTEBITS) == 0) /* Bytes per row */ { fprintf(stderr,"wrowb: error, bits per pixel = 0 ???\n"); return(-1); } #ifdef DEBUG fprintf(stderr,"wrowb: %d bytes per row\n",nbytes); #endif DEBUG switch(area->cv_bp) { case 1: break; #if BYTEBITS != SHORTBITS case BYTEBITS: { /* Character buffer */ char *ibuf; register char *bp; int mask = 1 << (icnt % BYTEBITS); if ((ibuf = malloc(icnt*sizeof(char)))== (char *) -1) { fprintf(stderr,"wrowb: core allocation error"); return(-1); } bp = ibuf; do { if ((mask >>= 1) == 0) { mask = 1 << (BYTEBITS - 1); obp++; } *bp++ = (*obp & mask) != 0; } while (--icnt > 0); if (write(fd,ibuf,nbytes) == nbytes) { free(ibuf); return(1); } else { free(ibuf); fprintf(stderr,"wrowb: write error"); return(-1); } } #endif #if SHORTBITS != LONGBITS case SHORTBITS: { /* Character buffer */ short *ibuf; register short *bp; int mask = 1 << (icnt % BYTEBITS); if ((ibuf = (short *)malloc(icnt*sizeof(short)))== (short *) -1) { fprintf(stderr,"wrowb: core allocation error"); return(-1); } bp = ibuf; do { if ((mask >>= 1) == 0) { mask = 1 << (BYTEBITS - 1); obp++; } *bp++ = (*obp & mask) != 0; } while (--icnt > 0); if (write(fd,ibuf,nbytes) == nbytes) { free(ibuf); return(1); } else { free(ibuf); fprintf(stderr,"wrowb: write error"); return(-1); } } #endif #if LONGBITS == DOUBLEBITS case LONGBITS: if(area->cv_ebp == 0 && area->cv_ebb == 0) { long *ibuf; int mask = 1 << (icnt % BYTEBITS); register long *bp; if((ibuf = (long *)malloc(icnt*sizeof(long)))==(long *) -1) { fprintf(stderr,"wrowb: core allocation error"); return(-1); } bp = ibuf; do { if ((mask >>= 1) == 0) { mask = 1 << (BYTEBITS - 1); obp++; } *bp++ = (*obp & mask) != 0; } while (--icnt > 0); if (write(fd,ibuf,nbytes) == nbytes) { free(ibuf); return(1); } else { free(ibuf); fprintf(stderr,"wrowb: write error"); return(-1); } } else { double *ibuf; register double *bp; int mask = 1 << (icnt % BYTEBITS); if ((ibuf=(double *)malloc(icnt*sizeof(double)))==(double *)-1) { fprintf(stderr,"wrowb: core allocation error"); return(-1); } bp = ibuf; do { if ((mask >>= 1) == 0) { mask = 1 << (BYTEBITS - 1); obp++; } *bp++ = (*obp & mask) != 0; } while (--icnt > 0); if (write(fd,ibuf,nbytes) == nbytes) { free(ibuf); return(1); } else { free(ibuf); fprintf(stderr,"wrowb: write error"); return(-1); } } #if FLOATBITS != DOUBLEBITS case FLOATBITS: { float *ibuf; register float *bp; int mask = 1 << (icnt % BYTEBITS); if ((ibuf = (float *)malloc(icnt*sizeof(float)))==(float *) -1) { fprintf(stderr,"wrowb: core allocation error"); return(-1); } bp = ibuf; do { if ((mask >>= 1) == 0) { mask = 1 << (BYTEBITS - 1); obp++; } *bp++ = (*obp & mask) != 0; } while (--icnt > 0); if (write(fd,ibuf,nbytes) == nbytes) { free(ibuf); return(1); } else { free(ibuf); fprintf(stderr,"wrowb: write error"); return(-1); } } #endif #else #if LONGBITS == FLOATBITS case LONGBITS: if(area->cv_ebp == 0 && area->cv_ebb == 0) { long *ibuf; register long *bp; int mask = 1 << (icnt % BYTEBITS); if ((ibuf = (long *)malloc(icnt*sizeof(long)))==(long *) -1) { fprintf(stderr,"wrowb: core allocation error"); return(-1); } bp = ibuf; do { if ((mask >>= 1) == 0) { mask = 1 << (BYTEBITS - 1); obp++; } *bp++ = (*obp & mask) != 0; } while (--icnt > 0); if (write(fd,ibuf,nbytes) == nbytes) { free(ibuf); return(1); } else { free(ibuf); fprintf(stderr,"wrowb: write error"); return(-1); } } else { float *ibuf; register float *bp; int mask = 1 << (icnt % BYTEBITS); if ((ibuf=(float *)malloc(icnt*sizeof(float)))==(float *)-1) { fprintf(stderr,"wrowb: core allocation error"); return(-1); } bp = ibuf; do { if ((mask >>= 1) == 0) { mask = 1 << (BYTEBITS - 1); obp++; } *bp++ = (*obp & mask) != 0; } while (--icnt > 0); if (write(fd,ibuf,nbytes) == nbytes) { free(ibuf); return(1); } else { free(ibuf); fprintf(stderr,"wrowb: write error"); return(-1); } } case DOUBLEBITS: { double *ibuf; register double *bp; int mask = 1 << (icnt % BYTEBITS); if ((ibuf= (double *)malloc(icnt*sizeof(double)))==(double *) -1) { fprintf(stderr,"wrowb: core allocation error"); return(-1); } bp = ibuf; do { if ((mask >>= 1) == 0) { mask = 1 << (BYTEBITS - 1); obp++; } *bp++ = (*obp & mask) != 0; } while (--icnt > 0); if (write(fd,ibuf,nbytes) == nbytes) { free(ibuf); return(1); } else { free(ibuf); fprintf(stderr,"wrowb: write error"); return(-1); } } #else case LONGBITS: { long *ibuf; register long *bp; int mask = 1 << (icnt % BYTEBITS); if ((ibuf = (long *) malloc(icnt*sizeof(long)))==(long *) -1) { fprintf(stderr,"wrowb: core allocation error"); return(-1); } bp = ibuf; do { if ((mask >>= 1) == 0) { mask = 1 << (BYTEBITS - 1); obp++; } *bp++ = (*obp & mask) != 0; } while (--icnt > 0); if (write(fd,ibuf,nbytes) == nbytes) { free(ibuf); return(1); } else { free(ibuf); fprintf(stderr,"wrowb: write error"); return(-1); } } #if FLOATBITS != DOUBLEBITS case FLOATBITS: { float *ibuf; register float *bp; int mask = 1 << (icnt % BYTEBITS); if ((ibuf = (float *)malloc(icnt*sizeof(float)))==(float *) -1) { fprintf(stderr,"wrowb: core allocation error"); return(-1); } bp = ibuf; do { if ((mask >>= 1) == 0) { mask = 1 << (BYTEBITS - 1); obp++; } *bp++ = (*obp & mask) != 0; } while (--icnt > 0); if (write(fd,ibuf,nbytes) == nbytes) { free(ibuf); return(1); } else { free(ibuf); fprintf(stderr,"wrowb: write error"); return(-1); } } #endif case DOUBLEBITS: { double *ibuf; register double *bp; int mask = 1 << (icnt % BYTEBITS); if ((ibuf=(double *)malloc(icnt*sizeof(double)))==(double *) -1) { fprintf(stderr,"wrowb: core allocation error"); return(-1); } bp = ibuf; do { if ((mask >>= 1) == 0) { mask = 1 << (BYTEBITS - 1); obp++; } *bp++ = (*obp & mask) != 0; } while (--icnt > 0); if (write(fd,ibuf,nbytes) == nbytes) { free(ibuf); return(1); } else { free(ibuf); fprintf(stderr,"wrowb: write error"); return(-1); } } #endif #endif default: fprintf(stderr,"wrowb: error, bits per pixel = %d\n",area->cv_bp); return(-1); } if (write (fd, (char *) buf, nbytes) == nbytes) return(1); else { fprintf(stderr,"wrowb: write error"); return(-1); } }