implement RImagefile; include "sys.m"; sys: Sys; include "draw.m"; include "bufio.m"; bufio: Bufio; Iobuf: import bufio; include "imagefile.m"; Header: adt { fd: ref Iobuf; ch: chan of (ref Rawimage, string); # variables in i/o routines buf: array of byte; bufi: int; nbuf: int; TYPE: string; CHAN: string; NCHAN: string; CMAP: int; dx: int; dy: int; }; NBUF: con 8*1024; init(iomod: Bufio) { if(sys == nil) sys = load Sys Sys->PATH; bufio = iomod; } read(fd: ref Iobuf): (ref Rawimage, string) { # spawn a subprocess so I/O errors can clean up easily ch := chan of (ref Rawimage, string); spawn readslave(fd, ch); return <-ch; } readmulti(fd: ref Iobuf): (array of ref Rawimage, string) { (i, err) := read(fd); if(i != nil){ a := array[1] of { i }; return (a, err); } return (nil, err); } readslave(fd: ref Iobuf, ch: chan of (ref Rawimage, string)) { (header, err) := header(fd, ch); if(header == nil){ ch <-= (nil, err); exit; } ch <-= image(header); } readerror(): string { return sys->sprint("ReadPIC: read error: %r"); } header(fd: ref Iobuf, ch: chan of (ref Rawimage, string)): (ref Header, string) { h := ref Header; h.fd = fd; h.ch = ch; h.CMAP = 0; h.dx = 0; h.dy = 0; cantparse := "ReadPIC: can't parse header"; for(;;){ s := fd.gets('\n'); if(s==nil || s[len s-1]!='\n') return (nil, cantparse); if(s == "\n") break; addfield(h, s[0:len s-1]); } if(h.dx<=0 || h.dy<=0) return (nil, "ReadPIC: empty picture or WINDOW not set"); return (h, nil); } addfield(h: ref Header, s: string) { baddata := "ReadPIC: not a PIC header"; for(i:=0; i