home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / 2d / mac / scale.c
Text File  |  1998-06-08  |  15KB  |  506 lines

  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.  * $Source: f:/miner/source/2d/rcs/scale.c $
  15.  * $Revision: 1.12 $
  16.  * $Author: john $
  17.  * $Date: 1995/03/14 15:14:11 $
  18.  * 
  19.  * Routines for scaling a bitmap.
  20.  * 
  21.  * $Log: scale.c $
  22.  * Revision 1.12  1995/03/14  15:14:11  john
  23.  * Increased max scanline length to 640.
  24.  * ..
  25.  * 
  26.  * Revision 1.11  1994/11/27  12:56:39  matt
  27.  * Took out unneeded include of 3d.h
  28.  * 
  29.  * Revision 1.10  1994/11/18  22:50:25  john
  30.  * Changed shorts to ints in parameters.
  31.  * 
  32.  * Revision 1.9  1994/11/09  16:35:02  john
  33.  * First version with working RLE bitmaps.
  34.  * 
  35.  * Revision 1.8  1994/06/09  13:15:17  john
  36.  * *** empty log message ***
  37.  * 
  38.  * Revision 1.7  1994/06/07  11:47:02  john
  39.  * Added back in the fast code for scaling up bitmaps.
  40.  * 
  41.  * Revision 1.6  1994/02/18  15:32:36  john
  42.  * *** empty log message ***
  43.  * 
  44.  * Revision 1.5  1994/01/22  14:35:01  john
  45.  * Added transparency as color index 255.
  46.  * 
  47.  * Revision 1.4  1994/01/17  16:59:12  john
  48.  * once again...
  49.  * 
  50.  * Revision 1.3  1994/01/17  16:51:17  john
  51.  * Added check so we don't draw outsibe
  52.  * the source bitmap's v coordinate... kind
  53.  * of a hack, but works.
  54.  * 
  55.  * Revision 1.2  1994/01/12  18:03:26  john
  56.  * The first iteration of fast scaler..
  57.  * 
  58.  * Revision 1.1  1994/01/11  14:48:42  john
  59.  * Initial revision
  60.  * 
  61.  * 
  62.  */
  63.  
  64.  
  65. #pragma off (unreferenced)
  66. static char rcsid[] = "$Id: scale.c 1.12 1995/03/14 15:14:11 john Exp $";
  67. #pragma on (unreferenced)
  68.  
  69. #include <math.h>
  70. #include <limits.h>
  71. #include <stdio.h>
  72. #include <conio.h>
  73. #include <stdlib.h>
  74.  
  75. #include "mono.h"
  76. #include "fix.h"
  77. #include "gr.h"
  78. #include "error.h"
  79. #include "rle.h"
  80.  
  81. #define TRANSPARENCY_COLOR 255;
  82.  
  83. static int Transparency_color = TRANSPARENCY_COLOR;
  84.  
  85. extern char scale_trans_color;
  86. extern int scale_error_term;
  87. extern int scale_initial_pixel_count;
  88. extern int scale_adj_up;
  89. extern int scale_adj_down;
  90. extern int scale_final_pixel_count;
  91. extern int scale_ydelta_minus_1;
  92. extern int scale_whole_step;
  93. extern ubyte * scale_source_ptr;
  94. extern ubyte * scale_dest_ptr;
  95. extern void rls_stretch_scanline_asm();
  96.  
  97. extern void scale_do_cc_scanline();
  98. extern void rls_do_cc_setup_asm();
  99.  
  100. void rls_stretch_scanline( char * source, char * dest, int XDelta, int YDelta );
  101. void rls_stretch_scanline_setup( int XDelta, int YDelta );
  102.  
  103. void scale_row_c( ubyte * sbits, ubyte * dbits, int width, fix u, fix du )
  104. {
  105.     int i;
  106.     ubyte c;
  107.  
  108.     for ( i=0; i<width; i++ )    {
  109.         c = sbits[ f2i(u) ];
  110.  
  111.         if ( c != Transparency_color )
  112.             *dbits = c;
  113.             
  114.         dbits++;
  115.         u += du;
  116.     }
  117. }
  118.  
  119. // esi, edi = source, dest    
  120. // ecx = width
  121. // ebx = u
  122. // edx = du
  123.  
  124. void scale_row_asm_transparent( ubyte * sbits, ubyte * dbits, int width, fix u, fix du );
  125. #pragma aux scale_row_asm_transparent parm [esi] [edi] [ecx] [ebx] [edx] modify exact [edi eax ebx ecx] = \
  126. "newpixel:    mov    eax, ebx            " \
  127. "                shr    eax, 16            " \
  128. "                mov    al, [esi+eax]    " \
  129. "                cmp    al, 255            " \
  130. "                je        skip_it            " \
  131. "                mov    [edi], al        " \
  132. "skip_it:    add    ebx, edx            " \
  133. "                inc    edi                " \
  134. "                dec    ecx                " \
  135. "                jne    newpixel            "
  136.  
  137. void scale_row_asm( ubyte * sbits, ubyte * dbits, int width, fix u, fix du );
  138. #pragma aux scale_row_asm parm [esi] [edi] [ecx] [ebx] [edx] modify exact [edi eax ebx ecx] = \
  139. "newpixel1:    mov    eax, ebx            " \
  140. "                shr    eax, 16            " \
  141. "                mov    al, [esi+eax]    " \
  142. "                add    ebx, edx            " \
  143. "                mov    [edi], al        " \
  144. "                inc    edi                " \
  145. "                dec    ecx                " \
  146. "                jne    newpixel1        "
  147.  
  148.  
  149. void rep_movsb( ubyte * sbits, ubyte * dbits, int width );
  150. #pragma aux rep_movsb parm [esi] [edi] [ecx] modify exact [esi edi ecx] = \
  151. "rep movsb"
  152.  
  153. #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (fixmuldiv((x)-(x0),(y1)-(y0),(x1)-(x0))+(y0))
  154.  
  155. // Scales bitmap, bp, into vertbuf[0] to vertbuf[1]
  156. void scale_bitmap(grs_bitmap *bp, grs_point *vertbuf )
  157. {
  158.     grs_bitmap * dbp = &grd_curcanv->cv_bitmap;
  159.     fix x0, y0, x1, y1;
  160.     fix u0, v0, u1, v1;
  161.     fix clipped_x0, clipped_y0, clipped_x1, clipped_y1;
  162.     fix clipped_u0, clipped_v0, clipped_u1, clipped_v1;
  163.     fix xmin, xmax, ymin, ymax;
  164.     int dx0, dy0, dx1, dy1;
  165.     int dtemp;
  166.     // Set initial variables....
  167.  
  168.     x0 = vertbuf[0].x; y0 = vertbuf[0].y;
  169.     x1 = vertbuf[2].x; y1 = vertbuf[2].y;
  170.  
  171.     xmin = 0; ymin = 0;
  172.     xmax = i2f(dbp->bm_w)-fl2f(.5); ymax = i2f(dbp->bm_h)-fl2f(.5);
  173.  
  174.     u0 = i2f(0); v0 = i2f(0);
  175.     u1 = i2f(bp->bm_w-1); v1 = i2f(bp->bm_h-1);
  176.  
  177.     // Check for obviously offscreen bitmaps...
  178.     if ( (y1<=y0) || (x1<=x0) ) return;
  179.     if ( (x1<0 ) || (x0>=xmax) ) return;
  180.     if ( (y1<0 ) || (y0>=ymax) ) return;
  181.  
  182.     clipped_u0 = u0; clipped_v0 = v0;
  183.     clipped_u1 = u1; clipped_v1 = v1;
  184.  
  185.     clipped_x0 = x0; clipped_y0 = y0;
  186.     clipped_x1 = x1; clipped_y1 = y1;
  187.  
  188.     // Clip the left, moving u0 right as necessary
  189.     if ( x0 < xmin )     {
  190.         clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
  191.         clipped_x0 = xmin;
  192.     }
  193.  
  194.     // Clip the right, moving u1 left as necessary
  195.     if ( x1 > xmax )    {
  196.         clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
  197.         clipped_x1 = xmax;
  198.     }
  199.  
  200.     // Clip the top, moving v0 down as necessary
  201.     if ( y0 < ymin )     {
  202.         clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
  203.         clipped_y0 = ymin;
  204.     }
  205.  
  206.     // Clip the bottom, moving v1 up as necessary
  207.     if ( y1 > ymax )     {
  208.         clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
  209.         clipped_y1 = ymax;
  210.     }
  211.     
  212.     dx0 = f2i(clipped_x0); dx1 = f2i(clipped_x1);
  213.     dy0 = f2i(clipped_y0); dy1 = f2i(clipped_y1);
  214.  
  215.     if (dx1<=dx0) return;
  216.     if (dy1<=dy0) return;
  217.  
  218.     Assert( dx0>=0 );
  219.     Assert( dy0>=0 );
  220.     Assert( dx1<dbp->bm_w );
  221.     Assert( dy1<dbp->bm_h );
  222.     Assert( f2i(u0)<=f2i(u1) );
  223.     Assert( f2i(v0)<=f2i(v1) );
  224.     Assert( f2i(u0)>=0 );
  225.     Assert( f2i(v0)>=0 );
  226.     Assert( u1<i2f(bp->bm_w) );
  227.     Assert( v1<i2f(bp->bm_h) );
  228.  
  229.     //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) );
  230.  
  231.     dtemp = f2i(clipped_u1)-f2i(clipped_u0);
  232.  
  233.     if ( bp->bm_flags & BM_FLAG_RLE )    {
  234.         if ( (dtemp < (f2i(clipped_x1)-f2i(clipped_x0))) && (dtemp>0) )
  235.             scale_bitmap_cc_asm_rle(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1  );
  236.         else
  237.             scale_bitmap_asm_rle(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1  );
  238.     } else {
  239.         if ( (dtemp < (f2i(clipped_x1)-f2i(clipped_x0))) && (dtemp>0) )
  240.             scale_bitmap_cc_asm(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1  );
  241.         else
  242.             scale_bitmap_asm(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1  );
  243.     }
  244. }
  245.  
  246.  
  247. 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  )
  248. {
  249.     fix u, v, du, dv;
  250.     int x, y;
  251.     ubyte * sbits, * dbits;
  252.  
  253.     du = (u1-u0) / (x1-x0);
  254.     dv = (v1-v0) / (y1-y0);
  255.  
  256.     v = v0;
  257.  
  258.     for (y=y0; y<=y1; y++ )            {
  259.         sbits = &source_bmp->bm_data[source_bmp->bm_rowsize*f2i(v)];
  260.         dbits = &dest_bmp->bm_data[dest_bmp->bm_rowsize*y+x0];
  261.         u = u0; 
  262.         v += dv;
  263.         for (x=x0; x<=x1; x++ )            {
  264.             *dbits++ = sbits[ u >> 16 ];
  265.             u += du;
  266.         }
  267.     }
  268. }
  269.  
  270. 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  )
  271. {
  272.     fix du, dv, v;
  273.     int y;
  274.  
  275.     du = (u1-u0) / (x1-x0);
  276.     dv = (v1-v0) / (y1-y0);
  277.  
  278.     v = v0;
  279.  
  280.     for (y=y0; y<=y1; y++ )            {
  281.         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 );
  282.         v += dv;
  283.     }
  284. }
  285.  
  286. ubyte scale_rle_data[640];
  287.  
  288. void decode_row( grs_bitmap * bmp, int y )
  289. {
  290.     int i, offset=4+bmp->bm_h;
  291.     
  292.     for (i=0; i<y; i++ )
  293.         offset += bmp->bm_data[4+i];
  294.     gr_rle_decode( &bmp->bm_data[offset], scale_rle_data );
  295. }
  296.  
  297. 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  )
  298. {
  299.     fix du, dv, v;
  300.     int y, last_row=-1;
  301.  
  302.     du = (u1-u0) / (x1-x0);
  303.     dv = (v1-v0) / (y1-y0);
  304.  
  305.     v = v0;
  306.  
  307.     for (y=y0; y<=y1; y++ )            {
  308.         if ( f2i(v) != last_row )    {
  309.             last_row = f2i(v);
  310.             decode_row( source_bmp, last_row );
  311.         }
  312.         scale_row_asm_transparent( scale_rle_data, &dest_bmp->bm_data[dest_bmp->bm_rowsize*y+x0], x1-x0+1, u0, du );
  313.         v += dv;
  314.     }
  315. }
  316.  
  317.  
  318. 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  )
  319. {
  320.     fix dv, v;
  321.     int y;
  322.  
  323.     dv = (v1-v0) / (y1-y0);
  324.         
  325.     rls_stretch_scanline_setup( (int)(x1-x0), f2i(u1)-f2i(u0) );
  326.     if ( scale_ydelta_minus_1 < 1 ) return;
  327.     rls_do_cc_setup_asm();
  328.  
  329.     v = v0;
  330.  
  331.     for (y=y0; y<=y1; y++ )            {
  332.         scale_source_ptr = &source_bmp->bm_data[source_bmp->bm_rowsize*f2i(v)+f2i(u0)];
  333.         scale_dest_ptr = &dest_bmp->bm_data[dest_bmp->bm_rowsize*y+x0];
  334.         scale_do_cc_scanline();
  335.         v += dv;
  336.     }
  337. }
  338.  
  339.  
  340. int scale_error_term;
  341. int scale_initial_pixel_count;
  342. int scale_adj_up;
  343. int scale_adj_down;
  344. int scale_final_pixel_count;
  345. int scale_ydelta_minus_1;
  346. int scale_whole_step;
  347. ubyte * scale_source_ptr;
  348. ubyte * scale_dest_ptr;
  349.  
  350.  
  351.     if ( bp->bm_flags & BM_FLAG_RLE )    {
  352.         if ( (dtemp < (f2i(clipped_x1)-f2i(clipped_x0))) && (dtemp>0) )
  353.             scale_up_bitmap_rle(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1  );
  354.         else
  355.             scale_bitmap_rle(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1  );
  356.     } else {
  357.         if ( (dtemp < (f2i(clipped_x1)-f2i(clipped_x0))) && (dtemp>0) )
  358.             scale_bitmap_cc_asm(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1  );
  359.         else
  360.             scale_up_bitmap_asm(bp, dbp, dx0, dy0, dx1, dy1, clipped_u0, clipped_v0, clipped_u1, clipped_v1  );
  361.     }
  362.  
  363.  
  364. void scale_up_bitmap(grs_bitmap *source_bmp, grs_bitmap *dest_bmp, int x0, int y0, int x1, int y1, fix u0, fix v0,  fix u1, fix v1  )
  365. {
  366.     fix dv, v;
  367.     int y;
  368.  
  369.     dv = (v1-v0) / (y1-y0);
  370.         
  371.     rls_stretch_scanline_setup( (int)(x1-x0), f2i(u1)-f2i(u0) );
  372.     if ( scale_ydelta_minus_1 < 1 ) return;
  373.  
  374.     v = v0;
  375.  
  376.     for (y=y0; y<=y1; y++ )            {
  377.         scale_source_ptr = &source_bmp->bm_data[source_bmp->bm_rowsize*f2i(v)+f2i(u0)];
  378.         scale_dest_ptr = &dest_bmp->bm_data[dest_bmp->bm_rowsize*y+x0];
  379.         rls_stretch_scanline();
  380.         v += dv;
  381.     }
  382. }
  383.  
  384.  
  385.  
  386.  
  387. void scale_up_bitmap_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  )
  388. {
  389.     fix dv, v;
  390.     int y, last_row = -1;
  391.  
  392.     dv = (v1-v0) / (y1-y0);
  393.         
  394.     rls_stretch_scanline_setup( (int)(x1-x0), f2i(u1)-f2i(u0) );
  395.     if ( scale_ydelta_minus_1 < 1 ) return;
  396.  
  397.     v = v0;
  398.  
  399.     for (y=y0; y<=y1; y++ )            {
  400.         if ( f2i(v) != last_row )    {
  401.             last_row = f2i(v);
  402.             decode_row( source_bmp, last_row );
  403.         }
  404.         scale_source_ptr = &scale_rle_data[f2i(u0)];
  405.         scale_dest_ptr = &dest_bmp->bm_data[dest_bmp->bm_rowsize*y+x0];
  406.         rls_stretch_scanline();
  407.         v += dv;
  408.     }
  409. }
  410.  
  411. void rls_stretch_scanline_setup( int XDelta, int YDelta )
  412. {
  413.         scale_trans_color = Transparency_color & 0xFF;
  414.         scale_ydelta_minus_1 = YDelta - 1;
  415.  
  416.       /* X major line */
  417.       /* Minimum # of pixels in a run in this line */
  418.       scale_whole_step = XDelta / YDelta;
  419.  
  420.       /* Error term adjust each time Y steps by 1; used to tell when one
  421.          extra pixel should be drawn as part of a run, to account for
  422.          fractional steps along the X axis per 1-pixel steps along Y */
  423.       scale_adj_up = (XDelta % YDelta) * 2;
  424.  
  425.       /* Error term adjust when the error term turns over, used to factor
  426.          out the X step made at that time */
  427.       scale_adj_down = YDelta * 2;
  428.  
  429.       /* Initial error term; reflects an initial step of 0.5 along the Y
  430.          axis */
  431.       scale_error_term = (XDelta % YDelta) - (YDelta * 2);
  432.  
  433.       /* The initial and last runs are partial, because Y advances only 0.5
  434.          for these runs, rather than 1. Divide one full run, plus the
  435.          initial pixel, between the initial and last runs */
  436.       scale_initial_pixel_count = (scale_whole_step / 2) + 1;
  437.       scale_final_pixel_count = scale_initial_pixel_count;
  438.  
  439.       /* If the basic run length is even and there's no fractional
  440.          advance, we have one pixel that could go to either the initial
  441.          or last partial run, which we'll arbitrarily allocate to the
  442.          last run */
  443.       if ((scale_adj_up == 0) && ((scale_whole_step & 0x01) == 0))
  444.       {
  445.          scale_initial_pixel_count--;
  446.       }
  447.      /* If there're an odd number of pixels per run, we have 1 pixel that can't
  448.      be allocated to either the initial or last partial run, so we'll add 0.5
  449.      to error term so this pixel will be handled by the normal full-run loop */
  450.       if ((scale_whole_step & 0x01) != 0)
  451.       {
  452.          scale_error_term += YDelta;
  453.       }
  454.  
  455. }
  456.  
  457. void rls_stretch_scanline()
  458. {
  459.     ubyte    c;
  460.     int i, j, len, ErrorTerm;
  461.  
  462.     // Setup initial variables
  463.     ErrorTerm = scale_error_term;
  464.  
  465.     // Draw the first, partial run of pixels
  466.  
  467.     c = *scale_source_ptr++;
  468.     if ( c != TRANSPARENT )    {
  469.         for (i=0; i<scale_initial_pixel_count; i++ )
  470.             *scale_dest_ptr++ = c;
  471.     } else {
  472.         scale_dest_ptr += scale_initial_pixel_count;
  473.     }
  474.  
  475.     // Draw all full runs 
  476.  
  477.     for (j=0; j<scale_ydelta_minus_1; j++)    {
  478.         len = scale_whole_step;        // run is at least this long
  479.  
  480.          // Advance the error term and add an extra pixel if the error term so indicates
  481.         if ((ErrorTerm += scale_adj_up) > 0)    {
  482.             len++;
  483.             ErrorTerm -= scale_adj_down;   // reset the error term
  484.         }
  485.          
  486.         // Draw this run o' pixels
  487.         c = *scale_source_ptr++;
  488.         if ( c != TRANSPARENT )    {
  489.             for (i=0; i<len; i++ )
  490.                 *scale_dest_ptr++ = c;
  491.         } else {
  492.             scale_dest_ptr += len;
  493.         }
  494.     }
  495.  
  496.     // Draw the final run of pixels
  497.     c = *scale_source_ptr++;
  498.     if ( c != TRANSPARENT )    {
  499.         for (i=0; i<scale_final_pixel_count; i++ )
  500.             *scale_dest_ptr++ = c;
  501.     } else {
  502.         scale_dest_ptr += scale_final_pixel_count;
  503.     }
  504. }
  505. 
  506.