File: util.c

package info (click to toggle)
libgiigic 1.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,000 kB
  • ctags: 854
  • sloc: sh: 9,160; ansic: 7,438; makefile: 275
file content (249 lines) | stat: -rw-r--r-- 5,420 bytes parent folder | download | duplicates (3)
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
/* $Id: util.c,v 1.5 2005/07/30 16:57:37 cegger Exp $
******************************************************************************

   Utility functions for the Snazzy Config Manager.
  
   Copyright (c) 1999 Andrew Apted  [andrew@ggi-project.org]
  
   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  
******************************************************************************
*/

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

#include "snazzy.h"


unsigned char *decode_image(unsigned char *img, int width, int height)
{
	unsigned char *buf, *cur;

	int x, y;
	int pix=0, count=0;
	
	buf = malloc((size_t)(width * height));

	if (buf == NULL) {
		return NULL;
	}

	cur = buf;

	for (y=0; y < height; y++)
	for (x=0; x < width;  x++) {

		if (count > 0) {
			*cur++ = pix;
			count--;
			continue;
		}
		
		if (*img > 96) {
			count = (*img++) - 97;
		}
		
		if (! *img) {
			/* Odd, an error in the image */
			free(buf);
			return NULL;
		}

		*cur++ = pix = (*img++) - 64;
	}

	return buf;
}


/* ---------------------------------------------------------------------- */


static uint8_t line_buffer[8192];

void draw_image(ggi_visual_t vis, int x, int y, int w, int h,
		unsigned char *img, int ix, int iy, int iw, int ih,
		ggi_pixel *colors)
{
	int ww;

	ggi_mode mode;

	ix %= iw;
	iy %= ih;

	ggiGetMode(vis, &mode);

	for (; h > 0; h--, y++, iy = (iy+1) % ih) {

		uint8_t  *buf1 = (uint8_t  *) line_buffer;
		uint16_t *buf2 = (uint16_t *) line_buffer;
		uint32_t *buf4 = (uint32_t *) line_buffer;

		switch ((GT_SIZE(mode.graphtype) + 7) / 8) {

		case 1: for (ww=0; ww < w; ww++) {
				*buf1++ = colors[img[iy*iw + (ix+ww)%iw]];
			}
			break;

		case 2: for (ww=0; ww < w; ww++) {
				*buf2++ = colors[img[iy*iw + (ix+ww)%iw]];
			}
			break;

		case 3: for (ww=0; ww < w; ww++) {
				ggi_pixel q = colors[img[iy*iw + (ix+ww)%iw]];
				*buf1++ = q; q >>= 8;
				*buf1++ = q; q >>= 8;
				*buf1++ = q;
			}
			break;

		case 4: for (ww=0; ww < w; ww++) {
				*buf4++ = colors[img[iy*iw + (ix+ww)%iw]];
			}
			break;
		}

		ggiPutHLine(vis, x, y, w, line_buffer);
	}
#if 0
	for (hh=0; hh < h; hh++) {
	for (ww=0; ww < w; ww++) {

		/* No points here for speed :-> */
		
		ggiPutPixel(vis, x+ww, y+hh, 
			colors[img[((iy+hh) % ih) * iw + (ix+ww) % iw]]);
	}
#endif
}

void draw_char (ggi_visual_t vis, int x, int y, int c, 
		unsigned char *font, int fw, int fh, ggi_pixel *colors)
{
	int ww, hh;
	int fx, fy;

	if (c < 0x20) {
		return;
	} else if (c < 0x80) {
		c -= 0x20;
	} else if (c < 0xA0) {	
		return;
	} else if (c < 0x100) {
		c -= 0x40;
	} else {
		return;
	}
	
	fx = (c % 16) * fw;
	fy = (c / 16) * fh;
	
	for (hh=0; hh < fh; hh++)
	for (ww=0; ww < fw; ww++) {

		/* No points here for speed :-> */
		
		int pix = font[(fy+hh) * 16 * fw + fx+ww];

		if (pix) {
			ggiPutPixel(vis, x+ww, y+hh, colors[pix-1]);
		}
	}
}

void draw_string(ggi_visual_t vis, int x, int y, const unsigned char *s, 
		 unsigned char *font, int fw, int fh, int step,
		 ggi_pixel *colors)
{
	for (; *s; s++, x += (fw + step)) {
	
		draw_char(vis, x, y, *s, font, fw, fh, colors);
	}
}


/* ---------------------------------------------------------------------- */


static
void draw_snazzy_char (ggi_visual_t vis, int x, int y, 
			int x_scale, int y_scale, int c, 
			unsigned char *font, int fw, int fh,
			unsigned char *img, int iw, int ih, 
			ggi_pixel *colors)
{
	int ww, hh;
	int fx, fy;
	int dx, dy;

	if (c < 0x20) {
		return;
	} else if (c < 0x80) {
		c -= 0x20;
	} else if (c < 0xA0) {	
		return;
	} else if (c < 0x100) {
		c -= 0x40;
	} else {
		return;
	}
	
	fx = (c % 16) * fw;
	fy = (c / 16) * fh;
	
	for (hh=0; hh < fh; hh++)
	for (ww=0; ww < fw; ww++) {

		/* This ain't no speed demon either :-> */
		
		int pix = font[(fy+hh) * 16 * fw + fx+ww];

		if (! pix) {
			continue;
		}

		for (dx=0; dx < x_scale; dx++)
		for (dy=0; dy < y_scale; dy++) {

			int sx = x + (fh-1-hh)*x_scale + dx;
			int sy = y + (     ww)*y_scale + dy;

			ggi_pixel q = colors[(pix == 1) ? 0 : 1 +
					img[(sy%ih)*iw + (sx%iw)]];

			ggiPutPixel(vis, sx, sy, q);
		}
	}
}

void draw_snazzy_string(ggi_visual_t vis, int x, int y,
			int x_scale, int y_scale,
			const unsigned char *s, unsigned char *font,
			int fw, int fh, unsigned char *img,
			int iw, int ih, ggi_pixel *colors)
{
	for (; *s; s++, y += fw * y_scale) {
	
		draw_snazzy_char(vis, x, y, x_scale, y_scale, *s, 
				 font, fw, fh, img, iw, ih, colors);
	}
}