File: fbdpsp.c

package info (click to toggle)
jfbterm 0.4.7-7.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 844 kB
  • ctags: 879
  • sloc: ansic: 6,563; sh: 3,092; makefile: 86
file content (1037 lines) | stat: -rw-r--r-- 23,290 bytes parent folder | download | duplicates (2)
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
/*
 * JFBTERM -
 * Copyright (C) 1999  Noritoshi MASUICHI (nmasu@ma3.justnet.ne.jp)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY NORITOSHI MASUICHI ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL NORITOSHI MASUICHI BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <stdio.h>
#include <string.h>
#include <endian.h>
#include <linux/fb.h>

#include "fbdpsp.h"
#include "fbcommon.h"

#ifndef min
#  define min(a, b)   (((a)>(b))?(b):(a))
#endif

#ifdef JFB_2BPP
/* 2bpp */

static u_char mask_2bpp_msb_left[] = {
	0xc0, 0x30, 0x0c, 0x03
};
static u_char mask_2bpp_msb_right[] = {
	0x03, 0x0c, 0x30, 0xc0
};
static u_char* mask_2bpp = mask_2bpp_msb_left;

void set_most_left(__u32 bpp, struct fb_bitfield red){
	if(red.offset != 0) return;
	if(red.length != bpp) return;
	/*
		This probing mechanism is known to work on:
		NEC MobileGear(DOS model), NEC MobileGear-II, 
		Psion s5. (but not work on Psion 5mx...)
		Call for more precise way...
	*/
	if(red.msb_right){
		mask_2bpp = mask_2bpp_msb_right;
	}
}

static u_char color_map_2bpp[] = {
	0x00,   // 0 (black)
	0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
	0xff,   // 7 (normal white)
	0x55,   // 8 (reverse underlined background)
	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
	0xff    // 15 (hilight white)
};

static inline u_char tfbm_select_2_color(u_int color)
{
#ifdef JFB_REVERSEVIDEO
	return (~ color_map_2bpp[color & 0xf]);
#else
	return (color_map_2bpp[color & 0xf]);
#endif
}

static inline void set_pixel_2bpp_packed(
	u_char *p, u_int x, u_int icol)
{
	// p points byte to be written
	u_int b = x / 4;        // Byte offset
	p[b] = ((p[b] & ~mask_2bpp[x & 3]) | (mask_2bpp[x & 3] & icol));
}

static inline void set_byte_2bpp_packed(
	u_char *bp, u_int icol)
{
	*bp = icol;
}

static inline void reverse_pixel_2bpp_packed(
	u_char *p, u_int x, u_int icol)
{
	*p = (*p & ~mask_2bpp[x & 3]) | ((*p ^ icol) & mask_2bpp[x & 3]);
}

void tfbm_fill_rect_2bpp_packed(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
	u_int y;
	u_int icol = tfbm_select_2_color(color);
	for (y = sy ; y < sy+ly ; y++) {
		u_char *line = p->smem + y * p->bytePerLine;
		u_char *bp = line + (sx / 4);   // byte position
		u_int xx = sx % 4;
		u_int pixels = lx;
		if (xx != 0) {
			// Fill until byte boundary
			while (xx % 4 != 0 && pixels != 0) {
				set_pixel_2bpp_packed(bp, xx++, icol);
				pixels--;
			}
			bp++;
		}
		while (pixels >= 4) {
			// Fill with byte
			set_byte_2bpp_packed(bp, icol);
			pixels -= 4;
			bp++;
		}
		xx = 0;
		while (pixels != 0) {
			// Fill rest pixels
			set_pixel_2bpp_packed(bp, xx++, icol);
			pixels--;
		}
	}
}

void tfbm_clear_all_2bpp_packed(
	TFrameBufferMemory* p, u_int color)
{
	u_int icol = tfbm_select_2_color(color);
	memset(p->smem, icol, p->slen);
}

