SCALE.C 15 KB

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