BITMAP.C 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/bitmap.c $
  15. * $Revision: 1.17 $
  16. * $Author: john $
  17. * $Date: 1994/11/18 22:50:25 $
  18. *
  19. * Graphical routines for manipulating grs_bitmaps.
  20. *
  21. * $Log: bitmap.c $
  22. * Revision 1.17 1994/11/18 22:50:25 john
  23. * Changed shorts to ints in parameters.
  24. *
  25. * Revision 1.16 1994/11/10 15:59:46 john
  26. * Fixed bugs with canvas's being created with bogus bm_flags.
  27. *
  28. * Revision 1.15 1994/10/26 23:55:53 john
  29. * Took out roller; Took out inverse table.
  30. *
  31. * Revision 1.14 1994/09/19 14:40:21 john
  32. * Changed dpmi stuff.
  33. *
  34. * Revision 1.13 1994/09/19 11:44:04 john
  35. * Changed call to allocate selector to the dpmi module.
  36. *
  37. * Revision 1.12 1994/06/09 13:14:57 john
  38. * Made selectors zero our
  39. * out, I meant.
  40. *
  41. * Revision 1.11 1994/05/06 12:50:07 john
  42. * Added supertransparency; neatend things up; took out warnings.
  43. *
  44. * Revision 1.10 1994/04/08 16:59:39 john
  45. * Add fading poly's; Made palette fade 32 instead of 16.
  46. *
  47. * Revision 1.9 1994/03/16 17:21:09 john
  48. * Added slow palette searching options.
  49. *
  50. * Revision 1.8 1994/03/14 17:59:35 john
  51. * Added function to check bitmap's transparency.
  52. *
  53. * Revision 1.7 1994/03/14 17:16:21 john
  54. * fixed bug with counting freq of pixels.
  55. *
  56. * Revision 1.6 1994/03/14 16:55:47 john
  57. * Changed grs_bitmap structure to include bm_flags.
  58. *
  59. * Revision 1.5 1994/02/18 15:32:22 john
  60. * *** empty log message ***
  61. *
  62. * Revision 1.4 1993/10/15 16:22:49 john
  63. * *** empty log message ***
  64. *
  65. * Revision 1.3 1993/09/08 17:37:11 john
  66. * Checking for errors with Yuan...
  67. *
  68. * Revision 1.2 1993/09/08 14:46:27 john
  69. * looking for possible bugs...
  70. *
  71. * Revision 1.1 1993/09/08 11:43:05 john
  72. * Initial revision
  73. *
  74. *
  75. */
  76. #include <stdlib.h>
  77. #include <malloc.h>
  78. #include <stdio.h>
  79. #include "mem.h"
  80. #include "gr.h"
  81. #include "grdef.h"
  82. #include "dpmi.h"
  83. grs_bitmap *gr_create_bitmap(int w, int h )
  84. {
  85. grs_bitmap *new;
  86. new = (grs_bitmap *)malloc( sizeof(grs_bitmap) );
  87. new->bm_x = 0;
  88. new->bm_y = 0;
  89. new->bm_w = w;
  90. new->bm_h = h;
  91. new->bm_type = 0;
  92. new->bm_flags = 0;
  93. new->bm_rowsize = w;
  94. new->bm_selector = 0;
  95. new->bm_data = (unsigned char *)malloc( w*h );
  96. return new;
  97. }
  98. grs_bitmap *gr_create_bitmap_raw(int w, int h, unsigned char * raw_data )
  99. {
  100. grs_bitmap *new;
  101. new = (grs_bitmap *)malloc( sizeof(grs_bitmap) );
  102. new->bm_x = 0;
  103. new->bm_y = 0;
  104. new->bm_w = w;
  105. new->bm_h = h;
  106. new->bm_flags = 0;
  107. new->bm_type = 0;
  108. new->bm_rowsize = w;
  109. new->bm_data = raw_data;
  110. new->bm_selector = 0;
  111. return new;
  112. }
  113. void gr_init_bitmap( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline, unsigned char * data )
  114. {
  115. bm->bm_x = x;
  116. bm->bm_y = y;
  117. bm->bm_w = w;
  118. bm->bm_h = h;
  119. bm->bm_flags = 0;
  120. bm->bm_type = mode;
  121. bm->bm_rowsize = bytesperline;
  122. bm->bm_data = data;
  123. bm->bm_selector = 0;
  124. }
  125. grs_bitmap *gr_create_sub_bitmap(grs_bitmap *bm, int x, int y, int w, int h )
  126. {
  127. grs_bitmap *new;
  128. new = (grs_bitmap *)malloc( sizeof(grs_bitmap) );
  129. new->bm_x = x+bm->bm_x;
  130. new->bm_y = y+bm->bm_y;
  131. new->bm_w = w;
  132. new->bm_h = h;
  133. new->bm_flags = bm->bm_flags;
  134. new->bm_type = bm->bm_type;
  135. new->bm_rowsize = bm->bm_rowsize;
  136. new->bm_data = bm->bm_data+(unsigned int)((y*bm->bm_rowsize)+x);
  137. new->bm_selector = 0;
  138. return new;
  139. }
  140. gr_free_bitmap(grs_bitmap *bm )
  141. {
  142. if (bm->bm_data!=NULL)
  143. free(bm->bm_data);
  144. bm->bm_data = NULL;
  145. if (bm!=NULL)
  146. free(bm);
  147. }
  148. gr_free_sub_bitmap(grs_bitmap *bm )
  149. {
  150. if (bm!=NULL)
  151. free(bm);
  152. }
  153. //NO_INVERSE_TABLE void build_colormap_asm( ubyte * palette, ubyte * cmap, int * count );
  154. //NO_INVERSE_TABLE #pragma aux build_colormap_asm parm [esi] [edi] [edx] modify exact [eax ebx ecx edx esi edi] = \
  155. //NO_INVERSE_TABLE "mov ecx, 256" \
  156. //NO_INVERSE_TABLE "xor eax,eax" \
  157. //NO_INVERSE_TABLE "again2x:" \
  158. //NO_INVERSE_TABLE "mov al,[esi]" \
  159. //NO_INVERSE_TABLE "inc esi" \
  160. //NO_INVERSE_TABLE "shr eax, 1" \
  161. //NO_INVERSE_TABLE "shl eax, 5" \
  162. //NO_INVERSE_TABLE "mov bl,[esi]" \
  163. //NO_INVERSE_TABLE "inc esi" \
  164. //NO_INVERSE_TABLE "shr bl, 1" \
  165. //NO_INVERSE_TABLE "or al, bl" \
  166. //NO_INVERSE_TABLE "shl eax, 5" \
  167. //NO_INVERSE_TABLE "mov bl,[esi]" \
  168. //NO_INVERSE_TABLE "inc esi" \
  169. //NO_INVERSE_TABLE "shr bl, 1" \
  170. //NO_INVERSE_TABLE "or al, bl" \
  171. //NO_INVERSE_TABLE "mov al, gr_inverse_table[eax]" \
  172. //NO_INVERSE_TABLE "mov [edi], al" \
  173. //NO_INVERSE_TABLE "inc edi" \
  174. //NO_INVERSE_TABLE "xor eax,eax" \
  175. //NO_INVERSE_TABLE "mov [edx], eax" \
  176. //NO_INVERSE_TABLE "add edx, 4" \
  177. //NO_INVERSE_TABLE "dec ecx" \
  178. //NO_INVERSE_TABLE "jne again2x" \
  179. void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count );
  180. #pragma aux decode_data_asm parm [esi] [ecx] [edi] [ebx] modify exact [esi edi eax ebx ecx] = \
  181. "again_ddn:" \
  182. "xor eax,eax" \
  183. "mov al,[esi]" \
  184. "inc dword ptr [ebx+eax*4]" \
  185. "mov al,[edi+eax]" \
  186. "mov [esi],al" \
  187. "inc esi" \
  188. "dec ecx" \
  189. "jne again_ddn"
  190. void gr_remap_bitmap( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
  191. {
  192. ubyte colormap[256];
  193. int freq[256];
  194. // This should be build_colormap_asm, but we're not using invert table, so...
  195. build_colormap_good( palette, colormap, freq );
  196. if ( (super_transparent_color>=0) && (super_transparent_color<=255))
  197. colormap[super_transparent_color] = 254;
  198. if ( (transparent_color>=0) && (transparent_color<=255))
  199. colormap[transparent_color] = 255;
  200. decode_data_asm(bmp->bm_data, bmp->bm_w * bmp->bm_h, colormap, freq );
  201. if ( (transparent_color>=0) && (transparent_color<=255) && (freq[transparent_color]>0) )
  202. bmp->bm_flags |= BM_FLAG_TRANSPARENT;
  203. if ( (super_transparent_color>=0) && (super_transparent_color<=255) && (freq[super_transparent_color]>0) )
  204. bmp->bm_flags |= BM_FLAG_SUPER_TRANSPARENT;
  205. }
  206. void build_colormap_good( ubyte * palette, ubyte * colormap, int * freq )
  207. {
  208. int i, r, g, b;
  209. for (i=0; i<256; i++ ) {
  210. r = *palette++;
  211. g = *palette++;
  212. b = *palette++;
  213. *colormap++ = gr_find_closest_color( r, g, b );
  214. *freq++ = 0;
  215. }
  216. }
  217. void gr_remap_bitmap_good( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
  218. {
  219. ubyte colormap[256];
  220. int freq[256];
  221. build_colormap_good( palette, colormap, freq );
  222. if ( (super_transparent_color>=0) && (super_transparent_color<=255))
  223. colormap[super_transparent_color] = 254;
  224. if ( (transparent_color>=0) && (transparent_color<=255))
  225. colormap[transparent_color] = 255;
  226. decode_data_asm(bmp->bm_data, bmp->bm_w * bmp->bm_h, colormap, freq );
  227. if ( (transparent_color>=0) && (transparent_color<=255) && (freq[transparent_color]>0) )
  228. bmp->bm_flags |= BM_FLAG_TRANSPARENT;
  229. if ( (super_transparent_color>=0) && (super_transparent_color<=255) && (freq[super_transparent_color]>0) )
  230. bmp->bm_flags |= BM_FLAG_SUPER_TRANSPARENT;
  231. }
  232. int gr_bitmap_assign_selector( grs_bitmap * bmp )
  233. {
  234. if (!dpmi_allocate_selector( bmp->bm_data, bmp->bm_w*bmp->bm_h, &bmp->bm_selector )) {
  235. bmp->bm_selector = 0;
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. void gr_bitmap_check_transparency( grs_bitmap * bmp )
  241. {
  242. int x, y;
  243. ubyte * data;
  244. data = bmp->bm_data;
  245. for (y=0; y<bmp->bm_h; y++ ) {
  246. for (x=0; x<bmp->bm_w; x++ ) {
  247. if (*data++ == 255 ) {
  248. bmp->bm_flags = BM_FLAG_TRANSPARENT;
  249. return;
  250. }
  251. }
  252. data += bmp->bm_rowsize - bmp->bm_w;
  253. }
  254. bmp->bm_flags = 0;
  255. }
  256.