void tfbm_overlay_2bpp_packed(
	TFrameBufferMemory* p,
	u_int xd, u_int yd,
	const u_char* ps, u_int lx, u_int ly, u_int gap, u_int color)
{
	u_int y;
	u_char* wp;
	const u_char* tps;
	u_int i;
	u_int sb;
	u_int icol = tfbm_select_2_color(color);

	for (y = yd ; y < yd+ly ; y++) {
		u_int xx = xd % 4;
		tps = ps;
		wp = p->smem + y * p->bytePerLine + (xd / 4);
		for (i = lx ; i >= 8 ; i -= 8) {
			sb = *tps++;
			if (sb & 0x80)
				set_pixel_2bpp_packed(wp, xx, icol);
			xx++;
			if (sb & 0x40)
				set_pixel_2bpp_packed(wp, xx, icol);
			xx++;
			if (sb & 0x20)
				set_pixel_2bpp_packed(wp, xx, icol);
			xx++;
			if (sb & 0x10)
				set_pixel_2bpp_packed(wp, xx, icol);
			xx++;
			if (sb & 0x08)
				set_pixel_2bpp_packed(wp, xx, icol);
			xx++;
			if (sb & 0x04)
				set_pixel_2bpp_packed(wp, xx, icol);
			xx++;
			if (sb & 0x02)
				set_pixel_2bpp_packed(wp, xx, icol);
			xx++;
			if (sb & 0x01)
				set_pixel_2bpp_packed(wp, xx, icol);
			xx++;
		}
		if (i) {
			sb = *tps++;
			switch (i) {
			case 7:
				if (sb & 0x02)
					set_pixel_2bpp_packed(wp, xx + 6, icol);
			case 6:
				if (sb & 0x04)
					set_pixel_2bpp_packed(wp, xx + 5, icol);
			case 5:
				if (sb & 0x08)
					set_pixel_2bpp_packed(wp, xx + 4, icol);
			case 4:
				if (sb & 0x10)
					set_pixel_2bpp_packed(wp, xx + 3, icol);
			case 3:
				if (sb & 0x20)
					set_pixel_2bpp_packed(wp, xx + 2, icol);
			case 2:
				if (sb & 0x40)
					set_pixel_2bpp_packed(wp, xx + 1, icol);
			case 1:
				if (sb & 0x80)
					set_pixel_2bpp_packed(wp, xx + 0, icol);
			}
		}
		ps += gap;
	}
}

void tfbm_reverse_2bpp_packed(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
	u_int y;
	u_int x;
	u_int icol = tfbm_select_2_color(color);
	for (y = sy ; y < min(sy+ly, p->height) ; y++) {
		unsigned char *line = p->smem + y * p->bytePerLine;
		for (x = sx ; x < sx + lx ; x++) {
			unsigned char *bp = line + (x / 4);     // byte position
			reverse_pixel_2bpp_packed(bp, x, icol);
		}
	}
}

#endif /* JFB_2BPP */


#ifdef JFB_8BPP
/* 8 bpp */

#ifdef EXPERMINAL
typedef u_int T_DWORD;
typedef u_short T_WORD;
typedef u_char T_BYTE;

T_DWORD mask_8bpp_packed[16] = {
#ifdef BIG_ENDIAN
	0x00000000, 0x000000FF, 0x0000FF00, 0x0000FFFF,
	0x00FF0000, 0x00FF00FF, 0x00FFFF00, 0x00FFFFFF,
	0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF,
	0xFFFF0000, 0xFFFF00FF, 0xFFFFFF00, 0xFFFFFFFF,
#else
#ifdef LITTLE_ENDIAN
	0x00000000, 0xFF000000, 0x00FF0000, 0xFFFF0000,
	0x0000FF00, 0xFF00FF00, 0x00FFFF00, 0xFFFFFF00,
	0x000000FF, 0xFF0000FF, 0x00FF00FF, 0xFFFF00FF,
	0x0000FFFF, 0xFF00FFFF, 0x00FFFFFF, 0xFFFFFFFF,
#else
#error "Byte ordering have to be defined. Cannot continue."
#endif
#endif
};
#endif /* EXPERMINAL*/

