summaryrefslogtreecommitdiff
path: root/graphics/bit2spr/bit2spr.c
blob: af3a47f45d3d11a5e5339d7079618e6e1928118f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Bitmap to LaTeX Sprite 
 * by Marc David Rovner (mrovner@ic.sunysb.edu)
 * June 7th, 1992
 */

#include	<stdio.h>
#include	<stdlib.h>

#define MAXFILENAME	1024
#define INITWIDTH	1.5
#define INITHEIGHT	1.5

typedef struct {
	char OutFileName[MAXFILENAME];
	float WidthRatio, HeightRatio;
} ConvInfo;

void conv_bitmap(Stuff,bitmapfile,spritefile,spritename)
ConvInfo Stuff;
FILE *bitmapfile, *spritefile;
char *spritename;
{ char buffer0[80], buffer1[80], temp;
  int width, height, Row, Column, byte, i;

	if ( (fscanf(bitmapfile, "%s %s %i", buffer0, buffer1, &width) == EOF)
		|| (fscanf(bitmapfile, "%s %s %i", buffer0, buffer1, &height) == EOF) )
	{
		fprintf(stderr, "File not correct bitmap file.\n");
		fclose(bitmapfile);
		exit(-1);
	}
	do
	{
		fscanf(bitmapfile, "%s", buffer0);
	}
	while (buffer0[0] != '{');
		
		fprintf(spritefile,"\\sprite{\\%s}(%i,%i)[%.3fin,%.3fin]\n",
			spritename, width, height,
			((width * Stuff.WidthRatio)/160.0),
			((height * Stuff.HeightRatio)/160.0) );

	for (Row = 0; Row < height; ++Row)
	{
		fprintf(spritefile,":");
		for (Column = 0; Column < width; Column = Column + 8)
		{
			if (fscanf(bitmapfile, "%i%c ", &byte, &temp) == EOF)
			{
				fprintf(stderr, "File not correct bitmap file.\n");
				fclose(bitmapfile);
				exit(-1);
			}
			for(i = 0; i<= 7; i++)
			{
				if ( (byte >> i) & 1)
				{
					fprintf(spritefile,"B");
				}
				else
				{
					fprintf(spritefile,".");
				}
			}
		}
		fprintf(spritefile," |\n");
	
	}
	fprintf(spritefile,"\\endsprite\n");

	fclose(bitmapfile);
}

void main(argc,argv)
int argc; char *argv[];
{ ConvInfo Stuff;
  FILE *bitmapfile, *spritefile;
  int filestatus;
  char *c;
  

	/* Assign initial values to conversion information record */
	Stuff.WidthRatio=INITWIDTH;
	Stuff.HeightRatio=INITHEIGHT;
	sprintf(Stuff.OutFileName,"\0");

	/* While there are still command line options...*/
	while (--argc>0 && (*++argv)[0] == '-')
	{
		c = ++(*argv);

		/* User is redefining the width ratio */
		if (strcmp("width",c) == 0)
		{
			--argc; (*++argv);
			Stuff.WidthRatio= atof(*argv);
		}

		/* User is redefining the height ratio */
		else if (strcmp("height",c) == 0)
		{
			--argc; (*++argv);
			Stuff.HeightRatio= atof(*argv);
		}

		/* User is naming a file to put the output in */
		else if (strcmp("output", c) == 0)
		{
			--argc; (*++argv);
			strcpy(Stuff.OutFileName, *argv);
		}

		/* User has given an invalid option */
		else
		{
			fprintf(stderr,"usage:\n  bit2spr [-width width] [-height height] [-output outputfile] [bitmapfiles]\n");
			exit(-1);
		}
	}

	/* Test to see if the user gave an output file */
	if (strcmp("\0", Stuff.OutFileName) != 0)
	{
		/* User did, so try to open it for writing */
		if ( (spritefile = fopen(Stuff.OutFileName, "wt")) == NULL)
		{
			/* Open failed, tell user and quit */
			fprintf(stderr,"Cannot open output file %s\n",Stuff.OutFileName);
			exit(-1);
		}
		/* Open sucess */
		else fprintf(stderr,"Writing to %s\n",Stuff.OutFileName);
	}

	/* User didn't supply an output file, so send output to
	 * standard output (usually the screen)
         */
	else spritefile = stdout;
		

	/* Test to see if user supplied files to be read... */
	if (argc == 0)
		/* User didn't, so get files from stardard input */
		conv_bitmap(Stuff,stdin,spritefile,"Dummy\0");

	/* User did supply files to be read, so while there are still name */
	else while (--argc>=0)
	{
		/* ...try opening current file for reading */
		if ( (bitmapfile=fopen(*argv,"r")) == NULL)
			/* Error opening file for read, so tell user & continue */
			fprintf(stderr,"%s doesn't exists.\n",*argv);
		/* Success opening file, so convert it */
		else
		{
			fprintf(stderr,"Converting %s...\n", *argv);
			conv_bitmap(Stuff,bitmapfile,spritefile,*argv);
		}

		/* move to next name */
		(*++argv);
	}
}