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