plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

bbits.c (2101B)


      1 #include	<u.h>
      2 #include	<libc.h>
      3 #include	<libg.h>
      4 #include	<bio.h>
      5 #include	"hdr.h"
      6 
      7 enum {
      8 	Charsperfont	= 157,
      9 	Void1b		= (0xC6-0xA2)*Charsperfont+0x3f,
     10 	Void1e		= (0xC9-0xA2)*Charsperfont - 1
     11 };
     12 
     13 Bitmap *
     14 breadbits(char *file, int n, long *chars, int size, uchar *bits, int **doneptr)
     15 {
     16 	Bitmap *bm;
     17 	Biobuf *bf;
     18 	int i, j, byt;
     19 	int nch;
     20 	long c;
     21 	uchar *b, *nbits;
     22 	int *done;
     23 	static uchar missing[32] = {0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x08, 0x00, 0x04, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x80, 0x00, 0x40, 0x00, 0x20, 0x00, 0x10, 0x00, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
     24 	uchar buf[32];
     25 
     26 	bf = Bopen(file, OREAD);
     27 	if(bf == 0){
     28 		fprint(2, "%s: %s: %r\n", argv0, file);
     29 		exits("bitfile open error");
     30 	}
     31 	done = (int *)malloc(n*sizeof(done[0]));
     32 	if(done == 0){
     33 		fprint(2, "%s: malloc error (%d bytes)\n", argv0, n);
     34 		exits("malloc error");
     35 	}
     36 	*doneptr = done;
     37 	byt = size/8;
     38 	nch = 0;
     39 	for(i = 0; i < n; i++){
     40 		done[i] = 1;
     41 		nch++;
     42 		c = chars[i];
     43 		if((c >= Void1b) && (c <= Void1e)){
     44 			done[i] = 0;
     45 			nch--;
     46 			continue;
     47 		}
     48 		/* magic via presotto for calculating the seek */
     49 		j = c;
     50 		if(c >= 2*Charsperfont)
     51 			j += 294;	/* baffling hole between A3 and A4 */
     52 		if(c > Void1e)
     53 			j -= 3*Charsperfont - 0x3F;
     54 		j *= byt*size;		/* bytes per char */
     55 		j += 256;		/* 256 front matter */
     56 		Bseek(bf, j, 0);
     57 		Bread(bf, buf, sizeof(missing));
     58 		if(memcmp(buf, missing, sizeof(missing)) == 0){
     59 			done[i] = 0;
     60 			nch--;
     61 			continue;
     62 		}
     63 		Bseek(bf, j, 0);
     64 		b = bits + i*byt;
     65 		for(j = 0; j < size; j++){	/* rows */
     66 			Bread(bf, b, byt);
     67 			b += n*byt;
     68 		}
     69 	}
     70 	nbits = (uchar *)malloc(nch*byt*size);
     71 	if(nbits == 0){
     72 		fprint(2, "%s: malloc error (%d bytes)\n", argv0, nch*byt);
     73 		exits("malloc error");
     74 	}
     75 	c = 0;
     76 	for(i = 0; i < n; i++)
     77 		if(done[i]){
     78 			for(j = 0; j < size; j++)
     79 				memmove(nbits+c*byt+j*nch*byt, bits+i*byt+j*n*byt, byt);
     80 			c++;
     81 		}
     82 	bm = balloc((Rectangle){(Point){0, 0}, (Point){nch*size, size}}, 0);
     83 	if(bm == 0){
     84 		fprint(2, "%s: balloc failure\n", argv0);
     85 		exits("balloc failure");
     86 	}
     87 	wrbitmap(bm, 0, size, nbits);
     88 	return(bm);
     89 }