xref: /freebsd/lib/libvgl/bitmap.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991-1997 Søren Schmidt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/types.h>
35 #include <signal.h>
36 #include <sys/fbio.h>
37 #include "vgl.h"
38 
39 #define min(x, y)	(((x) < (y)) ? (x) : (y))
40 
41 static byte mask[8] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
42 static int color2bit[16] = {0x00000000, 0x00000001, 0x00000100, 0x00000101,
43 			    0x00010000, 0x00010001, 0x00010100, 0x00010101,
44 			    0x01000000, 0x01000001, 0x01000100, 0x01000101,
45 			    0x01010000, 0x01010001, 0x01010100, 0x01010101};
46 
47 static void
48 WriteVerticalLine(VGLBitmap *dst, int x, int y, int width, byte *line)
49 {
50   int bwidth, i, pos, last, planepos, start_offset, end_offset, offset;
51   int len;
52   unsigned int word = 0;
53   byte *address;
54   byte *VGLPlane[4];
55 
56   switch (dst->Type) {
57   case VIDBUF4:
58   case VIDBUF4S:
59     start_offset = (x & 0x07);
60     end_offset = (x + width) & 0x07;
61     bwidth = (width + start_offset) / 8;
62     if (end_offset)
63 	bwidth++;
64     VGLPlane[0] = VGLBuf;
65     VGLPlane[1] = VGLPlane[0] + bwidth;
66     VGLPlane[2] = VGLPlane[1] + bwidth;
67     VGLPlane[3] = VGLPlane[2] + bwidth;
68     pos = 0;
69     planepos = 0;
70     last = 8 - start_offset;
71     while (pos < width) {
72       word = 0;
73       while (pos < last && pos < width)
74 	word = (word<<1) | color2bit[line[pos++]&0x0f];
75       VGLPlane[0][planepos] = word;
76       VGLPlane[1][planepos] = word>>8;
77       VGLPlane[2][planepos] = word>>16;
78       VGLPlane[3][planepos] = word>>24;
79       planepos++;
80       last += 8;
81     }
82     planepos--;
83     if (end_offset) {
84       word <<= (8 - end_offset);
85       VGLPlane[0][planepos] = word;
86       VGLPlane[1][planepos] = word>>8;
87       VGLPlane[2][planepos] = word>>16;
88       VGLPlane[3][planepos] = word>>24;
89     }
90     outb(0x3ce, 0x01); outb(0x3cf, 0x00);		/* set/reset enable */
91     outb(0x3ce, 0x08); outb(0x3cf, 0xff);		/* bit mask */
92     for (i=0; i<4; i++) {
93       outb(0x3c4, 0x02);
94       outb(0x3c5, 0x01<<i);
95       outb(0x3ce, 0x04);
96       outb(0x3cf, i);
97       pos = VGLAdpInfo.va_line_width*y + x/8;
98       if (dst->Type == VIDBUF4) {
99 	if (end_offset)
100 	  VGLPlane[i][planepos] |= dst->Bitmap[pos+planepos] & mask[end_offset];
101 	if (start_offset)
102 	  VGLPlane[i][0] |= dst->Bitmap[pos] & ~mask[start_offset];
103 	bcopy(&VGLPlane[i][0], dst->Bitmap + pos, bwidth);
104       } else {	/* VIDBUF4S */
105 	if (end_offset) {
106 	  offset = VGLSetSegment(pos + planepos);
107 	  VGLPlane[i][planepos] |= dst->Bitmap[offset] & mask[end_offset];
108 	}
109 	offset = VGLSetSegment(pos);
110 	if (start_offset)
111 	  VGLPlane[i][0] |= dst->Bitmap[offset] & ~mask[start_offset];
112 	for (last = bwidth; ; ) {
113 	  len = min(VGLAdpInfo.va_window_size - offset, last);
114 	  bcopy(&VGLPlane[i][bwidth - last], dst->Bitmap + offset, len);
115 	  pos += len;
116 	  last -= len;
117 	  if (last <= 0)
118 	    break;
119 	  offset = VGLSetSegment(pos);
120 	}
121       }
122     }
123     break;
124   case VIDBUF8X:
125     address = dst->Bitmap + VGLAdpInfo.va_line_width * y + x/4;
126     for (i=0; i<4; i++) {
127       outb(0x3c4, 0x02);
128       outb(0x3c5, 0x01 << ((x + i)%4));
129       for (planepos=0, pos=i; pos<width; planepos++, pos+=4)
130         address[planepos] = line[pos];
131       if ((x + i)%4 == 3)
132 	++address;
133     }
134     break;
135   case VIDBUF8S:
136   case VIDBUF16S:
137   case VIDBUF24S:
138   case VIDBUF32S:
139     width = width * dst->PixelBytes;
140     pos = (dst->VXsize * y + x) * dst->PixelBytes;
141     while (width > 0) {
142       offset = VGLSetSegment(pos);
143       i = min(VGLAdpInfo.va_window_size - offset, width);
144       bcopy(line, dst->Bitmap + offset, i);
145       line += i;
146       pos += i;
147       width -= i;
148     }
149     break;
150   case MEMBUF:
151   case VIDBUF8:
152   case VIDBUF16:
153   case VIDBUF24:
154   case VIDBUF32:
155     address = dst->Bitmap + (dst->VXsize * y + x) * dst->PixelBytes;
156     bcopy(line, address, width * dst->PixelBytes);
157     break;
158   default:
159     ;
160   }
161 }
162 
163 int
164 __VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
165 	      VGLBitmap *dst, int dstx, int dsty, int width, int hight)
166 {
167   byte *buffer, *p;
168   int mousemerge, srcline, dstline, yend, yextra, ystep;
169 
170   mousemerge = 0;
171   if (hight < 0) {
172     hight = -hight;
173     mousemerge = (dst == VGLDisplay &&
174 		  VGLMouseOverlap(dstx, dsty, width, hight));
175     if (mousemerge)
176       buffer = alloca(width*src->PixelBytes);
177   }
178   if (srcx>src->VXsize || srcy>src->VYsize
179 	|| dstx>dst->VXsize || dsty>dst->VYsize)
180     return -1;
181   if (srcx < 0) {
182     width=width+srcx; dstx-=srcx; srcx=0;
183   }
184   if (srcy < 0) {
185     hight=hight+srcy; dsty-=srcy; srcy=0;
186   }
187   if (dstx < 0) {
188     width=width+dstx; srcx-=dstx; dstx=0;
189   }
190   if (dsty < 0) {
191     hight=hight+dsty; srcy-=dsty; dsty=0;
192   }
193   if (srcx+width > src->VXsize)
194      width=src->VXsize-srcx;
195   if (srcy+hight > src->VYsize)
196      hight=src->VYsize-srcy;
197   if (dstx+width > dst->VXsize)
198      width=dst->VXsize-dstx;
199   if (dsty+hight > dst->VYsize)
200      hight=dst->VYsize-dsty;
201   if (width < 0 || hight < 0)
202      return -1;
203   yend = srcy + hight;
204   yextra = 0;
205   ystep = 1;
206   if (src->Bitmap == dst->Bitmap && srcy < dsty) {
207     yend = srcy - 1;
208     yextra = hight - 1;
209     ystep = -1;
210   }
211   for (srcline = srcy + yextra, dstline = dsty + yextra; srcline != yend;
212        srcline += ystep, dstline += ystep) {
213     p = src->Bitmap+(srcline*src->VXsize+srcx)*dst->PixelBytes;
214     if (mousemerge && VGLMouseOverlap(dstx, dstline, width, 1)) {
215       bcopy(p, buffer, width*src->PixelBytes);
216       p = buffer;
217       VGLMouseMerge(dstx, dstline, width, p);
218     }
219     WriteVerticalLine(dst, dstx, dstline, width, p);
220   }
221   return 0;
222 }
223 
224 int
225 VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
226 	      VGLBitmap *dst, int dstx, int dsty, int width, int hight)
227 {
228   int error;
229 
230   if (hight < 0)
231     return -1;
232   if (src == VGLDisplay)
233     src = &VGLVDisplay;
234   if (src->Type != MEMBUF)
235     return -1;		/* invalid */
236   if (dst == VGLDisplay) {
237     VGLMouseFreeze();
238     __VGLBitmapCopy(src, srcx, srcy, &VGLVDisplay, dstx, dsty, width, hight);
239     error = __VGLBitmapCopy(src, srcx, srcy, &VGLVDisplay, dstx, dsty,
240                             width, hight);
241     if (error != 0)
242       return error;
243     src = &VGLVDisplay;
244     srcx = dstx;
245     srcy = dsty;
246   } else if (dst->Type != MEMBUF)
247     return -1;		/* invalid */
248   error = __VGLBitmapCopy(src, srcx, srcy, dst, dstx, dsty, width, -hight);
249   if (dst == VGLDisplay)
250     VGLMouseUnFreeze();
251   return error;
252 }
253 
254 VGLBitmap
255 *VGLBitmapCreate(int type, int xsize, int ysize, byte *bits)
256 {
257   VGLBitmap *object;
258 
259   if (type != MEMBUF)
260     return NULL;
261   if (xsize < 0 || ysize < 0)
262     return NULL;
263   object = (VGLBitmap *)malloc(sizeof(*object));
264   if (object == NULL)
265     return NULL;
266   object->Type = type;
267   object->Xsize = xsize;
268   object->Ysize = ysize;
269   object->VXsize = xsize;
270   object->VYsize = ysize;
271   object->Xorigin = 0;
272   object->Yorigin = 0;
273   object->Bitmap = bits;
274   object->PixelBytes = VGLDisplay->PixelBytes;
275   return object;
276 }
277 
278 void
279 VGLBitmapDestroy(VGLBitmap *object)
280 {
281   if (object->Bitmap)
282     free(object->Bitmap);
283   free(object);
284 }
285 
286 int
287 VGLBitmapAllocateBits(VGLBitmap *object)
288 {
289   object->Bitmap = malloc(object->VXsize*object->VYsize*object->PixelBytes);
290   if (object->Bitmap == NULL)
291     return -1;
292   return 0;
293 }
294 
295 void
296 VGLBitmapCvt(VGLBitmap *src, VGLBitmap *dst)
297 {
298   u_long color;
299   int dstpos, i, pb, size, srcpb, srcpos;
300 
301   size = src->VXsize * src->VYsize;
302   srcpb = src->PixelBytes;
303   if (srcpb <= 0)
304     srcpb = 1;
305   pb = dst->PixelBytes;
306   if (pb == srcpb) {
307     bcopy(src->Bitmap, dst->Bitmap, size * pb);
308     return;
309   }
310   if (srcpb != 1)
311     return;		/* not supported */
312   for (srcpos = dstpos = 0; srcpos < size; srcpos++) {
313     color = VGLrgb332ToNative(src->Bitmap[srcpos]);
314     for (i = 0; i < pb; i++, color >>= 8)
315         dst->Bitmap[dstpos++] = color;
316   }
317 }
318