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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
|
/* -*- c-basic-offset: 8 -*-
rdesktop: A Remote Desktop Protocol client.
Generics backingstore operations
Copyright (C) Jay Sorg 2005-2006
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdlib.h>
#include <string.h>
#include "bsops.h"
/* externals */
extern int g_width;
extern int g_height;
extern int g_bs_bpp;
extern int g_bs_Bpp;
extern char * g_bs;
/* globals */
static int g_clip_left = 0;
static int g_clip_top = 0;
static int g_clip_right = 800;
static int g_clip_bottom = 600;
/* for bs_patblt */
static unsigned char g_hatch_patterns[] =
{
0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, /* 0 - bsHorizontal */
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, /* 1 - bsVertical */
0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, /* 2 - bsFDiagonal */
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, /* 3 - bsBDiagonal */
0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08, /* 4 - bsCross */
0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 /* 5 - bsDiagCross */
};
/*****************************************************************************/
/* do a raster op */
int
bs_do_rop(int rop, int src, int dst)
{
switch (rop)
{
case 0x0: return 0;
case 0x1: return ~(src | dst);
case 0x2: return (~src) & dst;
case 0x3: return ~src;
case 0x4: return src & (~dst);
case 0x5: return ~(dst);
case 0x6: return src ^ dst;
case 0x7: return ~(src & dst);
case 0x8: return src & dst;
case 0x9: return ~(src) ^ dst;
case 0xa: return dst;
case 0xb: return (~src) | dst;
case 0xc: return src;
case 0xd: return src | (~dst);
case 0xe: return src | dst;
case 0xf: return ~0;
}
return dst;
}
/*****************************************************************************/
/* get a pixel from the in memory copy of whats on the screen */
int
bs_get_pixel(int x, int y)
{
char * p;
if (x >= 0 && x < g_width && y >= 0 && y < g_height)
{
p = g_bs + (y * g_width * g_bs_Bpp) + (x * g_bs_Bpp);
if (g_bs_Bpp == 1)
{
return *((unsigned char *) p);
}
else if (g_bs_Bpp == 2)
{
return *((unsigned short *) p);
}
else
{
return *((unsigned int *) p);
}
}
else
{
return 0;
}
}
/*****************************************************************************/
/* set a pixel on the screen using the clip */
void
bs_set_pixel(int x, int y, int pixel, int rop, int use_clip)
{
char * p;
if (!use_clip ||
(x >= g_clip_left && x < g_clip_right &&
y >= g_clip_top && y < g_clip_bottom))
{
if (x >= 0 && x < g_width && y >= 0 && y < g_height)
{
p = g_bs + (y * g_width * g_bs_Bpp) + (x * g_bs_Bpp);
if (rop != 12)
{
pixel = bs_do_rop(rop, pixel, bs_get_pixel(x, y));
}
if (g_bs_Bpp == 1)
{
*((unsigned char *) p) = pixel;
}
else if (g_bs_Bpp == 2)
{
*((unsigned short *) p) = pixel;
}
else
{
*((unsigned int *) p) = pixel;
}
}
}
}
/*****************************************************************************/
static char *
get_bs_ptr(int x, int y)
{
char * p;
if (x >= 0 && x < g_width && y >= 0 && y < g_height)
{
p = g_bs + (y * g_width * g_bs_Bpp) + (x * g_bs_Bpp);
return p;
}
else
{
return 0;
}
}
/*****************************************************************************/
void
bs_init(void)
{
g_clip_left = 0;
g_clip_top = 0;
g_clip_right = g_width;
g_clip_bottom = g_height;
}
/*****************************************************************************/
void
bs_exit(void)
{
}
/*****************************************************************************/
void
bs_set_clip(int x, int y, int cx, int cy)
{
g_clip_left = x;
g_clip_top = y;
g_clip_right = x + cx;
g_clip_bottom = y + cy;
}
/*****************************************************************************/
void
bs_reset_clip(void)
{
g_clip_left = 0;
g_clip_top = 0;
g_clip_right = g_width;
g_clip_bottom = g_height;
}
/*****************************************************************************/
/* check if a certain pixel is set in a bitmap */
int
bs_is_pixel_on(char * data, int x, int y, int width, int bpp)
{
int start;
int shift;
if (bpp == 1)
{
width = (width + 7) / 8;
start = (y * width) + x / 8;
shift = x % 8;
return (data[start] & (0x80 >> shift)) != 0;
}
else if (bpp == 8)
{
return data[y * width + x] != 0;
}
else if (bpp == 24)
{
return data[(y * 3) * width + (x * 3)] != 0 &&
data[(y * 3) * width + (x * 3) + 1] != 0 &&
data[(y * 3) * width + (x * 3) + 2] != 0;
}
else
{
return 0;
}
}
/*****************************************************************************/
void
bs_set_pixel_on(char * data, int x, int y, int width, int bpp,
int pixel)
{
int start;
int shift;
if (bpp == 1)
{
width = (width + 7) / 8;
start = (y * width) + x / 8;
shift = x % 8;
if (pixel != 0)
{
data[start] = data[start] | (0x80 >> shift);
}
else
{
data[start] = data[start] & ~(0x80 >> shift);
}
}
else if (bpp == 8)
{
data[y * width + x] = pixel;
}
else if (bpp == 15 || bpp == 16)
{
((unsigned short *) data)[y * width + x] = pixel;
}
}
/*****************************************************************************/
void
bs_copy_mem(char * d, char * s, int n)
{
while (n & (~7))
{
*(d++) = *(s++);
*(d++) = *(s++);
*(d++) = *(s++);
*(d++) = *(s++);
*(d++) = *(s++);
*(d++) = *(s++);
*(d++) = *(s++);
*(d++) = *(s++);
n = n - 8;
}
while (n > 0)
{
*(d++) = *(s++);
n--;
}
}
/*****************************************************************************/
void
bs_copy_memb(char * d, char * s, int n)
{
d = (d + n) - 1;
s = (s + n) - 1;
while (n & (~7))
{
*(d--) = *(s--);
*(d--) = *(s--);
*(d--) = *(s--);
*(d--) = *(s--);
*(d--) = *(s--);
*(d--) = *(s--);
*(d--) = *(s--);
*(d--) = *(s--);
n = n - 8;
}
while (n > 0)
{
*(d--) = *(s--);
n--;
}
}
/*****************************************************************************/
/* return true is the is something to draw */
int
bs_warp_coords(int * x, int * y, int * cx, int * cy,
int * srcx, int * srcy)
{
int dx;
int dy;
if (g_clip_left > *x)
{
dx = g_clip_left - *x;
}
else
{
dx = 0;
}
if (g_clip_top > *y)
{
dy = g_clip_top - *y;
}
else
{
dy = 0;
}
if (*x + *cx > g_clip_right)
{
*cx = (*cx - ((*x + *cx) - g_clip_right));
}
if (*y + *cy > g_clip_bottom)
{
*cy = (*cy - ((*y + *cy) - g_clip_bottom));
}
*cx = *cx - dx;
*cy = *cy - dy;
if (*cx <= 0)
{
return 0;
}
if (*cy <= 0)
{
return 0;
}
*x = *x + dx;
*y = *y + dy;
if (srcx != 0)
{
*srcx = *srcx + dx;
}
if (srcy != 0)
{
*srcy = *srcy + dy;
}
return 1;
}
/*****************************************************************************/
void
bs_rect(int x, int y, int cx, int cy, int colour, int rop)
{
int i;
int j;
unsigned char * p8;
unsigned short * p16;
unsigned int * p32;
if (bs_warp_coords(&x, &y, &cx, &cy, 0, 0))
{
if (rop == 0) /* black */
{
rop = 12;
colour = 0;
}
else if (rop == 15) /* white */
{
rop = 12;
colour = 0xffffff;
}
if (rop == 12) /* copy */
{
if (g_bs_Bpp == 1)
{
for (i = 0; i < cy; i++)
{
p8 = (unsigned char *) get_bs_ptr(x, y + i);
if (p8 != 0)
{
for (j = 0; j < cx; j++)
{
*p8 = colour;
p8++;
}
}
}
}
else if (g_bs_Bpp == 2)
{
for (i = 0; i < cy; i++)
{
p16 = (unsigned short *) get_bs_ptr(x, y + i);
if (p16 != 0)
{
for (j = 0; j < cx; j++)
{
*p16 = colour;
p16++;
}
}
}
}
else
{
for (i = 0; i < cy; i++)
{
p32 = (unsigned int *) get_bs_ptr(x, y + i);
if (p32 != 0)
{
for (j = 0; j < cx; j++)
{
*p32 = colour;
p32++;
}
}
}
}
}
else /* slow */
{
for (i = 0; i < cy; i++)
{
for (j = 0; j < cx; j++)
{
bs_set_pixel(j + x, i + y, colour, rop, 0);
}
}
}
}
}
/*****************************************************************************/
void
bs_screenblt(int rop, int x, int y, int cx, int cy,
int srcx, int srcy)
{
int p;
int i;
int j;
char * src;
char * dst;
if (bs_warp_coords(&x, &y, &cx, &cy, &srcx, &srcy))
{
if (rop == 12) /* copy */
{
if (srcy < y) /* copy down - bottom to top */
{
for (i = cy - 1; i >= 0; i--)
{
src = get_bs_ptr(srcx, srcy + i);
dst = get_bs_ptr(x, y + i);
if (src != 0 && dst != 0)
{
bs_copy_mem(dst, src, cx * g_bs_Bpp);
}
}
}
else if (srcy > y || srcx > x) /* copy up or left - top to bottom */
{
for (i = 0; i < cy; i++)
{
src = get_bs_ptr(srcx, srcy + i);
dst = get_bs_ptr(x, y + i);
if (src != 0 && dst != 0)
{
bs_copy_mem(dst, src, cx * g_bs_Bpp);
}
}
}
else /* copy straight right */
{
for (i = 0; i < cy; i++)
{
src = get_bs_ptr(srcx, srcy + i);
dst = get_bs_ptr(x, y + i);
if (src != 0 && dst != 0)
{
bs_copy_memb(dst, src, cx * g_bs_Bpp);
}
}
}
}
else /* slow */
{
if (srcy < y) /* copy down - bottom to top */
{
for (i = cy - 1; i >= 0; i--)
{
for (j = 0; j < cx; j++)
{
p = bs_get_pixel(srcx + j, srcy + i);
bs_set_pixel(x + j, y + i, p, rop, 0);
}
}
}
else if (srcy > y || srcx > x) /* copy up or left - top to bottom */
{
for (i = 0; i < cy; i++)
{
for (j = 0; j < cx; j++)
{
p = bs_get_pixel(srcx + j, srcy + i);
bs_set_pixel(x + j, y + i, p, rop, 0);
}
}
}
else /* copy straight right */
{
for (i = 0; i < cy; i++)
{
for (j = cx - 1; j >= 0; j--)
{
p = bs_get_pixel(srcx + j, srcy + i);
bs_set_pixel(x + j, y + i, p, rop, 0);
}
}
}
}
}
}
/*****************************************************************************/
void
bs_memblt(int opcode, int x, int y, int cx, int cy,
void * srcdata, int srcwidth, int srcheight,
int srcx, int srcy)
{
int i;
int j;
int p;
char * dst;
char * src;
if (bs_warp_coords(&x, &y, &cx, &cy, &srcx, &srcy))
{
if (opcode == 12) /* copy */
{
if (g_bs_Bpp == 1)
{
src = (char *) (((unsigned char *) srcdata) + srcy * srcwidth + srcx);
}
else if (g_bs_Bpp == 2)
{
src = (char *) (((unsigned short *) srcdata) + srcy * srcwidth + srcx);
}
else
{
src = (char *) (((unsigned int *) srcdata) + srcy * srcwidth + srcx);
}
for (i = 0; i < cy; i++)
{
dst = get_bs_ptr(x, y + i);
if (dst != 0)
{
bs_copy_mem(dst, src, cx * g_bs_Bpp);
src += srcwidth * g_bs_Bpp;
}
}
}
else /* slow */
{
if (g_bs_Bpp == 1)
{
for (i = 0; i < cy; i++)
{
for (j = 0; j < cx; j++)
{
p = *(((unsigned char *) srcdata) +
((i + srcy) * srcwidth + (j + srcx)));
bs_set_pixel(x + j, y + i, p, opcode, 0);
}
}
}
else if (g_bs_Bpp == 2)
{
for (i = 0; i < cy; i++)
{
for (j = 0; j < cx; j++)
{
p = *(((unsigned short *) srcdata) +
((i + srcy) * srcwidth + (j + srcx)));
bs_set_pixel(x + j, y + i, p, opcode, 0);
}
}
}
else
{
for (i = 0; i < cy; i++)
{
for (j = 0; j < cx; j++)
{
p = *(((unsigned int *) srcdata) +
((i + srcy) * srcwidth + (j + srcx)));
bs_set_pixel(x + j, y + i, p, opcode, 0);
}
}
}
}
}
}
/*****************************************************************************/
void
bs_draw_glyph(int x, int y, char * glyph_data, int glyph_width,
int glyph_height, int fgcolour)
{
int i;
int j;
for (i = 0; i < glyph_height; i++)
{
for (j = 0; j < glyph_width; j++)
{
if (bs_is_pixel_on(glyph_data, j, i, glyph_width, 8))
{
bs_set_pixel(x + j, y + i, fgcolour, 12, 1);
}
}
}
}
/*****************************************************************************/
/* Bresenham's line drawing algorithm */
void
bs_line(int opcode, int startx, int starty, int endx, int endy,
int pen_width, int pen_style, int pen_colour)
{
int dx;
int dy;
int incx;
int incy;
int dpr;
int dpru;
int p;
if (startx > endx)
{
dx = startx - endx;
incx = -1;
}
else
{
dx = endx - startx;
incx = 1;
}
if (starty > endy)
{
dy = starty - endy;
incy = -1;
}
else
{
dy = endy - starty;
incy = 1;
}
if (dx >= dy)
{
dpr = dy << 1;
dpru = dpr - (dx << 1);
p = dpr - dx;
for (; dx >= 0; dx--)
{
if (startx != endx || starty != endy)
{
bs_set_pixel(startx, starty, pen_colour, opcode, 1);
}
if (p > 0)
{
startx += incx;
starty += incy;
p += dpru;
}
else
{
startx += incx;
p += dpr;
}
}
}
else
{
dpr = dx << 1;
dpru = dpr - (dy << 1);
p = dpr - dy;
for (; dy >= 0; dy--)
{
if (startx != endx || starty != endy)
{
bs_set_pixel(startx, starty, pen_colour, opcode, 1);
}
if (p > 0)
{
startx += incx;
starty += incy;
p += dpru;
}
else
{
starty += incy;
p += dpr;
}
}
}
}
/*****************************************************************************/
void
bs_patblt(int opcode, int x, int y, int cx, int cy,
int brush_style, char * brush_pattern,
int brush_x_org, int brush_y_org,
int bgcolour, int fgcolour)
{
int i;
int j;
char ipattern[8];
char * b;
b = 0;
switch (brush_style)
{
case 0:
bs_rect(x, y, cx, cy, fgcolour, opcode);
break;
case 2: /* Hatch */
b = g_hatch_patterns + brush_pattern[0] * 8;
break;
case 3:
for (i = 0; i < 8; i++)
{
ipattern[i] = ~brush_pattern[7 - i];
}
b = ipattern;
break;
}
if (b != 0)
{
for (i = 0; i < cy; i++)
{
for (j = 0; j < cx; j++)
{
if (bs_is_pixel_on(b, (x + j + brush_x_org) % 8,
(y + i + brush_y_org) % 8, 8, 1))
{
bs_set_pixel(x + j, y + i, fgcolour, opcode, 1);
}
else
{
bs_set_pixel(x + j, y + i, bgcolour, opcode, 1);
}
}
}
}
}
/*****************************************************************************/
void
bs_copy_box(char * dst, int x, int y, int cx, int cy, int line_size)
{
char * src;
int i;
/* shouldn't happen */
if (cx < 1 || cy < 1)
{
return;
}
/* nothing to draw, memset and leave */
if (x + cx < 0 || y + cy < 0 || x >= g_width || y >= g_height)
{
memset(dst, 0, cx * cy * g_bs_Bpp);
return;
}
/* check if it goes over an edge */
if (x < 0 || y < 0 || x + cx > g_width || y + cy > g_height)
{
memset(dst, 0, cx * cy * g_bs_Bpp);
if (x < 0)
{
cx += x;
dst += -x * g_bs_Bpp;
x = 0;
}
if (x + cx > g_width)
{
cx = g_width - x;
}
for (i = 0; i < cy; i++)
{
src = get_bs_ptr(x, y + i);
if (src != 0)
{
bs_copy_mem(dst, src, cx * g_bs_Bpp);
}
dst += line_size;
}
}
else /* whole box is within */
{
for (i = 0; i < cy; i++)
{
src = get_bs_ptr(x, y + i);
if (src != 0)
{
bs_copy_mem(dst, src, cx * g_bs_Bpp);
}
dst += line_size;
}
}
}
|