Annotation of researchv10no/cmd/pico/files.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include <sys/types.h>
                      3: #include <sys/stat.h>
                      4: #include "pico.h"
                      5: 
                      6: extern struct  SRC src[MANY];
                      7: extern short   CURSCRATCH, CUROLD;
                      8: extern int     nsrc, DEF_LL, DEF_NL;
                      9: extern char    frameb, usedold, metheus;
                     10: 
                     11: prepare(infile)
                     12:        char *infile;
                     13: {
                     14:        struct SRC *into = &src[nsrc];
                     15:        int from;
                     16: 
                     17:        into->ex = into->ll = DEF_LL;
                     18:        into->ey = into->nl = DEF_NL;
                     19:        into->nchan = DEF_CH;
                     20:        into->sx = into->sy = 0;
                     21: 
                     22:        if ((from = whatarray(infile)) != -1)
                     23:        {       fprintf(stderr, "sorry: base name clash of %s with %s\n",
                     24:                                                infile, src[from].fname);
                     25:                return 0;
                     26:        }
                     27:        strcpy(into->fname, infile);
                     28:        if ((from = eopen(infile)) > 0 && readheader(into, from))
                     29:        {       into->fd = from;
                     30:                into->used = 0;
                     31:                into->pixred = (unsigned char *) Emalloc(DEF_LL*DEF_NL);
                     32:                if (into->nchan >= 3)
                     33:                {       into->pixgrn = (unsigned char *) Emalloc(DEF_LL*DEF_NL);
                     34:                        into->pixblu = (unsigned char *) Emalloc(DEF_LL*DEF_NL);
                     35:                } else
                     36:                {       into->pixgrn = into->pixblu = into->pixred;
                     37:                }
                     38:                into->incore = 1;
                     39:                nsrc++;
                     40:                return nsrc-1;
                     41:        } else
                     42:        {       into->fname[0] = '\0';
                     43:                into->fd = -1;
                     44:                into->incore = into->used = 0;
                     45:                return 0;
                     46:        }
                     47: }
                     48: 
                     49: interpret(infile)
                     50:        char *infile;
                     51: {
                     52:        struct SRC *into = &src[nsrc];
                     53:        int from, hh;
                     54: 
                     55:        into->ex = into->ll = DEF_LL;
                     56:        into->ey = into->nl = DEF_NL;
                     57:        into->nchan = DEF_CH;
                     58:        into->sx = into->sy = 0;
                     59: 
                     60:        if ((from = eopen(infile)) > 0 && (hh = rawheader(into, from)))
                     61:        {       fprintf(stderr, "%s: ", &infile[findbase(infile)]);
                     62:                if (hh == 2)
                     63:                {       hh = less(from);
                     64:                        fprintf(stderr, "headerless file, ");
                     65:                        fprintf(stderr, "ll %d nl %d, ", into->ll, into->nl);
                     66:                        fprintf(stderr, "(%d = %dx512 bytes)\n", hh, hh/512);
                     67:                } else
                     68:                {       fprintf(stderr, "%s ", (into->nchan == 1)?"b&w  ":"color");
                     69:                        tpfile(into->type);
                     70:                        fprintf(stderr, "ll %d nl %d ", into->ll, into->nl);
                     71:                        wsize(into->sx, into->sy, into->ex, into->ey);
                     72:                }
                     73:                close(from);
                     74:        }
                     75:        into->fd = -1;
                     76:        into->incore = into->used = 0;
                     77: }
                     78: 
                     79: tpfile(n)
                     80: {
                     81:        switch (n) {
                     82:        case PICO:      fprintf(stderr, "pico-format "); break;
                     83:        case DUMP:      fprintf(stderr, "dump-format "); break;
                     84:        case RUNCODE:   fprintf(stderr, "runcode-format "); break;
                     85:        }
                     86: }
                     87: 
                     88: less(fd)
                     89: {      struct stat bam;
                     90:        if (fstat(fd, &bam)==0)
                     91:                return bam.st_size;
                     92:        return 0;
                     93: }
                     94: 
                     95: nopix(into)
                     96:        struct SRC *into;
                     97: {
                     98:        into->pixred = (unsigned char *) Emalloc(DEF_LL*DEF_NL);
                     99: /*
                    100:        into->pixgrn = (unsigned char *) Emalloc(DEF_LL*DEF_NL);
                    101:        into->pixblu = (unsigned char *) Emalloc(DEF_LL*DEF_NL);
                    102: */
                    103:        into->pixgrn = (unsigned char *) 0;
                    104:        into->pixblu = (unsigned char *) 0;
                    105:        into->ex = into->ll = DEF_LL;
                    106:        into->ey = into->nl = DEF_NL;
                    107:        into->nchan = DEF_CH;
                    108:        into->sx = into->sy = 0;
                    109:        into->ox = into->oy = 0;
                    110:        strcpy(into->fname, "_workspace_");
                    111:        into->incore = 1;
                    112: }
                    113: 
                    114: checkpix(into)
                    115:        struct SRC *into;
                    116: {
                    117:        if (!into->pixgrn)
                    118:                into->pixgrn = (unsigned char *) Emalloc(DEF_LL*DEF_NL);
                    119:        if (!into->pixblu)
                    120:                into->pixblu = (unsigned char *) Emalloc(DEF_LL*DEF_NL);
                    121: }
                    122: 
                    123: discard(s)
                    124:        char *s;
                    125: {      int nr;
                    126: 
                    127:        if ((nr = ungetpix(s)) < 2)
                    128:                return 0;
                    129: 
                    130:        close(src[nr].fd);
                    131:        src[nr].fd = -1;
                    132:        src[nr].sx = src[nr].sy = src[nr].nchan = 0;
                    133:        src[nr].ex = src[nr].ey = src[nr].ll = 0;
                    134:        strcpy(src[nr].fname, " empty");
                    135:        src[nr].used = 0;
                    136: 
                    137:        return nr;
                    138: }
                    139: 
                    140: ungetpix(s)
                    141:        char *s;
                    142: {      int nr;
                    143: 
                    144:        if ((nr = whatarray(s)) < 2)
                    145:        {       fprintf(stderr, "unknown file %s\n", s);
                    146:                return 0;
                    147:        }
                    148: 
                    149:        if (src[nr].incore == 0)
                    150:                return nr;
                    151: 
                    152:        free(src[nr].pixred);
                    153:        if (src[nr].nchan >= 3)
                    154:        {       free(src[nr].pixgrn);
                    155:                free(src[nr].pixblu);
                    156:        }
                    157:        src[nr].incore = 0;
                    158:        lseek(src[nr].fd, src[nr].sop, 0);
                    159: 
                    160:        return nr;
                    161: }
                    162: 
                    163: newscreen(infile)
                    164:        char *infile;
                    165: {
                    166:        if ((Old->fd = eopen(infile)) > 0 && readheader(Old, Old->fd))
                    167:        {       getpix(Old, infile);
                    168:                putscreen(CUROLD);
                    169:                setscratch(Old, Scratch);
                    170:                return 1;
                    171:        }       /* else reset */
                    172:        Old->ex = Old->ll = DEF_LL;
                    173:        Old->ey = Old->nl = DEF_NL;
                    174:        Old->nchan = DEF_CH;
                    175:        Old->sx = Old->sy = 0;
                    176:        return 0;
                    177: }
                    178: 
                    179: readheader(into, from)
                    180:        struct SRC *into;
                    181: {
                    182:        int i, ret = rawheader(into, from);
                    183:        int a = into->sx, b = into->sy;
                    184:        int c = into->ex, d = into->ey;
                    185:        int n = into->nchan;
                    186: 
                    187:        if (a > c) i = a, a = c, c = i;         /* flip */
                    188:        if (b > d) i = b, b = d, d = i;
                    189:        if (a < 0) c -= a, a = 0;               /* normalize  */
                    190:        if (b < 0) d -= b, b = 0;
                    191:        into->ll = c-a; into->nl = d-b;         /* scan ll,nl */
                    192:                        
                    193:        a = min(DEF_LL, a); c = min(DEF_LL, c); /* clip */
                    194:        b = min(DEF_NL, b); d = min(DEF_NL, d);
                    195: 
                    196:        into->sx = a; into->sy = b;
                    197:        into->ex = c; into->ey = d;
                    198:        into->ox = 0; into->oy = 0;
                    199: 
                    200:        if (ret == 0) return 0;
                    201: 
                    202:        if (a >= c || b >= d)
                    203:        {       fprintf(stderr, "pico: file window is outside screen area\n");
                    204:                return 0;
                    205:        }
                    206:        if (n < 1 || n > 4)
                    207:        {       fprintf(stderr, "pico: bad nr of channels (%d)\n", n);
                    208:                return 0;
                    209:        }
                    210: 
                    211:        return ret;
                    212: }
                    213: 
                    214: rawheader(into, from)
                    215:        struct SRC *into;
                    216: { char line[512];
                    217:   int i, j, a, b, c, d, N, sawtype=0;
                    218:   extern double sqrt();
                    219: 
                    220:        into->type = PICO;      /* default */
                    221:        for (j = 0;;)
                    222:        {       for (i = 0; i < 127; i++, j++)
                    223:                {       read(from, &line[i], 1);
                    224:                        if (line[i] == '\n')
                    225:                        {       line[i] = '\0';
                    226:                                break;
                    227:                }       }
                    228:                if (i == 0)                     /* saw end of header */
                    229:                {       if (sawtype)
                    230:                        {       into->sop = j;
                    231:                                return 1;
                    232:                        } else
                    233:                                i = 127;
                    234:                }
                    235:                if (i == 127)                   /* no header */
                    236:                {       into->nchan = 1;        /* assume b&w, square image */
                    237:                        N = (int) sqrt((double)less(from)+1.0);
                    238:                        into->ex = into->ey = N;
                    239:                        into->ll = into->nl = N;
                    240:                        lseek(from, 0L, 0);
                    241:                        into->sop = 0;
                    242:                        return 2;
                    243:                }
                    244:                if (sscanf(line, "WINDOW=%d %d %d %d", &a, &b, &c, &d) == 4)
                    245:                {       into->sx = a; into->sy = b;
                    246:                        into->ex = c; into->ey = d;
                    247:                        into->ll = c-a; into->nl = d-b;
                    248:                } else if (sscanf(line, "NCHAN=%d", &a) == 1)
                    249:                        into->nchan = a;
                    250:                   else if (strcmp(line, "TYPE=dump") == 0)
                    251:                 {      into->type = DUMP; sawtype = 1;
                    252:                 } else if (strcmp(line, "TYPE=runcode") == 0)
                    253:                 {      into->type = RUNCODE; sawtype = 1;
                    254:                 } else if (strcmp(line, "TYPE=pico") == 0)
                    255:                 {      into->type = PICO; sawtype = 1;
                    256:                 } else if (strncmp(line, "TYPE", 4) == 0)
                    257:                {       fprintf(stderr, "pico: unknown format: %s..\n", line);
                    258:                        return 0;
                    259:                }
                    260:        }
                    261: }
                    262: 
                    263: putheader(where, n, a, b, c, d)
                    264: {      char line[128];
                    265: 
                    266:        sprintf(line, "TYPE=pico\nWINDOW=%d %d %d %d\nNCHAN=%d\n\n", a,b,c,d,n);
                    267:        write(where, line, strlen(line));
                    268: }
                    269: 
                    270: putdpix(which, how)
                    271:        char *which;
                    272: {
                    273:        int fd, a, b, c, d;
                    274: 
                    275:        a = Old->sx; b = Old->sy;
                    276:        c = Old->ex; d = Old->ey;
                    277: 
                    278:        checkit();              /* make sure we have screen incore */
                    279:        if ((fd = creat(which, 0666)) <= 0)
                    280:                yyerror("cannot create file %s", which);
                    281: 
                    282:        if (!how)
                    283:                putwindow(fd, Old->pixred, a, b, c-a, d-b);
                    284:        else
                    285:        {       if (Old->nchan != 1 || c-a != d-b)
                    286:                        putheader(fd, min(3, Old->nchan), a, b, c, d);
                    287:                putfile(fd, a, b, c-a, d-b);
                    288:        }
                    289:        close(fd);
                    290: }
                    291: 
                    292: putfile(where, x, y, w, d)
                    293: {
                    294:        putwindow(where, Old->pixred, x, y, w, d);
                    295:        if (Old->nchan >= 3)
                    296:        {       putwindow(where, Old->pixgrn, x, y, w, d);
                    297:                putwindow(where, Old->pixblu, x, y, w, d);
                    298:        }
                    299:        close(where);
                    300: }
                    301: 
                    302: checkit()
                    303: {      register int i;
                    304: 
                    305:        if (usedold && src[CUROLD].incore != 2 && (frameb || metheus))
                    306:                fprintf(stderr, "warning: picture on screen wasn't read\n");
                    307: 
                    308:        for (i = 2; i < nsrc; i++)
                    309:        if (src[i].used && src[i].incore != 2)
                    310:        {       src[i].used = 0;
                    311:                getpix(&src[i], src[i].fname);
                    312:        }
                    313: }
                    314: 
                    315: setscratch(from, to)
                    316:        struct SRC *from, *to;
                    317: {
                    318:        register unsigned char *qr = from->pixred, *pr = to->pixred;
                    319:        register unsigned char *qg = from->pixgrn, *pg = to->pixgrn;
                    320:        register unsigned char *qb = from->pixblu, *pb = to->pixblu;
                    321:        memcpy(pr, qr, DEF_NL*DEF_LL);
                    322:        if (pg && qg) memcpy(pg, qg, DEF_NL*DEF_LL);
                    323:        if (pb && qb) memcpy(pb, qb, DEF_NL*DEF_LL);
                    324: }
                    325: 
                    326: getter(n)
                    327: {
                    328:        if (n <= 0)
                    329:                return;
                    330:        if (n >  1)
                    331:        {       if (src[n].incore != 2)
                    332:                getpix(&src[n], src[n].fname);
                    333:                setscratch(&src[n], Old);
                    334:        }
                    335:        putscreen(CUROLD);
                    336:        if (n >  1)
                    337:                setscratch(Old, Scratch);
                    338:        RESIDENT;
                    339: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.