|
|
1.1 ! root 1: /* Copyright (c) 1989, 1990 AT&T --- All Rights Reserved. */ ! 2: /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T. */ ! 3: /* The copyright notice does not imply actual or intended publication. */ ! 4: /* AUTHORS: */ ! 5: /* H. S. Baird - ATT-BL MH - first versions */ ! 6: ! 7: /* piclib.c - picture-file public functions. ! 8: alloc_PIC_hdr() - allocate initialized PIC_hdr, associate with FILE * ! 9: free_PIC_hdr() - free PIC_hdr & all malloc'ed fields ! 10: PIC_get_hdr() - get picture-file header ! 11: PIC_rline() - read one line of picture data ! 12: PIC_put_hdr() - get picture-file header ! 13: PIC_wline() - write one line of picture data ! 14: err_PIC_Line() - print line on stderr ! 15: PIC_hdr_toa() - PIC_hdr to ascii printable string ! 16: (PIC_* functions use system I/O, for speed) ! 17: */ ! 18: ! 19: #include <stdio.h> ! 20: #include <math.h> ! 21: #include <string.h> ! 22: #include "boole.h" ! 23: #include "limits.h" /* numeric extreme values */ ! 24: #include "Units.h" ! 25: #include "Coord.h" ! 26: #include "pic.h" ! 27: ! 28: #define PIC_debug (0) ! 29: double atof(); ! 30: ! 31: PIC_hdr *alloc_PIC_hdr( fp ) ! 32: FILE *fp; /* already-open file descr */ ! 33: { PIC_hdr *h; ! 34: if((h=(PIC_hdr *)malloc(sizeof(PIC_hdr)))==NULL) ! 35: abort("piclib: alloc_PIC_hdr: can't alloc"); ! 36: *h = empty_PIC_hdr; ! 37: h->fp = fp; ! 38: return(h); ! 39: } ! 40: ! 41: free_PIC_hdr( h ) ! 42: PIC_hdr *h; ! 43: { if(h->parms!=NULL) free(h->parms); ! 44: if(h->line!=NULL) free(h->line); ! 45: if(h->pline!=NULL) free(h->pline); ! 46: if(h->misc!=NULL) free(h->misc); ! 47: free(h); ! 48: } ! 49: ! 50: /* system I/O variation on fgets(3), except it replaces \n with \0, ! 51: and returns the number of chars read (including \0) */ ! 52: int PIC_fgets(bf,max,fd) ! 53: char *bf; ! 54: int max; ! 55: int fd; ! 56: { char *cp; ! 57: int stat,mny; ! 58: cp=bf; mny=0; ! 59: while(((stat=read(fd,cp,1))==1)&&(++mny<max)&&((*cp)!='\n')) cp++; ! 60: if(stat!=1) return(stat); ! 61: else if(mny>=max) return(-1); ! 62: *cp='\0'; ! 63: return(mny); ! 64: } ! 65: ! 66: /* Read picture file header from file descr fileno(h->fp), set up the rest of the ! 67: header, and return status: 1 OK, 0 EOF, <0 error */ ! 68: int PIC_get_hdr( h ) ! 69: PIC_hdr *h; ! 70: #define HL_MAX 300 ! 71: #define HTERM "=, \n" /* terminations for header words: "=,<sp><tab>" */ ! 72: { char *cp,*parm,hline[HL_MAX],oline[HL_MAX],*hl; ! 73: int status,nrd,ch,mny; ! 74: /* synchronize FILE * ptr with file descr ptr */ ! 75: fseek(h->fp,h->seek = lseek(fileno(h->fp),0L,1),0); ! 76: hline[0]='\0'; ! 77: if((mny=read(fileno(h->fp),hline,5))!=5) ! 78: return(0); ! 79: if(!(hline[0]=='T' && hline[1]=='Y' && hline[2]=='P' ! 80: && hline[3]=='E' && hline[4]=='=')) { ! 81: /* no "TYPE=" header; don't accept */ ! 82: if(0) /* don't accept */ { ! 83: if(PIC_debug) ! 84: err("PIC_get_hdr: can't find TYPE=... header"); ! 85: return(-1); ! 86: } ! 87: else /* accept as bitfile format (OBSOLESCENT) */ { ! 88: strcpy(h->type,"bitfile"); ! 89: h->res_x = h->res_y = 0; ! 90: fseek(h->fp,h->seek+2L,0); /* back up to byte no. 2 */ ! 91: /* read window */ ! 92: if((ch=getc(h->fp))!=EOF) { h->bx.a.x = ch; ! 93: if((ch=getc(h->fp))!=EOF) { h->bx.a.x += (ch<<8); ! 94: if((ch=getc(h->fp))!=EOF) { h->bx.a.y = ch; ! 95: if((ch=getc(h->fp))!=EOF) { h->bx.a.y += (ch<<8); ! 96: if((ch=getc(h->fp))!=EOF) { h->bx.b.x = ch; ! 97: if((ch=getc(h->fp))!=EOF) { h->bx.b.x += (ch<<8); ! 98: if((ch=getc(h->fp))!=EOF) { h->bx.b.y = ch; ! 99: if((ch=getc(h->fp))!=EOF) { h->bx.b.y += (ch<<8); }}}}}}}} ! 100: else return(0/*EOF*/); ! 101: h->bx.b.x--; h->bx.b.y--; ! 102: /* synchronize file descr ptr with FILE * ptr */ ! 103: lseek(fileno(h->fp),h->seek = ftell(h->fp),0); ! 104: }; ! 105: } ! 106: else { ! 107: if((status=PIC_fgets(hline+5,HL_MAX-5,fileno(h->fp)))<=0) ! 108: return(status); ! 109: strcpy(oline,hline); ! 110: hl=hline; while(*hl==' ') hl++; /* strip initial blanks */ ! 111: while(strlen(hl)>1) { ! 112: if(PIC_debug) err("PIC_get_hdr: hl \"%s\"",hl); ! 113: parm=strtok(hl,HTERM); ! 114: if(parm!=NULL&&strcmp(parm,"TYPE")==0) { ! 115: if((parm=strtok(0,HTERM))!=NULL) { ! 116: strcpy(h->type,parm); ! 117: if( (strcmp(h->type,"dump")==0) ! 118: || (strcmp(h->type,"pico")==0) ! 119: || (strcmp(h->type,"rle")==0) ! 120: || (strcmp(h->type,"binary")==0) ! 121: || (strcmp(h->type,"bitmap")==0) ! 122: || (strcmp(h->type,"dim")==0) ! 123: || (strcmp(h->type,"document-image")==0) ! 124: || (strcmp(h->type,"ccitt-g31")==0) ! 125: || (strcmp(h->type,"ccitt-g32")==0) ! 126: || (strcmp(h->type,"ccitt-g4")==0) ! 127: || (strcmp(h->type,"cdf")==0) ! 128: || (strcmp(h->type,"tiff")==0) ! 129: ) /* these types are expected */ { ! 130: if(PIC_debug) ! 131: err("PIC_get_hdr: good TYPE=\"%s\"",h->type); ! 132: } ! 133: else { if(PIC_debug) ! 134: err("PIC_get_hdr: unexpected TYPE=\"%s\"",h->type); ! 135: return(-1); ! 136: }; ! 137: } ! 138: else { if(PIC_debug) ! 139: err("PIC_get_hdr: 1st line must be TYPE=..."); ! 140: ! 141: return(-1); ! 142: }; ! 143: } ! 144: else if(parm!=NULL&&strcmp(parm,"WINDOW")==0) { ! 145: if((parm=strtok(0,HTERM))!=NULL) h->bx.a.x=atoi(parm); ! 146: else return(-1); ! 147: if((parm=strtok(0,HTERM))!=NULL) h->bx.a.y=atoi(parm); ! 148: else return(-1); ! 149: if((parm=strtok(0,HTERM))!=NULL) h->bx.b.x=atoi(parm)-1; ! 150: else return(-1); ! 151: if((parm=strtok(0,HTERM))!=NULL) h->bx.b.y=atoi(parm)-1; ! 152: else return(-1); ! 153: } ! 154: else if(parm!=NULL&&strcmp(parm,"RES")==0) { ! 155: if((parm=strtok(0,HTERM))!=NULL) ! 156: h->res_x=(int)(atof(parm)+0.5); ! 157: else return(-1); ! 158: if((parm=strtok(0,HTERM))!=NULL) ! 159: h->res_y=(int)(atof(parm)+0.5); ! 160: else return(-1); ! 161: } ! 162: else if(parm!=NULL&&strcmp(parm,"NCHAN")==0) { ! 163: int nchan; ! 164: if((parm=strtok(0,HTERM))!=NULL) nchan=atoi(parm); ! 165: else return(-1); ! 166: if(nchan!=1) { ! 167: err("PIC_get_hdr: NCHAN=%d illegal: must be 1",nchan); ! 168: return(-1); ! 169: }; ! 170: } ! 171: else if(parm!=NULL&&strcmp(parm,"PARMS")==0) { ! 172: int nchan; ! 173: if((parm=strtok(0,"\n"))!=NULL) { ! 174: h->parms = strdup(parm); ! 175: } ! 176: else return(-1); ! 177: } ! 178: else if(parm!=NULL) { ! 179: char *cat; ! 180: /* save all header lines with unexpected keywords */ ! 181: if(h->misc==NULL) h->misc=(char *)malloc(strlen(oline)+2); ! 182: else { cat=(char *)malloc(strlen(h->misc)+strlen(oline)+2); ! 183: strcpy(cat,h->misc); ! 184: free(h->misc); ! 185: h->misc=cat; ! 186: }; ! 187: strcat(h->misc,oline); ! 188: strcat(h->misc,"\n"); ! 189: } ! 190: else return(-1); ! 191: if((status=PIC_fgets(hline,HL_MAX,fileno(h->fp)))<=0) ! 192: return(status); ! 193: strcpy(oline,hline); ! 194: hl=hline; while(*hl==' ') hl++; /* strip initial blanks */ ! 195: }; ! 196: }; ! 197: ! 198: if(strcmp(h->type,"cdf")==0) { ! 199: int stat; ! 200: /* Compund Document Format has binary header */ ! 201: /* synchronize FILE * ptr with file descr ptr */ ! 202: fseek(h->fp,h->seek = lseek(fileno(h->fp),0L,1),0); ! 203: if((stat=CDF_get_hdr(h)) != 1) return(stat); ! 204: }; ! 205: ! 206: if(strcmp(h->type,"tiff")==0) { ! 207: int stat; ! 208: /* Tagged Image File Format has abinary header */ ! 209: /* synchronize FILE * ptr with file descr ptr */ ! 210: fseek(h->fp,h->seek = lseek(fileno(h->fp),0L,1),0); ! 211: if((stat=TIFF_get_hdr(h)) != 1) return(stat); ! 212: }; ! 213: ! 214: /* may want to allocate a line buffer here */ ! 215: h->bpl = bbx_wid(&(h->bx)); ! 216: if(strcmp(h->type,"binary")==0) ! 217: h->bpl = (h->bpl+7)/8; /* round up to byte boundary */ ! 218: else if(strcmp(h->type,"bitfile")==0) ! 219: h->bpl = 2*((h->bpl+15)/16); /* round up to 2-byte boundary */ ! 220: else if(strcmp(h->type,"bitmap")==0) ! 221: h->bpl = 2*((h->bpl+15)/16); /* round up to 2-byte boundary */ ! 222: else if( strcmp(h->type,"rle")==0 ! 223: || strcmp(h->type,"dim")==0 ! 224: || strcmp(h->type,"document-image")==0 ! 225: || strcmp(h->type,"cdf")==0 ! 226: || strcmp(h->type,"cdf-mrlc")==0 ! 227: || strcmp(h->type,"cdf-g31")==0 ! 228: || strcmp(h->type,"cdf-g32")==0 ! 229: || strcmp(h->type,"cdf-g4")==0 ! 230: || strcmp(h->type,"tiff")==0 ) ! 231: h->bpl = 0; ! 232: if(h->bpl==0) h->line = NULL; ! 233: else { /* allocate one extra byte in line buffer as a favor to RLE */ ! 234: if((h->line = (unsigned char *) malloc(h->bpl+1))==NULL) { ! 235: fprintf(stderr, ! 236: "piclib: PIC_get_hdr: can't alloc h->line (%d bytes) - abort\n", ! 237: h->bpl+1); ! 238: return(-1); ! 239: }; ! 240: memset(h->line,'\0',h->bpl); ! 241: if(strcmp(h->type,"bitfile")==0) { ! 242: if((h->pline = (unsigned char *) malloc(h->bpl+1))==NULL) { ! 243: fprintf(stderr, ! 244: "piclib: PIC_get_hdr: can't alloc h->pline (%d bytes) - abort\n", ! 245: h->bpl+1); ! 246: return(-1); ! 247: }; ! 248: }; ! 249: }; ! 250: h->cy = h->bx.a.y-1; /* just prior to 1st line */ ! 251: /* synchronize FILE * ptr with file descr ptr */ ! 252: fseek(h->fp,h->seek = lseek(fileno(h->fp),0L,1),0); ! 253: return(1); ! 254: } ! 255: ! 256: /* Read next page from AT&T Image Director file header from file descr ! 257: fileno(h->fp), set up the rest of the header, and return status: ! 258: 1 OK, 0 EOF, <0 error */ ! 259: int CDF_next_page( h ) ! 260: PIC_hdr *h; ! 261: { return(-1); /* unimplemented */ ! 262: } ! 263: ! 264: #define HASHEADER(h) ((strcmp((h)->type,"postscript")!=0&&strcmp((h)->type,"sunraster")!=0)) ! 265: ! 266: /* Read AT&T Image Director file header from file descr fileno(h->fp), ! 267: set up the rest of the header, and return status: 1 OK, 0 EOF, <0 error */ ! 268: int CDF_get_hdr( h ) ! 269: PIC_hdr *h; ! 270: { char *cp,*parm,hline[HL_MAX]; ! 271: int status,nrd,ch,mny; ! 272: #define dbg_cdf (0) ! 273: typedef struct CDF_file { ! 274: unsigned char raw[16]; ! 275: unsigned char op,np,at; ! 276: unsigned int l1,l2,len; ! 277: } CDF_file; ! 278: typedef struct CDF_page { ! 279: unsigned char raw[16]; ! 280: unsigned char op,pn,at; ! 281: unsigned char f1,f2,f3,f4; ! 282: unsigned int ptr; ! 283: } CDF_page; ! 284: typedef struct CDF_recd { ! 285: unsigned char raw[16]; ! 286: unsigned char op,a1,a2,a3; ! 287: unsigned int len; ! 288: Bbx bx; ! 289: } CDF_recd; ! 290: CDF_file fh; ! 291: CDF_page ph; ! 292: CDF_recd rh; ! 293: ! 294: if(dbg_cdf) err("CDF_get_hdr: enter %s",PIC_hdr_toa(h)); ! 295: /* synchronize FILE * ptr with file descr ptr */ ! 296: fseek(h->fp,h->seek = lseek(fileno(h->fp),0L,1),0); ! 297: strcpy(h->type,"cdf"); ! 298: ! 299: /* Read CDF File header */ ! 300: if(fread(fh.raw,1,16,h->fp)!=16) return(0); ! 301: fh.op = fh.raw[0]; ! 302: fh.np = fh.raw[1]; ! 303: fh.at = fh.raw[2]; ! 304: fh.l1 = fh.raw[4]+256*fh.raw[5]; ! 305: fh.l2 = fh.raw[6]+256*fh.raw[7]; ! 306: fh.len = fh.raw[12]+256*(fh.raw[13]+256*(fh.raw[14]+256*fh.raw[15])); ! 307: if(dbg_cdf) err("CDF_get_hdr: file hdr: op0x%x p%d at0x%x lim1:%u lim2:%u len%u", ! 308: fh.op,fh.np,fh.at,fh.l1,fh.l2,fh.len); ! 309: if(fh.op!=0xA0&&fh.op!=0xAF) abort("CDF_get_hdr: file must be CDF type"); ! 310: if(fh.np!=1) err("CDF_get_hdr: file has %d pages - ignore all but 1st",fh.np); ! 311: switch(fh.at>>4) { ! 312: case 0: h->res_x = h->res_y = 100; break; ! 313: case 1: h->res_x = h->res_y = 200; break; ! 314: case 2: h->res_x = h->res_y = 300; break; ! 315: default: err("CDF_get_hdr: file has unknown resolution nibble 0x%x -- assume 400 dpi",fh.at>>4); ! 316: h->res_x = h->res_y = 400; ! 317: break; ! 318: }; ! 319: h->bx.a.x = h->bx.a.y = 0; ! 320: h->bx.b.x = fh.l1; ! 321: h->bx.b.y = fh.l2; ! 322: if(fh.op==0xAF) { ! 323: abort("CDF_get_hdr: file has continuation header"); ! 324: }; ! 325: ! 326: /* Read 1st page table entry */ ! 327: if(fread(ph.raw,1,16,h->fp)!=16) return(0); ! 328: ph.op = ph.raw[0]; ! 329: ph.pn = ph.raw[1]; ! 330: ph.at = ph.raw[2]; ! 331: ph.ptr = ph.raw[12]+256*(ph.raw[13]+256*(ph.raw[14]+256*ph.raw[15])); ! 332: if(dbg_cdf) err("CDF_get_hdr: page hdr: op0x%x p%d at0x%x ptr%u", ! 333: ph.op,ph.pn,ph.at,ph.ptr); ! 334: if(ph.op!=0x80&&ph.op!=0x8F) ! 335: abort("CDF_get_hdr: page opcode 0x%x is peculiar",ph.op); ! 336: if(ph.pn!=1) err("CDF_get_hdr: page %d out of order - read anyway",ph.pn); ! 337: switch(ph.at&0x0F) { /* low nibble */ ! 338: case 0: /* portrait */ break; ! 339: case 1: /* landscape */ ! 340: default: err("CDF_get_hdr: page has peculiar format nibble 0x%x -- assume 40portrait",ph.at&0x0F); ! 341: break; ! 342: }; ! 343: switch(ph.at>>4) { /* high nibble */ ! 344: case 0xF: /* background is white */ break; ! 345: default: err("CDF_get_hdr: page has peculiar color nibble 0x%x -- assume white",ph.at>>4); ! 346: break; ! 347: }; ! 348: /* seek top of Page */ ! 349: fseek(h->fp,h->seek+=ph.ptr,0); ! 350: ! 351: /* Read CDF Record header */ ! 352: if(fread(rh.raw,1,16,h->fp)!=16) return(0); ! 353: rh.op = rh.raw[0]; ! 354: rh.a1 = rh.raw[1]; ! 355: rh.a2 = rh.raw[2]; ! 356: rh.a3 = rh.raw[3]; ! 357: rh.len = rh.raw[4]+256*rh.raw[5]; ! 358: rh.bx.a.x = rh.raw[8]+256*rh.raw[9]; ! 359: rh.bx.a.y = rh.raw[10]+256*rh.raw[11]; ! 360: rh.bx.b.x = rh.raw[12]+256*rh.raw[13]; ! 361: rh.bx.b.y = rh.raw[14]+256*rh.raw[15]; ! 362: if(dbg_cdf) err("CDF_get_hdr: record hdr: op0x%x a0x%x,0x%x,0x%x len%u bx%s", ! 363: rh.op,rh.a1,rh.a2,rh.a3,rh.len,bbx_toa(&rh.bx)); ! 364: switch(rh.op) { ! 365: case 0x20: /* Image */ break; ! 366: case 0x01: /* End of Page */ ! 367: case 0x10: /* Text */ ! 368: case 0x1F: /* Text + cont. */ ! 369: case 0x50: /* Text Document */ ! 370: case 0x5F: /* Text Document + cont. */ ! 371: case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: ! 372: case 0x45: case 0x46: case 0x47: case 0x48: case 0x49: case 0x4A: ! 373: case 0x4B: case 0x4C: case 0x4D: case 0x4E: case 0x4F: ! 374: /* Graphics */ ! 375: default: ! 376: abort("CDF_get_hdr: record not Image"); ! 377: }; ! 378: if(rh.a2!=0x01) err("CDF_get_hdr: record not opaque - assume opaque"); ! 379: switch(rh.a3&0x0F) { /* compression algorithm */ ! 380: case 1: /* modified run-length code */ ! 381: strcat(h->type,"-mrlc"); ! 382: break; ! 383: case 0: /* raw */ ! 384: abort("CDF_get_hdr: record compression 0x%x is legal but unsupported",rh.a3&0x0F); ! 385: break; ! 386: case 2: /* ccitt_g31 */ ! 387: strcat(h->type,"-g31"); ! 388: abort("CDF_get_hdr: record compression 0x%x is legal but unsupported",rh.a3&0x0F); ! 389: break; ! 390: case 3: /* ccitt_g32 or ccitt_g4 */ ! 391: strcat(h->type,"-g32"); ! 392: abort("CDF_get_hdr: record compression 0x%x is legal but unsupported",rh.a3&0x0F); ! 393: break; ! 394: default: ! 395: abort("CDF_get_hdr: record compression 0x%x is peculiar",rh.a3&0x0F); ! 396: break; ! 397: }; ! 398: switch(rh.a3>>4) { /* resolution */ ! 399: case 0: ! 400: h->res_y = 100; ! 401: break; ! 402: case 1: ! 403: h->res_y = 200; ! 404: break; ! 405: case 2: ! 406: h->res_y = 300; ! 407: break; ! 408: default: ! 409: err("CDF_get_hdr: record vert resolution peculiar 0x%x - assume 200 dpi", ! 410: rh.a3>>4); ! 411: h->res_y = 200; ! 412: break; ! 413: }; ! 414: /* synchronize file descr ptr with FILE * ptr */ ! 415: lseek(fileno(h->fp),h->seek = ftell(h->fp),0); ! 416: if(dbg_cdf) err("CDF_get_hdr: normal exit %s",PIC_hdr_toa(h)); ! 417: return(1); ! 418: } ! 419: ! 420: /* Read Tagged Image Format File (TIFF) header, from file descr fileno(h->fp), ! 421: set up the rest of the PIC header, and return status: 1 OK, 0 EOF, <0 error */ ! 422: int TIFF_get_hdr( h ) ! 423: PIC_hdr *h; ! 424: { err("TIFF not yet supported"); ! 425: return(-1); ! 426: } ! 427: ! 428: /* Write header to picture file, to file descriptor fileno(h->fp), and initialize ! 429: line buffer and cy. */ ! 430: PIC_put_hdr( h ) ! 431: PIC_hdr *h; ! 432: { char *cp,*parm,hline[HL_MAX]; ! 433: int status,nrd; ! 434: unsigned short s; ! 435: #define put_ushort(S) { s=(S); putc(s&0377,h->fp); putc(s>>8,h->fp); } ! 436: ! 437: /* write type header */ ! 438: if( strcmp(h->type,"bitfile")==0 ) { /* bitfile header */ ! 439: put_ushort(0); ! 440: } ! 441: else if(HASHEADER(h)) { ! 442: sprintf(hline,"TYPE=%s\n",h->type); ! 443: write(fileno(h->fp),hline,strlen(hline)); ! 444: }; ! 445: if(strcmp(h->type,"pico")==0 ! 446: || strcmp(h->type,"dump")==0) { ! 447: sprintf(hline,"NCHAN=1\n"); ! 448: write(fileno(h->fp),hline,strlen(hline)); ! 449: }; ! 450: ! 451: /* write window */ ! 452: h->bpl = bbx_wid(&(h->bx)); ! 453: if(strcmp(h->type,"binary")!=0 ! 454: && strcmp(h->type,"bitfile")!=0 ! 455: && strcmp(h->type,"bitmap")!=0 ! 456: && HASHEADER(h)) { ! 457: sprintf(hline,"WINDOW=%d %d %d %d\n", ! 458: h->bx.a.x,h->bx.a.y,h->bx.b.x+1,h->bx.b.y+1); ! 459: write(fileno(h->fp),hline,strlen(hline)); ! 460: } ! 461: else if(strcmp(h->type,"binary")==0) { ! 462: h->bpl = (h->bpl+7)/8; /* round up to byte boundary */ ! 463: sprintf(hline,"WINDOW=%d %d %d %d\n", ! 464: h->bx.a.x,h->bx.a.y,h->bx.b.x+1,h->bx.b.y+1); ! 465: write(fileno(h->fp),hline,strlen(hline)); ! 466: } ! 467: else if(strcmp(h->type,"bitmap")==0) { ! 468: h->bpl = 2*((h->bpl+15)/16); /* round up to 2-byte boundary */ ! 469: sprintf(hline,"WINDOW=%d %d %d %d\n", ! 470: h->bx.a.x,h->bx.a.y,h->bx.b.x+1,h->bx.b.y+1); ! 471: write(fileno(h->fp),hline,strlen(hline)); ! 472: } ! 473: else if(strcmp(h->type,"sunraster")!=0 ) { /* bitfile window */ ! 474: h->bpl = 2*((h->bpl+15)/16); /* round up to 2-byte boundary */ ! 475: put_ushort(h->bx.a.x); put_ushort(h->bx.a.y); ! 476: put_ushort(h->bx.b.x+1); put_ushort(h->bx.b.y+1); ! 477: fflush(h->fp); ! 478: }; ! 479: if( ((h->res_x!=0||h->res_y!=0)) && (HASHEADER(h)) ! 480: && strcmp(h->type,"bitfile")!=0 ) { ! 481: sprintf(hline,"RES=%d %d\n",h->res_x,h->res_y); ! 482: write(fileno(h->fp),hline,strlen(hline)); ! 483: }; ! 484: if(h->parms!=NULL && strlen(h->parms)>0 && strcmp(h->type,"bitfile")!=0 ) { ! 485: sprintf(hline,"PARMS=%s\n",h->parms); ! 486: write(fileno(h->fp),hline,strlen(hline)); ! 487: }; ! 488: if(h->misc!=NULL && strlen(h->misc)>0 && strcmp(h->type,"bitfile")!=0 ) { ! 489: write(fileno(h->fp),h->misc,strlen(h->misc)); ! 490: }; ! 491: /* terminating blank line */ ! 492: if( HASHEADER(h) && strcmp(h->type,"bitfile")!=0 ) { ! 493: hline[0] = '\n'; ! 494: write(fileno(h->fp),hline,1); ! 495: }; ! 496: ! 497: if(strcmp(h->type,"rle")==0) { h->bpl = 0; h->line = NULL; } ! 498: else { /* allocate one extra byte in line buffer as a favor to RLE */ ! 499: if((h->line = (unsigned char *) malloc(h->bpl+1))==NULL) { ! 500: fprintf(stderr, ! 501: "piclib: PIC_put_hdr: can't alloc h->line (%d bytes) - abort\n", ! 502: h->bpl+1); ! 503: return(-1); ! 504: }; ! 505: if(strcmp(h->type,"bitfile")==0) { ! 506: memset(h->line,'\0',h->bpl); ! 507: if((h->pline = (unsigned char *) malloc(h->bpl+1))==NULL) { ! 508: fprintf(stderr, ! 509: "piclib: PIC_put_hdr: can't alloc h->pline (%d bytes) - abort\n", ! 510: h->bpl+1); ! 511: return(-1); ! 512: }; ! 513: }; ! 514: }; ! 515: h->cy = h->bx.a.y-1; /* just prior to 1st line */ ! 516: /* synchronize FILE * ptr with file descr ptr */ ! 517: fseek(h->fp,h->seek = lseek(fileno(h->fp),0L,1),0); ! 518: } ! 519: ! 520: err_PIC_line(h,sl) ! 521: PIC_hdr *h; ! 522: char *sl; ! 523: #define BPL 20 /* bytes to display per line */ ! 524: { char *cp,*ep; ! 525: int bpl; /* bytes per display line */ ! 526: bpl=0; ! 527: for(cp=sl,ep=sl+h->bpl; cp!=ep; cp++) { ! 528: fprintf(stderr,"%o ",0377&(*cp)); ! 529: if((++bpl)%BPL==0) fprintf(stderr,"\n "); ! 530: }; ! 531: if((bpl)%BPL!=0)fprintf(stderr,"\n"); ! 532: } ! 533: ! 534: /* Skip `y' lines of PIC file */ ! 535: PIC_skip(h,y) ! 536: PIC_hdr *h; ! 537: int y; ! 538: { lseek(fileno(h->fp),(long)(y*h->bpl),1); ! 539: h->seek += y*h->bpl; ! 540: h->cy += y; ! 541: } ! 542: ! 543: /* Read next full line of picture data, and if OK update line & cy; ! 544: return status: 1 OK, 0 EOF, -1 ERR. ! 545: Free buffer in header on EOF. */ ! 546: int PIC_rline(h,lbpp) ! 547: PIC_hdr *h; ! 548: unsigned char **lbpp; ! 549: { int stat; ! 550: if( (stat=read(fileno(h->fp),h->line,h->bpl)) == h->bpl) { ! 551: *lbpp=h->line; ! 552: h->seek += h->bpl; ! 553: h->cy++; ! 554: if(PIC_debug) err("read %d bytes from fileno(h->fp) - OK",stat); ! 555: return(1); ! 556: } ! 557: else { /* EOF or ERR */ ! 558: *lbpp=NULL; ! 559: free(h->line); h->line = NULL; ! 560: if(PIC_debug) err("read from fileno(h->fp) stat%d",stat); ! 561: if((stat>=0)&&(stat<h->bpl)) return(0 /*EOF*/); ! 562: else return(-1); ! 563: }; ! 564: } ! 565: ! 566: /* write a full line of picture data, returning status: 1 OK, 0 EOF, -1 ERR */ ! 567: int PIC_wline(h,line) ! 568: PIC_hdr *h; ! 569: unsigned char *line; ! 570: { int stat; ! 571: if( (stat=write(fileno(h->fp),line,h->bpl)) == h->bpl) { ! 572: h->seek += h->bpl; ! 573: h->cy++; ! 574: if(PIC_debug) err("wrote %d bytes to fd%d - OK",stat,fileno(h->fp)); ! 575: return(1); ! 576: } ! 577: else { /* ERR */ ! 578: err("write to fd%d stat%d",fileno(h->fp),stat); ! 579: if((stat>=0)&&(stat<h->bpl)) return(0 /*EOF*/); ! 580: else return(-1); ! 581: }; ! 582: } ! 583: ! 584: char *PIC_hdr_toa(h) ! 585: PIC_hdr *h; ! 586: { static char s[120]; ! 587: sprintf(s,"{type=%s bx%s res%d,%d bpl%d cy%d sk%d}", ! 588: h->type, ! 589: bbx_toa(&(h->bx)), ! 590: h->res_x,h->res_y, ! 591: h->bpl, ! 592: h->cy, ! 593: h->seek ! 594: ); ! 595: return(s); ! 596: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.