void tfbm_fill_rect_8bpp_packed(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
	u_int y;
	for (y = sy ; y < min(sy+ly, p->height) ; y++) {
#if 0
 		memset(p->smem + y * p->bytePerLine + sx, color, lx);
#else
		unsigned char *line = p->smem + y * p->bytePerLine + sx;
		u_int x;
		for (x = 0 ; x < lx ; x++) {
			line[x] = color;
		}
#endif
	}
}

void tfbm_clear_all_8bpp_packed(
	TFrameBufferMemory* p, u_int color)
{
	memset(p->smem, color, p->slen);
}

void tfbm_overlay_8bpp_packed(
	TFrameBufferMemory* p,
	u_int xd, u_int yd,
	const u_char* ps, u_int lx, u_int ly, u_int gap, u_int color)
{
#ifdef EXPERMINAL
	const u_char* tps;
	u_int y;
	u_int x;
	u_int glyph;
	u_int bm;
	T_DWORD icol;
	T_DWORD *wp;

	icol = color; icol |= icol << 8; icol |= icol << 16;
	for (y = yd ; y < yd+ly ; y++) {
		tps = ps;
		wp = (T_DWORD*)(p->smem + y * p->bytePerLine + (xd & ~3u));
		glyph = *tps++;

		bm = glyph >> (xd & 0x3);
		x = lx + (xd & 0x3);
		while (x > 7) {
			*wp ^= mask_8bpp_packed[bm >> 4] & (icol ^ *wp);
			wp++;
			*wp ^= mask_8bpp_packed[bm & 0xF] & (icol ^ *wp);
			wp++;
			bm = (glyph << (8 - (xd & 0x3))) & 0xFF;
			glyph = *tps++;
			bm |= glyph >> (xd & 0x3);
			x -= 8;
		}
		if (x > 0) {
			bm &= 0xff00 >> x;
			*wp ^= mask_8bpp_packed[bm >> 4] & (icol ^ *wp);
			if (x > 4) {
				wp++;
				*wp ^= mask_8bpp_packed[bm & 0xF] & (icol ^ *wp);
			}
		}
		ps += gap;
	}
#else
	u_int y;
	u_char* wp;
	const u_char* tps;
	u_int i;
	u_int sb;

	for (y = yd ; y < yd+ly ; y++) {
		tps = ps;
		wp = p->smem + y * p->bytePerLine + xd;
		for (i = lx ; i >= 8 ; i -= 8) {
			sb = *tps++;
			if (sb & 0x80) wp[0] = color;
			if (sb & 0x40) wp[1] = color;
			if (sb & 0x20) wp[2] = color;
			if (sb & 0x10) wp[3] = color;
			if (sb & 0x08) wp[4] = color;
			if (sb & 0x04) wp[5] = color;
			if (sb & 0x02) wp[6] = color;
			if (sb & 0x01) wp[7] = color;
			wp += 8;
		}
		if (i) {
			sb = *tps++;
			switch (i) {
			case 7:	if (sb & 0x02) wp[6] = color;
			case 6:	if (sb & 0x04) wp[5] = color;
			case 5:	if (sb & 0x08) wp[4] = color;
			case 4:	if (sb & 0x10) wp[3] = color;
			case 3:	if (sb & 0x20) wp[2] = color;
			case 2:	if (sb & 0x40) wp[1] = color;
			case 1:	if (sb & 0x80) wp[0] = color;
			}
		}
		ps += gap;
	}
#endif
}

void tfbm_reverse_8bpp_packed(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
#ifdef EXPERMINAL
	u_int y;
	u_int x;
	u_int bm;
	T_DWORD icol;
	T_DWORD *wp;

	icol = color; icol |= icol << 8; icol |= icol << 16;
	for (y = sy ; y < min(sy+ly, p->height) ; y++) {
		wp = (T_DWORD*)(p->smem + y * p->bytePerLine + (sx & ~3u));
		bm = 0xF >> (sx & 3);
		x = lx + (sx & 3);
		while (x > 3) {
			*wp++ ^= mask_8bpp_packed[bm] & icol;
			bm = 0xF;
			x -= 4;
		}
		if (x > 0) {
			bm &= 0xf0 >> x;
			*wp ^= mask_8bpp_packed[bm] & icol;
		}
	}
#else
	u_int y;
	u_int x;
	for (y = sy ; y < min(sy+ly, p->height) ; y++) {
		for (x = sx ; x < sx+lx ; x++) {
			p->smem[y * p->bytePerLine + x] ^= color;
		}
	}
#endif
}
#endif /* JFB_8BPP */


