|
|
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: /* riclib.c - Ricoh scanner-file public functions:
7:
8: RIC_* functions use system I/O, for speed...
9: int RIC_get_hdr(fd,RIC_hdr *) - get scanner-file header
10: int RIC_line(char **) - read one line of scanner data
11: err_RIC_Line(char *,RIC_hdr) - print on stderr
12: int RIC_oline(char **) - write one line of scanner data
13: char *RIC_hdr_toa() - RIC_hdr to ascii printable string
14: */
15:
16: #include <stdio.h>
17: #include <math.h>
18: #include <string.h>
19: #include "CPU.h"
20: #include "stdocr.h"
21:
22: #define RIC_debug 0
23:
24: /* treated as local static: */
25: int RIC_fd; /* file descr. */
26: int RIC_bpl; /* bytes per line */
27: char *RIC_bf = NULL; /* (malloc space *) holds one line of scanner data */
28:
29: /* system I/O variation on fgets(3), except it replaces \n with \0,
30: and returns the number of chars read (including \0) */
31: int RIC_fgets(bf,max,fd)
32: char *bf;
33: int max;
34: int fd;
35: { char *cp;
36: int stat,mny;
37: cp=bf; mny=0;
38: while(((stat=read(fd,cp,1))==1)&&(++mny<max)&&((*cp)!='\n')) cp++;
39: if(stat!=1) return(stat);
40: else if(mny>=max) return(-1);
41: *cp='\0';
42: return(mny);
43: }
44:
45: /* read header from scanner file, return status: 1 OK, 0 EOF, -1 error */
46: int RIC_get_hdr( fd, hp )
47: int fd; /* should have been open'ed earlier */
48: RIC_hdr *hp;
49: #define HL_MAX 80
50: #define HTERM "=, \n" /* terminations for header words: "=,<sp><tab>" */
51: { char *cp,*parm,hline[HL_MAX];
52: int status,nrd;
53: RIC_fd = fd;
54: if((status=RIC_fgets(hline,HL_MAX,RIC_fd))<=0) return(status);
55: while(strlen(hline)>1) {
56: if(RIC_debug) err("hline \"%s\"",hline);
57: parm=strtok(hline,HTERM);
58: if(parm!=NULL&&strcmp(parm,"TYPE")==0) {
59: if((parm=strtok(0,HTERM))!=NULL
60: &&strcmp(parm,"binary")==0) ;
61: else return(-1);
62: }
63: else if(parm!=NULL&&strcmp(parm,"WINDOW")==0) {
64: if((parm=strtok(0,HTERM))!=NULL) hp->bx.a.x=atoi(parm);
65: else return(-1);
66: if((parm=strtok(0,HTERM))!=NULL) hp->bx.a.y=atoi(parm);
67: else return(-1);
68: if((parm=strtok(0,HTERM))!=NULL) hp->bx.b.x=atoi(parm)-1;
69: else return(-1);
70: hp->bpl=(hp->bx.b.x-hp->bx.a.x+1+7)/8;
71: if((parm=strtok(0,HTERM))!=NULL) hp->bx.b.y=atoi(parm)-1;
72: else return(-1);
73: }
74: else if(parm!=NULL&&strcmp(parm,"RES")==0) {
75: if((parm=strtok(0,HTERM))!=NULL) hp->res_x=atoi(parm);
76: else return(-1);
77: if((parm=strtok(0,HTERM))!=NULL) hp->res_y=atoi(parm);
78: else return(-1);
79: }
80: else return(-1);
81: if((status=RIC_fgets(hline,HL_MAX,RIC_fd))<=0) return(status);
82: };
83: RIC_bpl=hp->bpl;
84: /* allocate one extra byte in scanner buffer as a favor to RLE */
85: if((RIC_bf = (char *) malloc(RIC_bpl+1))==NULL) {
86: fprintf(stderr,
87: "riclib: can't alloc RIC_bf (%d bytes) - abort\n",
88: RIC_bpl+1);
89: return(-1);
90: };
91: return(1);
92: }
93:
94: /* write header to scanner file */
95: RIC_put_hdr( fd, hp )
96: int fd; /* should have been open'ed earlier */
97: RIC_hdr *hp;
98: { char *cp,*parm,hline[HL_MAX];
99: int status,nrd;
100: sprintf(hline,"TYPE=binary\n");
101: write(fd,hline,strlen(hline));
102: sprintf(hline,"WINDOW=%d %d %d %d\n",
103: hp->bx.a.x,hp->bx.a.y,hp->bpl,hp->bx.b.y+1);
104: write(fd,hline,strlen(hline));
105: sprintf(hline,"RES=%d %d\n\n",hp->res_x,hp->res_y);
106: write(fd,hline,strlen(hline));
107: }
108:
109: err_RIC_line(sl,shdr)
110: char *sl;
111: RIC_hdr shdr;
112: #define BPL 20 /* bytes to display per line */
113: { char *cp,*ep;
114: int bpl; /* bytes per display line */
115: bpl=0;
116: for(cp=sl,ep=sl+shdr.bpl; cp!=ep; cp++) {
117: fprintf(stderr,"%o ",0377&(*cp));
118: if((++bpl)%BPL==0) fprintf(stderr,"\n ");
119: };
120: if((bpl)%BPL!=0)fprintf(stderr,"\n");
121: }
122:
123: /* skip `y' lines, starting from current read pointer */
124: RIC_skip(y)
125: int y;
126: { lseek(RIC_fd,(long)(y*RIC_bpl),1);
127: }
128:
129: /* read next full line of scanner data, return status: 1 OK, 0 EOF, -1 ERR */
130: int RIC_line(lbpp)
131: char **lbpp;
132: { int stat;
133: if( (stat=read(RIC_fd,RIC_bf,RIC_bpl)) == RIC_bpl) {
134: *lbpp=RIC_bf;
135: if(RIC_debug) err("read %d bytes from RIC_fd - OK",stat);
136: return(1);
137: }
138: else { /* EOF or ERR */
139: *lbpp=NULL;
140: free(RIC_bf);
141: if(RIC_debug) err("read from RIC_fd stat%d",stat);
142: if((stat>=0)&&(stat<RIC_bpl)) return(0 /*EOF*/);
143: else return(-1);
144: };
145: }
146:
147: /* write a full line of scanner data, returning status: 1 OK, 0 EOF, -1 ERR */
148: int RIC_oline(fd,bf)
149: int fd;
150: char *bf;
151: { int stat;
152: if( (stat=write(fd,bf,RIC_bpl)) == RIC_bpl) {
153: if(RIC_debug) err("wrote %d bytes to fd%d - OK",stat,fd);
154: return(1);
155: }
156: else { /* ERR */
157: err("write to fd%d stat%d",fd,stat);
158: if((stat>=0)&&(stat<RIC_bpl)) return(0 /*EOF*/);
159: else return(-1);
160: };
161: }
162:
163: char *RIC_hdr_toa(hp)
164: RIC_hdr *hp;
165: { static char s[40];
166: sprintf(s,"{res%d,%d bpl%d bx%s}\n",
167: hp->res_x,hp->res_y,
168: hp->bpl,
169: bbx_toa(&(hp->bx)));
170: return(s);
171: }
172:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.