pike.git / src / modules / Image / pattern.c

version» Context lines:

pike.git/src/modules/Image/pattern.c:14:   #include <math.h>   #include <ctype.h>      #include "pike_macros.h"   #include "object.h"   #include "interpret.h"   #include "svalue.h"   #include "pike_error.h"   #include "threads.h"   #include "builtin_functions.h" + #include "mapping.h" + #include "constants.h" + #include "operators.h"      #include "image.h"         #define sp Pike_sp      extern struct program *image_program;   #ifdef THIS   #undef THIS   #endif      #define THIS ((struct image *)(Pike_fp->current_storage))   #define THISOBJ (Pike_fp->current_object)    - #define testrange(x) MAXIMUM(MINIMUM((x),255),0) -  +    /**************** noise ************************/      #define NOISE_PTS 1024   #define NOISE_PX 173   #define NOISE_PY 263   #define NOISE_PZ 337   #define NOISE_PHI 0.6180339      static const double noise_p1[NOISE_PTS]=   {0.7687, 0.7048, 0.2400, 0.8877, 0.3604, 0.3365, 0.4929, 0.6689,
pike.git/src/modules/Image/pattern.c:184:    int Ax[3],Ay[3];    int n,i,j;    double Sx[3],Sy[3];    double sum,dsum,f,fx,fy;       fx=floor(Vx);    fy=floor(Vy);       for (n=0; n<3; n++)    { -  Ax[n] = DOUBLE_TO_INT(floor(NOISE_PX*FRAC( (fx+n)*NOISE_PHI ))); -  Ay[n] = DOUBLE_TO_INT(floor(NOISE_PY*FRAC( (fy+n)*NOISE_PHI ))); +  Ax[n] = (int)floor(NOISE_PX*FRAC( (fx+n)*NOISE_PHI )); +  Ay[n] = (int)floor(NOISE_PY*FRAC( (fy+n)*NOISE_PHI ));    }       f=FRAC(Vx);    Sx[0]=0.5-f+0.5*f*f;    Sx[1]=0.5+f-f*f;    Sx[2]=0.5*f*f;       f=FRAC(Vy);    Sy[0]=0.5-f+0.5*f*f;    Sy[1]=0.5+f-f*f;
pike.git/src/modules/Image/pattern.c:219:    sum=0;    for (i=0; i<3; i++)    {    for (j=0,dsum=0; j<3; j++)    dsum+=Sy[j]*noise_p[ (Ax[i]+Ay[j]) & (NOISE_PTS-1) ];    sum+=Sx[i]*dsum;    }    return sum;   }    - static INLINE double turbulence(double x,double y,int octaves) + static inline double turbulence(double x,double y,int octaves)   {    double t=0;    double mul=1;    while (octaves-->0)    {    t+=noise(x*mul,y*mul,noise_p1)*mul;    mul*=0.5;    }    return t;   }
pike.git/src/modules/Image/pattern.c:244:    int i,n,k;    rgbd_group lrgb,*rgbp,*rgb;    double fr,fg,fb,q;    int b;       if (TYPEOF(*s) != T_ARRAY)    Pike_error("Illegal colorrange to %s\n",where);    else if (s->u.array->size<2)    Pike_error("Colorrange array too small (meaningless) (to %s)\n",where);    -  vp=v=(void*)xalloc(sizeof(double)*(s->u.array->size/2+1)); -  rgbp=rgb=(void*)xalloc(sizeof(rgbd_group)*(s->u.array->size/2+1)); +  vp=v=xalloc(sizeof(double)*(s->u.array->size/2+1)); +  rgbp=rgb=xalloc(sizeof(rgbd_group)*(s->u.array->size/2+1));       for (i=0; i<s->u.array->size-1; i+=2)    {    rgb_group rgbt;       if (TYPEOF(s->u.array->item[i]) == T_INT)    *vp = (double)s->u.array->item[i].u.integer;    else if (TYPEOF(s->u.array->item[i]) == T_FLOAT)    *vp = s->u.array->item[i].u.float_number;    else    bad_arg_error(where, -  0,0, 1, "array of int|float,color", 0, +  0, 1, "array of int|float,color", 0,    "%s: expected int or float at element %d"    " of colorrange\n",where,i);       if (*vp>1) *vp=1;    else if (*vp<0) *vp=0;    vp++;       if (!image_color_svalue(s->u.array->item+i+1,&rgbt))    bad_arg_error(where, -  0,0, 1, "array of int|float,color", 0, +  0, 1, "array of int|float,color", 0,    "%s: no color at element %d"    " of colorrange\n",where,i+1);       rgbp->r=rgbt.r;    rgbp->g=rgbt.g;    rgbp->b=rgbt.b;       rgbp++;    }    *vp=v[0]+1+1.0/(COLORRANGE_LEVELS-1);    lrgb=*rgbp=rgb[0]; /* back to original color */    -  for (k=1,i=DOUBLE_TO_INT(v[0]*(COLORRANGE_LEVELS-1)); +  for (k=1,i=(int)(v[0]*(COLORRANGE_LEVELS-1));    k<=s->u.array->size/2; k++)    { -  n = DOUBLE_TO_INT(v[k]*COLORRANGE_LEVELS); +  n = (int)(v[k]*COLORRANGE_LEVELS);       if (n>i)    {    q = 1.0/((double)(n-i));       fr=(rgb[k].r-lrgb.r)*q;    fg=(rgb[k].g-lrgb.g)*q;    fb=(rgb[k].b-lrgb.b)*q;       for (b=0;i<n && i<COLORRANGE_LEVELS;i++,b++)    { -  cr[i&(COLORRANGE_LEVELS-1)].r = DOUBLE_TO_COLORTYPE(lrgb.r+fr*b); -  cr[i&(COLORRANGE_LEVELS-1)].g = DOUBLE_TO_COLORTYPE(lrgb.g+fg*b); -  cr[i&(COLORRANGE_LEVELS-1)].b = DOUBLE_TO_COLORTYPE(lrgb.b+fb*b); +  cr[i&(COLORRANGE_LEVELS-1)].r = (COLORTYPE)(lrgb.r+fr*b); +  cr[i&(COLORRANGE_LEVELS-1)].g = (COLORTYPE)(lrgb.g+fg*b); +  cr[i&(COLORRANGE_LEVELS-1)].b = (COLORTYPE)(lrgb.b+fb*b);    }    }    lrgb=rgb[k];    }      #if 0    for (k=0; k<COLORRANGE_LEVELS; k++)    fprintf(stderr,"#%02x%02x%02x \n",    cr[k].r,cr[k].g,cr[k].b);   #endif
pike.git/src/modules/Image/pattern.c:322:      #define GET_FLOAT_ARG(sp,args,n,def,where) \    ( (args>n) \    ? ( (TYPEOF(sp[n-args]) == T_INT) ? (double)(sp[n-args].u.integer) \    : ( (TYPEOF(sp[n-args]) == T_FLOAT) ? sp[n-args].u.float_number \    : ( Pike_error("illegal argument(s) to %s\n", where), 0.0 ) ) ) \    : def )   #define GET_INT_ARG(sp,args,n,def,where) \    ( (args>n) \    ? ( (TYPEOF(sp[n-args]) == T_INT) ? sp[n-args].u.integer \ -  : ( (TYPEOF(sp[n-args]) == T_FLOAT) ? DOUBLE_TO_INT(sp[n-args].u.float_number) \ +  : ( (TYPEOF(sp[n-args]) == T_FLOAT) ? (int)sp[n-args].u.float_number \    : ( Pike_error("illegal argument(s) to %s\n", where), 0 ) ) ) \    : def )      /*   **! method void noise(array(float|int|array(int)) colorrange)   **! method void noise(array(float|int|array(int)) colorrange,float scale,float xdiff,float ydiff,float cscale)   **! Gives a new image with the old image's size,   **! filled width a 'noise' pattern.   **!   **! The random seed may be different with each instance of pike.
pike.git/src/modules/Image/pattern.c:385:    sizeof(rgb_group)*THIS->xsize*THIS->ysize+RGB_VEC_PAD);    }       cscale*=COLORRANGE_LEVELS;       d=img->img;    for (y=THIS->ysize,xp=xdiff; y--; xp+=1.0)    for (x=THIS->xsize,yp=ydiff; x--; yp+=1.0)    {    *(d++)= -  cr[DOUBLE_TO_INT((noise((double)x*scale,(double)y*scale,noise_p1) +  cr[(int)((noise((double)x*scale,(double)y*scale,noise_p1)   /* +noise((double)(x*0.5+y*0.8660254037844386)*scale, */   /* (double)(-y*0.5+x*0.8660254037844386)*scale, */   /* noise_p2) */    )    *cscale)&(COLORRANGE_LEVELS-1)];    }       pop_n_elems(args);    push_object(o);   }
pike.git/src/modules/Image/pattern.c:476: Inside #if 0
   if (y==0 && x<10)    {    fprintf(stderr,"%g*%g=%d => %d\n",    turbulence(xp*scale,yp*scale,octaves),    cscale,    (INT32)(turbulence(xp*scale,yp*scale,octaves)*cscale),    (INT32)(turbulence(xp*scale,yp*scale,octaves)*cscale)&(COLORRANGE_LEVELS)-1 );    }   #endif    *(d++)= -  cr[DOUBLE_TO_INT(turbulence(xp*scale,yp*scale,octaves)*cscale) +  cr[(int)(turbulence(xp*scale,yp*scale,octaves)*cscale)    & (COLORRANGE_LEVELS-1)];    }       pop_n_elems(args);    push_object(o);   }      /*   **! method object random()   **! method object random(int seed) -  + **! method object random(function(int:string) random)   **! method object randomgrey() - **! method object random(greyint seed) + **! method object randomgrey(int seed) + **! method object randomgrey(function(int:string) random)   **! Gives a randomized image;<br>   **! <table><tr valign=center>   **! <td><illustration> return lena(); </illustration></td>   **! <td><illustration> return lena()->random()</illustration></td>   **! <td><illustration> return lena()->random(17)</illustration></td>   **! <td><illustration> return lena()->random(17)->grey()</illustration></td>   **! <td><illustration> return lena()->random(17)->color(255,0,0)</illustration></td>   **! <td><illustration> return lena()->random(17)->color(255,0,0)->grey(1,0,0)</illustration></td>   **! </tr><tr>   **! <td>original</td>
pike.git/src/modules/Image/pattern.c:514:   **! </tr></table>   **!   **! Use with -><ref>grey</ref>() or -><ref>color</ref>()   **! for one-color-results.   **!   **! returns a new image   **!   **! see also test, noise   */    + static void select_random_string(INT32 args) + { +  if(args>1) +  Pike_error("Too may arguments.\n"); +  if(!args) +  { +  struct svalue *random_string = +  simple_mapping_string_lookup(get_builtin_constants(), "random_string"); +  if(!random_string || (TYPEOF(*random_string) != T_FUNCTION)) +  Pike_error("Unable to resolve random function.\n"); +  push_svalue(random_string); +  } +  else if(TYPEOF(sp[-1])==T_INT) +  { +  struct program *p; +  push_constant_text("Random.Deterministic"); +  SAFE_APPLY_MASTER("resolv_or_error",1); +  if(TYPEOF(sp[-1])!=T_PROGRAM) +  Pike_error("Unable to resolve Random.Deterministic program.\n"); +  p = sp[-1].u.program; +  stack_swap(); +  push_object(clone_object(p, 1)); +  push_constant_text("random_string"); +  o_index(); +  if(TYPEOF(sp[-1])!=T_FUNCTION) +  Pike_error("random_string is not a function.\n"); +  } +  else if(TYPEOF(sp[-1])!=T_FUNCTION) +  { +  Pike_error("Expected seed or random function.\n"); +  } + } +    void image_random(INT32 args)   {    struct object *o;    struct image *img; -  rgb_group *d; -  INT32 n; +  INT32 n=THIS->xsize*THIS->ysize*sizeof(COLORTYPE)*3;    -  +  select_random_string(args); +  push_int( n ); +  apply_svalue(&sp[-2], 1); +  if(TYPEOF(sp[-1])!=T_STRING) +  Pike_error("Couldn't generate random string.\n"); +  if( sp[-1].u.string->len != n || sp[-1].u.string->size_shift != 0) +  Pike_error("Wrong size random string generated.\n"); +     push_int(THIS->xsize);    push_int(THIS->ysize);    o=clone_object(image_program,2); -  img=(struct image*)get_storage(o,image_program); -  d=img->img; -  if (args) f_random_seed(args); +  img=get_storage(o,image_program);    -  THREADS_ALLOW(); -  -  n=img->xsize*img->ysize; -  while (n--) -  { -  d->r=(COLORTYPE)my_rand(); -  d->g=(COLORTYPE)my_rand(); -  d->b=(COLORTYPE)my_rand(); -  d++; -  } -  -  THREADS_DISALLOW(); -  +  memcpy(img->img, sp[-1].u.string->str, n);    push_object(o);   }      void image_randomgrey(INT32 args)   {    struct object *o;    struct image *img; -  +  COLORTYPE *colors;    rgb_group *d; -  INT32 n; +  INT32 n=THIS->xsize*THIS->ysize;    -  +  select_random_string(args); +  push_int( n*sizeof(COLORTYPE) ); +  apply_svalue(&sp[-2], 1); +  if(TYPEOF(sp[-1])!=T_STRING) +  Pike_error("Couldn't generate random string.\n"); +  if( (unsigned)sp[-1].u.string->len != n*sizeof(COLORTYPE) || +  sp[-1].u.string->size_shift != 0 ) +  Pike_error("Wrong size random string generated.\n"); +     push_int(THIS->xsize);    push_int(THIS->ysize);    o=clone_object(image_program,2); -  img=(struct image*)get_storage(o,image_program); +  img=get_storage(o,image_program);    d=img->img; -  if (args) f_random_seed(args); +     -  THREADS_ALLOW(); -  -  n=img->xsize*img->ysize; +  colors = (COLORTYPE*)sp[-1].u.string->str;    while (n--)    { -  d->r=d->g=d->b=(COLORTYPE)my_rand(); +  d->r=d->g=d->b=colors[n];    d++;    }    -  THREADS_DISALLOW(); -  +     push_object(o);   }