#ifdef JFB_15BPP
/* 15 bpp */
void tfbm_fill_rect_15bpp_packed(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
	u_int	x,y;
	u_short	*d;
	u_short icol;

	icol = tfbm_select_16_color(color);
	for (y = sy ; y < min(sy+ly, p->height) ; y++) {
		d=(u_short*)(p->smem + y * p->bytePerLine + sx * 2);
		for(x=0;x<lx;x++){
			*d = icol;
			d++;
		}
	}
}

void tfbm_clear_all_15bpp_packed(
	TFrameBufferMemory* p, u_int color)
{
	u_int lp;
	u_short	*d=(u_short*)(p->smem);
	u_short icol;

	icol = tfbm_select_16_color(color);
	for(lp=0;lp<((p->slen)/2);lp++){
		d[lp]=icol;
	}
}

void tfbm_overlay_15bpp_packed(
	TFrameBufferMemory* p,
	u_int xd, u_int yd,
	const u_char* ps, u_int lx, u_int ly, u_int gap, u_int color)
{
	u_int y;
	u_short* wp;
	const u_char* tps;
	u_int i;
	u_int sb;
	u_short icol;

	icol = tfbm_select_16_color(color);

	for (y = yd ; y < yd+ly ; y++) {
		tps = ps;
		wp = (u_short*)(p->smem + y * p->bytePerLine + xd * 2);
		for (i = lx ; i >= 8 ; i -= 8) {
			sb = *tps++;
			if (sb & 0x80) wp[0] = icol;
			if (sb & 0x40) wp[1] = icol;
			if (sb & 0x20) wp[2] = icol;
			if (sb & 0x10) wp[3] = icol;
			if (sb & 0x08) wp[4] = icol;
			if (sb & 0x04) wp[5] = icol;
			if (sb & 0x02) wp[6] = icol;
			if (sb & 0x01) wp[7] = icol;
			wp += 8;
		}
		if (i) {
			sb = *tps++;
			switch (i) {
			case 7:	if (sb & 0x02) wp[6] = icol;
			case 6:	if (sb & 0x04) wp[5] = icol;
			case 5:	if (sb & 0x08) wp[4] = icol;
			case 4:	if (sb & 0x10) wp[3] = icol;
			case 3:	if (sb & 0x20) wp[2] = icol;
			case 2:	if (sb & 0x40) wp[1] = icol;
			case 1:	if (sb & 0x80) wp[0] = icol;
			}
		}
		ps += gap;
	}
}

void tfbm_reverse_15bpp_packed(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
	u_int x,y;
	u_short	*d;
	u_short icol;

	icol = tfbm_select_16_color(color);
	for (y = sy ; y < min(sy+ly, p->height) ; y++) {
		d = (u_short*)(p->smem + y * p->bytePerLine + sx * 2);
		for (x = 0 ; x < lx ; x++) {
			d[x] ^= icol;
		}
	}
}
#endif /* JFBTERM_15BPP */

#ifdef JFB_16BPP
/* 16 bpp */
void tfbm_fill_rect_16bpp_packed(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
	u_int	x,y;
	u_short	*d;
	u_short icol;

	icol = tfbm_select_16_color(color);
	for (y = sy ; y < min(sy+ly, p->height) ; y++) {
		d=(u_short*)(p->smem + y * p->bytePerLine + sx * 2);
		for(x=0;x<lx;x++){
			*d = icol;
			d++;
		}
	}
}

void tfbm_clear_all_16bpp_packed(
	TFrameBufferMemory* p, u_int color)
{
	u_int lp;
	u_short	*d=(u_short*)(p->smem);
	u_short icol;

	icol = tfbm_select_16_color(color);
	for(lp=0;lp<((p->slen)/2);lp++){
		d[lp]=icol;
	}
}

