2 * Copyright (c) 1991-1997 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
36 static byte VGLSavePaletteRed[256];
37 static byte VGLSavePaletteGreen[256];
38 static byte VGLSavePaletteBlue[256];
40 #define ABS(a) (((a)<0) ? -(a) : (a))
41 #define SGN(a) (((a)<0) ? -1 : 1)
42 #define min(x, y) (((x) < (y)) ? (x) : (y))
43 #define max(x, y) (((x) > (y)) ? (x) : (y))
46 color2mem(u_long color, byte *b, int len)
50 b[3] = (color >> 24) & 0xff;
53 b[2] = (color >> 16) & 0xff;
56 b[1] = (color >> 8) & 0xff;
68 mem2color(byte *b, int len)
74 color |= (b[3] & 0xff) << 24;
77 color |= (b[2] & 0xff) << 16;
80 color |= (b[1] & 0xff) << 8;
84 color |= (b[0] & 0xff);
92 VGLSetXY(VGLBitmap *object, int x, int y, u_long color)
98 if (x>=0 && x<object->VXsize && y>=0 && y<object->VYsize) {
99 if (!VGLMouseFreeze(x, y, 1, 1, color)) {
100 switch (object->Type) {
103 object->Bitmap[y*object->VXsize+x]=((byte)color);
106 object->Bitmap[VGLSetSegment(y*object->VXsize+x)]=((byte)color);
111 color2mem(color, b, object->PixelBytes);
112 bcopy(b, &object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
118 color2mem(color, b, object->PixelBytes);
119 offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
120 bcopy(b, &object->Bitmap[offset], object->PixelBytes);
124 outb(0x3c5, 0x01 << (x&0x3));
125 object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)] = ((byte)color);
128 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
131 offset = y*VGLAdpInfo.va_line_width + x/8;
133 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
134 outb(0x3ce, 0x00); outb(0x3cf, (byte)color & 0x0f); /* set/reset */
135 outb(0x3ce, 0x01); outb(0x3cf, 0x0f); /* set/reset enable */
136 outb(0x3ce, 0x08); outb(0x3cf, 0x80 >> (x%8)); /* bit mask */
137 object->Bitmap[offset] |= (byte)color;
145 VGLGetXY(VGLBitmap *object, int x, int y)
156 if (x<0 || x>=object->VXsize || y<0 || y>=object->VYsize)
158 switch (object->Type) {
161 return object->Bitmap[((y*object->VXsize)+x)];
163 return object->Bitmap[VGLSetSegment(y*object->VXsize+x)];
167 bcopy(&object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
168 b, object->PixelBytes);
169 return (mem2color(b, object->PixelBytes));
173 offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
174 bcopy(&object->Bitmap[offset], b, object->PixelBytes);
176 return (mem2color(b, object->PixelBytes));
178 outb(0x3ce, 0x04); outb(0x3cf, x & 0x3);
179 return object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)];
181 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
184 offset = y*VGLAdpInfo.va_line_width + x/8;
187 return (object->Bitmap[offset]&(0x80>>(x%8))) ? 1 : 0; /* XXX */
190 mask = 0x80 >> (x%8);
191 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
192 outb(0x3ce, 0x04); outb(0x3cf, i);
193 color |= (object->Bitmap[offset] & mask) ? (1 << i) : 0;
198 return 0; /* XXX black? */
202 * Symmetric Double Step Line Algorithm by Brian Wyvill from
203 * "Graphics Gems", Academic Press, 1990.
206 #define SL_SWAP(a,b) {a^=b; b^=a; a^=b;}
207 #define SL_ABSOLUTE(i,j,k) ( (i-j)*(k = ( (i-j)<0 ? -1 : 1)))
210 plot(VGLBitmap * object, int x, int y, int flag, byte color)
212 /* non-zero flag indicates the pixels need swapping back. */
214 VGLSetXY(object, y, x, color);
216 VGLSetXY(object, x, y, color);
221 VGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
223 int dx, dy, incr1, incr2, D, x, y, xend, c, pixels_left;
224 int sign_x, sign_y, step, reverse, i;
226 dx = SL_ABSOLUTE(x2, x1, sign_x);
227 dy = SL_ABSOLUTE(y2, y1, sign_y);
228 /* decide increment sign by the slope sign */
229 if (sign_x == sign_y)
234 if (dy > dx) { /* chooses axis of greatest movement (make dx) */
241 /* note error check for dx==0 should be included here */
242 if (x1 > x2) { /* start from the smaller coordinate */
255 /* Note dx=n implies 0 - n or (dx+1) pixels to be set */
256 /* Go round loop dx/4 times then plot last 0,1,2 or 3 pixels */
257 /* In fact (dx-1)/4 as 2 pixels are already plotted */
259 pixels_left = (dx - 1) % 4; /* number of pixels left over at the
261 plot(object, x, y, reverse, color);
263 return; /* plot only one pixel for zero length
265 plot(object, x1, y1, reverse, color); /* plot first two points */
266 incr2 = 4 * dy - 2 * dx;
267 if (incr2 < 0) { /* slope less than 1/2 */
272 for (i = 0; i < xend; i++) { /* plotting loop */
276 /* pattern 1 forwards */
277 plot(object, x, y, reverse, color);
278 plot(object, ++x, y, reverse, color);
279 /* pattern 1 backwards */
280 plot(object, x1, y1, reverse, color);
281 plot(object, --x1, y1, reverse, color);
285 /* pattern 2 forwards */
286 plot(object, x, y, reverse, color);
287 plot(object, ++x, y += step, reverse,
289 /* pattern 2 backwards */
290 plot(object, x1, y1, reverse, color);
291 plot(object, --x1, y1 -= step, reverse,
294 /* pattern 3 forwards */
295 plot(object, x, y += step, reverse, color);
296 plot(object, ++x, y, reverse, color);
297 /* pattern 3 backwards */
298 plot(object, x1, y1 -= step, reverse,
300 plot(object, --x1, y1, reverse, color);
306 /* plot last pattern */
309 plot(object, ++x, y, reverse, color); /* pattern 1 */
311 plot(object, ++x, y, reverse, color);
313 plot(object, --x1, y1, reverse, color);
316 plot(object, ++x, y, reverse, color); /* pattern 2 */
318 plot(object, ++x, y += step, reverse, color);
320 plot(object, --x1, y1, reverse, color);
323 plot(object, ++x, y += step, reverse, color);
325 plot(object, ++x, y, reverse, color);
327 plot(object, --x1, y1 -= step, reverse, color);
330 } /* end if pixels_left */
332 /* end slope < 1/2 */
333 else { /* slope greater than 1/2 */
337 for (i = 0; i < xend; i++) {
341 /* pattern 4 forwards */
342 plot(object, x, y += step, reverse, color);
343 plot(object, ++x, y += step, reverse, color);
344 /* pattern 4 backwards */
345 plot(object, x1, y1 -= step, reverse, color);
346 plot(object, --x1, y1 -= step, reverse, color);
350 /* pattern 2 forwards */
351 plot(object, x, y, reverse, color);
352 plot(object, ++x, y += step, reverse,
355 /* pattern 2 backwards */
356 plot(object, x1, y1, reverse, color);
357 plot(object, --x1, y1 -= step, reverse,
360 /* pattern 3 forwards */
361 plot(object, x, y += step, reverse, color);
362 plot(object, ++x, y, reverse, color);
363 /* pattern 3 backwards */
364 plot(object, x1, y1 -= step, reverse, color);
365 plot(object, --x1, y1, reverse, color);
370 /* plot last pattern */
373 plot(object, ++x, y += step, reverse, color); /* pattern 4 */
375 plot(object, ++x, y += step, reverse,
378 plot(object, --x1, y1 -= step, reverse,
382 plot(object, ++x, y, reverse, color); /* pattern 2 */
384 plot(object, ++x, y += step, reverse, color);
386 plot(object, --x1, y1, reverse, color);
389 plot(object, ++x, y += step, reverse, color);
391 plot(object, ++x, y, reverse, color);
392 if (pixels_left > 2) {
393 if (D > c) /* step 3 */
394 plot(object, --x1, y1 -= step, reverse, color);
396 plot(object, --x1, y1, reverse, color);
405 VGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
407 VGLLine(object, x1, y1, x2, y1, color);
408 VGLLine(object, x2, y1, x2, y2, color);
409 VGLLine(object, x2, y2, x1, y2, color);
410 VGLLine(object, x1, y2, x1, y1, color);
414 VGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
418 for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
422 set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
425 VGLSetXY(object, xc+x, yc+y, color);
426 VGLSetXY(object, xc-x, yc+y, color);
428 VGLSetXY(object, xc+x, yc-y, color);
429 VGLSetXY(object, xc-x, yc-y, color);
433 VGLSetXY(object, xc, yc+y, color);
435 VGLSetXY(object, xc, yc-y, color);
440 VGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
442 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
443 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
446 set4pixels(object, x, y, xc, yc, color);
448 y--; dy-=asq2; d-=dy;
450 x++; dx+=bsq2; d+=bsq+dx;
452 d+=(3*(asq-bsq)/2-(dx+dy))/2;
454 set4pixels(object, x, y, xc, yc, color);
456 x++; dx+=bsq2; d+=dx;
458 y--; dy-=asq2; d+=asq-dy;
463 set2lines(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
466 VGLLine(object, xc+x, yc+y, xc-x, yc+y, color);
468 VGLLine(object, xc+x, yc-y, xc-x, yc-y, color);
471 VGLLine(object, xc, yc+y, xc, yc-y, color);
476 VGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
478 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
479 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
482 set2lines(object, x, y, xc, yc, color);
484 y--; dy-=asq2; d-=dy;
486 x++; dx+=bsq2; d+=bsq+dx;
488 d+=(3*(asq-bsq)/2-(dx+dy))/2;
490 set2lines(object, x, y, xc, yc, color);
492 x++; dx+=bsq2; d+=dx;
494 y--; dy-=asq2; d+=asq-dy;
499 VGLClear(VGLBitmap *object, u_long color)
507 VGLMouseFreeze(0, 0, object->Xsize, object->Ysize, color); /* XXX */
508 switch (object->Type) {
511 memset(object->Bitmap, (byte)color, object->VXsize*object->VYsize);
515 for (offset = 0; offset < object->VXsize*object->VYsize; ) {
516 VGLSetSegment(offset);
517 len = min(object->VXsize*object->VYsize - offset,
518 VGLAdpInfo.va_window_size);
519 memset(object->Bitmap, (byte)color, len);
526 color2mem(color, b, object->PixelBytes);
527 total = object->VXsize*object->VYsize*object->PixelBytes;
528 for (i = 0; i < total; i += object->PixelBytes)
529 bcopy(b, object->Bitmap + i, object->PixelBytes);
535 color2mem(color, b, object->PixelBytes);
536 total = object->VXsize*object->VYsize*object->PixelBytes;
537 for (offset = 0; offset < total; ) {
538 VGLSetSegment(offset);
539 len = min(total - offset, VGLAdpInfo.va_window_size);
540 for (i = 0; i < len; i += object->PixelBytes)
541 bcopy(b, object->Bitmap + offset + i, object->PixelBytes);
547 /* XXX works only for Xsize % 4 = 0 */
549 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
550 memset(object->Bitmap, (byte)color, VGLAdpInfo.va_line_width*object->VYsize);
555 /* XXX works only for Xsize % 8 = 0 */
556 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
557 outb(0x3ce, 0x05); outb(0x3cf, 0x02); /* mode 2 */
558 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
559 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
560 for (offset = 0; offset < VGLAdpInfo.va_line_width*object->VYsize; ) {
561 VGLSetSegment(offset);
562 len = min(object->VXsize*object->VYsize - offset,
563 VGLAdpInfo.va_window_size);
564 memset(object->Bitmap, (byte)color, len);
567 outb(0x3ce, 0x05); outb(0x3cf, 0x00);
581 for (i=0; i<256; i++) {
582 outb(0x3C9, VGLSavePaletteRed[i]);
584 outb(0x3C9, VGLSavePaletteGreen[i]);
586 outb(0x3C9, VGLSavePaletteBlue[i]);
601 for (i=0; i<256; i++) {
602 VGLSavePaletteRed[i] = inb(0x3C9);
604 VGLSavePaletteGreen[i] = inb(0x3C9);
606 VGLSavePaletteBlue[i] = inb(0x3C9);
614 VGLSetPalette(byte *red, byte *green, byte *blue)
618 for (i=0; i<256; i++) {
619 VGLSavePaletteRed[i] = red[i];
620 VGLSavePaletteGreen[i] = green[i];
621 VGLSavePaletteBlue[i] = blue[i];
627 for (i=0; i<256; i++) {
628 outb(0x3C9, VGLSavePaletteRed[i]);
630 outb(0x3C9, VGLSavePaletteGreen[i]);
632 outb(0x3C9, VGLSavePaletteBlue[i]);
640 VGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
642 VGLSavePaletteRed[color] = red;
643 VGLSavePaletteGreen[color] = green;
644 VGLSavePaletteBlue[color] = blue;
649 outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
655 VGLSetBorder(byte color)
659 outb(0x3C0,0x11); outb(0x3C0, color);
665 VGLBlankDisplay(int blank)
670 outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
671 outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));