xref: /freebsd/lib/libvgl/bitmap.c (revision a96d3de6b30feccd00db6951976febf437460c62)
19a57b7d2SSøren Schmidt /*-
29a57b7d2SSøren Schmidt  * Copyright (c) 1991-1997 S�ren Schmidt
39a57b7d2SSøren Schmidt  * All rights reserved.
49a57b7d2SSøren Schmidt  *
59a57b7d2SSøren Schmidt  * Redistribution and use in source and binary forms, with or without
69a57b7d2SSøren Schmidt  * modification, are permitted provided that the following conditions
79a57b7d2SSøren Schmidt  * are met:
89a57b7d2SSøren Schmidt  * 1. Redistributions of source code must retain the above copyright
99a57b7d2SSøren Schmidt  *    notice, this list of conditions and the following disclaimer,
109a57b7d2SSøren Schmidt  *    in this position and unchanged.
119a57b7d2SSøren Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
129a57b7d2SSøren Schmidt  *    notice, this list of conditions and the following disclaimer in the
139a57b7d2SSøren Schmidt  *    documentation and/or other materials provided with the distribution.
149a57b7d2SSøren Schmidt  * 3. The name of the author may not be used to endorse or promote products
159a57b7d2SSøren Schmidt  *    derived from this software withough specific prior written permission.
169a57b7d2SSøren Schmidt  *
179a57b7d2SSøren Schmidt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
189a57b7d2SSøren Schmidt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199a57b7d2SSøren Schmidt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209a57b7d2SSøren Schmidt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
219a57b7d2SSøren Schmidt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
229a57b7d2SSøren Schmidt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239a57b7d2SSøren Schmidt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249a57b7d2SSøren Schmidt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259a57b7d2SSøren Schmidt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
269a57b7d2SSøren Schmidt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279a57b7d2SSøren Schmidt  */
289a57b7d2SSøren Schmidt 
29e67f5b9fSMatthew Dillon #include <sys/cdefs.h>
30e67f5b9fSMatthew Dillon __FBSDID("$FreeBSD$");
31e67f5b9fSMatthew Dillon 
329a57b7d2SSøren Schmidt #include <sys/types.h>
339a57b7d2SSøren Schmidt #include <signal.h>
3400d25f51SPoul-Henning Kamp #include <sys/fbio.h>
359a57b7d2SSøren Schmidt #include "vgl.h"
369a57b7d2SSøren Schmidt 
375acf51eaSKazutaka YOKOTA #define min(x, y)	(((x) < (y)) ? (x) : (y))
385acf51eaSKazutaka YOKOTA 
399a57b7d2SSøren Schmidt static byte mask[8] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
409a57b7d2SSøren Schmidt static int color2bit[16] = {0x00000000, 0x00000001, 0x00000100, 0x00000101,
419a57b7d2SSøren Schmidt 			    0x00010000, 0x00010001, 0x00010100, 0x00010101,
429a57b7d2SSøren Schmidt 			    0x01000000, 0x01000001, 0x01000100, 0x01000101,
439a57b7d2SSøren Schmidt 			    0x01010000, 0x01010001, 0x01010100, 0x01010101};
449a57b7d2SSøren Schmidt 
459a57b7d2SSøren Schmidt static void
469a57b7d2SSøren Schmidt WriteVerticalLine(VGLBitmap *dst, int x, int y, int width, byte *line)
479a57b7d2SSøren Schmidt {
489a57b7d2SSøren Schmidt   int i, pos, last, planepos, start_offset, end_offset, offset;
495acf51eaSKazutaka YOKOTA   int len;
509a57b7d2SSøren Schmidt   unsigned int word = 0;
519a57b7d2SSøren Schmidt   byte *address;
525acf51eaSKazutaka YOKOTA   byte *VGLPlane[4];
539a57b7d2SSøren Schmidt 
549a57b7d2SSøren Schmidt   switch (dst->Type) {
559a57b7d2SSøren Schmidt   case VIDBUF4:
565acf51eaSKazutaka YOKOTA   case VIDBUF4S:
579a57b7d2SSøren Schmidt     start_offset = (x & 0x07);
589a57b7d2SSøren Schmidt     end_offset = (x + width) & 0x07;
595acf51eaSKazutaka YOKOTA     i = (width + start_offset) / 8;
605acf51eaSKazutaka YOKOTA     if (end_offset)
615acf51eaSKazutaka YOKOTA 	i++;
625acf51eaSKazutaka YOKOTA     VGLPlane[0] = VGLBuf;
635acf51eaSKazutaka YOKOTA     VGLPlane[1] = VGLPlane[0] + i;
645acf51eaSKazutaka YOKOTA     VGLPlane[2] = VGLPlane[1] + i;
655acf51eaSKazutaka YOKOTA     VGLPlane[3] = VGLPlane[2] + i;
669a57b7d2SSøren Schmidt     pos = 0;
679a57b7d2SSøren Schmidt     planepos = 0;
685acf51eaSKazutaka YOKOTA     last = 8 - start_offset;
699a57b7d2SSøren Schmidt     while (pos < width) {
709a57b7d2SSøren Schmidt       word = 0;
719a57b7d2SSøren Schmidt       while (pos < last && pos < width)
729a57b7d2SSøren Schmidt 	word = (word<<1) | color2bit[line[pos++]&0x0f];
739a57b7d2SSøren Schmidt       VGLPlane[0][planepos] = word;
749a57b7d2SSøren Schmidt       VGLPlane[1][planepos] = word>>8;
759a57b7d2SSøren Schmidt       VGLPlane[2][planepos] = word>>16;
769a57b7d2SSøren Schmidt       VGLPlane[3][planepos] = word>>24;
779a57b7d2SSøren Schmidt       planepos++;
785acf51eaSKazutaka YOKOTA       last += 8;
799a57b7d2SSøren Schmidt     }
809a57b7d2SSøren Schmidt     planepos--;
819a57b7d2SSøren Schmidt     if (end_offset) {
829a57b7d2SSøren Schmidt       word <<= (8 - end_offset);
839a57b7d2SSøren Schmidt       VGLPlane[0][planepos] = word;
849a57b7d2SSøren Schmidt       VGLPlane[1][planepos] = word>>8;
859a57b7d2SSøren Schmidt       VGLPlane[2][planepos] = word>>16;
869a57b7d2SSøren Schmidt       VGLPlane[3][planepos] = word>>24;
879a57b7d2SSøren Schmidt     }
889a57b7d2SSøren Schmidt     if (start_offset || end_offset)
899a57b7d2SSøren Schmidt       width+=8;
905acf51eaSKazutaka YOKOTA     width /= 8;
915acf51eaSKazutaka YOKOTA     outb(0x3ce, 0x01); outb(0x3cf, 0x00);		/* set/reset enable */
925acf51eaSKazutaka YOKOTA     outb(0x3ce, 0x08); outb(0x3cf, 0xff);		/* bit mask */
939a57b7d2SSøren Schmidt     for (i=0; i<4; i++) {
949a57b7d2SSøren Schmidt       outb(0x3c4, 0x02);
959a57b7d2SSøren Schmidt       outb(0x3c5, 0x01<<i);
969a57b7d2SSøren Schmidt       outb(0x3ce, 0x04);
979a57b7d2SSøren Schmidt       outb(0x3cf, i);
985acf51eaSKazutaka YOKOTA       pos = VGLAdpInfo.va_line_width*y + x/8;
995acf51eaSKazutaka YOKOTA       if (dst->Type == VIDBUF4) {
1009a57b7d2SSøren Schmidt 	if (end_offset)
1015acf51eaSKazutaka YOKOTA 	  VGLPlane[i][planepos] |= dst->Bitmap[pos+planepos] & mask[end_offset];
1025acf51eaSKazutaka YOKOTA 	if (start_offset)
1035acf51eaSKazutaka YOKOTA 	  VGLPlane[i][0] |= dst->Bitmap[pos] & ~mask[start_offset];
1045acf51eaSKazutaka YOKOTA 	bcopy(&VGLPlane[i][0], dst->Bitmap + pos, width);
1055acf51eaSKazutaka YOKOTA       } else {	/* VIDBUF4S */
1065acf51eaSKazutaka YOKOTA 	if (end_offset) {
1075acf51eaSKazutaka YOKOTA 	  offset = VGLSetSegment(pos + planepos);
1085acf51eaSKazutaka YOKOTA 	  VGLPlane[i][planepos] |= dst->Bitmap[offset] & mask[end_offset];
1095acf51eaSKazutaka YOKOTA 	}
1105acf51eaSKazutaka YOKOTA 	offset = VGLSetSegment(pos);
1115acf51eaSKazutaka YOKOTA 	if (start_offset)
1125acf51eaSKazutaka YOKOTA 	  VGLPlane[i][0] |= dst->Bitmap[offset] & ~mask[start_offset];
1135acf51eaSKazutaka YOKOTA 	for (last = width; ; ) {
1145acf51eaSKazutaka YOKOTA 	  len = min(VGLAdpInfo.va_window_size - offset, last);
1155acf51eaSKazutaka YOKOTA 	  bcopy(&VGLPlane[i][width - last], dst->Bitmap + offset, len);
1165acf51eaSKazutaka YOKOTA 	  pos += len;
1175acf51eaSKazutaka YOKOTA 	  last -= len;
1185acf51eaSKazutaka YOKOTA 	  if (last <= 0)
1195acf51eaSKazutaka YOKOTA 	    break;
1205acf51eaSKazutaka YOKOTA 	  offset = VGLSetSegment(pos);
1215acf51eaSKazutaka YOKOTA 	}
1225acf51eaSKazutaka YOKOTA       }
1239a57b7d2SSøren Schmidt     }
1249a57b7d2SSøren Schmidt     break;
1259a57b7d2SSøren Schmidt   case VIDBUF8X:
1265acf51eaSKazutaka YOKOTA     address = dst->Bitmap + VGLAdpInfo.va_line_width * y + x/4;
1279a57b7d2SSøren Schmidt     for (i=0; i<4; i++) {
1289a57b7d2SSøren Schmidt       outb(0x3c4, 0x02);
1295e7a62b2SKazutaka YOKOTA       outb(0x3c5, 0x01 << ((x + i)%4));
1305e7a62b2SKazutaka YOKOTA       for (planepos=0, pos=i; pos<width; planepos++, pos+=4)
1319a57b7d2SSøren Schmidt         address[planepos] = line[pos];
1325e7a62b2SKazutaka YOKOTA       if ((x + i)%4 == 3)
1335e7a62b2SKazutaka YOKOTA 	++address;
1349a57b7d2SSøren Schmidt     }
1359a57b7d2SSøren Schmidt     break;
1365acf51eaSKazutaka YOKOTA   case VIDBUF8S:
1375acf51eaSKazutaka YOKOTA     pos = dst->VXsize * y + x;
1385acf51eaSKazutaka YOKOTA     while (width > 0) {
1395acf51eaSKazutaka YOKOTA       offset = VGLSetSegment(pos);
1405acf51eaSKazutaka YOKOTA       i = min(VGLAdpInfo.va_window_size - offset, width);
1415acf51eaSKazutaka YOKOTA       bcopy(line, dst->Bitmap + offset, i);
1425acf51eaSKazutaka YOKOTA       line += i;
1435acf51eaSKazutaka YOKOTA       pos += i;
1445acf51eaSKazutaka YOKOTA       width -= i;
1455acf51eaSKazutaka YOKOTA     }
1465acf51eaSKazutaka YOKOTA     break;
147933d455fSNicolas Souchu   case VIDBUF16S:
148933d455fSNicolas Souchu   case VIDBUF24S:
149933d455fSNicolas Souchu   case VIDBUF32S:
150933d455fSNicolas Souchu     width = width * dst->PixelBytes;
151933d455fSNicolas Souchu     pos = (dst->VXsize * y + x) * dst->PixelBytes;
152933d455fSNicolas Souchu     while (width > 0) {
153933d455fSNicolas Souchu       offset = VGLSetSegment(pos);
154933d455fSNicolas Souchu       i = min(VGLAdpInfo.va_window_size - offset, width);
155933d455fSNicolas Souchu       bcopy(line, dst->Bitmap + offset, i);
156933d455fSNicolas Souchu       line += i;
157933d455fSNicolas Souchu       pos += i;
158933d455fSNicolas Souchu       width -= i;
159933d455fSNicolas Souchu     }
160933d455fSNicolas Souchu     break;
1619a57b7d2SSøren Schmidt   case VIDBUF8:
1629a57b7d2SSøren Schmidt   case MEMBUF:
1635acf51eaSKazutaka YOKOTA     address = dst->Bitmap + dst->VXsize * y + x;
1649a57b7d2SSøren Schmidt     bcopy(line, address, width);
1659a57b7d2SSøren Schmidt     break;
166933d455fSNicolas Souchu   case VIDBUF16:
167933d455fSNicolas Souchu   case VIDBUF24:
168933d455fSNicolas Souchu   case VIDBUF32:
169933d455fSNicolas Souchu     address = dst->Bitmap + (dst->VXsize * y + x) * dst->PixelBytes;
170933d455fSNicolas Souchu     bcopy(line, address, width * dst->PixelBytes);
171933d455fSNicolas Souchu     break;
1729a57b7d2SSøren Schmidt   default:
173a96d3de6SGarrett Wollman     ;
1749a57b7d2SSøren Schmidt   }
1759a57b7d2SSøren Schmidt }
1769a57b7d2SSøren Schmidt 
1779a57b7d2SSøren Schmidt static void
1789a57b7d2SSøren Schmidt ReadVerticalLine(VGLBitmap *src, int x, int y, int width, byte *line)
1799a57b7d2SSøren Schmidt {
1809a57b7d2SSøren Schmidt   int i, bit, pos, count, planepos, start_offset, end_offset, offset;
1815acf51eaSKazutaka YOKOTA   int width2, len;
1829a57b7d2SSøren Schmidt   byte *address;
1835acf51eaSKazutaka YOKOTA   byte *VGLPlane[4];
1849a57b7d2SSøren Schmidt 
1859a57b7d2SSøren Schmidt   switch (src->Type) {
1865acf51eaSKazutaka YOKOTA   case VIDBUF4S:
1879a57b7d2SSøren Schmidt     start_offset = (x & 0x07);
1889a57b7d2SSøren Schmidt     end_offset = (x + width) & 0x07;
1895acf51eaSKazutaka YOKOTA     count = (width + start_offset) / 8;
1909a57b7d2SSøren Schmidt     if (end_offset)
1919a57b7d2SSøren Schmidt       count++;
1925acf51eaSKazutaka YOKOTA     VGLPlane[0] = VGLBuf;
1935acf51eaSKazutaka YOKOTA     VGLPlane[1] = VGLPlane[0] + count;
1945acf51eaSKazutaka YOKOTA     VGLPlane[2] = VGLPlane[1] + count;
1955acf51eaSKazutaka YOKOTA     VGLPlane[3] = VGLPlane[2] + count;
1965acf51eaSKazutaka YOKOTA     for (i=0; i<4; i++) {
1975acf51eaSKazutaka YOKOTA       outb(0x3ce, 0x04);
1985acf51eaSKazutaka YOKOTA       outb(0x3cf, i);
1995acf51eaSKazutaka YOKOTA       pos = VGLAdpInfo.va_line_width*y + x/8;
2005acf51eaSKazutaka YOKOTA       for (width2 = count; width2 > 0; ) {
2015acf51eaSKazutaka YOKOTA 	offset = VGLSetSegment(pos);
2025acf51eaSKazutaka YOKOTA 	len = min(VGLAdpInfo.va_window_size - offset, width2);
2035acf51eaSKazutaka YOKOTA 	bcopy(src->Bitmap + offset, &VGLPlane[i][count - width2], len);
2045acf51eaSKazutaka YOKOTA 	pos += len;
2055acf51eaSKazutaka YOKOTA 	width2 -= len;
2065acf51eaSKazutaka YOKOTA       }
2075acf51eaSKazutaka YOKOTA     }
2085acf51eaSKazutaka YOKOTA     goto read_planar;
2095acf51eaSKazutaka YOKOTA   case VIDBUF4:
2105acf51eaSKazutaka YOKOTA     address = src->Bitmap + VGLAdpInfo.va_line_width * y + x/8;
2115acf51eaSKazutaka YOKOTA     start_offset = (x & 0x07);
2125acf51eaSKazutaka YOKOTA     end_offset = (x + width) & 0x07;
2135acf51eaSKazutaka YOKOTA     count = (width + start_offset) / 8;
2145acf51eaSKazutaka YOKOTA     if (end_offset)
2155acf51eaSKazutaka YOKOTA       count++;
2165acf51eaSKazutaka YOKOTA     VGLPlane[0] = VGLBuf;
2175acf51eaSKazutaka YOKOTA     VGLPlane[1] = VGLPlane[0] + count;
2185acf51eaSKazutaka YOKOTA     VGLPlane[2] = VGLPlane[1] + count;
2195acf51eaSKazutaka YOKOTA     VGLPlane[3] = VGLPlane[2] + count;
2209a57b7d2SSøren Schmidt     for (i=0; i<4; i++) {
2219a57b7d2SSøren Schmidt       outb(0x3ce, 0x04);
2229a57b7d2SSøren Schmidt       outb(0x3cf, i);
2239a57b7d2SSøren Schmidt       bcopy(address, &VGLPlane[i][0], count);
2249a57b7d2SSøren Schmidt     }
2255acf51eaSKazutaka YOKOTA read_planar:
2269a57b7d2SSøren Schmidt     pos = 0;
2279a57b7d2SSøren Schmidt     planepos = 0;
2285acf51eaSKazutaka YOKOTA     bit = 7 - start_offset;
2299a57b7d2SSøren Schmidt     while (pos < width) {
2305acf51eaSKazutaka YOKOTA       for (; bit >= 0 && pos < width; bit--, pos++) {
2319a57b7d2SSøren Schmidt         line[pos] = (VGLPlane[0][planepos] & (1<<bit) ? 1 : 0) |
2329a57b7d2SSøren Schmidt                     ((VGLPlane[1][planepos] & (1<<bit) ? 1 : 0) << 1) |
2339a57b7d2SSøren Schmidt                     ((VGLPlane[2][planepos] & (1<<bit) ? 1 : 0) << 2) |
2349a57b7d2SSøren Schmidt                     ((VGLPlane[3][planepos] & (1<<bit) ? 1 : 0) << 3);
2359a57b7d2SSøren Schmidt       }
2369a57b7d2SSøren Schmidt       planepos++;
2375acf51eaSKazutaka YOKOTA       bit = 7;
2389a57b7d2SSøren Schmidt     }
2399a57b7d2SSøren Schmidt     break;
2409a57b7d2SSøren Schmidt   case VIDBUF8X:
2415acf51eaSKazutaka YOKOTA     address = src->Bitmap + VGLAdpInfo.va_line_width * y + x/4;
2429a57b7d2SSøren Schmidt     for (i=0; i<4; i++) {
2439a57b7d2SSøren Schmidt       outb(0x3ce, 0x04);
2445e7a62b2SKazutaka YOKOTA       outb(0x3cf, (x + i)%4);
2455e7a62b2SKazutaka YOKOTA       for (planepos=0, pos=i; pos<width; planepos++, pos+=4)
2469a57b7d2SSøren Schmidt         line[pos] = address[planepos];
2475e7a62b2SKazutaka YOKOTA       if ((x + i)%4 == 3)
2485e7a62b2SKazutaka YOKOTA 	++address;
2499a57b7d2SSøren Schmidt     }
2509a57b7d2SSøren Schmidt     break;
2515acf51eaSKazutaka YOKOTA   case VIDBUF8S:
2525acf51eaSKazutaka YOKOTA     pos = src->VXsize * y + x;
2535acf51eaSKazutaka YOKOTA     while (width > 0) {
2545acf51eaSKazutaka YOKOTA       offset = VGLSetSegment(pos);
2555acf51eaSKazutaka YOKOTA       i = min(VGLAdpInfo.va_window_size - offset, width);
2565acf51eaSKazutaka YOKOTA       bcopy(src->Bitmap + offset, line, i);
2575acf51eaSKazutaka YOKOTA       line += i;
2585acf51eaSKazutaka YOKOTA       pos += i;
2595acf51eaSKazutaka YOKOTA       width -= i;
2605acf51eaSKazutaka YOKOTA     }
2615acf51eaSKazutaka YOKOTA     break;
262933d455fSNicolas Souchu   case VIDBUF16S:
263933d455fSNicolas Souchu   case VIDBUF24S:
264933d455fSNicolas Souchu   case VIDBUF32S:
265933d455fSNicolas Souchu     width = width * src->PixelBytes;
266933d455fSNicolas Souchu     pos = (src->VXsize * y + x) * src->PixelBytes;
267933d455fSNicolas Souchu     while (width > 0) {
268933d455fSNicolas Souchu       offset = VGLSetSegment(pos);
269933d455fSNicolas Souchu       i = min(VGLAdpInfo.va_window_size - offset, width);
270933d455fSNicolas Souchu       bcopy(src->Bitmap + offset, line, i);
271933d455fSNicolas Souchu       line += i;
272933d455fSNicolas Souchu       pos += i;
273933d455fSNicolas Souchu       width -= i;
274933d455fSNicolas Souchu     }
275933d455fSNicolas Souchu     break;
2769a57b7d2SSøren Schmidt   case VIDBUF8:
2779a57b7d2SSøren Schmidt   case MEMBUF:
2785acf51eaSKazutaka YOKOTA     address = src->Bitmap + src->VXsize * y + x;
2799a57b7d2SSøren Schmidt     bcopy(address, line, width);
2809a57b7d2SSøren Schmidt     break;
281933d455fSNicolas Souchu   case VIDBUF16:
282933d455fSNicolas Souchu   case VIDBUF24:
283933d455fSNicolas Souchu   case VIDBUF32:
284933d455fSNicolas Souchu     address = src->Bitmap + (src->VXsize * y + x) * src->PixelBytes;
285933d455fSNicolas Souchu     bcopy(address, line, width * src->PixelBytes);
286933d455fSNicolas Souchu     break;
2879a57b7d2SSøren Schmidt   default:
288a96d3de6SGarrett Wollman     ;
2899a57b7d2SSøren Schmidt   }
2909a57b7d2SSøren Schmidt }
2919a57b7d2SSøren Schmidt 
2929a57b7d2SSøren Schmidt int
2939a57b7d2SSøren Schmidt __VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
2949a57b7d2SSøren Schmidt 	      VGLBitmap *dst, int dstx, int dsty, int width, int hight)
2959a57b7d2SSøren Schmidt {
2969a57b7d2SSøren Schmidt   int srcline, dstline;
2979a57b7d2SSøren Schmidt 
2985acf51eaSKazutaka YOKOTA   if (srcx>src->VXsize || srcy>src->VYsize
2995acf51eaSKazutaka YOKOTA 	|| dstx>dst->VXsize || dsty>dst->VYsize)
3009a57b7d2SSøren Schmidt     return -1;
3019a57b7d2SSøren Schmidt   if (srcx < 0) {
3029a57b7d2SSøren Schmidt     width=width+srcx; dstx-=srcx; srcx=0;
3039a57b7d2SSøren Schmidt   }
3049a57b7d2SSøren Schmidt   if (srcy < 0) {
3059a57b7d2SSøren Schmidt     hight=hight+srcy; dsty-=srcy; srcy=0;
3069a57b7d2SSøren Schmidt   }
3079a57b7d2SSøren Schmidt   if (dstx < 0) {
3089a57b7d2SSøren Schmidt     width=width+dstx; srcx-=dstx; dstx=0;
3099a57b7d2SSøren Schmidt   }
3109a57b7d2SSøren Schmidt   if (dsty < 0) {
3119a57b7d2SSøren Schmidt     hight=hight+dsty; srcy-=dsty; dsty=0;
3129a57b7d2SSøren Schmidt   }
3135acf51eaSKazutaka YOKOTA   if (srcx+width > src->VXsize)
3145acf51eaSKazutaka YOKOTA      width=src->VXsize-srcx;
3155acf51eaSKazutaka YOKOTA   if (srcy+hight > src->VYsize)
3165acf51eaSKazutaka YOKOTA      hight=src->VYsize-srcy;
3175acf51eaSKazutaka YOKOTA   if (dstx+width > dst->VXsize)
3185acf51eaSKazutaka YOKOTA      width=dst->VXsize-dstx;
3195acf51eaSKazutaka YOKOTA   if (dsty+hight > dst->VYsize)
3205acf51eaSKazutaka YOKOTA      hight=dst->VYsize-dsty;
3219a57b7d2SSøren Schmidt   if (width < 0 || hight < 0)
3229a57b7d2SSøren Schmidt      return -1;
3239a57b7d2SSøren Schmidt   if (src->Type == MEMBUF) {
3249a57b7d2SSøren Schmidt     for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
3259a57b7d2SSøren Schmidt       WriteVerticalLine(dst, dstx, dstline, width,
3265acf51eaSKazutaka YOKOTA 	(src->Bitmap+(srcline*src->VXsize)+srcx));
3279a57b7d2SSøren Schmidt     }
3289a57b7d2SSøren Schmidt   }
3299a57b7d2SSøren Schmidt   else if (dst->Type == MEMBUF) {
3309a57b7d2SSøren Schmidt     for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
3319a57b7d2SSøren Schmidt       ReadVerticalLine(src, srcx, srcline, width,
3325acf51eaSKazutaka YOKOTA 	 (dst->Bitmap+(dstline*dst->VXsize)+dstx));
3339a57b7d2SSøren Schmidt     }
3349a57b7d2SSøren Schmidt   }
3359a57b7d2SSøren Schmidt   else {
3365acf51eaSKazutaka YOKOTA     byte buffer[2048];	/* XXX */
3375acf51eaSKazutaka YOKOTA     byte *p;
3385acf51eaSKazutaka YOKOTA 
3395acf51eaSKazutaka YOKOTA     if (width > sizeof(buffer)) {
3405acf51eaSKazutaka YOKOTA       p = malloc(width);
3415acf51eaSKazutaka YOKOTA       if (p == NULL)
3425acf51eaSKazutaka YOKOTA 	return 1;
3435acf51eaSKazutaka YOKOTA     } else {
3445acf51eaSKazutaka YOKOTA       p = buffer;
3459a57b7d2SSøren Schmidt     }
3465acf51eaSKazutaka YOKOTA     for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
3475acf51eaSKazutaka YOKOTA       ReadVerticalLine(src, srcx, srcline, width, p);
3485acf51eaSKazutaka YOKOTA       WriteVerticalLine(dst, dstx, dstline, width, p);
3495acf51eaSKazutaka YOKOTA     }
3505acf51eaSKazutaka YOKOTA     if (width > sizeof(buffer))
3515acf51eaSKazutaka YOKOTA       free(p);
3529a57b7d2SSøren Schmidt   }
3539a57b7d2SSøren Schmidt   return 0;
3549a57b7d2SSøren Schmidt }
3559a57b7d2SSøren Schmidt 
3569a57b7d2SSøren Schmidt int
3579a57b7d2SSøren Schmidt VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
3589a57b7d2SSøren Schmidt 	      VGLBitmap *dst, int dstx, int dsty, int width, int hight)
3599a57b7d2SSøren Schmidt {
3609a57b7d2SSøren Schmidt   int error;
3619a57b7d2SSøren Schmidt 
3629a57b7d2SSøren Schmidt   VGLMouseFreeze(dstx, dsty, width, hight, 0);
3639a57b7d2SSøren Schmidt   error = __VGLBitmapCopy(src, srcx, srcy, dst, dstx, dsty, width, hight);
3649a57b7d2SSøren Schmidt   VGLMouseUnFreeze();
3659a57b7d2SSøren Schmidt   return error;
3669a57b7d2SSøren Schmidt }
3679a57b7d2SSøren Schmidt 
3685acf51eaSKazutaka YOKOTA VGLBitmap
3695acf51eaSKazutaka YOKOTA *VGLBitmapCreate(int type, int xsize, int ysize, byte *bits)
3705acf51eaSKazutaka YOKOTA {
3715acf51eaSKazutaka YOKOTA   VGLBitmap *object;
3725acf51eaSKazutaka YOKOTA 
3735acf51eaSKazutaka YOKOTA   if (type != MEMBUF)
3745acf51eaSKazutaka YOKOTA     return NULL;
3755acf51eaSKazutaka YOKOTA   if (xsize < 0 || ysize < 0)
3765acf51eaSKazutaka YOKOTA     return NULL;
3775acf51eaSKazutaka YOKOTA   object = (VGLBitmap *)malloc(sizeof(*object));
3785acf51eaSKazutaka YOKOTA   if (object == NULL)
3795acf51eaSKazutaka YOKOTA     return NULL;
3805acf51eaSKazutaka YOKOTA   object->Type = type;
3815acf51eaSKazutaka YOKOTA   object->Xsize = xsize;
3825acf51eaSKazutaka YOKOTA   object->Ysize = ysize;
3835acf51eaSKazutaka YOKOTA   object->VXsize = xsize;
3845acf51eaSKazutaka YOKOTA   object->VYsize = ysize;
3855acf51eaSKazutaka YOKOTA   object->Xorigin = 0;
3865acf51eaSKazutaka YOKOTA   object->Yorigin = 0;
3875acf51eaSKazutaka YOKOTA   object->Bitmap = bits;
3885acf51eaSKazutaka YOKOTA   return object;
3895acf51eaSKazutaka YOKOTA }
3905acf51eaSKazutaka YOKOTA 
3915acf51eaSKazutaka YOKOTA void
3925acf51eaSKazutaka YOKOTA VGLBitmapDestroy(VGLBitmap *object)
3935acf51eaSKazutaka YOKOTA {
3945acf51eaSKazutaka YOKOTA   if (object->Bitmap)
3955acf51eaSKazutaka YOKOTA     free(object->Bitmap);
3965acf51eaSKazutaka YOKOTA   free(object);
3975acf51eaSKazutaka YOKOTA }
3985acf51eaSKazutaka YOKOTA 
3995acf51eaSKazutaka YOKOTA int
4005acf51eaSKazutaka YOKOTA VGLBitmapAllocateBits(VGLBitmap *object)
4015acf51eaSKazutaka YOKOTA {
4025acf51eaSKazutaka YOKOTA   object->Bitmap = (byte *)malloc(object->VXsize*object->VYsize);
4035acf51eaSKazutaka YOKOTA   if (object->Bitmap == NULL)
4045acf51eaSKazutaka YOKOTA     return -1;
4055acf51eaSKazutaka YOKOTA   return 0;
4065acf51eaSKazutaka YOKOTA }
407