void tfbm_overlay_16bpp_packed(
	TFrameBufferMemory* p,
	u_int xd, u_int yd,
	const u_char* ps, u_int lx, u_int ly, u_int gap, u_int color)
{
	u_int y;
	u_short* wp;
	const u_char* tps;
	u_int i;
	u_int sb;
	u_short icol;

	icol = tfbm_select_16_color(color);
	for (y = yd ; y < yd+ly ; y++) {
		tps = ps;
		wp = (u_short*)(p->smem + y * p->bytePerLine + xd * 2);
		for (i = lx ; i >= 8 ; i -= 8) {
			sb = *tps++;
			if (sb & 0x80) wp[0] = icol;
			if (sb & 0x40) wp[1] = icol;
			if (sb & 0x20) wp[2] = icol;
			if (sb & 0x10) wp[3] = icol;
			if (sb & 0x08) wp[4] = icol;
			if (sb & 0x04) wp[5] = icol;
			if (sb & 0x02) wp[6] = icol;
			if (sb & 0x01) wp[7] = icol;
			wp += 8;
		}
		if (i) {
			sb = *tps++;
			switch (i) {
			case 7:	if (sb & 0x02) wp[6] = icol;
			case 6:	if (sb & 0x04) wp[5] = icol;
			case 5:	if (sb & 0x08) wp[4] = icol;
			case 4:	if (sb & 0x10) wp[3] = icol;
			case 3:	if (sb & 0x20) wp[2] = icol;
			case 2:	if (sb & 0x40) wp[1] = icol;
			case 1:	if (sb & 0x80) wp[0] = icol;
			}
		}
		ps += gap;
	}
}

#ifndef min
#  define min(a, b)    (((a) > (b))?(b):(a))
#endif

void tfbm_reverse_16bpp_packed(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
	u_int x,y;
	u_short	*d;
	u_short icol;

	icol = tfbm_select_16_color(color);
	for (y = sy ; y < min(sy+ly, p->height) ; y++) {
		d = (u_short*)(p->smem + y * p->bytePerLine + sx * 2);
		for (x = 0 ; x < lx ; x++) {
			d[x] ^= icol;
		}
	}
}
#endif /* JFB_16BPP */

#ifdef JFB_24BPP
/* 24 bpp */
void tfbm_fill_rect_24bpp_packed(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
	u_int	x,y;
	u_char*	d;
	u_int t, m, b;
	u_int icol;

	icol = tfbm_select_32_color(color);
#if __BYTE_ORDER == __BIG_ENDIAN
	b = 0xff & (icol >> 16); m = 0xff & (icol >> 8); t = 0xff & icol;
#elif __BYTE_ORDER == __LITTLE_ENDIAN
	t = 0xff & (icol >> 16); m = 0xff & (icol >> 8); b = 0xff & icol;
#else
#	error FIXME : No endianness ?
#endif 

	for (y = sy ; y < sy+ly ; y++) {
		d=p->smem + y * p->bytePerLine + sx * 3;
		for(x=0;x<lx;x++){
			d[0] = t;
			d[1] = m;
			d[2] = b;
			d+=3;
		}
	}
}

void tfbm_clear_all_24bpp_packed(
	TFrameBufferMemory* p, u_int color)
{
	u_int lp;
	u_int t, m, b;
	u_int icol;

	icol = tfbm_select_32_color(color);
#if __BYTE_ORDER == __BIG_ENDIAN
	b = 0xff & (icol >> 16); m = 0xff & (icol >> 8); t = 0xff & icol;
#elif __BYTE_ORDER == __LITTLE_ENDIAN
	t = 0xff & (icol >> 16); m = 0xff & (icol >> 8); b = 0xff & icol;
#else
#	error FIXME : No endianness ?
#endif 

	for(lp=0;lp<((p->slen)-2);lp+=3){
		p->smem[lp]   = t;
		p->smem[lp+1] = m;
		p->smem[lp+2] = b;
	}
}

