File: shmbitmap.c

package info (click to toggle)
koules 1.4-18
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,288 kB
  • ctags: 2,542
  • sloc: ansic: 15,508; makefile: 837; asm: 379; tcl: 362; sh: 168
file content (421 lines) | stat: -rw-r--r-- 9,874 bytes parent folder | download | duplicates (11)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/***********************************************************
*                      K O U L E S                         *
*----------------------------------------------------------*
*  C1995 JAHUSOFT                                          *
*        Jan Hubicka                                       *
*        Dukelskych Bojovniku 1944                         *
*        390 03 Tabor                                      *
*        Czech Republic                                    *
*        Phone: 0041-361-32613                             *
*        eMail: hubicka@limax.paru.cas.cz                  *
*----------------------------------------------------------*
*   Copyright(c)1995,1996 by Jan Hubicka.See README for    *
*                     licence details.                     *
*----------------------------------------------------------*
*  shmbitmap.c framebuffer graphics routines               *
***********************************************************/
#ifdef MITSHM
/*adapted from: */
/* Framebuffer Graphics Libary for Linux, Copyright 1993 Harm Hanemaayer */
/* cbitmap.c    Compiled bitmaps */
/*thanks */


#include <stdlib.h>
#include "../koules.h"
#define BYTEWIDTH MAPWIDTH
#define __clipx1 0
#define __clipx2 (MAPWIDTH-2)
#define __clipy1 0
#define __clipy2 (MAPHEIGHT+17)
#define VBUF current.vbuff



typedef unsigned char uchar;

void
ShmCompileBitmap (int w, int h, void *_dp1, void *_dp2)
{
/* Compiled format: <bytes_to_skip (0-254)><number_of_pixels (0-255)> */
/*                  <pixel_data>[<end_of_line(255)>]... */
  uchar          *dp1 = _dp1;
  uchar          *dp2 = _dp2;
  int             i;
  for (i = 0; i < h; i++)
    {
      int             x = 0;
      while (x < w)
	{
	  int             count;
	  /* count zeroes */
	  count = 0;
	  while (x < w && *(dp1 + count) == notusedc && count < 254)
	    {
	      count++;
	      x++;
	    }
	  dp1 += count;
	  if (x < w)
	    {
	      *dp2++ = count;
	      /* count nonzeroes */
	      count = 0;
	      while (x < w && *(dp1 + count) != notusedc && count < 255)
		{
		  *(dp2 + count + 1) = *(dp1 + count);
		  count++;
		  x++;
		}
	      *dp2 = count;
	      dp2 += count + 1;
	      dp1 += count;
	    }
	}
      *dp2++ = 0xff;
    }
}

int
ShmBitmapSize (int w, int h, void *_dp1)
{
/* Compiled format: <bytes_to_skip (0-254)><number_of_pixels (0-255)> */
/*                  <pixel_data>[<end_of_line(255)>]... */
  uchar          *dp1 = _dp1;
  int             size = 0;
  int             i;
  for (i = 0; i < h; i++)
    {
      int             x = 0;
      while (x < w)
	{
	  int             count;
	  /* count zeroes */
	  count = 0;
	  while (x < w && *(dp1 + count) == notusedc && count < 254)
	    {
	      count++;
	      x++;
	    }
	  size++;
	  dp1 += count;
	  /* count nonzeroes */
	  if (x < w)
	    {
	      count = 0;
	      while (x < w && *(dp1 + count) != notusedc && count < 255)
		{
		  count++;
		  x++;
		}
	      size += count + 1;
	      dp1 += count;
	    }
	}
      size++;
    }
  return size;
}
static void
putmaskcompiledclip (CONST int nx, CONST int ny, CONST int nw, CONST int nh, CONST int _x,
		     CONST int _y, CONST int w, CONST int h, void *_dp)
{
/* Special case costly clipping */
  uchar          *dp = _dp;
  uchar          *vp, *vpline;
  int             y;
  vpline = (uchar *) VBUF + _y * BYTEWIDTH + _x;
  for (y = _y; y < ny + nh; y++)
    {
      int             x = _x;
      vp = vpline;
      for (;;)
	{
	  int             count = *dp++;
	  if (count == 0xff)
	    break;		/* end of line */
	  vp += count;
	  x += count;
	  count = *dp++;
	  /* __memcpy gives severe bug here */
	  if (y >= ny)
	    if (x >= nx)
	      if (x + count > __clipx2 + 1)
		{
		  if (x <= __clipx2)
		    __memcpyb (vp, dp, __clipx2 - x + 1);
		}
	      else
		__memcpyb (vp, dp, count);
	    else if (x + count > __clipx1)
	      if (x + count > __clipx2 + 1)
		__memcpyb (vp + __clipx1 - x,
			   dp + __clipx1 - x,
			   __clipx2 - __clipx1 + 1);
	      else
		__memcpy (vp + __clipx1 - x,
			  dp + __clipx1 - x,
			  count - __clipx1 + x);
	  x += count;
	  vp += count;
	  dp += count;
	}
      vpline += BYTEWIDTH;
    }
}

