2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991-1997 Søren Schmidt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
38 static byte VGLSavePaletteRed[256];
39 static byte VGLSavePaletteGreen[256];
40 static byte VGLSavePaletteBlue[256];
42 #define ABS(a) (((a)<0) ? -(a) : (a))
43 #define SGN(a) (((a)<0) ? -1 : 1)
44 #define min(x, y) (((x) < (y)) ? (x) : (y))
45 #define max(x, y) (((x) > (y)) ? (x) : (y))
48 color2mem(u_long color, byte *b, int len)
52 b[3] = (color >> 24) & 0xff;
55 b[2] = (color >> 16) & 0xff;
58 b[1] = (color >> 8) & 0xff;
70 mem2color(byte *b, int len)
76 color |= (b[3] & 0xff) << 24;
79 color |= (b[2] & 0xff) << 16;
82 color |= (b[1] & 0xff) << 8;
86 color |= (b[0] & 0xff);
94 VGLSetXY(VGLBitmap *object, int x, int y, u_long color)
100 if (x>=0 && x<object->VXsize && y>=0 && y<object->VYsize) {
101 if (!VGLMouseFreeze(x, y, 1, 1, color)) {
102 switch (object->Type) {
105 object->Bitmap[y*object->VXsize+x]=((byte)color);
108 object->Bitmap[VGLSetSegment(y*object->VXsize+x)]=((byte)color);
113 color2mem(color, b, object->PixelBytes);
114 bcopy(b, &object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
120 color2mem(color, b, object->PixelBytes);
121 offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
122 bcopy(b, &object->Bitmap[offset], object->PixelBytes);
126 outb(0x3c5, 0x01 << (x&0x3));
127 object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)] = ((byte)color);
130 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
133 offset = y*VGLAdpInfo.va_line_width + x/8;
135 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
136 outb(0x3ce, 0x00); outb(0x3cf, (byte)color & 0x0f); /* set/reset */
137 outb(0x3ce, 0x01); outb(0x3cf, 0x0f); /* set/reset enable */
138 outb(0x3ce, 0x08); outb(0x3cf, 0x80 >> (x%8)); /* bit mask */
139 object->Bitmap[offset] |= (byte)color;
147 VGLGetXY(VGLBitmap *object, int x, int y)
158 if (x<0 || x>=object->VXsize || y<0 || y>=object->VYsize)
160 switch (object->Type) {
163 return object->Bitmap[((y*object->VXsize)+x)];
165 return object->Bitmap[VGLSetSegment(y*object->VXsize+x)];
169 bcopy(&object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
170 b, object->PixelBytes);
171 return (mem2color(b, object->PixelBytes));
175 offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
176 bcopy(&object->Bitmap[offset], b, object->PixelBytes);
178 return (mem2color(b, object->PixelBytes));
180 outb(0x3ce, 0x04); outb(0x3cf, x & 0x3);
181 return object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)];
183 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
186 offset = y*VGLAdpInfo.va_line_width + x/8;
189 return (object->Bitmap[offset]&(0x80>>(x%8))) ? 1 : 0; /* XXX */
192 mask = 0x80 >> (x%8);
193 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
194 outb(0x3ce, 0x04); outb(0x3cf, i);
195 color |= (object->Bitmap[offset] & mask) ? (1 << i) : 0;
200 return 0; /* XXX black? */
204 * Symmetric Double Step Line Algorithm by Brian Wyvill from
205 * "Graphics Gems", Academic Press, 1990.
208 #define SL_SWAP(a,b) {a^=b; b^=a; a^=b;}
209 #define SL_ABSOLUTE(i,j,k) ( (i-j)*(k = ( (i-j)<0 ? -1 : 1)))
212 plot(VGLBitmap * object, int x, int y, int flag, byte color)
214 /* non-zero flag indicates the pixels need swapping back. */
216 VGLSetXY(object, y, x, color);
218 VGLSetXY(object, x, y, color);
223 VGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
225 int dx, dy, incr1, incr2, D, x, y, xend, c, pixels_left;
226 int sign_x, sign_y, step, reverse, i;
228 dx = SL_ABSOLUTE(x2, x1, sign_x);
229 dy = SL_ABSOLUTE(y2, y1, sign_y);
230 /* decide increment sign by the slope sign */
231 if (sign_x == sign_y)
236 if (dy > dx) { /* chooses axis of greatest movement (make dx) */
243 /* note error check for dx==0 should be included here */
244 if (x1 > x2) { /* start from the smaller coordinate */
257 /* Note dx=n implies 0 - n or (dx+1) pixels to be set */
258 /* Go round loop dx/4 times then plot last 0,1,2 or 3 pixels */
259 /* In fact (dx-1)/4 as 2 pixels are already plotted */
261 pixels_left = (dx - 1) % 4; /* number of pixels left over at the
263 plot(object, x, y, reverse, color);
265 return; /* plot only one pixel for zero length
267 plot(object, x1, y1, reverse, color); /* plot first two points */
268 incr2 = 4 * dy - 2 * dx;
269 if (incr2 < 0) { /* slope less than 1/2 */
274 for (i = 0; i < xend; i++) { /* plotting loop */
278 /* pattern 1 forwards */
279 plot(object, x, y, reverse, color);
280 plot(object, ++x, y, reverse, color);
281 /* pattern 1 backwards */
282 plot(object, x1, y1, reverse, color);
283 plot(object, --x1, y1, reverse, color);
287 /* pattern 2 forwards */
288 plot(object, x, y, reverse, color);
289 plot(object, ++x, y += step, reverse,
291 /* pattern 2 backwards */
292 plot(object, x1, y1, reverse, color);
293 plot(object, --x1, y1 -= step, reverse,
296 /* pattern 3 forwards */
297 plot(object, x, y += step, reverse, color);
298 plot(object, ++x, y, reverse, color);
299 /* pattern 3 backwards */
300 plot(object, x1, y1 -= step, reverse,
302 plot(object, --x1, y1, reverse, color);
308 /* plot last pattern */
311 plot(object, ++x, y, reverse, color); /* pattern 1 */
313 plot(object, ++x, y, reverse, color);
315 plot(object, --x1, y1, reverse, color);
318 plot(object, ++x, y, reverse, color); /* pattern 2 */
320 plot(object, ++x, y += step, reverse, color);
322 plot(object, --x1, y1, reverse, color);
325 plot(object, ++x, y += step, reverse, color);
327 plot(object, ++x, y, reverse, color);
329 plot(object, --x1, y1 -= step, reverse, color);
332 } /* end if pixels_left */
334 /* end slope < 1/2 */
335 else { /* slope greater than 1/2 */
339 for (i = 0; i < xend; i++) {
343 /* pattern 4 forwards */
344 plot(object, x, y += step, reverse, color);
345 plot(object, ++x, y += step, reverse, color);
346 /* pattern 4 backwards */
347 plot(object, x1, y1 -= step, reverse, color);
348 plot(object, --x1, y1 -= step, reverse, color);
352 /* pattern 2 forwards */
353 plot(object, x, y, reverse, color);
354 plot(object, ++x, y += step, reverse,
357 /* pattern 2 backwards */
358 plot(object, x1, y1, reverse, color);
359 plot(object, --x1, y1 -= step, reverse,
362 /* pattern 3 forwards */
363 plot(object, x, y += step, reverse, color);
364 plot(object, ++x, y, reverse, color);
365 /* pattern 3 backwards */
366 plot(object, x1, y1 -= step, reverse, color);
367 plot(object, --x1, y1, reverse, color);
372 /* plot last pattern */
375 plot(object, ++x, y += step, reverse, color); /* pattern 4 */
377 plot(object, ++x, y += step, reverse,
380 plot(object, --x1, y1 -= step, reverse,
384 plot(object, ++x, y, reverse, color); /* pattern 2 */
386 plot(object, ++x, y += step, reverse, color);
388 plot(object, --x1, y1, reverse, color);
391 plot(object, ++x, y += step, reverse, color);
393 plot(object, ++x, y, reverse, color);
394 if (pixels_left > 2) {
395 if (D > c) /* step 3 */
396 plot(object, --x1, y1 -= step, reverse, color);
398 plot(object, --x1, y1, reverse, color);
407 VGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
409 VGLLine(object, x1, y1, x2, y1, color);
410 VGLLine(object, x2, y1, x2, y2, color);
411 VGLLine(object, x2, y2, x1, y2, color);
412 VGLLine(object, x1, y2, x1, y1, color);
416 VGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
420 for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
424 set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
427 VGLSetXY(object, xc+x, yc+y, color);
428 VGLSetXY(object, xc-x, yc+y, color);
430 VGLSetXY(object, xc+x, yc-y, color);
431 VGLSetXY(object, xc-x, yc-y, color);
435 VGLSetXY(object, xc, yc+y, color);
437 VGLSetXY(object, xc, yc-y, color);
442 VGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
444 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
445 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
448 set4pixels(object, x, y, xc, yc, color);
450 y--; dy-=asq2; d-=dy;
452 x++; dx+=bsq2; d+=bsq+dx;
454 d+=(3*(asq-bsq)/2-(dx+dy))/2;
456 set4pixels(object, x, y, xc, yc, color);
458 x++; dx+=bsq2; d+=dx;
460 y--; dy-=asq2; d+=asq-dy;
465 set2lines(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
468 VGLLine(object, xc+x, yc+y, xc-x, yc+y, color);
470 VGLLine(object, xc+x, yc-y, xc-x, yc-y, color);
473 VGLLine(object, xc, yc+y, xc, yc-y, color);
478 VGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
480 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
481 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
484 set2lines(object, x, y, xc, yc, color);
486 y--; dy-=asq2; d-=dy;
488 x++; dx+=bsq2; d+=bsq+dx;
490 d+=(3*(asq-bsq)/2-(dx+dy))/2;
492 set2lines(object, x, y, xc, yc, color);
494 x++; dx+=bsq2; d+=dx;
496 y--; dy-=asq2; d+=asq-dy;
501 VGLClear(VGLBitmap *object, u_long color)
509 VGLMouseFreeze(0, 0, object->Xsize, object->Ysize, color); /* XXX */
510 switch (object->Type) {
513 memset(object->Bitmap, (byte)color, object->VXsize*object->VYsize);
517 for (offset = 0; offset < object->VXsize*object->VYsize; ) {
518 VGLSetSegment(offset);
519 len = min(object->VXsize*object->VYsize - offset,
520 VGLAdpInfo.va_window_size);
521 memset(object->Bitmap, (byte)color, len);
528 color2mem(color, b, object->PixelBytes);
529 total = object->VXsize*object->VYsize*object->PixelBytes;
530 for (i = 0; i < total; i += object->PixelBytes)
531 bcopy(b, object->Bitmap + i, object->PixelBytes);
537 color2mem(color, b, object->PixelBytes);
538 total = object->VXsize*object->VYsize*object->PixelBytes;
539 for (offset = 0; offset < total; ) {
540 VGLSetSegment(offset);
541 len = min(total - offset, VGLAdpInfo.va_window_size);
542 for (i = 0; i < len; i += object->PixelBytes)
543 bcopy(b, object->Bitmap + offset + i, object->PixelBytes);
549 /* XXX works only for Xsize % 4 = 0 */
551 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
552 memset(object->Bitmap, (byte)color, VGLAdpInfo.va_line_width*object->VYsize);
557 /* XXX works only for Xsize % 8 = 0 */
558 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
559 outb(0x3ce, 0x05); outb(0x3cf, 0x02); /* mode 2 */
560 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
561 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
562 for (offset = 0; offset < VGLAdpInfo.va_line_width*object->VYsize; ) {
563 VGLSetSegment(offset);
564 len = min(object->VXsize*object->VYsize - offset,
565 VGLAdpInfo.va_window_size);
566 memset(object->Bitmap, (byte)color, len);
569 outb(0x3ce, 0x05); outb(0x3cf, 0x00);
583 for (i=0; i<256; i++) {
584 outb(0x3C9, VGLSavePaletteRed[i]);
586 outb(0x3C9, VGLSavePaletteGreen[i]);
588 outb(0x3C9, VGLSavePaletteBlue[i]);
603 for (i=0; i<256; i++) {
604 VGLSavePaletteRed[i] = inb(0x3C9);
606 VGLSavePaletteGreen[i] = inb(0x3C9);
608 VGLSavePaletteBlue[i] = inb(0x3C9);
616 VGLSetPalette(byte *red, byte *green, byte *blue)
620 for (i=0; i<256; i++) {
621 VGLSavePaletteRed[i] = red[i];
622 VGLSavePaletteGreen[i] = green[i];
623 VGLSavePaletteBlue[i] = blue[i];
629 for (i=0; i<256; i++) {
630 outb(0x3C9, VGLSavePaletteRed[i]);
632 outb(0x3C9, VGLSavePaletteGreen[i]);
634 outb(0x3C9, VGLSavePaletteBlue[i]);
642 VGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
644 VGLSavePaletteRed[color] = red;
645 VGLSavePaletteGreen[color] = green;
646 VGLSavePaletteBlue[color] = blue;
651 outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
657 VGLSetBorder(byte color)
661 outb(0x3C0,0x11); outb(0x3C0, color);
667 VGLBlankDisplay(int blank)
672 outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
673 outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));