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