home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / 2d / bitmap.c < prev    next >
Text File  |  1998-06-08  |  8KB  |  302 lines

  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.  
  77. #include <stdlib.h>
  78. #include <malloc.h>
  79. #include <stdio.h>
  80.  
  81. #include "mem.h"
  82.  
  83.  
  84. #include "gr.h"
  85. #include "grdef.h"
  86. #include "dpmi.h"
  87.  
  88. grs_bitmap *gr_create_bitmap(int w, int h )
  89. {
  90.     grs_bitmap *new;
  91.  
  92.     new = (grs_bitmap *)malloc( sizeof(grs_bitmap) );
  93.     new->bm_x = 0;
  94.     new->bm_y = 0;
  95.     new->bm_w = w;
  96.     new->bm_h = h;
  97.     new->bm_type = 0;
  98.     new->bm_flags = 0;
  99.     new->bm_rowsize = w;
  100.     new->bm_selector = 0;
  101.  
  102.     new->bm_data = (unsigned char *)malloc( w*h );
  103.  
  104.     return new;
  105. }
  106.  
  107. grs_bitmap *gr_create_bitmap_raw(int w, int h, unsigned char * raw_data )
  108. {
  109.     grs_bitmap *new;
  110.  
  111.     new = (grs_bitmap *)malloc( sizeof(grs_bitmap) );
  112.     new->bm_x = 0;
  113.     new->bm_y = 0;
  114.     new->bm_w = w;
  115.     new->bm_h = h;
  116.     new->bm_flags = 0;
  117.     new->bm_type = 0;
  118.     new->bm_rowsize = w;
  119.     new->bm_data = raw_data;
  120.     new->bm_selector = 0;
  121.  
  122.     return new;
  123. }
  124.  
  125. void gr_init_bitmap( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline, unsigned char * data )
  126. {
  127.     bm->bm_x = x;
  128.     bm->bm_y = y;
  129.     bm->bm_w = w;
  130.     bm->bm_h = h;
  131.     bm->bm_flags = 0;
  132.     bm->bm_type = mode;
  133.     bm->bm_rowsize = bytesperline;
  134.     bm->bm_data = data;
  135.     bm->bm_selector = 0;
  136. }
  137.  
  138.  
  139. grs_bitmap *gr_create_sub_bitmap(grs_bitmap *bm, int x, int y, int w, int h )
  140. {
  141.     grs_bitmap *new;
  142.  
  143.     new = (grs_bitmap *)malloc( sizeof(grs_bitmap) );
  144.     new->bm_x = x+bm->bm_x;
  145.     new->bm_y = y+bm->bm_y;
  146.     new->bm_w = w;
  147.     new->bm_h = h;
  148.     new->bm_flags = bm->bm_flags;
  149.     new->bm_type = bm->bm_type;
  150.     new->bm_rowsize = bm->bm_rowsize;
  151.     new->bm_data = bm->bm_data+(unsigned int)((y*bm->bm_rowsize)+x);
  152.     new->bm_selector = 0;
  153.  
  154.     return new;
  155. }
  156.  
  157.  
  158. gr_free_bitmap(grs_bitmap *bm )
  159. {
  160.     if (bm->bm_data!=NULL)    
  161.     free(bm->bm_data);
  162.     bm->bm_data = NULL;
  163.     if (bm!=NULL)
  164.     free(bm);
  165. }
  166.  
  167. gr_free_sub_bitmap(grs_bitmap *bm )
  168. {
  169.     if (bm!=NULL)
  170.     free(bm);
  171. }
  172.  
  173. //NO_INVERSE_TABLE void build_colormap_asm( ubyte * palette, ubyte * cmap, int * count );
  174. //NO_INVERSE_TABLE #pragma aux build_colormap_asm parm [esi] [edi] [edx] modify exact [eax ebx ecx edx esi edi] = \
  175. //NO_INVERSE_TABLE     "mov  ecx, 256"            \
  176. //NO_INVERSE_TABLE     "xor    eax,eax"                \
  177. //NO_INVERSE_TABLE "again2x:"                        \
  178. //NO_INVERSE_TABLE     "mov    al,[esi]"            \
  179. //NO_INVERSE_TABLE     "inc    esi"                    \
  180. //NO_INVERSE_TABLE     "shr    eax, 1"                \
  181. //NO_INVERSE_TABLE     "shl    eax, 5"                \
  182. //NO_INVERSE_TABLE     "mov    bl,[esi]"            \
  183. //NO_INVERSE_TABLE     "inc    esi"                    \
  184. //NO_INVERSE_TABLE     "shr    bl, 1"                \
  185. //NO_INVERSE_TABLE     "or    al, bl"                \
  186. //NO_INVERSE_TABLE     "shl    eax, 5"                \
  187. //NO_INVERSE_TABLE     "mov    bl,[esi]"            \
  188. //NO_INVERSE_TABLE     "inc    esi"                    \
  189. //NO_INVERSE_TABLE     "shr    bl, 1"                \
  190. //NO_INVERSE_TABLE     "or     al, bl"                \
  191. //NO_INVERSE_TABLE     "mov    al, gr_inverse_table[eax]"            \
  192. //NO_INVERSE_TABLE     "mov    [edi], al"            \
  193. //NO_INVERSE_TABLE     "inc    edi"                    \
  194. //NO_INVERSE_TABLE     "xor    eax,eax"                \
  195. //NO_INVERSE_TABLE     "mov    [edx], eax"            \
  196. //NO_INVERSE_TABLE     "add    edx, 4"                    \
  197. //NO_INVERSE_TABLE     "dec    ecx"                    \
  198. //NO_INVERSE_TABLE     "jne    again2x"                \
  199.  
  200. void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count );
  201. #pragma aux decode_data_asm parm [esi] [ecx] [edi] [ebx] modify exact [esi edi eax ebx ecx] = \
  202. "again_ddn:"                            \
  203.     "xor    eax,eax"                \
  204.     "mov    al,[esi]"            \
  205.     "inc    dword ptr [ebx+eax*4]"        \
  206.     "mov    al,[edi+eax]"        \
  207.     "mov    [esi],al"            \
  208.     "inc    esi"                    \
  209.     "dec    ecx"                    \
  210.     "jne    again_ddn"
  211.  
  212. void gr_remap_bitmap( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
  213. {
  214.     ubyte colormap[256];
  215.     int freq[256];
  216.  
  217.     // This should be build_colormap_asm, but we're not using invert table, so...
  218.     build_colormap_good( palette, colormap, freq );
  219.  
  220.     if ( (super_transparent_color>=0) && (super_transparent_color<=255))
  221.         colormap[super_transparent_color] = 254;
  222.  
  223.     if ( (transparent_color>=0) && (transparent_color<=255))
  224.         colormap[transparent_color] = 255;
  225.  
  226.     decode_data_asm(bmp->bm_data, bmp->bm_w * bmp->bm_h, colormap, freq );
  227.  
  228.     if ( (transparent_color>=0) && (transparent_color<=255) && (freq[transparent_color]>0) )
  229.         bmp->bm_flags |= BM_FLAG_TRANSPARENT;
  230.  
  231.     if ( (super_transparent_color>=0) && (super_transparent_color<=255) && (freq[super_transparent_color]>0) )
  232.         bmp->bm_flags |= BM_FLAG_SUPER_TRANSPARENT;
  233. }
  234.  
  235. void build_colormap_good( ubyte * palette, ubyte * colormap, int * freq )
  236. {
  237.     int i, r, g, b;
  238.  
  239.     for (i=0; i<256; i++ )    {
  240.         r = *palette++;        
  241.         g = *palette++;        
  242.         b = *palette++;        
  243.          *colormap++ = gr_find_closest_color( r, g, b );
  244.         *freq++ = 0;
  245.     }
  246. }
  247.  
  248.  
  249. void gr_remap_bitmap_good( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
  250. {
  251.     ubyte colormap[256];
  252.     int freq[256];
  253.    
  254.     build_colormap_good( palette, colormap, freq );
  255.  
  256.     if ( (super_transparent_color>=0) && (super_transparent_color<=255))
  257.         colormap[super_transparent_color] = 254;
  258.  
  259.     if ( (transparent_color>=0) && (transparent_color<=255))
  260.         colormap[transparent_color] = 255;
  261.  
  262.     decode_data_asm(bmp->bm_data, bmp->bm_w * bmp->bm_h, colormap, freq );
  263.  
  264.     if ( (transparent_color>=0) && (transparent_color<=255) && (freq[transparent_color]>0) )
  265.         bmp->bm_flags |= BM_FLAG_TRANSPARENT;
  266.  
  267.     if ( (super_transparent_color>=0) && (super_transparent_color<=255) && (freq[super_transparent_color]>0) )
  268.         bmp->bm_flags |= BM_FLAG_SUPER_TRANSPARENT;
  269. }
  270.  
  271.  
  272. int gr_bitmap_assign_selector( grs_bitmap * bmp )
  273. {
  274.     if (!dpmi_allocate_selector( bmp->bm_data, bmp->bm_w*bmp->bm_h, &bmp->bm_selector )) {
  275.         bmp->bm_selector = 0;
  276.         return 1;
  277.     }
  278.     return 0;
  279. }
  280.  
  281. void gr_bitmap_check_transparency( grs_bitmap * bmp )
  282. {
  283.     int x, y;
  284.     ubyte * data;
  285.  
  286.     data = bmp->bm_data;
  287.     
  288.     for (y=0; y<bmp->bm_h; y++ )    {
  289.         for (x=0; x<bmp->bm_w; x++ )    {
  290.             if (*data++ == 255 )    {
  291.                 bmp->bm_flags = BM_FLAG_TRANSPARENT;
  292.                 return;
  293.             }
  294.         }
  295.         data += bmp->bm_rowsize - bmp->bm_w;
  296.     }
  297.  
  298.     bmp->bm_flags = 0;
  299.  
  300. }
  301. 
  302.