1 /* $Id: ibitblt.c,v 1.8 2003-11-07 06:16:15 btb Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 * Rountines to copy a bitmap on top of another bitmap, but
19 * only copying to pixels that are transparent.
21 * Routines to to inverse bitblitting -- well not really.
22 * We don't inverse bitblt like in the PC, but this code
23 * does set up a structure that blits around the cockpit
25 * d2x uses the "Mac" version for everything except __MSDOS__
28 * Revision 1.3 1995/09/13 11:43:22 allender
29 * start on optimizing cockpit copy code
31 * Revision 1.2 1995/09/07 10:16:57 allender
32 * fixed up cockpit and rearview hole blitting
34 * Revision 1.1 1995/08/18 15:50:48 allender
37 * Revision 1.6 1994/11/28 17:07:29 john
38 * Took out some unused functions in linear.asm, moved
39 * gr_linear_movsd from linear.asm to bitblt.c, made sure that
40 * the code in ibiblt.c sets the direction flags before rep movsing.
42 * Revision 1.5 1994/11/18 22:50:22 john
43 * Changed shorts to ints in parameters.
45 * Revision 1.4 1994/11/09 16:35:16 john
46 * First version with working RLE bitmaps.
48 * Revision 1.3 1994/10/03 17:18:05 john
49 * Fixed bug with edi not getting intialized to zero
52 * Revision 1.2 1994/05/31 11:10:55 john
53 * *** empty log message ***
55 * Revision 1.1 1994/05/30 16:08:27 john
65 static char rcsid[] = "$Id: ibitblt.c,v 1.8 2003-11-07 06:16:15 btb Exp $";
68 #ifdef __MSDOS__ //ndef MACINTOSH
76 #include "pa_enabl.h" //$$POLY_ACC
92 #define OPCODE_ADD 0x81
93 #define OPCODE_ESI 0xC6 // Followed by a dword (add esi, ????)
94 #define OPCODE_EDI 0xC7 // Followed by a dword (add edi, ????)
95 #define OPCODE_MOV_ECX 0xB9 // Followed by a dword (mov ecx,????)
96 #define OPCODE_MOVSB 0xA4 // movsb
97 #define OPCODE_16BIT 0x66 // movsw
98 #define OPCODE_MOVSD 0xA5 // movsd
99 #define OPCODE_REP 0xF3 // rep
100 #define OPCODE_RET 0xC3 // ret
101 #define OPCODE_MOV_EAX 0xB8 // mov eax, im dword
102 #define OPCODE_MOV_EBX 0xBB // mov ebx, im dword
103 #define OPCODE_CALL_EBX1 0xFF // call
104 #define OPCODE_CALL_EBX2 0xD3 // ebx
105 #define OPCODE_MOV_EDI 0xBF // mov edi, im dword
108 ubyte *Code_pointer = NULL;
109 int Code_counter = 0;
110 int ibitblt_svga_page = 0;
116 void count_block( int ecx )
122 case 1: Code_counter++; ecx = 0; break; // MOVSB
123 case 2: Code_counter+=2; ecx = 0; break; // MOVSW
124 case 3: Code_counter+=3; ecx = 0; break; // MOVSW, MOVSB
125 case 4: Code_counter++; ecx = 0; break; // MOVSD
129 Code_counter++; // MOVSD
138 void move_and_count( int dsource, int ddest, int ecx )
156 linear_address += ddest; // Skip to next block
158 p1 = linear_address >> 16; o1 = linear_address & 0xFFFF;
159 p2 = (linear_address+ecx) >> 16;
160 if ( p1 != ibitblt_svga_page ) {
162 // MOV EAX, ?, CALL EBX
164 ibitblt_svga_page = p1;
167 Code_counter += 5; // mov edi, ????
173 nbytes = 0xFFFF-o1+1;
174 count_block( nbytes );
177 Code_counter += 7; // mov eax,???? call ebx
179 ibitblt_svga_page = p2;
181 Code_counter += 5; // mov edi, ????
183 nbytes = ecx - nbytes;
185 count_block( nbytes );
187 linear_address += ecx;
193 void draw_block( int ecx )
202 *Code_pointer++ = OPCODE_MOVSB;
207 *Code_pointer++ = OPCODE_16BIT;
208 *Code_pointer++ = OPCODE_MOVSD;
213 *Code_pointer++ = OPCODE_16BIT;
214 *Code_pointer++ = OPCODE_MOVSD;
215 *Code_pointer++ = OPCODE_MOVSB;
220 *Code_pointer++ = OPCODE_MOVSD;
228 *Code_pointer++ = OPCODE_MOVSD;
231 *Code_pointer++ = OPCODE_MOV_ECX;
232 iptr = (int *)Code_pointer;
234 Code_pointer = (ubyte *)iptr;
236 *Code_pointer++ = OPCODE_REP;
237 *Code_pointer++ = OPCODE_MOVSD;
245 void move_and_draw( int dsource, int ddest, int ecx )
254 *Code_pointer++ = OPCODE_ADD;
255 *Code_pointer++ = OPCODE_ESI;
256 iptr = (int *)Code_pointer;
258 Code_pointer = (ubyte *)iptr;
263 *Code_pointer++ = OPCODE_ADD;
264 *Code_pointer++ = OPCODE_EDI;
265 iptr = (int *)Code_pointer;
267 Code_pointer = (ubyte *)iptr;
275 linear_address += ddest; // Skip to next block
277 p1 = linear_address >> 16; o1 = linear_address & 0xFFFF;
278 p2 = (linear_address+ecx) >> 16;
279 if ( p1 != ibitblt_svga_page ) {
282 *Code_pointer++ = OPCODE_MOV_EAX;
284 memcpy( Code_pointer, &temp, sizeof(int) );
285 Code_pointer += sizeof(int);
287 *Code_pointer++ = OPCODE_CALL_EBX1;
288 *Code_pointer++ = OPCODE_CALL_EBX2;
289 ibitblt_svga_page = p1;
292 temp_offset = 0xA0000 + o1;
293 *Code_pointer++ = OPCODE_MOV_EDI;
294 iptr = (int *)Code_pointer;
295 *iptr++ = temp_offset;
296 Code_pointer = (ubyte *)iptr;
302 nbytes = 0xFFFF-o1+1;
303 draw_block( nbytes );
306 *Code_pointer++ = OPCODE_MOV_EAX;
308 memcpy( Code_pointer, &temp, sizeof(int) );
309 Code_pointer += sizeof(int);
311 *Code_pointer++ = OPCODE_CALL_EBX1;
312 *Code_pointer++ = OPCODE_CALL_EBX2;
313 ibitblt_svga_page = p2;
315 temp_offset = 0xA0000;
316 *Code_pointer++ = OPCODE_MOV_EDI;
317 iptr = (int *)Code_pointer;
318 *iptr++ = temp_offset;
319 Code_pointer = (ubyte *)iptr;
321 nbytes = ecx - nbytes;
323 draw_block( nbytes );
325 linear_address += ecx;
330 //-----------------------------------------------------------------------------------------
331 // Given bitmap, bmp, finds the size of the code
333 int gr_ibitblt_find_code_size_sub( grs_bitmap * mask_bmp, int sx, int sy, int sw, int sh, int srowsize, int dest_type )
337 int draw_mode = MODE_NONE;
338 int source_offset = 0;
340 int num_to_draw, draw_start_source, draw_start_dest;
343 Assert( (!(mask_bmp->bm_flags&BM_FLAG_RLE)) );
347 if ( dest_type == BM_SVGA ) {
348 Code_counter += 1+4; // move ebx, gr_vesa_set_page
349 Code_counter += 1+4; // move eax, 0
350 Code_counter += 2; // call ebx
351 ibitblt_svga_page = 0;
358 esi = source_offset = 0;
359 edi = dest_offset = 0;
360 draw_start_source = draw_start_dest = 0;
362 for ( y=sy; y<sy+sh; y++ ) {
363 for ( x=sx; x<sx+sw; x++ ) {
364 dest_offset = y*mask_bmp->bm_rowsize+x;
365 pixel = mask_bmp->bm_data[dest_offset];
367 switch ( draw_mode) {
369 move_and_count( draw_start_source-esi, draw_start_dest-edi, num_to_draw );
370 esi = draw_start_source + num_to_draw;
371 edi = draw_start_dest + num_to_draw;
377 draw_mode = MODE_SKIP;
379 switch ( draw_mode) {
382 draw_start_source = source_offset;
383 draw_start_dest = dest_offset;
390 draw_mode = MODE_DRAW;
394 if ( draw_mode == MODE_DRAW ) {
395 move_and_count( draw_start_source-esi, draw_start_dest-edi, num_to_draw );
396 esi = draw_start_source + num_to_draw;
397 edi = draw_start_dest + num_to_draw;
399 draw_mode = MODE_NONE;
400 source_offset += (srowsize - sw);
402 Code_counter++; // for return
404 //printf( "Code will be %d bytes\n", Code_counter );
406 Code_counter += 16; // for safety was 16
411 int gr_ibitblt_find_code_size( grs_bitmap * mask_bmp, int sx, int sy, int sw, int sh, int srowsize )
413 return gr_ibitblt_find_code_size_sub( mask_bmp, sx, sy, sw, sh, srowsize, BM_LINEAR );
416 int gr_ibitblt_find_code_size_svga( grs_bitmap * mask_bmp, int sx, int sy, int sw, int sh, int srowsize )
418 return gr_ibitblt_find_code_size_sub( mask_bmp, sx, sy, sw, sh, srowsize, BM_SVGA );
421 //-----------------------------------------------------------------------------------------
422 // Given bitmap, bmp, create code that transfers a bitmap of size sw*sh to position
423 // (sx,sy) on top of bmp, only overwritting transparent pixels of the bitmap.
425 ubyte *gr_ibitblt_create_mask_sub( grs_bitmap * mask_bmp, int sx, int sy, int sw, int sh, int srowsize, int dest_type )
429 int draw_mode = MODE_NONE;
430 int source_offset = 0;
432 int num_to_draw, draw_start_source, draw_start_dest;
438 Assert( (!(mask_bmp->bm_flags&BM_FLAG_RLE)) );
440 if ( dest_type == BM_SVGA )
441 code_size = gr_ibitblt_find_code_size_svga( mask_bmp, sx, sy, sw, sh, srowsize );
443 code_size = gr_ibitblt_find_code_size( mask_bmp, sx, sy, sw, sh, srowsize );
445 code = malloc( code_size );
451 if ( dest_type == BM_SVGA ) {
452 // MOV EBX, gr_vesa_setpage
453 *Code_pointer++ = OPCODE_MOV_EBX;
454 temp = (uint)gr_vesa_setpage;
455 memcpy( Code_pointer, &temp, sizeof(int) );
456 Code_pointer += sizeof(int);
458 *Code_pointer++ = OPCODE_MOV_EAX;
460 memcpy( Code_pointer, &temp, sizeof(int) );
461 Code_pointer += sizeof(int);
463 *Code_pointer++ = OPCODE_CALL_EBX1;
464 *Code_pointer++ = OPCODE_CALL_EBX2;
466 ibitblt_svga_page = 0;
472 esi = source_offset = 0;
473 edi = dest_offset = 0;
474 draw_start_source = draw_start_dest = 0;
476 for ( y=sy; y<sy+sh; y++ ) {
477 for ( x=sx; x<sx+sw; x++ ) {
478 dest_offset = y*mask_bmp->bm_rowsize+x;
479 pixel = mask_bmp->bm_data[dest_offset];
481 switch ( draw_mode) {
483 move_and_draw( draw_start_source-esi, draw_start_dest-edi, num_to_draw );
484 esi = draw_start_source + num_to_draw;
485 edi = draw_start_dest + num_to_draw;
491 draw_mode = MODE_SKIP;
493 switch ( draw_mode) {
496 draw_start_source = source_offset;
497 draw_start_dest = dest_offset;
504 draw_mode = MODE_DRAW;
508 if ( draw_mode == MODE_DRAW ) {
509 move_and_draw( draw_start_source-esi, draw_start_dest-edi, num_to_draw );
510 esi = draw_start_source + num_to_draw;
511 edi = draw_start_dest + num_to_draw;
513 draw_mode = MODE_NONE;
514 source_offset += (srowsize - sw);
516 *Code_pointer++ = OPCODE_RET;
518 if ( Code_pointer >= &code[code_size-1] )
519 Error( "ibitblt overwrote allocated code block\n" );
521 //printf( "Code is %d bytes\n", Code_pointer - code );
526 ubyte *gr_ibitblt_create_mask( grs_bitmap * mask_bmp, int sx, int sy, int sw, int sh, int srowsize )
528 return gr_ibitblt_create_mask_sub( mask_bmp, sx, sy, sw, sh, srowsize, BM_LINEAR );
531 #if defined(POLY_ACC)
532 ulong *pa_emit_blit(int gencode, ulong *buf, int w, int h, int sx, int sy, int dx, int dy)
539 buf[0] = (w << 16) | h;
540 buf[1] = (sx << 16) | sy;
541 buf[2] = (dx << 16) | dy;
546 ubyte *gr_ibitblt_create_mask_pa( grs_bitmap * mask_bmp, int sx, int sy, int sw, int sh, int srowsize )
548 ulong *ret, *code = 0;
552 Assert(mask_bmp->bm_type == BM_LINEAR15);
558 // make two passes, first pass gets size of output block, second actually creates data.
559 for(pass = 0; pass != 2; ++pass)
561 for (y = sy; y < sy + sh; y++ )
563 // first byte of interest in mask
564 s = (ushort *)(mask_bmp->bm_data + y * mask_bmp->bm_rowsize + sx * PA_BPP);
567 for(; x != sw && (s[x] & 0x8000); ++x) // while opaque...
569 for(n = 0; x != sw && !(s[x] & 0x8000); ++n, ++x) // while transparent...
571 code = pa_emit_blit(pass, code, n, 1, x - n, y - sy, x + sx - n, y);
576 ret = malloc((int)code + sizeof(ulong));
577 ret[0] = (int)code / sizeof(ulong); // store num ulongs in list.
585 ubyte *gr_ibitblt_create_mask_svga( grs_bitmap * mask_bmp, int sx, int sy, int sw, int sh, int srowsize )
587 return gr_ibitblt_create_mask_sub( mask_bmp, sx, sy, sw, sh, srowsize, BM_SVGA );
592 void gr_ibitblt_do_asm(char *start_si, char *start_di, ubyte * code);
593 #pragma aux gr_ibitblt_do_asm parm [esi] [edi] [eax] modify [ecx edi esi eax] = \
600 void gr_ibitblt(grs_bitmap * source_bmp, grs_bitmap * dest_bmp, ubyte * mask )
602 #if defined(POLY_ACC)
603 Assert(source_bmp->bm_type == BM_LINEAR15);
604 pa_ibitblt(source_bmp->bm_data, dest_bmp->bm_data, mask);
607 gr_ibitblt_do_asm( source_bmp->bm_data, dest_bmp->bm_data, mask );
612 void gr_ibitblt_find_hole_size( grs_bitmap * mask_bmp, int *minx, int *miny, int *maxx, int *maxy )
615 #if defined(POLY_ACC)
621 Assert( (!(mask_bmp->bm_flags&BM_FLAG_RLE)) );
623 #if defined(POLY_ACC)
624 Assert(mask_bmp->bm_type == BM_LINEAR15);
628 *minx = mask_bmp->bm_w-1;
630 *miny = mask_bmp->bm_h-1;
633 for ( y=0; y<mask_bmp->bm_h; y++ )
634 for ( x=0; x<mask_bmp->bm_w; x++ ) {
635 #if defined(POLY_ACC)
636 c = *(short *)(mask_bmp->bm_data + mask_bmp->bm_rowsize * y + x * PA_BPP);
637 if (c >= 0) { // hi true means opaque.
639 c = mask_bmp->bm_data[mask_bmp->bm_rowsize*y+x];
642 if ( x < *minx ) *minx = x;
643 if ( y < *miny ) *miny = y;
644 if ( x > *maxx ) *maxx = x;
645 if ( y > *maxy ) *maxy = y;
651 Error( "Bitmap for ibitblt doesn't have transparency!\n" );
655 #else /* __MSDOS__ */ // was: /* !MACINTOSH */
657 #include "pa_enabl.h"
665 #if defined(POLY_ACC)
666 #include "poly_acc.h"
672 #define MAX_WIDTH 640
673 #define MAX_SCANLINES 480
676 static short start_points[MAX_SCANLINES][MAX_HOLES];
677 static short hole_length[MAX_SCANLINES][MAX_HOLES];
678 static double *scanline = NULL;
680 void gr_ibitblt(grs_bitmap *src_bmp, grs_bitmap *dest_bmp, ubyte pixel_double)
682 int x, y, sw, sh, srowsize, drowsize, dstart, sy, dy;
684 short *current_hole, *current_hole_length;
688 #if defined(POLY_ACC)
695 srowsize = src_bmp->bm_rowsize;
696 drowsize = dest_bmp->bm_rowsize;
697 src = src_bmp->bm_data;
698 dest = dest_bmp->bm_data;
701 while (start_points[sy][0] == -1) {
707 ubyte *scan = (ubyte *)scanline; // set up for byte processing of scanline
710 for (y = sy; y < sy + sh; y++) {
711 gr_linear_rep_movsd_2x(src, scan, sw); // was: gr_linear_movsd_double(src, scan, sw*2);
712 current_hole = start_points[dy];
713 current_hole_length = hole_length[dy];
714 for (x = 0; x < MAX_HOLES; x++) {
715 if (*current_hole == -1)
717 dstart = *current_hole;
718 gr_linear_movsd(&(scan[dstart]), &(dest[dstart]), *current_hole_length);
720 current_hole_length++;
724 current_hole = start_points[dy];
725 current_hole_length = hole_length[dy];
726 for (x = 0;x < MAX_HOLES; x++) {
727 if (*current_hole == -1)
729 dstart = *current_hole;
730 gr_linear_movsd(&(scan[dstart]), &(dest[dstart]), *current_hole_length);
732 current_hole_length++;
739 Assert(sw <= MAX_WIDTH);
740 Assert(sh <= MAX_SCANLINES);
741 for (y = sy; y < sy + sh; y++) {
742 for (x = 0; x < MAX_HOLES; x++) {
743 if (start_points[y][x] == -1)
745 dstart = start_points[y][x];
746 gr_linear_movsd(&(src[dstart]), &(dest[dstart]), hole_length[y][x]);
754 #if defined(POLY_ACC)
756 ulong *pa_emit_blit(int gencode, ulong *buf, int w, int h, int sx, int sy, int dx, int dy)
763 buf[0] = (w << 16) | h;
764 buf[1] = (sx << 16) | sy;
765 buf[2] = (dx << 16) | dy;
770 void gr_ibitblt_create_mask_pa( grs_bitmap * mask_bmp, int sx, int sy, int sw, int sh, int srowsize )
772 ulong *ret, *code = 0;
776 Assert(mask_bmp->bm_type == BM_LINEAR15);
782 // make two passes, first pass gets size of output block, second actually creates data.
783 for(pass = 0; pass != 2; ++pass)
785 for (y = sy; y < sy + sh; y++ )
787 // first byte of interest in mask
788 s = (ushort *)(mask_bmp->bm_data + y * mask_bmp->bm_rowsize + sx * PA_BPP);
791 for(; x != sw && (s[x] & 0x8000); ++x) // while opaque...
793 for(n = 0; x != sw && !(s[x] & 0x8000); ++n, ++x) // while transparent...
795 code = pa_emit_blit(pass, code, n, 1, x - n, y - sy, x + sx - n, y);
800 ret = malloc((int)code + sizeof(ulong));
801 ret[0] = (int)code / sizeof(ulong); // store num ulongs in list.
805 // return (ubyte *)ret;
809 void gr_ibitblt_create_mask(grs_bitmap *mask_bmp, int sx, int sy, int sw, int sh, int srowsize)
815 Assert( (!(mask_bmp->bm_flags&BM_FLAG_RLE)) );
817 for (y = 0; y < MAX_SCANLINES; y++) {
818 for (x = 0; x < MAX_HOLES; x++) {
819 start_points[y][x] = -1;
820 hole_length[y][x] = -1;
824 for (y = sy; y < sy+sh; y++) {
827 for (x = sx; x < sx + sw; x++) {
828 if ((mode == FIND_START) && (mask_bmp->bm_data[mask_bmp->bm_rowsize*y+x] == TRANSPARENCY_COLOR)) {
829 start_points[y][count] = x;
831 } else if ((mode == FIND_STOP) && (mask_bmp->bm_data[mask_bmp->bm_rowsize*y+x] != TRANSPARENCY_COLOR)) {
832 hole_length[y][count] = x - start_points[y][count];
837 if (mode == FIND_STOP) {
838 hole_length[y][count] = x - start_points[y][count];
841 Assert(count <= MAX_HOLES);
845 #if defined(POLY_ACC)
847 void gr_ibitblt_find_hole_size_pa( grs_bitmap * mask_bmp, int *minx, int *miny, int *maxx, int *maxy )
852 Assert( (!(mask_bmp->bm_flags&BM_FLAG_RLE)) );
854 Assert(mask_bmp->bm_type == BM_LINEAR15);
857 *minx = mask_bmp->bm_w-1;
859 *miny = mask_bmp->bm_h-1;
862 for ( y=0; y<mask_bmp->bm_h; y++ )
863 for ( x=0; x<mask_bmp->bm_w; x++ ) {
864 c = *(short *)(mask_bmp->bm_data + mask_bmp->bm_rowsize * y + x * PA_BPP);
865 if (c >= 0) { // hi true means opaque.
866 if ( x < *minx ) *minx = x;
867 if ( y < *miny ) *miny = y;
868 if ( x > *maxx ) *maxx = x;
869 if ( y > *maxy ) *maxy = y;
875 Error( "Bitmap for ibitblt doesn't have transparency!\n" );
880 void gr_ibitblt_find_hole_size(grs_bitmap *mask_bmp, int *minx, int *miny, int *maxx, int *maxy)
885 #if defined(POLY_ACC)
887 gr_ibitblt_find_hole_size_pa( mask_bmp, minx, miny, maxx, maxy );
892 Assert( (!(mask_bmp->bm_flags&BM_FLAG_RLE)) );
893 Assert( mask_bmp->bm_flags&BM_FLAG_TRANSPARENT );
895 *minx = mask_bmp->bm_w - 1;
897 *miny = mask_bmp->bm_h - 1;
900 if (scanline == NULL)
901 scanline = (double *)malloc(sizeof(double) * (MAX_WIDTH / sizeof(double)));
903 for (y = 0; y < mask_bmp->bm_h; y++) {
904 for (x = 0; x < mask_bmp->bm_w; x++) {
905 c = mask_bmp->bm_data[mask_bmp->bm_rowsize*y+x];
906 if (c == TRANSPARENCY_COLOR) { // don't look for transparancy color here.
908 if (x < *minx) *minx = x;
909 if (y < *miny) *miny = y;
910 if (x > *maxx) *maxx = x;
911 if (y > *maxy) *maxy = y;
918 #endif /* __MSDOS__ */ // was: /* !MACINTOSH */