xref: /freebsd/lib/libvgl/bitmap.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
1 /*-
2  * Copyright (c) 1991-1997 S�ren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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 withough specific prior written permission.
16  *
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.
27  *
28  *  $Id: bitmap.c,v 1.8 1997/08/15 12:32:59 sos Exp $
29  */
30 
31 #include <sys/types.h>
32 #include <signal.h>
33 #include "vgl.h"
34 
35 static byte VGLPlane[4][128];
36 static byte mask[8] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
37 static int color2bit[16] = {0x00000000, 0x00000001, 0x00000100, 0x00000101,
38 			    0x00010000, 0x00010001, 0x00010100, 0x00010101,
39 			    0x01000000, 0x01000001, 0x01000100, 0x01000101,
40 			    0x01010000, 0x01010001, 0x01010100, 0x01010101};
41 
42 static void
43 WriteVerticalLine(VGLBitmap *dst, int x, int y, int width, byte *line)
44 {
45   int i, pos, last, planepos, start_offset, end_offset, offset;
46   unsigned int word = 0;
47   byte *address;
48 
49   switch (dst->Type) {
50   case VIDBUF4:
51     address = dst->Bitmap + (dst->Xsize/8 * y) + x/8;
52     start_offset = (x & 0x07);
53     end_offset = (x + width) & 0x07;
54     offset = start_offset;
55     pos = 0;
56     planepos = 0;
57     while (pos < width) {
58       word = 0;
59       last = pos + 8 - offset;
60       while (pos < last && pos < width)
61 	word = (word<<1) | color2bit[line[pos++]&0x0f];
62       VGLPlane[0][planepos] = word;
63       VGLPlane[1][planepos] = word>>8;
64       VGLPlane[2][planepos] = word>>16;
65       VGLPlane[3][planepos] = word>>24;
66       planepos++;
67       offset = 0;
68     }
69     planepos--;
70     if (end_offset) {
71       word <<= (8 - end_offset);
72       VGLPlane[0][planepos] = word;
73       VGLPlane[1][planepos] = word>>8;
74       VGLPlane[2][planepos] = word>>16;
75       VGLPlane[3][planepos] = word>>24;
76     }
77     if (start_offset || end_offset)
78       width+=8;
79     for (i=0; i<4; i++) {
80       outb(0x3c4, 0x02);
81       outb(0x3c5, 0x01<<i);
82       outb(0x3ce, 0x04);
83       outb(0x3cf, i);
84       if (start_offset)
85 	VGLPlane[i][0] |= *address & ~mask[start_offset];
86       if (end_offset)
87 	VGLPlane[i][planepos] |= *(address + planepos) & mask[end_offset];
88       bcopy(&VGLPlane[i][0], address, width/8);
89     }
90     break;
91   case VIDBUF8X:
92     address = dst->Bitmap + ((dst->Xsize * y) + x)/2;
93     for (i=0; i<4; i++) {
94       outb(0x3c4, 0x02);
95       outb(0x3c5, 0x01<<i);
96       pos = i;
97       for (planepos=0; planepos<width/4; planepos++, pos+=4)
98         address[planepos] = line[pos];
99     }
100     break;
101   case VIDBUF8:
102   case MEMBUF:
103     address = dst->Bitmap + (dst->Xsize * y) + x;
104     bcopy(line, address, width);
105     break;
106 
107   default:
108   }
109 }
110 
111 static void
112 ReadVerticalLine(VGLBitmap *src, int x, int y, int width, byte *line)
113 {
114   int i, bit, pos, count, planepos, start_offset, end_offset, offset;
115   byte *address;
116 
117   switch (src->Type) {
118   case VIDBUF4:
119     address = src->Bitmap + (src->Xsize/8 * y) + x/8;
120     start_offset = (x & 0x07);
121     end_offset = (x + width) & 0x07;
122     offset = start_offset;
123     if (start_offset)
124 	count = (width - (8 - start_offset)) / 8 + 1;
125     else
126 	count = width / 8;
127     if (end_offset)
128 	count++;
129     for (i=0; i<4; i++) {
130       outb(0x3ce, 0x04);
131       outb(0x3cf, i);
132       bcopy(address, &VGLPlane[i][0], count);
133     }
134     pos = 0;
135     planepos = 0;
136     while (pos < width) {
137       for (bit = (7-offset); bit >= 0 && pos < width; bit--, pos++) {
138         line[pos] = (VGLPlane[0][planepos] & (1<<bit) ? 1 : 0) |
139                     ((VGLPlane[1][planepos] & (1<<bit) ? 1 : 0) << 1) |
140                     ((VGLPlane[2][planepos] & (1<<bit) ? 1 : 0) << 2) |
141                     ((VGLPlane[3][planepos] & (1<<bit) ? 1 : 0) << 3);
142       }
143       planepos++;
144       offset = 0;
145     }
146     break;
147   case VIDBUF8X:
148     address = src->Bitmap + ((src->Xsize * y) + x)/2;
149     for (i=0; i<4; i++) {
150       outb(0x3ce, 0x04);
151       outb(0x3cf, i);
152       pos = i;
153       for (planepos=0; planepos<width/4; planepos++, pos+=4)
154         line[pos] = address[planepos];
155     }
156     break;
157   case VIDBUF8:
158   case MEMBUF:
159     address = src->Bitmap + (src->Xsize * y) + x;
160     bcopy(address, line, width);
161     break;
162   default:
163   }
164 }
165 
166 int
167 __VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
168 	      VGLBitmap *dst, int dstx, int dsty, int width, int hight)
169 {
170   int srcline, dstline;
171 
172   if (srcx>src->Xsize||srcy>src->Ysize||dsty>dst->Xsize||dsty>dst->Ysize)
173     return -1;
174   if (srcx < 0) {
175     width=width+srcx; dstx-=srcx; srcx=0;
176   }
177   if (srcy < 0) {
178     hight=hight+srcy; dsty-=srcy; srcy=0;
179   }
180   if (dstx < 0) {
181     width=width+dstx; srcx-=dstx; dstx=0;
182   }
183   if (dsty < 0) {
184     hight=hight+dsty; srcy-=dsty; dsty=0;
185   }
186   if (srcx+width > src->Xsize)
187      width=src->Xsize-srcx;
188   if (srcy+hight > src->Ysize)
189      hight=src->Ysize-srcy;
190   if (dstx+width > dst->Xsize)
191      width=dst->Xsize-dstx;
192   if (dsty+hight > dst->Ysize)
193      hight=dst->Ysize-dsty;
194   if (width < 0 || hight < 0)
195      return -1;
196   if (src->Type == MEMBUF) {
197     for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
198       WriteVerticalLine(dst, dstx, dstline, width,
199 	(src->Bitmap+(srcline*src->Xsize)+srcx));
200     }
201   }
202   else if (dst->Type == MEMBUF) {
203     for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
204       ReadVerticalLine(src, srcx, srcline, width,
205 	 (dst->Bitmap+(dstline*dst->Xsize)+dstx));
206     }
207   }
208   else {
209     byte buffer[1024];
210     for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
211       ReadVerticalLine(src, srcx, srcline, width, buffer);
212       WriteVerticalLine(dst, dstx, dstline, width, buffer);
213     }
214   }
215   return 0;
216 }
217 
218 int
219 VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
220 	      VGLBitmap *dst, int dstx, int dsty, int width, int hight)
221 {
222   int error;
223 
224   VGLMouseFreeze(dstx, dsty, width, hight, 0);
225   error = __VGLBitmapCopy(src, srcx, srcy, dst, dstx, dsty, width, hight);
226   VGLMouseUnFreeze();
227   return error;
228 }
229 
230