Annotation of researchv10no/cmd/dimpress/bimp.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include "imPRESS.h"
                      3: 
                      4: #define min(a,b)       ((a<=b) ? a : b)
                      5: 
                      6: #define        LINES   32
                      7: #define        RASTSIZ 108
                      8: char rasters[LINES][RASTSIZ] = { 0 };
                      9: 
                     10: glyphpage(out, name, size, xoff, yoff)
                     11: FILE *out;
                     12: char *name;
                     13: int size, xoff, yoff;
                     14: {
                     15:        int nrasters, rastwid, rastbyt, nghoriz, ngvert, krast, i, iprev, j;
                     16:        int x1, y1, x2, y2;
                     17:        FILE *in;
                     18:        if ((in = fopen(name, "r")) == NULL) {
                     19:                fprintf(stderr, "cannot open %s\n", name);
                     20:                return -1;
                     21:        }
                     22: 
                     23:        if (getw(in) != 0) {
                     24:                fclose(in);
                     25:                return 1;
                     26:        }
                     27:        x1 = getw(in);
                     28:        y1 = getw(in);
                     29:        x2 = getw(in);
                     30:        y2 = getw(in);
                     31:        nrasters = (y2 - y1 < 0) ? y1 - y2 : y2 - y1;
                     32:        rastwid = (x2 - x1 < 0) ? x1 - x2 : x2 - x1;
                     33:        if (nrasters <= 0 || nrasters > 1024 || rastwid  <= 0 || rastwid  >  800) {
                     34:                fclose(in);
                     35:                return -1;
                     36:        }
                     37: 
                     38:        xoff >>= size;
                     39:        yoff >>= size;
                     40:        rastbyt = ((rastwid + xoff + 15) / 16) * 2;
                     41:        rastwid = (rastwid + 15) / 16;
                     42:        ngvert  = (nrasters + yoff + 31) / 32;
                     43:        nghoriz = (rastbyt + 3) / 4;
                     44: 
                     45:        putc(imP_SET_MAGN , out);
                     46:        putc(         size, out);
                     47:        putc(imP_BITMAP   , out);
                     48:        putc(         0x0F, out);
                     49:        putc(      nghoriz, out);
                     50:        putc(       ngvert, out);
                     51: 
                     52:        /* clear out old junk in array */
                     53:        for (i=0; i<32; i++)
                     54:                for (j=0; j<rastbyt+2; j++)
                     55:                        rasters[i][j] = 0;
                     56:        /* send the rasters */
                     57:        for (nrasters+=yoff; nrasters>0; nrasters -= krast) {
                     58:                krast=min(nrasters,32);
                     59:                for (iprev=(yoff-1)&0x1f,i=yoff; i<krast; iprev=i++) {
                     60:                        for (j=0; j<rastbyt+2; j++)
                     61:                                rasters[i][j] = 0;
                     62:                        if (readrast(rasters[i],rastwid,in) < 0){
                     63:                                fclose(in);
                     64:                                return -1;
                     65:                        }
                     66:                        shiftrast(rasters[i], xoff, rastbyt);
                     67:                        for (j=0; j<rastbyt; j++)
                     68:                                rasters[i][j] ^= rasters[iprev][j];
                     69:                }
                     70:                /* clear out old junk in array */
                     71:                for (i=krast; i<32; i++){
                     72:                        for (j=0; j<rastbyt+2; j++) {
                     73:                                rasters[i][j] = 0;
                     74:                        }
                     75:                }
                     76:                for (j=0; j<rastbyt; j+=4) {
                     77:                        for (i=0; i<32; i++)
                     78:                                fwrite(&rasters[i][j],4,1,out);
                     79:                }
                     80:                yoff=0;
                     81:        }
                     82:        fclose(in);
                     83:        return 0;
                     84: }
                     85: 
                     86: static
                     87: readrast(p1,nw,in)
                     88: char *p1;
                     89: int nw;
                     90: FILE *in;
                     91: {
                     92:        int count, ctype;
                     93: 
                     94:        while (nw>0) {
                     95:                if ((count=getc(in)) <= 0)
                     96:                        return -1;
                     97:                ctype = count & 0x80;
                     98:                count &= 0x7f;
                     99:                nw -= count;
                    100:                count *= 2;
                    101: 
                    102:                if (ctype) {
                    103:                        if (fread(p1,2,1,in) <= 0)
                    104:                                return -1;
                    105:                        for (count-=2; count>0; count--) {
                    106:                                *(p1+2) = *p1;
                    107:                                p1 += 1;
                    108:                        }
                    109:                        p1 += 2;
                    110:                } else {
                    111:                        if (fread(p1,count,1,in) <= 0)
                    112:                                return -1;
                    113:                        p1 += count;
                    114:                }
                    115:        }
                    116:        return (nw == 0 ? 0 : -1);
                    117: }
                    118: 
                    119: static
                    120: getw(stream)
                    121: FILE *stream;
                    122: {
                    123:        register l = getc(stream);
                    124:        return (getc(stream)<<8) | l;
                    125: }
                    126: iglyphpage(out, name, size, xoff, yoff)
                    127: FILE *out;
                    128: char *name;
                    129: int size, xoff, yoff;
                    130: {
                    131:        int nrasters, rastwid, rastbyt, nghoriz, ngvert, krast, i, iprev, j;
                    132:        int h=0, v, nbits;
                    133:        FILE *in;
                    134: 
                    135:        if ((in = fopen(name, "r")) == NULL) {
                    136:                fprintf(stderr, "cannot open %s\n", name);
                    137:                return -1;
                    138:        }
                    139: 
                    140:        for (v=0; nbits = nexticon(rasters, in); v++)
                    141:                if (nbits > h)
                    142:                        h = nbits;
                    143:        rewind(in);
                    144: 
                    145:        nrasters = v;
                    146:        rastwid = h;
                    147:        if (nrasters <= 0 || nrasters > 1024 || rastwid  <= 0 || rastwid  >  800) {
                    148:                fclose(in);
                    149:                return -1;
                    150:        }
                    151: 
                    152:        xoff >>= size;
                    153:        yoff >>= size;
                    154: 
                    155:        rastbyt = ((rastwid + xoff + 15) / 16) * 2;
                    156:        rastwid = (rastwid + 15) / 16;
                    157:        ngvert  = (nrasters + yoff + 31) / 32;
                    158:        nghoriz = (rastbyt + 3) / 4;
                    159: 
                    160:        putc(imP_SET_MAGN, out);
                    161:        putc(        size, out);
                    162: 
                    163:        putc(imP_BITMAP  , out);
                    164:        putc(        0x0F, out);
                    165:        putc(     nghoriz, out);
                    166:        putc(      ngvert, out);
                    167: 
                    168:        /* clear out old junk in array */
                    169:        for (i=0; i<32; i++)
                    170:                for (j=0; j<rastbyt+2; j++)
                    171:                        rasters[i][j] = 0;
                    172:        /* send the rasters */
                    173:        for (nrasters+=yoff; nrasters>0; nrasters -= krast) {
                    174:                krast=min(nrasters,32);
                    175:                for (iprev=(yoff-1)&0x1f,i=yoff; i<krast; iprev=i++) {
                    176:                        for (j=0; j<rastbyt+2; j++)
                    177:                                rasters[i][j] = 0;
                    178:                        if (nexticon(rasters[i], in) == 0) {
                    179:                                fclose(in);
                    180:                                return -1;
                    181:                        }
                    182:                        shiftrast(rasters[i], xoff, rastbyt);
                    183:                }
                    184:                /* clear out old junk in array */
                    185:                for (i=krast; i<32; i++){
                    186:                        for (j=0; j<rastbyt+2; j++) {
                    187:                                rasters[i][j] = 0;
                    188:                        }
                    189:                }
                    190:                for (j=0; j<rastbyt; j+=4) {
                    191:                        for (i=0; i<32; i++){
                    192:                                fwrite(&rasters[i][j],4,1,out);
                    193:                        }
                    194:                }
                    195:                yoff=0;
                    196:        }
                    197:        fclose(in);
                    198:        return 0;
                    199: }
                    200: static int
                    201: nexticon(rast, in)
                    202: short *rast;
                    203: FILE *in;
                    204: {
                    205:        register j = 0, nbits; char delim[8], *p, c;
                    206:        do {
                    207:                if (fscanf(in, " 0x%4hx%[, \t\n]", (p = (char *)&rast[j++]), delim) <= 0)
                    208:                        return 0;
                    209:                c = *p; *p = *(p+1); *(p+1) = c;        /* swap the bytes */
                    210:        } while (strchr(delim, '\n') == 0);
                    211:        nbits = 16*j;
                    212:        return nbits;
                    213: }
                    214: 
                    215: shiftrast(s, b, w)
                    216: unsigned char *s;
                    217: {
                    218:        register unsigned char *from, *to;
                    219:        register int i, bits, byte;
                    220:        i = b >> 3;
                    221:        if (i != 0) {
                    222:                for (to = &s[w], from = &s[w-i]; from != s; )
                    223:                        *--to = *--from;
                    224:                while (to != s)
                    225:                        *--to = 0;
                    226:        }
                    227:        bits = 0;
                    228:        i = b & 0x7;
                    229:        if (i != 0)
                    230:                for (to = s; to != &s[w]; to++) {
                    231:                        byte = (bits | *to) >> i;
                    232:                        bits = (*to & ((1 << i) - 1)) << 8;
                    233:                        *to = byte;
                    234:                }
                    235: }

unix.superglobalmegacorp.com

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