]> icculus.org git repositories - btb/d2x.git/blob - 2d/ibitblt.c
allow specifying file to extract
[btb/d2x.git] / 2d / ibitblt.c
1 /* $Id: ibitblt.c,v 1.5 2002-08-27 04:03:50 btb Exp $ */
2 /*
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-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *  Routines to to inverse bitblitting -- well not really.
17  *  We don't inverse bitblt like in the PC, but this code
18  *  does set up a structure that blits around the cockpit
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <conf.h>
24 #endif
25
26 #include <string.h>
27 #include "pstypes.h"
28 #include "gr.h"
29 #include "grdef.h"
30 #include "ibitblt.h"
31 #include "error.h"
32 //added 05/17/99 Matt Mueller
33 #include "u_mem.h"
34 //end addition -MM
35
36 #define FIND_START      1
37 #define FIND_STOP       2
38
39 #define MAX_WIDTH       1280
40 #define MAX_SCANLINES   1024
41 #define MAX_HOLES       10
42
43 static short start_points[MAX_SCANLINES][MAX_HOLES];
44 static short hole_length[MAX_SCANLINES][MAX_HOLES];
45 static double *scanline = NULL;
46
47 // adb: gr_linear_movsd assumes c >= 4
48 #define gr_linear_movsd(s,d,c) memcpy(d,s,c)
49
50 void gr_ibitblt(grs_bitmap *src_bmp, grs_bitmap *dest_bmp, ubyte pixel_double)
51 {
52         int x, y, sw, sh, srowsize, drowsize, dstart, sy, dy;
53         ubyte *src, *dest;
54         short *current_hole, *current_hole_length;
55
56 // variable setup
57
58         sw = src_bmp->bm_w;
59         sh = src_bmp->bm_h;
60         srowsize = src_bmp->bm_rowsize;
61         drowsize = dest_bmp->bm_rowsize;
62         src = src_bmp->bm_data;
63         dest = dest_bmp->bm_data;
64
65         sy = 0;
66         while (start_points[sy][0] == -1) {
67                 sy++;
68                 dest += drowsize;
69         }
70         
71         if (pixel_double) {
72                 ubyte *scan = (ubyte *)scanline;                // set up for byte processing of scanline
73                 
74                 dy = sy;
75                 for (y = sy; y < sy + sh; y++) {
76                         gr_linear_rep_movsd_2x(src, scan, sw);
77                         current_hole = start_points[dy];
78                         current_hole_length = hole_length[dy];
79                         for (x = 0; x < MAX_HOLES; x++) {
80                                 if (*current_hole == -1)
81                                         break;
82                                 dstart = *current_hole;
83                                 gr_linear_movsd(&(scan[dstart]), &(dest[dstart]), *current_hole_length);
84                                 current_hole++;
85                                 current_hole_length++;
86                         }
87                         dy++;
88                         dest += drowsize;
89                         current_hole = start_points[dy];
90                         current_hole_length = hole_length[dy];
91                         for (x = 0;x < MAX_HOLES; x++) {
92                                 if (*current_hole == -1)
93                                         break;
94                                 dstart = *current_hole;
95                                 gr_linear_movsd(&(scan[dstart]), &(dest[dstart]), *current_hole_length);
96                                 current_hole++;
97                                 current_hole_length++;
98                         }
99                         dy++;
100                         dest += drowsize;
101                         src += srowsize;
102                 }
103         } else {
104                 Assert(sw <= MAX_WIDTH);
105                 Assert(sh <= MAX_SCANLINES);
106                 for (y = sy; y < sy + sh; y++) {
107                         for (x = 0; x < MAX_HOLES; x++) {
108                                 if (start_points[y][x] == -1)
109                                         break;
110                                 dstart = start_points[y][x];
111                                 gr_linear_movsd(&(src[dstart]), &(dest[dstart]), hole_length[y][x]);
112                         }
113                         dest += drowsize;
114                         src += srowsize;
115                 }
116         }
117 }
118
119 void gr_ibitblt_create_mask(grs_bitmap *mask_bmp, int sx, int sy, int sw, int sh, int srowsize)
120 {
121         int x, y;
122         ubyte mode;
123         int count = 0;
124         
125         Assert( (!(mask_bmp->bm_flags&BM_FLAG_RLE)) );
126
127         for (y = 0; y < MAX_SCANLINES; y++) {
128                 for (x = 0; x < MAX_HOLES; x++) {
129                         start_points[y][x] = -1;
130                         hole_length[y][x] = -1;
131                 }
132         }
133         
134         for (y = sy; y < sy+sh; y++) {
135                 count = 0;
136                 mode = FIND_START;
137                 for (x = sx; x < sx + sw; x++) {
138                         if ((mode == FIND_START) && (mask_bmp->bm_data[mask_bmp->bm_rowsize*y+x] == TRANSPARENCY_COLOR)) {
139                                 start_points[y][count] = x;
140                                 mode = FIND_STOP;
141                         } else if ((mode == FIND_STOP) && (mask_bmp->bm_data[mask_bmp->bm_rowsize*y+x] != TRANSPARENCY_COLOR)) {
142                                 hole_length[y][count] = x - start_points[y][count];
143                                 count++;
144                                 mode = FIND_START;
145                         }
146                 }
147                 if (mode == FIND_STOP) {
148                         hole_length[y][count] = x - start_points[y][count];
149                         count++;
150                 }
151                 Assert(count <= MAX_HOLES);
152         }
153 }
154
155 //added 7/11/99 by adb to prevent memleaks
156 static void free_scanline(void)
157 {
158         if (scanline) d_free(scanline);
159 }
160 //end additions - adb
161
162
163 void gr_ibitblt_find_hole_size(grs_bitmap *mask_bmp, int *minx, int *miny, int *maxx, int *maxy)
164 {
165         ubyte c;
166         int x, y, count = 0;
167         
168         Assert( (!(mask_bmp->bm_flags&BM_FLAG_RLE)) );
169         Assert( mask_bmp->bm_flags&BM_FLAG_TRANSPARENT );
170         
171         *minx = mask_bmp->bm_w - 1;
172         *maxx = 0;
173         *miny = mask_bmp->bm_h - 1;
174         *maxy = 0;
175
176         //changed 7/11/99 by adb to prevent memleaks
177         if (scanline == NULL) {
178                 scanline = (double *)d_malloc(sizeof(double) * (MAX_WIDTH / sizeof(double)));
179                 atexit(free_scanline);
180         }
181         //end changes - adb
182
183         for (y = 0; y < mask_bmp->bm_h; y++) {
184                 for (x = 0; x < mask_bmp->bm_w; x++) {
185                         c = mask_bmp->bm_data[mask_bmp->bm_rowsize*y+x];
186                         if (c == TRANSPARENCY_COLOR) { // don't look for transparency color here.
187                                 count++;
188                                 if (x < *minx) *minx = x;
189                                 if (y < *miny) *miny = y;
190                                 if (x > *maxx) *maxx = x;
191                                 if (y > *maxy) *maxy = y;
192                         }
193                 }
194         }
195         Assert (count);
196 }