xref: /freebsd/lib/libvgl/bitmap.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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  * $FreeBSD$
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/2 * y) + x/4;
93     for (i=0; i<4; i++) {
94       outb(0x3c4, 0x02);
95       outb(0x3c5, 0x01 << ((x + i)%4));
96       for (planepos=0, pos=i; pos<width; planepos++, pos+=4)
97         address[planepos] = line[pos];
98       if ((x + i)%4 == 3)
99 	++address;
100     }
101     break;
102   case VIDBUF8:
103   case MEMBUF:
104     address = dst->Bitmap + (dst->Xsize * y) + x;
105     bcopy(line, address, width);
106     break;
107 
108   default:
109   }
110 }
111 
112 static void
113 ReadVerticalLine(VGLBitmap *src, int x, int y, int width, byte *line)
114 {
115   int i, bit, pos, count, planepos, start_offset, end_offset, offset;
116   byte *address;
117 
118   switch (src->Type) {
119   case VIDBUF4:
120     address = src->Bitmap + (src->Xsize/8 * y) + x/8;
121     start_offset = (x & 0x07);
122     end_offset = (x + width) & 0x07;
123     offset = start_offset;
124     if (start_offset)
125 	count = (width - (8 - start_offset)) / 8 + 1;
126     else
127 	count = width / 8;
128     if (end_offset)
129 	count++;
130     for (i=0; i<4; i++) {
131       outb(0x3ce, 0x04);
132       outb(0x3cf, i);
133       bcopy(address, &VGLPlane[i][0], count);
134     }
135     pos = 0;
136     planepos = 0;
137     while (pos < width) {
138       for (bit = (7-offset); bit >= 0 && pos < width; bit--, pos++) {
139         line[pos] = (VGLPlane[0][planepos] & (1<<bit) ? 1 : 0) |
140                     ((VGLPlane[1][planepos] & (1<<bit) ? 1 : 0) << 1) |
141                     ((VGLPlane[2][planepos] & (1<<bit) ? 1 : 0) << 2) |
142                     ((VGLPlane[3][planepos] & (1<<bit) ? 1 : 0) << 3);
143       }
144       planepos++;
145       offset = 0;
146     }
147     break;
148   case VIDBUF8X:
149     address = src->Bitmap + (src->Xsize/2 * y) + x/4;
150     for (i=0; i<4; i++) {
151       outb(0x3ce, 0x04);
152       outb(0x3cf, (x + i)%4);
153       for (planepos=0, pos=i; pos<width; planepos++, pos+=4)
154         line[pos] = address[planepos];
155       if ((x + i)%4 == 3)
156 	++address;
157     }
158     break;
159   case VIDBUF8:
160   case MEMBUF:
161     address = src->Bitmap + (src->Xsize * y) + x;
162     bcopy(address, line, width);
163     break;
164   default:
165   }
166 }
167 
168 int
169 __VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
170 	      VGLBitmap *dst, int dstx, int dsty, int width, int hight)
171 {
172   int srcline, dstline;
173 
174   if (srcx>src->Xsize||srcy>src->Ysize||dstx>dst->Xsize||dsty>dst->Ysize)
175     return -1;
176   if (srcx < 0) {
177     width=width+srcx; dstx-=srcx; srcx=0;
178   }
179   if (srcy < 0) {
180     hight=hight+srcy; dsty-=srcy; srcy=0;
181   }
182   if (dstx < 0) {
183     width=width+dstx; srcx-=dstx; dstx=0;
184   }
185   if (dsty < 0) {
186     hight=hight+dsty; srcy-=dsty; dsty=0;
187   }
188   if (srcx+width > src->Xsize)
189      width=src->Xsize-srcx;
190   if (srcy+hight > src->Ysize)
191      hight=src->Ysize-srcy;
192   if (dstx+width > dst->Xsize)
193      width=dst->Xsize-dstx;
194   if (dsty+hight > dst->Ysize)
195      hight=dst->Ysize-dsty;
196   if (width < 0 || hight < 0)
197      return -1;
198   if (src->Type == MEMBUF) {
199     for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
200       WriteVerticalLine(dst, dstx, dstline, width,
201 	(src->Bitmap+(srcline*src->Xsize)+srcx));
202     }
203   }
204   else if (dst->Type == MEMBUF) {
205     for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
206       ReadVerticalLine(src, srcx, srcline, width,
207 	 (dst->Bitmap+(dstline*dst->Xsize)+dstx));
208     }
209   }
210   else {
211     byte buffer[1024];
212     for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
213       ReadVerticalLine(src, srcx, srcline, width, buffer);
214       WriteVerticalLine(dst, dstx, dstline, width, buffer);
215     }
216   }
217   return 0;
218 }
219 
220 int
221 VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
222 	      VGLBitmap *dst, int dstx, int dsty, int width, int hight)
223 {
224   int error;
225 
226   VGLMouseFreeze(dstx, dsty, width, hight, 0);
227   error = __VGLBitmapCopy(src, srcx, srcy, dst, dstx, dsty, width, hight);
228   VGLMouseUnFreeze();
229   return error;
230 }
231 
232