#include "php.h"
#include "php_imlib.h"
/* aliases for the exported symbols */
#define php_filter_init php_bumpmap_LTX_php_filter_init
#define php_filter_deinit php_bumpmap_LTX_php_filter_deinit
#define php_filter_exec php_bumpmap_LTX_php_filter_exec
#define php_filter_getfiltercount php_bumpmap_LTX_php_filter_getfiltercount
#define php_filter_getfilternames php_bumpmap_LTX_php_filter_getfilternames
#define php_filter_getextinfo php_bumpmap_LTX_php_filter_getextinfo
#define php_filter_getparams php_bumpmap_LTX_php_filter_getparams
static char* _php_bumpmap_stradd(char* str1,char* str2)
{
char* retval;
if ((str1)&&(str2))
{
retval=emalloc((strlen(str1)+strlen(str2)+1)*sizeof(char));
if (retval)
{
strcpy(retval,str1);
strcat(retval,str2);
efree(str1);
return retval;
}
}
return str1;
}
static int
_php_bumpmap_exec(char* filter, HashTable* params,int index)
{
char *allpnames[][10]={
{"map","angle","elevation","depth","red","green","blue","ambient",0},
{"map","x","y","z","depth","red","green","blue","ambient",0}
};
int allptypes[][10]={
{'I','i','i','i','i','i','i','i'},
{'I','i','i','i','i','i','i','i','i'}
};
char **paramnames;
int *paramtypes;
char *script;
Imlib_Image im=NULL;
HashPosition pos;
paramnames=allpnames[index];
paramtypes=allptypes[index];
script=estrdup(filter);
script=_php_bumpmap_stradd(script,"(");
zend_hash_internal_pointer_reset_ex(params,&pos);
while (pos)
{
char *str_index;
ulong num_index;
int retval;
retval=zend_hash_get_current_key_ex(params,&str_index,NULL,&num_index,0,&pos);
if (retval==HASH_KEY_IS_STRING)
{
int match=0;
int index=0;
while (paramnames[index])
{
if (strcmp(str_index,paramnames[index])==0)
{
char val[32];
zval **data;
zend_hash_get_current_data_ex(params,(void**)&data,&pos);
switch(paramtypes[index])
{
case 'I':
im=(Imlib_Image)_php_imlib_get_image(data); /* Why is cast to Imlib_Image needed? */
script=_php_bumpmap_stradd(script,"map=[]");
break;
case 'i':
convert_to_long_ex(data);
sprintf(val,"%d",(int)Z_LVAL_PP(data));
script=_php_bumpmap_stradd(script,str_index);
script=_php_bumpmap_stradd(script,"=");
script=_php_bumpmap_stradd(script,val);
break;
}
script=_php_bumpmap_stradd(script,",");
match=1;
break;
}
index++;
}
if (!match)
{
php_error(E_NOTICE,"Unknown argument %s ignored",str_index);
}
}
zend_hash_move_forward_ex(params,&pos);
}
if (script[strlen(script)-1]==',') script[strlen(script)-1]='\0';
script=_php_bumpmap_stradd(script,");");
if (!script) return FAILURE;
/*zend_printf("Script: %s, map: %p<br>",script,im);*/
imlib_apply_filter(script,im);
efree(script);
return SUCCESS;
}
int
php_filter_init()
{
return SUCCESS;
}
void
php_filter_deinit()
{
return;
}
int
php_filter_exec(Imlib_Image im, char *filter, HashTable *params)
{
int index=_php_bumpmap_getfilterindex(filter);
if (index!=-1)
{
int rv;
imlib_context_set_image(im);
rv=_php_bumpmap_exec(filter,params,index);
return rv;
}
return FAILURE;
}
int
php_filter_getfiltercount()
{
return 2;
}
void
php_filter_getfilternames(char** filternames)
{
filternames[0]="bump_map";
filternames[1]="bump_map_point";
}
int
_php_bumpmap_getfilterindex(char* filtername)
{
char** filternames;
int i;
filternames=(char**)emalloc(php_filter_getfiltercount()*sizeof(char*));
php_filter_getfilternames(filternames);
for (i=0;i<php_filter_getfiltercount();i++)
{
if (strcmp(filtername,filternames[i])==0) return i;
}
return -1;
}
void
php_filter_getextinfo(char** info)
{
info[0]="Bump Mapping";
info[1]="Provides PHP interface to Willem Monsuwe's bump mapping filter.";
info[2]="Piotr Pawlow (pp@siedziba.pl)";
}
char*
php_filter_getparams(char* filter)
{
char* filterparams[]={
NULL,
"Image map, int angle=0, int elevation=30, int depth=0x200, int red=0x200, int green=0x200, int blue=0x200, int ambient=0",
"Image map, int x=0, int y=0, int z=30, int depth=0x200, int red=0x200, int green=0x200, int blue=0x200, int ambient=0"
};
return filterparams[_php_bumpmap_getfilterindex(filter)+1];
}