void tfbm_overlay_24bpp_packed(
	TFrameBufferMemory* p,
	u_int xd, u_int yd,
	const u_char* ps, u_int lx, u_int ly, u_int gap, u_int color)
{
	u_int y,i,sb;
	u_char* wp;
	const u_char* tps;
	u_int t, m, b;
	u_int icol;

	icol = tfbm_select_32_color(color);
#if __BYTE_ORDER == __BIG_ENDIAN
	b = 0xff & (icol >> 16); m = 0xff & (icol >> 8); t = 0xff & icol;
#elif __BYTE_ORDER == __LITTLE_ENDIAN
	t = 0xff & (icol >> 16); m = 0xff & (icol >> 8); b = 0xff & icol;
#else
#	error FIXME : No endianness ?
#endif 

	for (y = yd ; y < yd+ly ; y++) {
		tps = ps;
		wp = p->smem + y * p->bytePerLine + xd * 3;
		for (i = lx ; i >= 8 ; i -= 8) {
			sb = *tps++;
			if (sb & 0x80) wp[0] = t,wp[1] = m,wp[2] = b;
			if (sb & 0x40) wp[3] = t,wp[4] = m,wp[5] = b;
			if (sb & 0x20) wp[6] = t,wp[7] = m,wp[8] = b;
			if (sb & 0x10) wp[9] = t,wp[10] = m,wp[11] = b;
			if (sb & 0x08) wp[12] = t,wp[13] = m,wp[14] = b;
			if (sb & 0x04) wp[15] = t,wp[16] = m,wp[17] = b;
			if (sb & 0x02) wp[18] = t,wp[19] = m,wp[20] = b;
			if (sb & 0x01) wp[21] = t,wp[22] = m,wp[23] = b;
			wp += 24;
		}
		if (i) {
			sb = *tps++;
			switch (i) {
			case 7:	if (sb & 0x02) wp[18] = t,wp[19] = m,wp[20] = b;
			case 6:	if (sb & 0x04) wp[15] = t,wp[16] = m,wp[17] = b;
			case 5:	if (sb & 0x08) wp[12] = t,wp[13] = m,wp[14] = b;
			case 4:	if (sb & 0x10) wp[9] = t,wp[10] = m,wp[11] = b;
			case 3:	if (sb & 0x20) wp[6] = t,wp[7] = m,wp[8] = b;
			case 2:	if (sb & 0x40) wp[3] = t,wp[4] = m,wp[5] = b;
			case 1:	if (sb & 0x80) wp[0] = t,wp[1] = m,wp[2] = b;
			}
		}
		ps += gap;
	}
}

void tfbm_reverse_24bpp_packed(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
	u_int x,y;
	u_int t, m, b;
	u_int icol;

	icol = tfbm_select_32_color(color);
#if __BYTE_ORDER == __BIG_ENDIAN
	b = 0xff & (icol >> 16); m = 0xff & (icol >> 8); t = 0xff & icol;
#elif __BYTE_ORDER == __LITTLE_ENDIAN
	t = 0xff & (icol >> 16); m = 0xff & (icol >> 8); b = 0xff & icol;
#else
#	error FIXME : No endianness ?
#endif 

	for (y = sy ; y < sy+ly ; y++) {
		for (x = 0 ; x < (lx*3) ; x+=3) {
			p->smem[y * p->bytePerLine + sx * 3 + x] ^= t;
			p->smem[y * p->bytePerLine + sx * 3 + x + 1] ^= m;
			p->smem[y * p->bytePerLine + sx * 3 + x + 2] ^= b;
		}
	}
}
#endif /* JFB_24BPP */

#ifdef JFB_32BPP
/* 32 bpp */
void tfbm_fill_rect_32bpp_packed(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
	u_int x,y;
	u_int icol;
	u_int *d;

	icol = tfbm_select_32_color(color);
	for (y = sy ; y < sy+ly ; y++) {
		d=(u_int*)(p->smem + y * p->bytePerLine + sx * 4);
		for(x = 0 ; x < lx ; x++){
			*d++ = icol;
		}
	}
}

void tfbm_clear_all_32bpp_packed(
	TFrameBufferMemory* p, u_int color)
{
	u_int lp;
	u_int	*d=(u_int*)(p->smem);
	u_int icol;

	icol = tfbm_select_32_color(color);
	for(lp = 0 ; lp < ((p->slen)/2) ; lp++) {
		d[lp]=icol;
	}
}

