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 withough 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.
28 * $FreeBSD: src/lib/libvgl/bitmap.c,v 1.4 1999/11/08 11:37:39 yokota Exp $
31 #include <sys/types.h>
33 #include <machine/console.h>
36 #define min(x, y) (((x) < (y)) ? (x) : (y))
38 static byte mask[8] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
39 static int color2bit[16] = {0x00000000, 0x00000001, 0x00000100, 0x00000101,
40 0x00010000, 0x00010001, 0x00010100, 0x00010101,
41 0x01000000, 0x01000001, 0x01000100, 0x01000101,
42 0x01010000, 0x01010001, 0x01010100, 0x01010101};
45 WriteVerticalLine(VGLBitmap *dst, int x, int y, int width, byte *line)
47 int i, pos, last, planepos, start_offset, end_offset, offset;
49 unsigned int word = 0;
56 start_offset = (x & 0x07);
57 end_offset = (x + width) & 0x07;
58 i = (width + start_offset) / 8;
62 VGLPlane[1] = VGLPlane[0] + i;
63 VGLPlane[2] = VGLPlane[1] + i;
64 VGLPlane[3] = VGLPlane[2] + i;
67 last = 8 - start_offset;
70 while (pos < last && pos < width)
71 word = (word<<1) | color2bit[line[pos++]&0x0f];
72 VGLPlane[0][planepos] = word;
73 VGLPlane[1][planepos] = word>>8;
74 VGLPlane[2][planepos] = word>>16;
75 VGLPlane[3][planepos] = word>>24;
81 word <<= (8 - end_offset);
82 VGLPlane[0][planepos] = word;
83 VGLPlane[1][planepos] = word>>8;
84 VGLPlane[2][planepos] = word>>16;
85 VGLPlane[3][planepos] = word>>24;
87 if (start_offset || end_offset)
90 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
91 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
97 pos = VGLAdpInfo.va_line_width*y + x/8;
98 if (dst->Type == VIDBUF4) {
100 VGLPlane[i][planepos] |= dst->Bitmap[pos+planepos] & mask[end_offset];
102 VGLPlane[i][0] |= dst->Bitmap[pos] & ~mask[start_offset];
103 bcopy(&VGLPlane[i][0], dst->Bitmap + pos, width);
104 } else { /* VIDBUF4S */
106 offset = VGLSetSegment(pos + planepos);
107 VGLPlane[i][planepos] |= dst->Bitmap[offset] & mask[end_offset];
109 offset = VGLSetSegment(pos);
111 VGLPlane[i][0] |= dst->Bitmap[offset] & ~mask[start_offset];
112 for (last = width; ; ) {
113 len = min(VGLAdpInfo.va_window_size - offset, last);
114 bcopy(&VGLPlane[i][width - last], dst->Bitmap + offset, len);
119 offset = VGLSetSegment(pos);
125 address = dst->Bitmap + VGLAdpInfo.va_line_width * y + x/4;
126 for (i=0; i<4; i++) {
128 outb(0x3c5, 0x01 << ((x + i)%4));
129 for (planepos=0, pos=i; pos<width; planepos++, pos+=4)
130 address[planepos] = line[pos];
136 pos = dst->VXsize * y + x;
138 offset = VGLSetSegment(pos);
139 i = min(VGLAdpInfo.va_window_size - offset, width);
140 bcopy(line, dst->Bitmap + offset, i);
148 address = dst->Bitmap + dst->VXsize * y + x;
149 bcopy(line, address, width);
157 ReadVerticalLine(VGLBitmap *src, int x, int y, int width, byte *line)
159 int i, bit, pos, count, planepos, start_offset, end_offset, offset;
166 start_offset = (x & 0x07);
167 end_offset = (x + width) & 0x07;
168 count = (width + start_offset) / 8;
171 VGLPlane[0] = VGLBuf;
172 VGLPlane[1] = VGLPlane[0] + count;
173 VGLPlane[2] = VGLPlane[1] + count;
174 VGLPlane[3] = VGLPlane[2] + count;
175 for (i=0; i<4; i++) {
178 pos = VGLAdpInfo.va_line_width*y + x/8;
179 for (width2 = count; width2 > 0; ) {
180 offset = VGLSetSegment(pos);
181 len = min(VGLAdpInfo.va_window_size - offset, width2);
182 bcopy(src->Bitmap + offset, &VGLPlane[i][count - width2], len);
189 address = src->Bitmap + VGLAdpInfo.va_line_width * y + x/8;
190 start_offset = (x & 0x07);
191 end_offset = (x + width) & 0x07;
192 count = (width + start_offset) / 8;
195 VGLPlane[0] = VGLBuf;
196 VGLPlane[1] = VGLPlane[0] + count;
197 VGLPlane[2] = VGLPlane[1] + count;
198 VGLPlane[3] = VGLPlane[2] + count;
199 for (i=0; i<4; i++) {
202 bcopy(address, &VGLPlane[i][0], count);
207 bit = 7 - start_offset;
208 while (pos < width) {
209 for (; bit >= 0 && pos < width; bit--, pos++) {
210 line[pos] = (VGLPlane[0][planepos] & (1<<bit) ? 1 : 0) |
211 ((VGLPlane[1][planepos] & (1<<bit) ? 1 : 0) << 1) |
212 ((VGLPlane[2][planepos] & (1<<bit) ? 1 : 0) << 2) |
213 ((VGLPlane[3][planepos] & (1<<bit) ? 1 : 0) << 3);
220 address = src->Bitmap + VGLAdpInfo.va_line_width * y + x/4;
221 for (i=0; i<4; i++) {
223 outb(0x3cf, (x + i)%4);
224 for (planepos=0, pos=i; pos<width; planepos++, pos+=4)
225 line[pos] = address[planepos];
231 pos = src->VXsize * y + x;
233 offset = VGLSetSegment(pos);
234 i = min(VGLAdpInfo.va_window_size - offset, width);
235 bcopy(src->Bitmap + offset, line, i);
243 address = src->Bitmap + src->VXsize * y + x;
244 bcopy(address, line, width);
251 __VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
252 VGLBitmap *dst, int dstx, int dsty, int width, int hight)
254 int srcline, dstline;
256 if (srcx>src->VXsize || srcy>src->VYsize
257 || dstx>dst->VXsize || dsty>dst->VYsize)
260 width=width+srcx; dstx-=srcx; srcx=0;
263 hight=hight+srcy; dsty-=srcy; srcy=0;
266 width=width+dstx; srcx-=dstx; dstx=0;
269 hight=hight+dsty; srcy-=dsty; dsty=0;
271 if (srcx+width > src->VXsize)
272 width=src->VXsize-srcx;
273 if (srcy+hight > src->VYsize)
274 hight=src->VYsize-srcy;
275 if (dstx+width > dst->VXsize)
276 width=dst->VXsize-dstx;
277 if (dsty+hight > dst->VYsize)
278 hight=dst->VYsize-dsty;
279 if (width < 0 || hight < 0)
281 if (src->Type == MEMBUF) {
282 for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
283 WriteVerticalLine(dst, dstx, dstline, width,
284 (src->Bitmap+(srcline*src->VXsize)+srcx));
287 else if (dst->Type == MEMBUF) {
288 for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
289 ReadVerticalLine(src, srcx, srcline, width,
290 (dst->Bitmap+(dstline*dst->VXsize)+dstx));
294 byte buffer[2048]; /* XXX */
297 if (width > sizeof(buffer)) {
304 for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
305 ReadVerticalLine(src, srcx, srcline, width, p);
306 WriteVerticalLine(dst, dstx, dstline, width, p);
308 if (width > sizeof(buffer))
315 VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
316 VGLBitmap *dst, int dstx, int dsty, int width, int hight)
320 VGLMouseFreeze(dstx, dsty, width, hight, 0);
321 error = __VGLBitmapCopy(src, srcx, srcy, dst, dstx, dsty, width, hight);
327 *VGLBitmapCreate(int type, int xsize, int ysize, byte *bits)
333 if (xsize < 0 || ysize < 0)
335 object = (VGLBitmap *)malloc(sizeof(*object));
339 object->Xsize = xsize;
340 object->Ysize = ysize;
341 object->VXsize = xsize;
342 object->VYsize = ysize;
345 object->Bitmap = bits;
350 VGLBitmapDestroy(VGLBitmap *object)
353 free(object->Bitmap);
358 VGLBitmapAllocateBits(VGLBitmap *object)
360 object->Bitmap = (byte *)malloc(object->VXsize*object->VYsize);
361 if (object->Bitmap == NULL)