BITMAP.C 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #include <stdlib.h>
  14. #include <malloc.h>
  15. #include <stdio.h>
  16. #include "pa_enabl.h" //$$POLY_ACC
  17. #include "mem.h"
  18. #include "error.h"
  19. #include "gr.h"
  20. #include "grdef.h"
  21. #if defined(POLY_ACC)
  22. #include "poly_acc.h"
  23. #endif
  24. grs_bitmap *gr_create_bitmap(int w, int h )
  25. {
  26. grs_bitmap *new;
  27. new = (grs_bitmap *)malloc( sizeof(grs_bitmap) );
  28. new->bm_x = 0;
  29. new->bm_y = 0;
  30. new->bm_w = w;
  31. new->bm_h = h;
  32. new->bm_type = 0;
  33. new->bm_flags = 0;
  34. new->bm_rowsize = w;
  35. new->bm_handle = 0;
  36. new->bm_data = (unsigned char *)malloc( w*h );
  37. return new;
  38. }
  39. grs_bitmap *gr_create_bitmap_raw(int w, int h, unsigned char * raw_data )
  40. {
  41. grs_bitmap *new;
  42. new = (grs_bitmap *)malloc( sizeof(grs_bitmap) );
  43. new->bm_x = 0;
  44. new->bm_y = 0;
  45. new->bm_w = w;
  46. new->bm_h = h;
  47. new->bm_flags = 0;
  48. new->bm_type = 0;
  49. new->bm_rowsize = w;
  50. new->bm_data = raw_data;
  51. new->bm_handle = 0;
  52. return new;
  53. }
  54. #if defined(POLY_ACC)
  55. //
  56. // Creates a bitmap of the requested size and type.
  57. // w, and h are in pixels.
  58. // type is a BM_... and is used to set the rowsize.
  59. // if data is NULL, memory is allocated, otherwise data is used for bm_data.
  60. //
  61. // This function is used only by the polygon accelerator code to handle the mixture of 15bit and
  62. // 8bit bitmaps.
  63. //
  64. grs_bitmap *gr_create_bitmap2(int w, int h, int type, void *data )
  65. {
  66. grs_bitmap *new;
  67. new = (grs_bitmap *)malloc( sizeof(grs_bitmap) );
  68. new->bm_x = 0;
  69. new->bm_y = 0;
  70. new->bm_w = w;
  71. new->bm_h = h;
  72. new->bm_flags = 0;
  73. new->bm_type = type;
  74. switch(type)
  75. {
  76. case BM_LINEAR: new->bm_rowsize = w; break;
  77. case BM_LINEAR15: new->bm_rowsize = w*PA_BPP; break;
  78. default: Int3(); // unsupported type.
  79. }
  80. if(data)
  81. new->bm_data = data;
  82. else
  83. new->bm_data = malloc(new->bm_rowsize * new->bm_h);
  84. new->bm_handle = 0;
  85. return new;
  86. }
  87. #endif
  88. void gr_init_bitmap( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline, unsigned char * data )
  89. {
  90. bm->bm_x = x;
  91. bm->bm_y = y;
  92. bm->bm_w = w;
  93. bm->bm_h = h;
  94. bm->bm_flags = 0;
  95. bm->bm_type = mode;
  96. bm->bm_rowsize = bytesperline;
  97. bm->bm_data = data;
  98. bm->bm_handle = 0;
  99. }
  100. grs_bitmap *gr_create_sub_bitmap(grs_bitmap *bm, int x, int y, int w, int h )
  101. {
  102. grs_bitmap *new;
  103. new = (grs_bitmap *)malloc( sizeof(grs_bitmap) );
  104. new->bm_x = x+bm->bm_x;
  105. new->bm_y = y+bm->bm_y;
  106. new->bm_w = w;
  107. new->bm_h = h;
  108. new->bm_flags = bm->bm_flags;
  109. new->bm_type = bm->bm_type;
  110. new->bm_rowsize = bm->bm_rowsize;
  111. new->bm_data = bm->bm_data+(unsigned int)((y*bm->bm_rowsize)+x);
  112. new->bm_handle = 0;
  113. return new;
  114. }
  115. void gr_free_bitmap(grs_bitmap *bm )
  116. {
  117. if (bm->bm_data != NULL)
  118. free(bm->bm_data);
  119. bm->bm_data = NULL;
  120. if (bm != NULL)
  121. free(bm);
  122. }
  123. void gr_free_sub_bitmap(grs_bitmap *bm )
  124. {
  125. if (bm != NULL)
  126. free(bm);
  127. }
  128. //NO_INVERSE_TABLE void build_colormap_asm( ubyte * palette, ubyte * cmap, int * count );
  129. //NO_INVERSE_TABLE #pragma aux build_colormap_asm parm [esi] [edi] [edx] modify exact [eax ebx ecx edx esi edi] = \
  130. //NO_INVERSE_TABLE "mov ecx, 256" \
  131. //NO_INVERSE_TABLE "xor eax,eax" \
  132. //NO_INVERSE_TABLE "again2x:" \
  133. //NO_INVERSE_TABLE "mov al,[esi]" \
  134. //NO_INVERSE_TABLE "inc esi" \
  135. //NO_INVERSE_TABLE "shr eax, 1" \
  136. //NO_INVERSE_TABLE "shl eax, 5" \
  137. //NO_INVERSE_TABLE "mov bl,[esi]" \
  138. //NO_INVERSE_TABLE "inc esi" \
  139. //NO_INVERSE_TABLE "shr bl, 1" \
  140. //NO_INVERSE_TABLE "or al, bl" \
  141. //NO_INVERSE_TABLE "shl eax, 5" \
  142. //NO_INVERSE_TABLE "mov bl,[esi]" \
  143. //NO_INVERSE_TABLE "inc esi" \
  144. //NO_INVERSE_TABLE "shr bl, 1" \
  145. //NO_INVERSE_TABLE "or al, bl" \
  146. //NO_INVERSE_TABLE "mov al, gr_inverse_table[eax]" \
  147. //NO_INVERSE_TABLE "mov [edi], al" \
  148. //NO_INVERSE_TABLE "inc edi" \
  149. //NO_INVERSE_TABLE "xor eax,eax" \
  150. //NO_INVERSE_TABLE "mov [edx], eax" \
  151. //NO_INVERSE_TABLE "add edx, 4" \
  152. //NO_INVERSE_TABLE "dec ecx" \
  153. //NO_INVERSE_TABLE "jne again2x" \
  154. void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count );
  155. #ifndef MACINTOSH
  156. #pragma aux decode_data_asm parm [esi] [ecx] [edi] [ebx] modify exact [esi edi eax ebx ecx] = \
  157. "again_ddn:" \
  158. "xor eax,eax" \
  159. "mov al,[esi]" \
  160. "inc dword ptr [ebx+eax*4]" \
  161. "mov al,[edi+eax]" \
  162. "mov [esi],al" \
  163. "inc esi" \
  164. "dec ecx" \
  165. "jne again_ddn"
  166. #else
  167. void decode_data_asm(ubyte *data, int num_pixels, ubyte *colormap, int *count)
  168. {
  169. int i;
  170. for (i = 0; i < num_pixels; i++) {
  171. count[*data]++;
  172. *data = colormap[*data];
  173. data++;
  174. }
  175. }
  176. #endif
  177. void build_colormap_good( ubyte * palette, ubyte * colormap, int * freq )
  178. {
  179. int i, r, g, b;
  180. for (i=0; i<256; i++ ) {
  181. r = *palette++;
  182. g = *palette++;
  183. b = *palette++;
  184. *colormap++ = gr_find_closest_color( r, g, b );
  185. *freq++ = 0;
  186. }
  187. }
  188. void gr_remap_bitmap( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
  189. {
  190. ubyte colormap[256];
  191. int freq[256];
  192. if (bmp->bm_type != BM_LINEAR)
  193. return; //can't do it
  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] = TRANSPARENCY_COLOR;
  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 gr_remap_bitmap_good( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
  207. {
  208. ubyte colormap[256];
  209. int freq[256];
  210. if (bmp->bm_type != BM_LINEAR) {
  211. Int3();
  212. return; //can't do it
  213. }
  214. build_colormap_good( palette, colormap, freq );
  215. if ( (super_transparent_color>=0) && (super_transparent_color<=255))
  216. colormap[super_transparent_color] = 254;
  217. if ( (transparent_color>=0) && (transparent_color<=255))
  218. colormap[transparent_color] = TRANSPARENCY_COLOR;
  219. if (bmp->bm_w == bmp->bm_rowsize)
  220. decode_data_asm(bmp->bm_data, bmp->bm_w * bmp->bm_h, colormap, freq );
  221. else {
  222. int y;
  223. ubyte *p = bmp->bm_data;
  224. for (y=0;y<bmp->bm_h;y++,p+=bmp->bm_rowsize)
  225. decode_data_asm(p, bmp->bm_w, colormap, freq );
  226. }
  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. void gr_bitmap_check_transparency( grs_bitmap * bmp )
  233. {
  234. int x, y;
  235. ubyte * data;
  236. data = bmp->bm_data;
  237. for (y=0; y<bmp->bm_h; y++ ) {
  238. for (x=0; x<bmp->bm_w; x++ ) {
  239. if (*data++ == TRANSPARENCY_COLOR ) {
  240. bmp->bm_flags = BM_FLAG_TRANSPARENT;
  241. return;
  242. }
  243. }
  244. data += bmp->bm_rowsize - bmp->bm_w;
  245. }
  246. bmp->bm_flags = 0;
  247. }