#define ADJUSTBITMAPBOX() \
	nw = w; nh = h; nx = x; ny = y;				\
	if (nx + nw < __clipx1 || nx > __clipx2)		\
		return;						\
	if (ny + nh < __clipy1 || ny > __clipy2)		\
		return;						\
	if (nx < __clipx1) {		/* left adjust */	\
		nw += nx - __clipx1;				\
		nx = __clipx1;					\
	}							\
	if (ny < __clipy1) {		/* top adjust */	\
		nh += ny - __clipy1;				\
		ny = __clipy1;					\
	}							\
	if (nx + nw > __clipx2)		/* right adjust */	\
		nw = __clipx2 - nx + 1;				\
	if (ny + nh > __clipy2)		/* bottom adjust */	\
		nh = __clipy2 - ny + 1;				\


void
ShmPutBitmap (int x, int y, int w, int h, void *_dp)
{
/* no clipping */
  uchar          *dp = _dp;
  uchar          *vp, *vpline;
  int             i;
  if (Clipping)
    {
      int             nx, ny, nw, nh;
      ADJUSTBITMAPBOX ();
      if (nw != w || nh != h)
	{
	  putmaskcompiledclip (nx, ny, nw, nh, x, y, w, h,
			       dp);
	  return;
	}
    }
  vpline = (uchar *) VBUF + y * BYTEWIDTH + x;
  for (i = 0; i < h; i++)
    {
      vp = vpline;
      for (;;)
	{
	  int             count = *dp++;
	  if (count == 0xff)
	    break;		/* end of line */
	  vp += count;
	  count = *dp++;
	  /* __memcpy gives severe bug here */
	  __memcpyb (vp, dp, count);
	  vp += count;
	  dp += count;
	}
      vpline += BYTEWIDTH;
    }
}


/*following routines are ripped from vgagl library */
/* We use the 32-bit to 64-bit multiply and 64-bit to 32-bit divide of the */
/* 386 (which gcc doesn't know well enough) to efficiently perform integer */
/* scaling without having to worry about overflows. */
#define swap(x, y) { int temp = x; x = y; y = temp; }
#define setpixel (*(backscreen->ff.driver_setpixel_func))
#undef __clipx2
#define __clipx2 (MAPWIDTH-1)
#undef __clipx1
#define __clipx1 0
#undef __clipy1
#define __clipy1 0
#undef __clipy2
#define __clipy2 (MAPHEIGHT+19)
#ifdef __i386__
static INLINE int
muldiv64 (int CONST m1, int CONST m2, int CONST d)
{
/* int32 * int32 -> int64 / int32 -> int32 */
  int             result;
  __asm__ (
	    "imull %%edx\n\t"
	    "idivl %3\n\t"
:	    "=a" (result)	/* out */
:	    "a" (m1), "d" (m2), "g" (d)		/* in */
:	    "ax", "dx"		/* mod */
    );
  return result;
}

#define INC_IF_NEG(y)                                  \
{                                                       \
        __asm__("btl $31,%1\n\t"                        \
                "adcl $0,%0"                            \
                : "=r" ((int) result)                   \
                : "rm" ((int) (y)), "0" ((int) result)  \
                );                                      \
}
static INLINE int
gl_regioncode (CONST int x, CONST int y)
{
  int             dx1, dx2, dy1, dy2;
  int             result;
  result = 0;
  dy2 = __clipy2 - y;
  INC_IF_NEG (dy2);
  result <<= 1;
  dy1 = y - __clipy1;
  INC_IF_NEG (dy1);
  result <<= 1;
  dx2 = __clipx2 - x;
  INC_IF_NEG (dx2);
  result <<= 1;
  dx1 = x - __clipx1;
  INC_IF_NEG (dx1);
  return result;
}


