SCALE.C 16 KB

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