00001 #include "procutil.h"
00002 #include <time.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include <assert.h>
00006 #include "errorlog.h"
00007
00008 static uint32 lastRandomNumber = 0xA2A9;
00009
00010 void ProcUtil_Randomize( void )
00011 {
00012 uint32 cnt;
00013 cnt = clock();
00014 cnt &= 0xFFFF;
00015 while(cnt--)
00016 {
00017 lastRandomNumber = lastRandomNumber*65539+3;
00018 lastRandomNumber = lastRandomNumber*1009 +7;
00019 }
00020 }
00021
00022 uint32 ProcUtil_Rand( uint32 max )
00023 {
00024 uint32 a;
00025
00026 lastRandomNumber = lastRandomNumber*65539+3;
00027
00028 a = lastRandomNumber>>16;
00029
00030 return a%max;
00031 }
00032
00033 float ProcUtil_SinTable[TABLE_SIZE];
00034 float ProcUtil_CosTable[TABLE_SIZE];
00035
00036 int ProcUtil_ByteSinTable[TABLE_SIZE];
00037 int ProcUtil_ByteCosTable[TABLE_SIZE];
00038
00039 void ProcUtil_Init(void)
00040 {
00041 int i;
00042 double radVal;
00043
00044 for (i = 0; i < TABLE_SIZE; i ++)
00045 {
00046 radVal = i * (TWO_PI / TABLE_SIZE );
00047
00048 ProcUtil_SinTable[i] = (float)sin(radVal);
00049 ProcUtil_CosTable[i] = (float)cos(radVal);
00050 }
00051
00052 for (i = 0; i < 256; i ++)
00053 {
00054 radVal = i * (TWO_PI / 256.0);
00055
00056 ProcUtil_ByteSinTable[i] = (int) (127.0 * (sin(radVal) + 1.0) + 0.49);
00057 ProcUtil_ByteCosTable[i] = (int) (127.0 * (cos(radVal) + 1.0) + 0.49);
00058 }
00059 }
00060
00061
00062
00063
00064
00065
00066 geBoolean ProcUtil_SetPaletteFromString(geBitmap * Bitmap,char ** pParams)
00067 {
00068 geBitmap_Info Info;
00069 geBitmap_Palette *Pal;
00070 void * PalData = NULL;
00071 gePixelFormat PalFormat;
00072 int PalSize;
00073
00074 char ParamWork[1024];
00075 char *pstr;
00076 const char * strbreakers = " ,\t\n\r\34\39\09";
00077
00078 #define nextparam() do { if ( ! pstr ) { geErrorLog_AddString(-1,"ProcEng_CreatePalette : params insufficient",NULL); goto fail; }; pstr = strtok(NULL,strbreakers); } while(0)
00079 #define getint() ( pstr == NULL ) ? 0 : atol(pstr); nextparam();
00080 #define getfloat() ( pstr == NULL ) ? 0.0f : (float)atof(pstr); nextparam();
00081
00082 if ( pParams )
00083 {
00084 assert(*pParams);
00085 strcpy(ParamWork,*pParams);
00086 }
00087 else
00088 {
00089 strcpy(ParamWork,"list, 3, 0,0,0,0, 200,50,0,100, 255,100,50,255");
00090 }
00091
00092 pstr = strtok(ParamWork,strbreakers);
00093
00094 assert(Bitmap);
00095
00096 Pal = geBitmap_Palette_Create(GE_PIXELFORMAT_32BIT_ARGB,256);
00097 if ( ! Pal )
00098 goto fail;
00099 if ( ! geBitmap_SetPalette(Bitmap,Pal) )
00100 goto fail;
00101 geBitmap_Palette_Destroy(&Pal);
00102
00103 geBitmap_GetInfo(Bitmap,&Info,NULL);
00104
00105 if ( ! (Pal = geBitmap_GetPalette(Bitmap)) )
00106 goto fail;
00107
00108 if ( ! geBitmap_Palette_Lock(Pal,&PalData, &PalFormat, &PalSize) )
00109 goto fail;
00110 if ( PalSize < 256 )
00111 goto fail;
00112
00113 if ( strnicmp(pstr,"list",4) == 0 )
00114 {
00115 int p,lp,r,g,b,a;
00116 uint8 * PalPtr;
00117 int NumColors;
00118 int pr,pg,pb,pa;
00119 int nr,ng,nb,na;
00120 double cstep,nextc,icstep;
00121
00122 nextparam();
00123 NumColors = getint();
00124 if ( NumColors == 0 )
00125 {
00126 *pParams += (int)(pstr - ParamWork);
00127 pParams = NULL;
00128 strcpy(ParamWork,"list, 3, 0,0,0,0, 200,50,0,100, 255,100,50,255");
00129 }
00130 else if ( NumColors < 2 )
00131 goto fail;
00132
00133 cstep = 256.0 / (NumColors - 1);
00134 nextc = 0.0;
00135 icstep = 1.0 / cstep;
00136
00137 NumColors--;
00138 nr = getint();
00139 ng = getint();
00140 nb = getint();
00141 na = getint();
00142
00143 PalPtr = PalData;
00144 for(p=0;p<256;p++)
00145 {
00146 if ( p >= (int)nextc )
00147 {
00148 pr = nr;
00149 pg = ng;
00150 pb = nb;
00151 pa = na;
00152 nr = getint();
00153 ng = getint();
00154 nb = getint();
00155 na = getint();
00156 nextc += cstep;
00157 lp = p;
00158 }
00159
00160
00161
00162 r = (int)(pr + (nr - pr)*(p - lp)*icstep);
00163 g = (int)(pg + (ng - pg)*(p - lp)*icstep);
00164 b = (int)(pb + (nb - pb)*(p - lp)*icstep);
00165 a = (int)(pa + (na - pa)*(p - lp)*icstep);
00166
00167 gePixelFormat_PutColor(PalFormat,&PalPtr,min(r,255),min(g,255),min(b,255),min(a,255));
00168 }
00169 }
00170 else if ( strnicmp(pstr,"pow",4) == 0 )
00171 {
00172 int p,r,g,b,a;
00173 uint8 * PalPtr;
00174 double pr,pg,pb,pa,frac;
00175 int nr,ng,nb,na;
00176
00177 nextparam();
00178
00179 nr = getint();
00180 ng = getint();
00181 nb = getint();
00182 na = getint();
00183 if ( pstr )
00184 {
00185 pr = getfloat();
00186 pg = getfloat();
00187 pb = getfloat();
00188 pa = getfloat();
00189 }
00190 else
00191 {
00192 pr = pg = pb = pa = 0.5;
00193 }
00194
00195 PalPtr = PalData;
00196 for(p=0;p<256;p++)
00197 {
00198 frac = (double)p * (1.0/256.0);
00199 r = (int)( nr * pow( frac, pr) );
00200 g = (int)( ng * pow( frac, pg) );
00201 b = (int)( nb * pow( frac, pb) );
00202 a = (int)( na * pow( frac, pa) );
00203 gePixelFormat_PutColor(PalFormat,&PalPtr,min(r,255),min(g,255),min(b,255),min(a,255));
00204 }
00205 }
00206 else if ( strnicmp(pstr,"trig",4) == 0 )
00207 {
00208 int p,r,g,b,a;
00209 uint8 * PalPtr;
00210 float r_mult,g_mult,b_mult,a_mult;
00211 float r_freq,g_freq,b_freq,a_freq;
00212 float r_base,g_base,b_base,a_base;
00213 float frac;
00214
00215 nextparam();
00216
00217 r_mult = getfloat();
00218 g_mult = getfloat();
00219 b_mult = getfloat();
00220 a_mult = getfloat();
00221 r_freq = getfloat();
00222 g_freq = getfloat();
00223 b_freq = getfloat();
00224 a_freq = getfloat();
00225 r_base = getfloat(); r_base *= TWO_PI;
00226 g_base = getfloat(); g_base *= TWO_PI;
00227 b_base = getfloat(); b_base *= TWO_PI;
00228 a_base = getfloat(); a_base *= TWO_PI;
00229
00230 PalPtr = PalData;
00231 for(p=0;p<256;p++)
00232 {
00233 frac = (float)p * (TWO_PI/256.0f);
00234 r = (int)( r_mult * (cos( r_freq * frac + r_base ) + 1.0) );
00235 g = (int)( g_mult * (cos( g_freq * frac + g_base ) + 1.0) );
00236 b = (int)( b_mult * (cos( b_freq * frac + b_base ) + 1.0) );
00237 a = (int)( a_mult * (cos( a_freq * frac + a_base ) + 1.0) );
00238 gePixelFormat_PutColor(PalFormat,&PalPtr,min(r,255),min(g,255),min(b,255),min(a,255));
00239 }
00240 }
00241 else
00242 {
00243 geErrorLog_AddString(-1,"ProcEng_MakePalette : no palette type!",NULL);
00244 }
00245
00246 PalData = NULL;
00247
00248 if ( ! geBitmap_Palette_UnLock(Pal) )
00249 return GE_FALSE;
00250
00251 if ( pParams )
00252 {
00253 *pParams += (int)pstr - (int)ParamWork;
00254 }
00255
00256 return GE_TRUE;
00257
00258 fail:
00259
00260 if ( PalData )
00261 geBitmap_Palette_UnLock(Pal);
00262
00263 return GE_FALSE;
00264 }