#else

static INLINE int
gl_regioncode (CONST int x, CONST int y)
{
  int             result = 0;
  if (x < 0)
    result |= 1;
  else if (x > __clipx2)
    result |= 2;
  if (y < 0)
    result |= 4;
  else if (y > __clipy2)
    result |= 8;
  return result;
}
#endif

/* Partly based on vgalib by Tommy Frandsen */
/* This would be a lot faster if setpixel was inlined */

void
Line (int x1, int y1, int x2, int y2, int c)
{
  int             dx, dy, ax, ay, sx, sy, x, y;
  int             syp;
  char           *point;
  if (!shm)
    {
      qLine (x1, y1, x2, y2, c);
      return;
    }
  c = cpixels (c);
  /* Cohen & Sutherland algorithm */
  if (Clipping)
    for (;;)
      {
	int             r1 = gl_regioncode (x1, y1);
	int             r2 = gl_regioncode (x2, y2);
	if (!(r1 | r2))
	  break;		/* completely inside */
	if (r1 & r2)
	  return;		/* completely outside */
	if (r1 == 0)
	  {
	    swap (x1, x2);	/* make sure first */
	    swap (y1, y2);	/* point is outside */
	    r1 = r2;
	  }
	if (r1 & 1)
	  {			/* left */
#ifdef __i386__
	    y1 += muldiv64 (__clipx1 - x1, y2 - y1, x2 - x1);
#else
	    y1 += (long) (__clipx1 - x1) * (long) (y2 - y1) / (long) (x2 - x1);
#endif
	    x1 = __clipx1;
	  }
	else if (r1 & 2)
	  {			/* right */
#ifdef __i386__
	    y1 += muldiv64 (__clipx2 - x1, y2 - y1, x2 - x1);
#else
	    y1 += (long) (__clipx2 - x1) * (long) (y2 - y1) / (long) (x2 - x1);
#endif
	    x1 = __clipx2;
	  }
	else if (r1 & 4)
	  {			/* top */
#ifdef __i386__
	    x1 += muldiv64 (__clipy1 - y1, x2 - x1, y2 - y1);
#else
	    x1 += (long) (__clipy1 - y1) * (long) (x2 - x1) / (long) (y2 - y1);
#endif
	    y1 = __clipy1;
	  }
	else if (r1 & 8)
	  {			/* bottom */
#ifdef __i386__
	    x1 += muldiv64 (__clipy2 - y1, x2 - x1, y2 - y1);
#else
	    x1 += (long) (__clipy2 - y1) * (long) (x2 - x1) / (long) (y2 - y1);
#endif
	    y1 = __clipy2;
	  }
      }
  dx = x2 - x1;
  dy = y2 - y1;
  ax = abs (dx) << 1;
  ay = abs (dy) << 1;
  sx = (dx >= 0) ? 1 : -1;
  sy = (dy >= 0) ? 1 : -1;
  x = x1;
  y = y1;

  point = VScreenToBuffer (backscreen) + x + y * MAPWIDTH;
  if (ax > ay)
    {
      int             d = ay - (ax >> 1);
      syp = sy * MAPWIDTH;
      while (x != x2)
	{
	  *point = c;
	  if (d > 0 || (d == 0 && sx == 1))
	    {
	      y += sy;
	      point += syp;
	      d -= ax;
	    }
	  x += sx;
	  point += sx;
	  d += ay;
	}
    }
  else
    {
      int             sy = (dy >= 0) ? 1 : -1;
      int             d = ax - (ay >> 1);
      syp = sy * MAPWIDTH;
      while (y != y2)
	{
	  *(point) = c;
	  if (d > 0 || (d == 0 && sy == 1))
	    {
	      x += sx;
	      point += sx;
	      d -= ay;
	    }
	  y += sy;
	  point += syp;
	  d += ax;
	}
    }
  *(point) = c;
  point++;
}
#endif