DOOM64-RE

DOOM 64 Reverse Engineering
Log | Files | Refs | README | LICENSE

c_convert.c (6138B)


      1 /* c_convert.c  */
      2 
      3 /*-----------------------------------*/
      4 /* Color Converter RGB2HSV & HSV2RGB */
      5 /*-----------------------------------*/
      6 
      7 #include "doomdef.h"
      8 
      9 /*
     10 ===================
     11 =
     12 = LightGetHSV
     13 = Set HSV values based on given RGB
     14 =
     15 ===================
     16 */
     17 
     18 void LightGetHSV(int r,int g,int b,int *h,int *s,int *v) // 800020BC
     19 {
     20     register u32 fpstat, fpstatset;
     21 
     22     int min;
     23     int max;
     24     float deltamin;
     25     float deltamax;
     26     float j;
     27     float x;
     28     float xr;
     29     float xg;
     30     float xb;
     31     float sum;
     32 
     33     max = MAXINT;
     34 
     35     if(r < max) {
     36         max = r;
     37     }
     38     if(g < max) {
     39         max = g;
     40     }
     41     if(b < max) {
     42         max = b;
     43     }
     44 
     45     min = MININT;
     46 
     47     if(r > min) {
     48         min = r;
     49     }
     50     if(g > min) {
     51         min = g;
     52     }
     53     if(b > min) {
     54         min = b;
     55     }
     56 
     57     deltamin = (float)((double)min / (double)255.0);
     58     deltamax = deltamin - (float)((double)max / (double)255.0);
     59 
     60     if((double)deltamin == 0.0) {
     61         j = 0.0;
     62     }
     63     else {
     64         j = deltamax / deltamin;
     65     }
     66 
     67     if((double)j != 0.0)
     68     {
     69         xr = (float)((double)r / (double)255.0);
     70         xg = (float)((double)g / (double)255.0);
     71         xb = (float)((double)b / (double)255.0);
     72 
     73         if(xr != deltamin)
     74         {
     75             if(xg != deltamin)
     76             {
     77                 if(xb == deltamin)
     78                 {
     79                     sum = ((deltamin - xg) / deltamax + 4.0) -
     80                           ((deltamin - xr) / deltamax);
     81                 }
     82             }
     83             else
     84             {
     85                 sum = ((deltamin - xr) / deltamax + 2.0) -
     86                       ((deltamin - xb) / deltamax);
     87             }
     88         }
     89         else
     90         {
     91             sum = ((deltamin - xb) / deltamax) -
     92                   ((deltamin - xg) / deltamax);
     93         }
     94 
     95         x = (sum * 60.0);
     96 
     97         if(x < 0.0)
     98         {
     99             x = (float)((double)x + (double)360.0);
    100         }
    101     }
    102     else
    103     {
    104         j = 0.0;
    105     }
    106 
    107     // fetch the current floating-point control/status register
    108 	fpstat = __osGetFpcCsr();
    109 	// enable round to negative infinity for floating point
    110 	fpstatset = (fpstat | FPCSR_RM_RM) ^ 2;
    111 	// _Disable_ unimplemented operation exception for floating point.
    112 	__osSetFpcCsr(fpstatset);
    113 
    114     *h = (int)(((double)x / (double)360.0) * (double)255.0);
    115 
    116     // fetch the current floating-point control/status register
    117     fpstat = __osGetFpcCsr();
    118     // enable round to negative infinity for floating point
    119     fpstatset = (fpstat | FPCSR_RM_RM) ^ 2;
    120     // _Disable_ unimplemented operation exception for floating point.
    121     __osSetFpcCsr(fpstatset);
    122     *s = (int)((double)j * (double)255.0);
    123 
    124     // fetch the current floating-point control/status register
    125     fpstat = __osGetFpcCsr();
    126     // enable round to negative infinity for floating point
    127     fpstatset = (fpstat | FPCSR_RM_RM) ^ 2;
    128     // _Disable_ unimplemented operation exception for floating point.
    129     __osSetFpcCsr(fpstatset);
    130     *v = (int)((double)deltamin * (double)255.0);
    131 }
    132 
    133 /*
    134 ===================
    135 =
    136 = LightGetRGB
    137 = Set RGB values based on given HSV
    138 =
    139 ===================
    140 */
    141 
    142 void LightGetRGB(int h,int s,int v,int *r,int *g,int *b) // 8000248C
    143 {
    144     register u32 fpstat, fpstatset;
    145 
    146     float x;
    147     float j;
    148     float i;
    149     float t;
    150     int table;
    151     float xr;
    152     float xg;
    153     float xb;
    154 
    155     j = (float)(((double)h / (double)255.0) * (double)360.0);
    156 
    157     if((double)360.0 <= (double)j) {
    158         j = (float)((double)j - (double)360.0);
    159     }
    160 
    161     x = ((double)s / (double)255.0);
    162     i = ((double)v / (double)255.0);
    163 
    164     if(x != 0.0)
    165     {
    166         // fetch the current floating-point control/status register
    167         fpstat = __osGetFpcCsr();
    168         // enable round to negative infinity for floating point
    169         fpstatset = (fpstat | FPCSR_RM_RM) ^ 2;
    170         // _Disable_ unimplemented operation exception for floating point.
    171         __osSetFpcCsr(fpstatset);
    172 
    173         table = (int)(j / 60.0);
    174         if(table < 6)
    175         {
    176             t = (j / 60.0);
    177             switch(table) {
    178             case 0:
    179                 xr = i;
    180                 xg = ((1.0 - ((1.0 - (t - (float)table)) * x)) * i);
    181                 xb = ((1.0 - x) * i);
    182                 break;
    183             case 1:
    184                 xr = ((1.0 - (x * (t - (float)table))) * i);
    185                 xg = i;
    186                 xb = ((1.0 - x) * i);
    187                 break;
    188             case 2:
    189                 xr = ((1.0 - x) * i);
    190                 xg = i;
    191                 xb = ((1.0 - ((1.0 - (t - (float)table)) * x)) * i);
    192                 break;
    193             case 3:
    194                 xr = ((1.0 - x) * i);
    195                 xg = ((1.0 - (x * (t - (float)table))) * i);
    196                 xb = i;
    197                 break;
    198             case 4:
    199                 xr = ((1.0 - ((1.0 - (t - (float)table)) * x)) * i);
    200                 xg = ((1.0 - x) * i);
    201                 xb = i;
    202                 break;
    203             case 5:
    204                 xr = i;
    205                 xg = ((1.0 - x) * i);
    206                 xb = ((1.0 - (x * (t - (float)table))) * i);
    207                 break;
    208             }
    209         }
    210     }
    211     else
    212     {
    213         xr = xg = xb = i;
    214     }
    215 
    216     // fetch the current floating-point control/status register
    217 	fpstat = __osGetFpcCsr();
    218 	// enable round to negative infinity for floating point
    219 	fpstatset = (fpstat | FPCSR_RM_RM) ^ 2;
    220 	// _Disable_ unimplemented operation exception for floating point.
    221 	__osSetFpcCsr(fpstatset);
    222 
    223     *r = (int)((double)xr * (double)255.0);
    224 
    225     // fetch the current floating-point control/status register
    226     fpstat = __osGetFpcCsr();
    227     // enable round to negative infinity for floating point
    228     fpstatset = (fpstat | FPCSR_RM_RM) ^ 2;
    229     // _Disable_ unimplemented operation exception for floating point.
    230     __osSetFpcCsr(fpstatset);
    231 
    232     *g = (int)((double)xg * (double)255.0);
    233 
    234     // fetch the current floating-point control/status register
    235     fpstat = __osGetFpcCsr();
    236     // enable round to negative infinity for floating point
    237     fpstatset = (fpstat | FPCSR_RM_RM) ^ 2;
    238     // _Disable_ unimplemented operation exception for floating point.
    239     __osSetFpcCsr(fpstatset);
    240 
    241     *b = (int)((double)xb * (double)255.0);
    242 }