make-gray50.c
changeset 0 2b12eedda295
equal deleted inserted replaced
-1:000000000000 0:2b12eedda295
       
     1 #include <stdio.h>
       
     2 #include <stdlib.h>
       
     3 
       
     4 
       
     5 static int width, height;
       
     6 static char bname[50] = "noname";
       
     7 static int perline = 13;
       
     8 
       
     9 static char mask[9] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
       
    10 static char imask[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
       
    11 
       
    12 void
       
    13 parameters(const int argn, char **argc)
       
    14 {
       
    15 	if (argn != 3)
       
    16 	{
       
    17 		fprintf(stderr, "usage: %s <width> <height>\n", argc[0]);
       
    18 		exit(1);
       
    19 	}
       
    20 
       
    21 	width = atoi(argc[1]);
       
    22 	height = atoi(argc[2]);
       
    23 }
       
    24 
       
    25 void
       
    26 writesize(int w, int h)
       
    27 {
       
    28 	printf("#define %s_width %i\n", bname, w);
       
    29 	printf("#define %s_height %i\n", bname, h);
       
    30 }
       
    31 
       
    32 char
       
    33 cstart(int p, int n)
       
    34 {
       
    35 	char c;
       
    36 
       
    37 	if (p==0)
       
    38 		c=0xaa;
       
    39 	else
       
    40 		c=0x55;
       
    41 
       
    42 	return c & mask[n];
       
    43 }
       
    44 
       
    45 char
       
    46 cend(int p, int n)
       
    47 {
       
    48 	char c;
       
    49 
       
    50 	if (p==0)
       
    51 		c=0xaa;
       
    52 	else
       
    53 		c=0x55;
       
    54 
       
    55 	return c & imask[n];
       
    56 }
       
    57 
       
    58 void
       
    59 writebits(int w, int h)
       
    60 {
       
    61 	int column;
       
    62 	int i;
       
    63 	int bits;
       
    64 	unsigned char c;
       
    65 	int pair; /* boolean */
       
    66 	int leftline;
       
    67 
       
    68 	printf("static char %s_bits[] = {\n", bname);
       
    69 
       
    70 	bits=w*h;
       
    71 	leftline=w;
       
    72 	i=0;
       
    73 	column=0;
       
    74 	pair=0;
       
    75 	while(i < bits)
       
    76 	{
       
    77 		if(column == 0)
       
    78 			putchar(' ');
       
    79 
       
    80 		/*fprintf(stderr, "leftline=%i\n", leftline);*/
       
    81 		if (leftline >= 8)
       
    82 		{
       
    83 			c = cstart(pair, 8);
       
    84 			printf("0x%2x", (unsigned int) c);
       
    85 			column++;
       
    86 			leftline -= 8;
       
    87 			i += 8;
       
    88 		}
       
    89 		else if (leftline > 0)
       
    90 		{
       
    91 			c = cend(pair, leftline);
       
    92 			pair ^= 1; /* 1->0, 0->1 */
       
    93 			c |= cstart(pair, 8 - leftline);
       
    94 			printf("0x%2x", (unsigned int) c);
       
    95 			i += leftline;
       
    96 			leftline = w;
       
    97 			column++;
       
    98 		}
       
    99 		else if (leftline == 0)
       
   100 		{	
       
   101 			leftline = w;
       
   102 			pair ^= 1; /* 1->0, 0->1 */
       
   103 		}
       
   104 
       
   105 		if(i < bits && leftline != 0)
       
   106 			putchar(',');
       
   107 
       
   108 		if (column > perline && leftline != 0)
       
   109 		{
       
   110 			putchar('\n');
       
   111 			column = 0;
       
   112 		}
       
   113 
       
   114 	}
       
   115 
       
   116 	printf("};\n");
       
   117 }
       
   118 
       
   119 int main(int argn, char **argc)
       
   120 {
       
   121 	parameters(argn, argc);
       
   122 	writesize(width, height);
       
   123 	writebits(width, height);
       
   124 	return 0;
       
   125 }