void tfbm_overlay_32bpp_packed(
	TFrameBufferMemory* p,
	u_int xd, u_int yd,
	const u_char* ps, u_int lx, u_int ly, u_int gap, u_int color)
{
	u_int y;
	u_int* wp;
	const u_char* tps;
	u_int i;
	u_int sb;
	u_int icol;

	icol = tfbm_select_32_color(color);
	for (y = yd ; y < yd+ly ; y++) {
		tps = ps;
		wp = (u_int*)(p->smem + y * p->bytePerLine + xd * 4);
		for (i = lx ; i >= 8 ; i -= 8) {
			sb = *tps++;
			if (sb & 0x80) wp[0] = icol;
			if (sb & 0x40) wp[1] = icol;
			if (sb & 0x20) wp[2] = icol;
			if (sb & 0x10) wp[3] = icol;
			if (sb & 0x08) wp[4] = icol;
			if (sb & 0x04) wp[5] = icol;
			if (sb & 0x02) wp[6] = icol;
			if (sb & 0x01) wp[7] = icol;
			wp += 8;
		}
		if (i) {
			sb = *tps++;
			switch (i) {
			case 7:	if (sb & 0x02) wp[6] = icol;
			case 6:	if (sb & 0x04) wp[5] = icol;
			case 5:	if (sb & 0x08) wp[4] = icol;
			case 4:	if (sb & 0x10) wp[3] = icol;
			case 3:	if (sb & 0x20) wp[2] = icol;
			case 2:	if (sb & 0x40) wp[1] = icol;
			case 1:	if (sb & 0x80) wp[0] = icol;
			}
		}
		ps += gap;
	}
}

void tfbm_reverse_32bpp_packed(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
	u_int x,y;
	u_int *d;
	u_int icol;

	icol = tfbm_select_32_color(color);
	for (y = sy ; y < sy+ly ; y++) {
		d = (u_int*)(p->smem + y * p->bytePerLine + sx * 4);
		for (x = 0 ; x < lx ; x++) {
			d[x] ^= icol;
		}
	}
}
#endif /* JFB_32BPP */

#ifdef JFB_VGA16FB

#ifdef __GLIBC__
#include <sys/io.h>
#else
#include <asm/io.h>
#endif

#define GRAPHICS_ADDR_REG 0x3ce         /* Graphics address register. */
#define GRAPHICS_DATA_REG 0x3cf         /* Graphics data register. */
#define SET_RESET_INDEX 0               /* Set/Reset Register index. */
#define ENABLE_SET_RESET_INDEX 1        /* Enable Set/Reset Register index. */
#define DATA_ROTATE_INDEX 3             /* Data Rotate Register index. */
#define GRAPHICS_MODE_INDEX 5           /* Graphics Mode Register index. */
#define BIT_MASK_INDEX 8                /* Bit Mask Register index. */

static inline void rmw(volatile char *p)
{
        *p |= 1;
}
static inline void setmode(int mode)
{
        outb(GRAPHICS_MODE_INDEX, GRAPHICS_ADDR_REG);
        outb(mode, GRAPHICS_DATA_REG);
}
static inline void selectmask(void)
{
        outb(BIT_MASK_INDEX, GRAPHICS_ADDR_REG);
}
static inline void setmask(int mask)
{
        outb(mask, GRAPHICS_DATA_REG);
}
static inline void setop(int op)
{
        outb(DATA_ROTATE_INDEX, GRAPHICS_ADDR_REG);
        outb(op, GRAPHICS_DATA_REG);
}
static inline void setsr(int sr)
{
        outb(ENABLE_SET_RESET_INDEX, GRAPHICS_ADDR_REG);
        outb(sr, GRAPHICS_DATA_REG);
}
static inline void setcolor(int color)
{
        outb(SET_RESET_INDEX, GRAPHICS_ADDR_REG);
        outb(color, GRAPHICS_DATA_REG);
}
static inline void setindex(int idx)
{
        outb(idx, GRAPHICS_ADDR_REG);
}
static inline void setrplane(int idx)
{
        outb(0x4, GRAPHICS_ADDR_REG);
        outb(idx, GRAPHICS_DATA_REG);
}

