1 /* $Id: scale.c,v 1.4 2004-08-28 23:17:45 schaffner 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-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
16 * Routines for scaling a bitmap.
25 static char rcsid[] = "$Id: scale.c,v 1.4 2004-08-28 23:17:45 schaffner Exp $";
40 #define TRANSPARENCY_COLOR 255;
43 static int Transparency_color = TRANSPARENCY_COLOR;
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 );
54 void scale_row_c( ubyte * sbits, ubyte * dbits, int width, fix u, fix du )
59 for ( i=0; i<width; i++ ) {
62 if ( c != Transparency_color )
70 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (fixmuldiv((x)-(x0),(y1)-(y0),(x1)-(x0))+(y0))
72 // Scales bitmap, bp, into vertbuf[0] to vertbuf[1]
73 void scale_bitmap(grs_bitmap *bp, grs_point *vertbuf ,int orientation)
75 grs_bitmap * dbp = &grd_curcanv->cv_bitmap;
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;
83 // Set initial variables....
85 x0 = vertbuf[0].x; y0 = vertbuf[0].y;
86 x1 = vertbuf[2].x; y1 = vertbuf[2].y;
89 xmax = i2f(dbp->bm_w)-fl2f(.5); ymax = i2f(dbp->bm_h)-fl2f(.5);
91 u0 = i2f(0); v0 = i2f(0);
92 u1 = i2f(bp->bm_w-1); v1 = i2f(bp->bm_h-1);
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;
99 clipped_u0 = u0; clipped_v0 = v0;
100 clipped_u1 = u1; clipped_v1 = v1;
102 clipped_x0 = x0; clipped_y0 = y0;
103 clipped_x1 = x1; clipped_y1 = y1;
105 // Clip the left, moving u0 right as necessary
107 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
111 // Clip the right, moving u1 left as necessary
113 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
117 // Clip the top, moving v0 down as necessary
119 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
123 // Clip the bottom, moving v1 up as necessary
125 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
129 dx0 = f2i(clipped_x0); dx1 = f2i(clipped_x1);
130 dy0 = f2i(clipped_y0); dy1 = f2i(clipped_y1);
132 if (dx1<=dx0) return;
133 if (dy1<=dy0) return;
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) );
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) );
148 dtemp = f2i(clipped_u1)-f2i(clipped_u0);
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 );
154 scale_bitmap_asm_rle(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1 );
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 );
159 scale_bitmap_asm(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1 );
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 )
168 ubyte * sbits, * dbits;
170 du = (u1-u0) / (x1-x0);
171 dv = (v1-v0) / (y1-y0);
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];
180 for (x=x0; x<=x1; x++ ) {
181 *dbits++ = sbits[ u >> 16 ];
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 )
192 du = (u1-u0) / (x1-x0);
193 dv = (v1-v0) / (y1-y0);
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 );
203 ubyte scale_rle_data[640];
205 void decode_row( grs_bitmap * bmp, int y )
207 int i, offset=4+bmp->bm_h;
210 offset += bmp->bm_data[4+i];
211 gr_rle_decode( &bmp->bm_data[offset], scale_rle_data );
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 )
219 du = (u1-u0) / (x1-x0);
220 dv = (v1-v0) / (y1-y0);
224 for (y=y0; y<=y1; y++ ) {
225 if ( f2i(v) != last_row ) {
227 decode_row( source_bmp, last_row );
229 scale_row_asm_transparent( scale_rle_data, &dest_bmp->bm_data[dest_bmp->bm_rowsize*y+x0], x1-x0+1, u0, du );
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 )
240 dv = (v1-v0) / (y1-y0);
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();
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();
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 )
259 int y, last_row = -1;
261 dv = (v1-v0) / (y1-y0);
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();
269 for (y=y0; y<=y1; y++ ) {
270 if ( f2i(v) != last_row ) {
272 decode_row( source_bmp, last_row );
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();
284 // Run-length slice bitmap scan line stretcher
286 void DrawHorizontalRun(char *ScreenPtr, int RunLength, int Color)
290 for (i=0; i<RunLength; i++)
291 *ScreenPtr++ = Color;
294 void rls_stretch_scanline( char * source, char * dest, int XDelta, int YDelta )
296 int AdjUp, AdjDown, ErrorTerm;
297 int WholeStep, InitialPixelCount, FinalPixelCount, i, RunLength;
300 /* Minimum # of pixels in a run in this line */
301 WholeStep = XDelta / YDelta;
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;
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;
312 /* Initial error term; reflects an initial step of 0.5 along the Y
314 ErrorTerm = (XDelta % YDelta) - (YDelta * 2);
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;
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
326 if ((AdjUp == 0) && ((WholeStep & 0x01) == 0))
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)
337 /* Draw the first, partial run of pixels */
338 //if ( *source != Transparency_color )
339 rep_stosb(dest, InitialPixelCount, *source );
340 dest += InitialPixelCount;
343 /* Draw all full runs */
344 for (i=0; i<(YDelta-1); i++)
346 RunLength = WholeStep; /* run is at least this long */
348 /* Advance the error term and add an extra pixel if the error term so indicates */
349 if ((ErrorTerm += AdjUp) > 0)
352 ErrorTerm -= AdjDown; /* reset the error term */
354 /* Draw this scan line's run */
356 //if ( *source != Transparency_color )
357 rep_stosb(dest, RunLength, *source );
363 /* Draw the final run of pixels */
364 //if ( *source != Transparency_color )
365 rep_stosb(dest, FinalPixelCount, *source );
373 void rls_stretch_scanline_setup( int XDelta, int YDelta )
375 scale_trans_color = Transparency_color & 0xFF;
376 scale_ydelta_minus_1 = YDelta - 1;
379 /* Minimum # of pixels in a run in this line */
380 scale_whole_step = XDelta / YDelta;
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;
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;
391 /* Initial error term; reflects an initial step of 0.5 along the Y
393 scale_error_term = (XDelta % YDelta) - (YDelta * 2);
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;
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
405 if ((scale_adj_up == 0) && ((scale_whole_step & 0x01) == 0))
407 scale_initial_pixel_count--;
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)
414 scale_error_term += YDelta;