]> icculus.org git repositories - btb/d2x.git/blob - 2d/scale.c
fix bug: some characters would not display because of signed/unsigned errors
[btb/d2x.git] / 2d / scale.c
1 /* $Id: scale.c,v 1.4 2004-08-28 23:17:45 schaffner 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 for scaling a bitmap.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #ifdef RCS
25 static char rcsid[] = "$Id: scale.c,v 1.4 2004-08-28 23:17:45 schaffner Exp $";
26 #endif
27
28 #include <math.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "mono.h"
34 #include "fix.h"
35 #include "gr.h"
36 #include "error.h"
37 #include "rle.h"
38
39 #if 0
40 #define TRANSPARENCY_COLOR 255;
41 #endif
42
43 static int Transparency_color = TRANSPARENCY_COLOR;
44
45 #include "scalea.h"
46
47 void rls_stretch_scanline( char * source, char * dest, int XDelta, int YDelta );
48 void rls_stretch_scanline_setup( int XDelta, int YDelta );
49 void scale_bitmap_asm(grs_bitmap *source_bmp, grs_bitmap *dest_bmp, int x0, int y0, int x1, int y1, fix u0, fix v0,  fix u1, fix v1  );
50 void scale_bitmap_asm_rle(grs_bitmap *source_bmp, grs_bitmap *dest_bmp, int x0, int y0, int x1, int y1, fix u0, fix v0,  fix u1, fix v1  );
51 void scale_bitmap_cc_asm(grs_bitmap *source_bmp, grs_bitmap *dest_bmp, int x0, int y0, int x1, int y1, fix u0, fix v0,  fix u1, fix v1  );
52 void scale_bitmap_cc_asm_rle(grs_bitmap *source_bmp, grs_bitmap *dest_bmp, int x0, int y0, int x1, int y1, fix u0, fix v0,  fix u1, fix v1  );
53
54 void scale_row_c( ubyte * sbits, ubyte * dbits, int width, fix u, fix du )
55 {
56         int i;
57         ubyte c;
58
59         for ( i=0; i<width; i++ )       {
60                 c = sbits[ f2i(u) ];
61
62                 if ( c != Transparency_color )
63                         *dbits = c;
64                         
65                 dbits++;
66                 u += du;
67         }
68 }
69
70 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (fixmuldiv((x)-(x0),(y1)-(y0),(x1)-(x0))+(y0))
71
72 // Scales bitmap, bp, into vertbuf[0] to vertbuf[1]
73 void scale_bitmap(grs_bitmap *bp, grs_point *vertbuf ,int orientation)
74 {
75         grs_bitmap * dbp = &grd_curcanv->cv_bitmap;
76         fix x0, y0, x1, y1;
77         fix u0, v0, u1, v1;
78         fix clipped_x0, clipped_y0, clipped_x1, clipped_y1;
79         fix clipped_u0, clipped_v0, clipped_u1, clipped_v1;
80         fix xmin, xmax, ymin, ymax;
81         int dx0, dy0, dx1, dy1;
82         int dtemp;
83         // Set initial variables....
84
85         x0 = vertbuf[0].x; y0 = vertbuf[0].y;
86         x1 = vertbuf[2].x; y1 = vertbuf[2].y;
87
88         xmin = 0; ymin = 0;
89         xmax = i2f(dbp->bm_w)-fl2f(.5); ymax = i2f(dbp->bm_h)-fl2f(.5);
90
91         u0 = i2f(0); v0 = i2f(0);
92         u1 = i2f(bp->bm_w-1); v1 = i2f(bp->bm_h-1);
93
94         // Check for obviously offscreen bitmaps...
95         if ( (y1<=y0) || (x1<=x0) ) return;
96         if ( (x1<0 ) || (x0>=xmax) ) return;
97         if ( (y1<0 ) || (y0>=ymax) ) return;
98
99         clipped_u0 = u0; clipped_v0 = v0;
100         clipped_u1 = u1; clipped_v1 = v1;
101
102         clipped_x0 = x0; clipped_y0 = y0;
103         clipped_x1 = x1; clipped_y1 = y1;
104
105         // Clip the left, moving u0 right as necessary
106         if ( x0 < xmin )        {
107                 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
108                 clipped_x0 = xmin;
109         }
110
111         // Clip the right, moving u1 left as necessary
112         if ( x1 > xmax )        {
113                 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
114                 clipped_x1 = xmax;
115         }
116
117         // Clip the top, moving v0 down as necessary
118         if ( y0 < ymin )        {
119                 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
120                 clipped_y0 = ymin;
121         }
122
123         // Clip the bottom, moving v1 up as necessary
124         if ( y1 > ymax )        {
125                 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
126                 clipped_y1 = ymax;
127         }
128         
129         dx0 = f2i(clipped_x0); dx1 = f2i(clipped_x1);
130         dy0 = f2i(clipped_y0); dy1 = f2i(clipped_y1);
131
132         if (dx1<=dx0) return;
133         if (dy1<=dy0) return;
134
135         Assert( dx0>=0 );
136         Assert( dy0>=0 );
137         Assert( dx1<dbp->bm_w );
138         Assert( dy1<dbp->bm_h );
139         Assert( f2i(u0)<=f2i(u1) );
140         Assert( f2i(v0)<=f2i(v1) );
141         Assert( f2i(u0)>=0 );
142         Assert( f2i(v0)>=0 );
143         Assert( u1<i2f(bp->bm_w) );
144         Assert( v1<i2f(bp->bm_h) );
145
146         //mprintf( 0, "(%.2f,%.2f) to (%.2f,%.2f) using (%.2f,%.2f) to (%.2f,%.2f)\n", f2fl(clipped_x0), f2fl(clipped_y0), f2fl(clipped_x1), f2fl(clipped_y1), f2fl(clipped_u0), f2fl(clipped_v0), f2fl(clipped_u1), f2fl(clipped_v1) );
147
148         dtemp = f2i(clipped_u1)-f2i(clipped_u0);
149
150         if ( bp->bm_flags & BM_FLAG_RLE )       {
151                 if ( (dtemp < (f2i(clipped_x1)-f2i(clipped_x0))) && (dtemp>0) )
152                         scale_bitmap_cc_asm_rle(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1  );
153                 else
154                         scale_bitmap_asm_rle(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1  );
155         } else {
156                 if ( (dtemp < (f2i(clipped_x1)-f2i(clipped_x0))) && (dtemp>0) )
157                         scale_bitmap_cc_asm(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1  );
158                 else
159                         scale_bitmap_asm(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1  );
160         }
161 }
162
163
164 void scale_bitmap_c(grs_bitmap *source_bmp, grs_bitmap *dest_bmp, int x0, int y0, int x1, int y1, fix u0, fix v0,  fix u1, fix v1  )
165 {
166         fix u, v, du, dv;
167         int x, y;
168         ubyte * sbits, * dbits;
169
170         du = (u1-u0) / (x1-x0);
171         dv = (v1-v0) / (y1-y0);
172
173         v = v0;
174
175         for (y=y0; y<=y1; y++ )                 {
176                 sbits = &source_bmp->bm_data[source_bmp->bm_rowsize*f2i(v)];
177                 dbits = &dest_bmp->bm_data[dest_bmp->bm_rowsize*y+x0];
178                 u = u0; 
179                 v += dv;
180                 for (x=x0; x<=x1; x++ )                 {
181                         *dbits++ = sbits[ u >> 16 ];
182                         u += du;
183                 }
184         }
185 }
186
187 void scale_bitmap_asm(grs_bitmap *source_bmp, grs_bitmap *dest_bmp, int x0, int y0, int x1, int y1, fix u0, fix v0,  fix u1, fix v1  )
188 {
189         fix du, dv, v;
190         int y;
191
192         du = (u1-u0) / (x1-x0);
193         dv = (v1-v0) / (y1-y0);
194
195         v = v0;
196
197         for (y=y0; y<=y1; y++ )                 {
198                 scale_row_asm_transparent( &source_bmp->bm_data[source_bmp->bm_rowsize*f2i(v)], &dest_bmp->bm_data[dest_bmp->bm_rowsize*y+x0], x1-x0+1, u0, du );
199                 v += dv;
200         }
201 }
202
203 ubyte scale_rle_data[640];
204
205 void decode_row( grs_bitmap * bmp, int y )
206 {
207         int i, offset=4+bmp->bm_h;
208         
209         for (i=0; i<y; i++ )
210                 offset += bmp->bm_data[4+i];
211         gr_rle_decode( &bmp->bm_data[offset], scale_rle_data );
212 }
213
214 void scale_bitmap_asm_rle(grs_bitmap *source_bmp, grs_bitmap *dest_bmp, int x0, int y0, int x1, int y1, fix u0, fix v0,  fix u1, fix v1  )
215 {
216         fix du, dv, v;
217         int y, last_row=-1;
218
219         du = (u1-u0) / (x1-x0);
220         dv = (v1-v0) / (y1-y0);
221
222         v = v0;
223
224         for (y=y0; y<=y1; y++ )                 {
225                 if ( f2i(v) != last_row )       {
226                         last_row = f2i(v);
227                         decode_row( source_bmp, last_row );
228                 }
229                 scale_row_asm_transparent( scale_rle_data, &dest_bmp->bm_data[dest_bmp->bm_rowsize*y+x0], x1-x0+1, u0, du );
230                 v += dv;
231         }
232 }
233
234
235 void scale_bitmap_cc_asm(grs_bitmap *source_bmp, grs_bitmap *dest_bmp, int x0, int y0, int x1, int y1, fix u0, fix v0,  fix u1, fix v1  )
236 {
237         fix dv, v;
238         int y;
239
240         dv = (v1-v0) / (y1-y0);
241                 
242         rls_stretch_scanline_setup( (int)(x1-x0), f2i(u1)-f2i(u0) );
243         if ( scale_ydelta_minus_1 < 1 ) return;
244         rls_do_cc_setup_asm();
245
246         v = v0;
247
248         for (y=y0; y<=y1; y++ )                 {
249                 scale_source_ptr = &source_bmp->bm_data[source_bmp->bm_rowsize*f2i(v)+f2i(u0)];
250                 scale_dest_ptr = &dest_bmp->bm_data[dest_bmp->bm_rowsize*y+x0];
251                 scale_do_cc_scanline();
252                 v += dv;
253         }
254 }
255
256 void scale_bitmap_cc_asm_rle(grs_bitmap *source_bmp, grs_bitmap *dest_bmp, int x0, int y0, int x1, int y1, fix u0, fix v0,  fix u1, fix v1  )
257 {
258         fix dv, v;
259         int y, last_row = -1;
260
261         dv = (v1-v0) / (y1-y0);
262                 
263         rls_stretch_scanline_setup( (int)(x1-x0), f2i(u1)-f2i(u0) );
264         if ( scale_ydelta_minus_1 < 1 ) return;
265         rls_do_cc_setup_asm();
266
267         v = v0;
268
269         for (y=y0; y<=y1; y++ )                 {
270                 if ( f2i(v) != last_row )       {
271                         last_row = f2i(v);
272                         decode_row( source_bmp, last_row );
273                 }
274                 //scale_source_ptr = &source_bmp->bm_data[source_bmp->bm_rowsize*f2i(v)+f2i(u0)];
275                 scale_source_ptr = &scale_rle_data[f2i(u0)];
276                 scale_dest_ptr = &dest_bmp->bm_data[dest_bmp->bm_rowsize*y+x0];
277                 scale_do_cc_scanline();
278                 v += dv;
279         }
280 }
281
282
283
284 // Run-length slice bitmap scan line stretcher 
285
286 void DrawHorizontalRun(char *ScreenPtr, int RunLength, int Color)
287 {
288    int i;
289
290    for (i=0; i<RunLength; i++)
291       *ScreenPtr++ = Color;
292 }
293
294 void rls_stretch_scanline( char * source, char * dest, int XDelta, int YDelta )
295 {
296            int AdjUp, AdjDown, ErrorTerm;
297         int WholeStep, InitialPixelCount, FinalPixelCount, i, RunLength;
298
299       /* X major line */
300       /* Minimum # of pixels in a run in this line */
301       WholeStep = XDelta / YDelta;
302
303       /* Error term adjust each time Y steps by 1; used to tell when one
304          extra pixel should be drawn as part of a run, to account for
305          fractional steps along the X axis per 1-pixel steps along Y */
306       AdjUp = (XDelta % YDelta) * 2;
307
308       /* Error term adjust when the error term turns over, used to factor
309          out the X step made at that time */
310       AdjDown = YDelta * 2;
311
312       /* Initial error term; reflects an initial step of 0.5 along the Y
313          axis */
314       ErrorTerm = (XDelta % YDelta) - (YDelta * 2);
315
316       /* The initial and last runs are partial, because Y advances only 0.5
317          for these runs, rather than 1. Divide one full run, plus the
318          initial pixel, between the initial and last runs */
319       InitialPixelCount = (WholeStep / 2) + 1;
320       FinalPixelCount = InitialPixelCount;
321
322       /* If the basic run length is even and there's no fractional
323          advance, we have one pixel that could go to either the initial
324          or last partial run, which we'll arbitrarily allocate to the
325          last run */
326       if ((AdjUp == 0) && ((WholeStep & 0x01) == 0))
327       {
328          InitialPixelCount--;
329       }
330      /* If there're an odd number of pixels per run, we have 1 pixel that can't
331      be allocated to either the initial or last partial run, so we'll add 0.5
332      to error term so this pixel will be handled by the normal full-run loop */
333       if ((WholeStep & 0x01) != 0)
334       {
335          ErrorTerm += YDelta;
336       }
337       /* Draw the first, partial run of pixels */
338                 //if ( *source != Transparency_color )
339         rep_stosb(dest, InitialPixelCount, *source );
340                 dest += InitialPixelCount;
341                 source++;
342
343       /* Draw all full runs */
344       for (i=0; i<(YDelta-1); i++)
345       {
346          RunLength = WholeStep;  /* run is at least this long */
347
348          /* Advance the error term and add an extra pixel if the error term so indicates */
349          if ((ErrorTerm += AdjUp) > 0)
350          {
351             RunLength++;
352             ErrorTerm -= AdjDown;   /* reset the error term */
353          }
354          /* Draw this scan line's run */
355
356                         //if ( *source != Transparency_color )
357                 rep_stosb(dest, RunLength, *source );
358                         dest += RunLength;
359                         source++;
360
361       }
362
363       /* Draw the final run of pixels */
364                 //if ( *source != Transparency_color )
365               rep_stosb(dest, FinalPixelCount, *source );
366
367       return;
368 }
369
370
371
372
373 void rls_stretch_scanline_setup( int XDelta, int YDelta )
374 {
375                 scale_trans_color = Transparency_color & 0xFF;
376                 scale_ydelta_minus_1 = YDelta - 1;
377
378       /* X major line */
379       /* Minimum # of pixels in a run in this line */
380       scale_whole_step = XDelta / YDelta;
381
382       /* Error term adjust each time Y steps by 1; used to tell when one
383          extra pixel should be drawn as part of a run, to account for
384          fractional steps along the X axis per 1-pixel steps along Y */
385       scale_adj_up = (XDelta % YDelta) * 2;
386
387       /* Error term adjust when the error term turns over, used to factor
388          out the X step made at that time */
389       scale_adj_down = YDelta * 2;
390
391       /* Initial error term; reflects an initial step of 0.5 along the Y
392          axis */
393       scale_error_term = (XDelta % YDelta) - (YDelta * 2);
394
395       /* The initial and last runs are partial, because Y advances only 0.5
396          for these runs, rather than 1. Divide one full run, plus the
397          initial pixel, between the initial and last runs */
398       scale_initial_pixel_count = (scale_whole_step / 2) + 1;
399       scale_final_pixel_count = scale_initial_pixel_count;
400
401       /* If the basic run length is even and there's no fractional
402          advance, we have one pixel that could go to either the initial
403          or last partial run, which we'll arbitrarily allocate to the
404          last run */
405       if ((scale_adj_up == 0) && ((scale_whole_step & 0x01) == 0))
406       {
407          scale_initial_pixel_count--;
408       }
409      /* If there're an odd number of pixels per run, we have 1 pixel that can't
410      be allocated to either the initial or last partial run, so we'll add 0.5
411      to error term so this pixel will be handled by the normal full-run loop */
412       if ((scale_whole_step & 0x01) != 0)
413       {
414          scale_error_term += YDelta;
415       }
416
417 }
418