void tfbm_fill_rect_vga16(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
	int y;
	unsigned char *wp;
	unsigned char mask;
	u_int sofs = sx % 8;
	u_int eofs = (sx+lx) % 8;
	unsigned char emask = 0xff00 >> eofs;
	u_int ix;

	setmode(0);
	setop(0);
	setsr(0xf);
	setcolor(color);
	selectmask();
	setmask(0xff);

	for (y = sy ; y < sy+ly ; y++) {
		wp = p->smem + y*p->bytePerLine + sx/8;
		mask = 0xff >> sofs;

		for (ix = lx + sofs ; ix > 7 ; ix -= 8) {
			setmask(mask);
			rmw(wp);
			wp++;
			mask = 0xff;
		}
		if (ix) {
			setmask(mask & emask);
			rmw(wp);
		}
	}
}

void tfbm_clear_all_vga16(
	TFrameBufferMemory* p, u_int color)
{
	setmode(0);
	setop(0);
	setsr(0xf);
	setcolor(color);
	selectmask();

	setmask(0xff);

	memset(p->smem, 0, p->slen);
}

void tfbm_overlay_vga16(
	TFrameBufferMemory* p,
	u_int xd, u_int yd,
	const u_char* ps, u_int lx, u_int ly, u_int gap, u_int color)
{
	int y;
	volatile unsigned char *wp;
	const unsigned char *tps;
	volatile unsigned char mask;

	u_int sofs = xd & 7;
	u_int eofs = (xd+lx) & 7;
	u_int xds8 = xd/8;
	u_int xde8 = (xd+lx+7)/8;
	unsigned char emask;
	unsigned char smask;
	u_int ix;

	eofs = eofs ? eofs : 8;
	emask = 0xff00 >> eofs;
	smask = 0xff << sofs;

	setmode(0);
	setop(0);
	setsr(0xf);
	setcolor(color);
	selectmask();

	for (y = yd ; y < yd+ly ; y++) {
		tps = ps;
		wp = p->smem + y*p->bytePerLine + xds8;

		if (xds8+1 == xde8) {
			mask = ((*tps >> sofs) & emask);
			setmask(mask);
			rmw(wp);
		} else {
			mask = (*tps++ >> sofs);
			setmask(mask);
			rmw(wp);
			wp++;
			for (ix = xds8+1 ; ix < xde8-1 ; ix++) {
				mask = (*tps >> sofs) | (*(tps-1) << (8-sofs));
				tps++;
				setmask(mask);
				rmw(wp);
				wp++;
			}
			if (eofs <= sofs) {
				if (sofs == 0) {
					mask = *tps;
				} else {
					mask = (*(tps-1) << (8-sofs)) & emask;
				}
			} else {
				mask = ((*tps >> sofs) |
					(*(tps-1) << (8-sofs))) & emask;
			}
			setmask(mask);
			rmw(wp);
		}

		ps += gap;
	}
}

void tfbm_reverse_vga16(
	TFrameBufferMemory* p,
	u_int sx, u_int sy, u_int lx, u_int ly, u_int color)
{
	int y;
	unsigned char *wp;
	unsigned char mask;
	u_int sofs = sx % 8;
	u_int eofs = (sx+lx) % 8;
	unsigned char emask = 0xff00 >> eofs;
	u_int ix;

	setmode(0);
	setop(0x18);
	setsr(0xf);
	setcolor(color);
	selectmask();
  
	for (y = sy ; y < sy+ly ; y++) {
		wp = p->smem + y*p->bytePerLine + sx/8;
		mask = 0xff >> sofs;

		for (ix = lx + sofs ; ix > 7 ; ix -= 8) {
			setmask(mask);
			rmw(wp);
			wp++;
			mask = 0xff;
		}
		if (ix) {
			setmask(mask & emask);
			rmw(wp);
		}
	}
}

#endif /* JFB_VGA16 */