File: bitmfile.c

package info (click to toggle)
glhack 1.2-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 24,604 kB
  • ctags: 18,992
  • sloc: ansic: 208,570; cpp: 13,139; yacc: 2,005; makefile: 1,161; lex: 377; sh: 321; awk: 89; sed: 11
file content (340 lines) | stat: -rw-r--r-- 8,118 bytes parent folder | download | duplicates (24)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/****************************\
* Bitmap mit Farbtabelle als *
* Graphik-Datei speichern		 *
* Autor: Gabriel Schmidt		 *
* (c) 1992 by MAXON-Computer *
* Modifiziert von Sebastian	 *
* Bieber, Dez. 1994					 *
* -> Programmcode						 *
\****************************/

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#include "bitmfile.h"

/* --- (X) IMG-Implementation ----------------- */

#define IMG_COMPRESSED

typedef struct
	{
	UWORD img_version;
	UWORD img_headlen;
	UWORD img_nplanes;
	UWORD img_patlen;
	UWORD img_pixw;
	UWORD img_pixh;
	UWORD img_w;
	UWORD img_h;
	} IMG_HEADER;

typedef enum {NONE, SOLID0, SOLID1, PATRUN, BITSTR} IMG_MODE;

typedef UBYTE IMG_SOLID;

typedef enum { RGB=0, CMY=1, Pantone=2 } XIMG_COLMODEL;

typedef struct
	{
	ULONG img_ximg;
	XIMG_COLMODEL img_colmodel;
	} XIMG_HEADER;

typedef struct RGB XIMG_RGB;


