SCALEMAC.C 13 KB

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