int bitmap_to_img(FILE_TYP typ,	int ww, int wh,
									unsigned int pixw, unsigned int pixh,
									unsigned int planes, unsigned int colors,
									const char *filename,
									void(*get_color)(unsigned int colind, struct RGB *rgb),
									void(*get_pixel)(int x, int y, unsigned int *colind) )
	{
	int file, error, cnt;
	IMG_HEADER header;
	XIMG_HEADER xheader;
	XIMG_RGB xrgb;
	IMG_MODE mode;
	UBYTE *line_buf, *write_buf;
	register UBYTE *startpnt, *bufpnt;
	unsigned int colind, line_len, line, bit;
	register unsigned int byte;
	register UBYTE count;

	/* fill in (X) IMG-Header */

	header.img_version = 1;
	header.img_headlen = (UWORD) sizeof(header) /2;
	if (typ == XIMG)
		header.img_headlen += (UWORD)(sizeof(xheader)+colors*sizeof(xrgb))/2;

	header.img_nplanes	= planes;
	header.img_patlen		= 2;
	header.img_pixw			= pixw;
	header.img_pixh			= pixh;
	header.img_w				= ww;
	header.img_h				= wh;

	xheader.img_ximg		= XIMG_MAGIC;
	xheader.img_colmodel= RGB;

	/* calculate linelength, allocate buffer. */

	line_len 	= (ww+7)/8;

	line_buf 	= malloc((size_t)planes*line_len);
	if (line_buf == NULL)
		return(ENOMEM);

	/* Worst case: the bufferd line could grow to max. 3 times the length */
	/* of the original!																	*/

	write_buf = malloc((size_t)3*line_len);
	if (write_buf == NULL)
		{
		free(line_buf);
		return(ENOMEM);
		};

	/* open file */

	file = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
	if (file<0)
		{
		error = errno;
		free(line_buf);
		free(write_buf);
		return(error);
		};

	/* write Header */

	if (write (file, &header, sizeof(header)) != sizeof(header) ||
	 	  (typ == XIMG &&	write (file, &xheader, sizeof(xheader) ) != sizeof(xheader)))
			{
			error = errno;
			close(file);
			free(line_buf);
			free(write_buf);
			return(error);
			};

	/* save the colortable if possible */

	if ( typ == XIMG )
	for (cnt=0; cnt<colors; cnt++)
		{
		get_color(cnt,&xrgb);
		if (write(file,&xrgb,sizeof(xrgb)) != sizeof(xrgb))
			{
			error = errno;
			close(file);
			free(line_buf);
			free(write_buf);
			return(error);
			};
		};

	/* And now line by line ... */

	for (line=0; line<wh; line++)
		{
		/* get pixel, split it up and */
		/* store it as planes in buffer */

		for (byte=0; byte<line_len; byte++)
			{
			for (cnt=0; cnt<planes; cnt++)
				line_buf[cnt*line_len+byte] = 0x00;

			for (bit=0; bit<8; bit++)
				{
				if (8*byte+bit < ww)
					get_pixel(8*byte+bit, line, &colind);

				for (cnt=0; cnt<planes; cnt++)
					{
					line_buf[cnt*line_len+byte] <<= 1;
					line_buf[cnt*line_len+byte]  |= colind & 0x01;
					colind >>= 1;
					};
				};
			};

		/* compress bitstrings in buffer */
		/* and write it to file        */

		for (cnt=0; cnt<planes; cnt++)
			{
			/* Bitstringpointer to start of plane */

			startpnt = &line_buf[cnt*line_len];
			bufpnt = write_buf;

			while (startpnt < &line_buf[(cnt+1)*line_len])
				{
				/*********************************************/
				/* Which _new_ compression-mode "fits" the	*/
				/* the current byte?									*/
				/* Note: the compressing modes get choosen	*/
				/* "positive". The non compressing BITSTR-	*/
				/* mode is choosen only if nothing else 		*/
				/* "fits" ...											*/
				/*********************************************/

				switch (*startpnt)
					{
					case 0x00:
						mode = SOLID0;
						break;
					case 0xFF:
						mode = SOLID1;
						break;
					default:
						if ( startpnt	<  &line_buf[(cnt+1)*line_len-3] &&
								*(startpnt)   == *(startpnt+2)						 &&
								*(startpnt+1) == *(startpnt+3)						 )
							mode = PATRUN;
						else
							mode = BITSTR;
					};

				/************************************************/
				/* The mode is choosen, now work with it.			*/
				/* The compressing modi stay current as long as	*/
				/* possible.												*/
				/************************************************/

				count = 0;

				switch (mode)
					{
					case SOLID0:
						while (	(startpnt < &line_buf[(cnt+1)*line_len])	&&
									 	(*(startpnt)==0x00)											&&
									 	(count < 0x7F) 				 										)
									{
									startpnt++;
									count++;
									};
						*(bufpnt++) = count;
						break;

					case SOLID1:
						while (	(startpnt < &line_buf[(cnt+1)*line_len])	&&
									 	(*(startpnt)==0xFF)											&&
									 	(count < 0x7F) 				 										)
									{
									startpnt++;
									count++;
									};
						*(bufpnt++) = 0x80 | count;
						break;

					case PATRUN:
						*(bufpnt++) = 0x00;
						startpnt += 2;
						count = 1;
						while (	startpnt < &line_buf[(cnt+1)*line_len-1]	&&
										*(startpnt)		== *(startpnt-2)						&&
										*(startpnt+1)	== *(startpnt-1)						&&
										(count < 0xFF)														)
									{
									count++;
									startpnt += 2;
									};
						*(bufpnt++) = count;
						*(bufpnt++) = *(startpnt-2);
						*(bufpnt++) = *(startpnt-1);
						break;

					/************************************************/
					/* The while Condition is ment as follows:		*/
					/*																							*/
					/* while ( NOT(2-Byte-Solidrun possible)	&&		*/
					/*			 NOT(6-Byte-Patternrun possible)	&&		*/
					/*				 count < 0xFF										&&		*/
					/*			 still Bytes remaining			)			*/
					/*																							*/
					/* As soon as a _compressing_ alternative	shows	*/
					/* up, BITSTR gets cancelled!							*/
					/************************************************/

					case BITSTR:
						*(bufpnt++) = 0x80;
						while ( !(((startpnt+count)<&line_buf[(cnt+1)*line_len-1])&&
											(((*(startpnt+count)==0xFF) && (*(startpnt+count+1)==0xFF))||
											 ((*(startpnt+count)==0x00) && (*(startpnt+count+1)==0x00))))		&&
										!(((startpnt+count)<&line_buf[(cnt+1)*line_len-5])&&
											(*(startpnt+count)   == *(startpnt+count+2))&&
											(*(startpnt+count+1) == *(startpnt+count+3))&&
											(*(startpnt+count)   == *(startpnt+count+4))&&
											(*(startpnt+count+1) == *(startpnt+count+5)))												&&
										(count < 0xFF)																												&&
										((startpnt+count) < &line_buf[(cnt+1)*line_len]) 											)
										count++;
						*(bufpnt++) = count;
						for(; count>0; count--)
							*(bufpnt++) = *(startpnt++);
						break;
					};
				};

			if (write(file,write_buf,bufpnt-write_buf) != (bufpnt-write_buf))
				{
				error = errno;
				close(file);
				free(line_buf);
				free(write_buf);
				return(error);
				};

			};
		};

	/*close file, free buffer. */

	close(file);
	free(line_buf);
	free(write_buf);
	return(0);

}

/*---filetype-dispatcher--------------------*/

const char *get_file_ext(FILE_TYP typ)
	{
	switch (typ)
		{
		case IMG:
		case XIMG:
			return("IMG");
		default:
			return("");
		};
}


int bitmap_to_file(FILE_TYP typ, int ww, int wh,
									 unsigned int pwx, unsigned int pwy,
									 unsigned int planes, unsigned int colors,
									 const char *filename,
									 void (*get_color)(unsigned int colind, struct RGB *rgb),
									 void (*get_pixel)(int x, int y, unsigned int *colind))
	{

	switch (typ)
		{
		case IMG:
		case XIMG:
			return(bitmap_to_img(typ,ww,wh,pwx,pwy,planes,colors,filename,get_color,get_pixel));
		default:
			return(-1);
		};
}