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

version» Context lines:

pike.git/src/modules/Image/blit.c:7:   #include "global.h"      /*   **! module Image   **! class Image   */      #include <math.h>   #include <ctype.h>    - #include "global.h" +    #include "pike_macros.h"   #include "interpret.h"   #include "svalue.h"   #include "pike_error.h"   #include "threads.h"      #include "image.h"   #include "image_machine.h"      #define sp Pike_sp      extern struct program *image_program;   #ifdef THIS   #undef THIS /* Needed for NT */   #endif   #define THIS ((struct image *)(Pike_fp->current_storage))   #define THISOBJ (Pike_fp->current_object)    - #define absdiff(a,b) ((a)<(b)?((b)-(a)):((a)-(b))) -  +    #if 0   #include <sys/resource.h>   #define CHRONO(X) chrono(X)      static void chrono(char *x)   {    struct rusage r;    static struct rusage rold;    getrusage(RUSAGE_SELF,&r);    fprintf(stderr,"%s: %ld.%06ld - %ld.%06ld\n",x,
pike.git/src/modules/Image/blit.c:54:    );       rold=r;   }   #else   #define CHRONO(X)   #endif      /***************** internals ***********************************/    - #define testrange(x) MAXIMUM(MINIMUM((x),255),0) -  - #define apply_alpha(x,y,alpha) \ -  ((unsigned char)((y*(255L-(alpha))+x*(alpha))/255L)) -  - #define set_rgb_group_alpha(dest,src,alpha) \ -  (((dest).r=apply_alpha((dest).r,(src).r,alpha)), \ -  ((dest).g=apply_alpha((dest).g,(src).g,alpha)), \ -  ((dest).b=apply_alpha((dest).b,(src).b,alpha))) -  - #define pixel(_img,x,y) ((_img)->img[(x)+(y)*(_img)->xsize]) -  - #define setpixel(x,y) \ -  (THIS->alpha? \ -  set_rgb_group_alpha(THIS->img[(x)+(y)*THIS->xsize],THIS->rgb,THIS->alpha): \ -  ((pixel(THIS,x,y)=THIS->rgb),0)) -  - #define setpixel_test(x,y) \ -  (((x)<0||(y)<0||(x)>=THIS->xsize||(y)>=THIS->ysize)? \ -  0:(setpixel(x,y),0)) -  - static INLINE int getrgb(struct image *img, + static inline int getrgb(struct image *img,    INT32 args_start,INT32 args,INT32 max,char *name)   {    INT32 i;    if (args-args_start<1) return 0;       if (image_color_svalue(sp-args+args_start,&(img->rgb)))    return 1;       if (max<3 || args-args_start<3) return 0;   
pike.git/src/modules/Image/blit.c:113:         /*** end internals ***/         void img_clear(rgb_group *dest, rgb_group rgb, ptrdiff_t size)   {    if(!size) return;    THREADS_ALLOW();    if( ( rgb.r == rgb.g && rgb.r == rgb.b ) ) -  MEMSET(dest, rgb.r, size*sizeof(rgb_group) ); +  memset(dest, rgb.r, size*sizeof(rgb_group) );    else if(size)    {    int increment = 1;    rgb_group *from = dest;    *(dest++)=rgb;    size -= 1;    while (size>increment)    { -  MEMCPY(dest,from,increment*sizeof(rgb_group)); +  memcpy(dest,from,increment*sizeof(rgb_group));    size-=increment,dest+=increment;    if (increment<1024) increment *= 2;    } -  if(size>0) MEMCPY(dest,from,size*sizeof(rgb_group)); +  if(size>0) memcpy(dest,from,size*sizeof(rgb_group));    }    THREADS_DISALLOW();   }      void img_box_nocheck(INT32 x1,INT32 y1,INT32 x2,INT32 y2)   {    INT32 x,mod;    rgb_group *foo,*end,rgb;    struct image *this;   
pike.git/src/modules/Image/blit.c:155:    if(!mod)    img_clear(foo,rgb,end-foo);    else {    THREADS_ALLOW();    do {    int length = x2-x1+1, xs=this->xsize, y=y2-y1+1;    rgb_group *from = foo;    if(!length)    break; /* Break to the while(0). */    for(x=0; x<length; x++) *(foo+x) = rgb; -  while(--y) MEMCPY((foo+=xs), from, length*sizeof(rgb_group)); +  while(--y) memcpy((foo+=xs), from, length*sizeof(rgb_group));    } while(0);    THREADS_DISALLOW();    }    }    else    {    THREADS_ALLOW();    do {    for (; foo<end; foo+=mod)    for (x=x1; x<=x2; x++,foo++)
pike.git/src/modules/Image/blit.c:182:      void img_blit(rgb_group *dest,rgb_group *src,INT32 width,    INT32 lines,INT32 moddest,INT32 modsrc)   {    if(width <= 0 || lines <= 0)    return;   CHRONO("image_blit begin");       THREADS_ALLOW();    if(!moddest && !modsrc) -  MEMCPY(dest,src,sizeof(rgb_group)*width*lines); +  memcpy(dest,src,sizeof(rgb_group)*width*lines);    else    while (lines--)    { -  MEMCPY(dest,src,sizeof(rgb_group)*width); +  memcpy(dest,src,sizeof(rgb_group)*width);    dest+=moddest;    src+=modsrc;    }    THREADS_DISALLOW();   CHRONO("image_blit end");      }      void img_crop(struct image *dest,    struct image *img,
pike.git/src/modules/Image/blit.c:215:    if (x1>x2) tmp=x1, x1=x2, x2=tmp;    if (y1>y2) tmp=y1, y1=y2, y2=tmp;       new=xalloc( (x2-x1+1)*(y2-y1+1)*sizeof(rgb_group)+RGB_VEC_PAD );       if (x1==0 && y1==0 &&    img->xsize-1==x2 && img->ysize-1==y2)    {    *dest=*img;    THREADS_ALLOW(); -  MEMCPY(new,img->img,(x2-x1+1)*(y2-y1+1)*sizeof(rgb_group)); +  memcpy(new,img->img,(x2-x1+1)*(y2-y1+1)*sizeof(rgb_group));    THREADS_DISALLOW();    dest->img=new;    return;    }       img_clear(new,THIS->rgb,(x2-x1+1)*(y2-y1+1));       dest->xsize=x2-x1+1;    dest->ysize=y2-y1+1;       xp=MAXIMUM(0,-x1);    yp=MAXIMUM(0,-y1);    xs=MAXIMUM(0,x1);    ys=MAXIMUM(0,y1);       if( ! (( x2 < 0) || (y2 < 0) || (x1>=img->xsize) || (y1>=img->ysize))) {    -  if (x1<0) x1=0; else if (x1>=img->xsize) x1=img->xsize-1; -  if (y1<0) y1=0; else if (y1>=img->ysize) y1=img->ysize-1; -  if (x2<0) x2=0; else if (x2>=img->xsize) x2=img->xsize-1; -  if (y2<0) y2=0; else if (y2>=img->ysize) y2=img->ysize-1; +  if (x1<0) x1=0; +  if (y1<0) y1=0; +  if (x2>=img->xsize) x2=img->xsize-1; +  if (y2>=img->ysize) y2=img->ysize-1;       img_blit(new+xp+yp*dest->xsize,    img->img+xs+(img->xsize)*ys,    x2-x1+1,    y2-y1+1,    dest->xsize,    img->xsize);       }    dest->img=new;   }      void img_clone(struct image *newimg,struct image *img)   {    if (newimg->img) free(newimg->img);    newimg->img=xalloc(sizeof(rgb_group)*img->xsize*img->ysize+RGB_VEC_PAD);    THREADS_ALLOW(); -  MEMCPY(newimg->img,img->img,sizeof(rgb_group)*img->xsize*img->ysize); +  memcpy(newimg->img,img->img,sizeof(rgb_group)*img->xsize*img->ysize);    THREADS_DISALLOW();    newimg->xsize=img->xsize;    newimg->ysize=img->ysize;    newimg->rgb=img->rgb;   }      /*   **! method object paste(object image)   **! method object paste(object image,int x,int y)   **! Pastes a given image over the current image.
pike.git/src/modules/Image/blit.c:284:   **! see also: paste_mask, paste_alpha, paste_alpha_color   */      void image_paste(INT32 args)   {    struct image *img=NULL;    INT32 x1,y1,x2,y2,blitwidth,blitheight;       if (args<1    || TYPEOF(sp[-args]) != T_OBJECT -  || !(img=(struct image*)get_storage(sp[-args].u.object,image_program))) -  bad_arg_error("image->paste",sp-args,args,1,"",sp+1-1-args, -  "Bad argument 1 to image->paste()\n"); +  || !(img=get_storage(sp[-args].u.object,image_program))) +  bad_arg_error("paste",args,1,"",sp+1-1-args, +  "Bad argument 1 to paste.\n");    if (!THIS->img) return;       if (!img->img) return;       if (args>1)    {    if (args<3    || TYPEOF(sp[1-args]) != T_INT    || TYPEOF(sp[2-args]) != T_INT) -  bad_arg_error("image->paste",sp-args,args,0,"",sp-args, -  "Bad arguments to image->paste()\n"); +  bad_arg_error("paste",args,0,"",sp-args, +  "Bad arguments to paste.\n");    x1=sp[1-args].u.integer;    y1=sp[2-args].u.integer;    }    else x1=y1=0;       if(x1 >= THIS->xsize || y1 >= THIS->ysize) /* Per */    {    pop_n_elems(args);    ref_push_object(THISOBJ);    return;
pike.git/src/modules/Image/blit.c:363:   */      void image_paste_alpha(INT32 args)   {    struct image *img = NULL;    INT32 x1,y1;       if (args<2    || TYPEOF(sp[-args]) != T_OBJECT    || !sp[-args].u.object -  || !(img=(struct image*)get_storage(sp[-args].u.object,image_program)) +  || !(img=get_storage(sp[-args].u.object,image_program))    || TYPEOF(sp[1-args]) != T_INT) -  bad_arg_error("image->paste_alpha",sp-args,args,0,"",sp-args, -  "Bad arguments to image->paste_alpha()\n"); +  bad_arg_error("paste_alpha",args,0,"",sp-args, +  "Bad arguments to paste_alpha.\n");    if (!THIS->img) return;    if (!img->img) return;    THIS->alpha=(unsigned char)(sp[1-args].u.integer);       if (args>=4)    {    if (TYPEOF(sp[2-args]) != T_INT    || TYPEOF(sp[3-args]) != T_INT) -  bad_arg_error("image->paste_alpha",sp-args,args,0,"",sp-args, -  "Bad arguments to image->paste_alpha()\n"); +  bad_arg_error("paste_alpha",args,0,"",sp-args, +  "Bad arguments to paste_alpha.\n");    x1=sp[2-args].u.integer;    y1=sp[3-args].u.integer;    }    else x1=y1=0;       if(x1 >= THIS->xsize || y1 >= THIS->ysize) /* Per */    {    pop_n_elems(args);    ref_push_object(THISOBJ);    return;
pike.git/src/modules/Image/blit.c:452:    struct image *img = NULL, *mask = NULL;    INT32 x1,y1,x,y,x2,y2,smod,dmod,mmod;    rgb_group *s,*d,*m;    double q;      CHRONO("image_paste_mask init");       if (args<2)    Pike_error("illegal number of arguments to image->paste_mask()\n");    if (TYPEOF(sp[-args]) != T_OBJECT -  || !(img=(struct image*)get_storage(sp[-args].u.object,image_program))) -  bad_arg_error("image->paste_mask",sp-args,args,1,"",sp+1-1-args, -  "Bad argument 1 to image->paste_mask()\n"); +  || !(img=get_storage(sp[-args].u.object,image_program))) +  bad_arg_error("paste_mask",args,1,"",sp+1-1-args, +  "Bad argument 1 to paste_mask.\n");    if (TYPEOF(sp[1-args]) != T_OBJECT -  || !(mask=(struct image*)get_storage(sp[1-args].u.object,image_program))) -  bad_arg_error("image->paste_mask",sp-args,args,2,"",sp+2-1-args, -  "Bad argument 2 to image->paste_mask()\n"); +  || !(mask=get_storage(sp[1-args].u.object,image_program))) +  bad_arg_error("paste_mask",args,2,"",sp+2-1-args, +  "Bad argument 2 to paste_mask.\n");    if (!THIS->img) return;       if (!mask->img) return;    if (!img->img) return;       if (args>=4)    {    if (TYPEOF(sp[2-args]) != T_INT    || TYPEOF(sp[3-args]) != T_INT)    Pike_error("illegal coordinate arguments to image->paste_mask()\n");
pike.git/src/modules/Image/blit.c:496:       q=1.0/255;       THREADS_ALLOW();    for (y=MAXIMUM(0,-y1); y<y2; y++)    {    for (x=MAXIMUM(0,-x1); x<x2; x++)    {    if (m->r==255) d->r=s->r;    else if (m->r==0) {} -  else d->r = DOUBLE_TO_COLORTYPE(((d->r*(255-m->r))+(s->r*m->r))*q); +  else d->r = (COLORTYPE)(((d->r*(255-m->r))+(s->r*m->r))*q);    if (m->g==255) d->g=s->g;    else if (m->g==0) {} -  else d->g = DOUBLE_TO_COLORTYPE(((d->g*(255-m->g))+(s->g*m->g))*q); +  else d->g = (COLORTYPE)(((d->g*(255-m->g))+(s->g*m->g))*q);    if (m->b==255) d->b=s->b;    else if (m->b==0) {} -  else d->b = DOUBLE_TO_COLORTYPE(((d->b*(255-m->b))+(s->b*m->b))*q); +  else d->b = (COLORTYPE)(((d->b*(255-m->b))+(s->b*m->b))*q);    s++; m++; d++;    }    s+=smod; m+=mmod; d+=dmod;    }    THREADS_DISALLOW();   CHRONO("image_paste_mask end");       pop_n_elems(args);    ref_push_object(THISOBJ);   }
pike.git/src/modules/Image/blit.c:555:   void image_paste_alpha_color(INT32 args)   {    struct image *mask = NULL;    INT32 x1,y1,x,y,x2,y2;    rgb_group rgb,*d,*m;    INT32 mmod,dmod;    double q;    int arg=1;       if (args<1) -  SIMPLE_TOO_FEW_ARGS_ERROR("image->paste_alpha_color",1); +  SIMPLE_WRONG_NUM_ARGS_ERROR("image->paste_alpha_color",1);    if (TYPEOF(sp[-args]) != T_OBJECT    || !sp[-args].u.object -  || !(mask=(struct image*)get_storage(sp[-args].u.object,image_program))) -  bad_arg_error("image->paste_alpha_color",sp-args,args,1,"",sp+1-1-args, -  "Bad argument 1 to image->paste_alpha_color()\n"); +  || !(mask=get_storage(sp[-args].u.object,image_program))) +  bad_arg_error("paste_alpha_color",args,1,"",sp+1-1-args, +  "Bad argument 1 to paste_alpha_color.\n");    if (!THIS->img) return;    if (!mask->img) return;       if (args==6 || args==4 || args==2 || args==3) /* color at arg 2.. */ -  arg=1+getrgb(THIS,1,args,3,"image->paste_alpha_color()\n"); +  arg=1+getrgb(THIS,1,args,3,"paste_alpha_color");    if (args>arg+1)    {    if (TYPEOF(sp[arg-args]) != T_INT    || TYPEOF(sp[1+arg-args]) != T_INT)    Pike_error("illegal coordinate arguments to image->paste_alpha_color()\n");    x1=sp[arg-args].u.integer;    y1=sp[1+arg-args].u.integer;    }    else x1=y1=0;   
pike.git/src/modules/Image/blit.c:598:       rgb=THIS->rgb;       THREADS_ALLOW();    for (y=MAXIMUM(0,-y1); y<y2; y++)    {    for (x=MAXIMUM(0,-x1); x<x2; x++)    {    if (m->r==255) d->r=rgb.r;    else if (m->r==0) ; -  else d->r = DOUBLE_TO_COLORTYPE(((d->r*(255-m->r))+(rgb.r*m->r))*q); +  else d->r = (COLORTYPE)(((d->r*(255-m->r))+(rgb.r*m->r))*q);    if (m->g==255) d->g=rgb.g;    else if (m->g==0) ; -  else d->g = DOUBLE_TO_COLORTYPE(((d->g*(255-m->g))+(rgb.g*m->g))*q); +  else d->g = (COLORTYPE)(((d->g*(255-m->g))+(rgb.g*m->g))*q);    if (m->b==255) d->b=rgb.b;    else if (m->b==0) ; -  else d->b = DOUBLE_TO_COLORTYPE(((d->b*(255-m->b))+(rgb.b*m->b))*q); +  else d->b = (COLORTYPE)(((d->b*(255-m->b))+(rgb.b*m->b))*q);    m++; d++;    }    m+=mmod; d+=dmod;    }    THREADS_DISALLOW();   CHRONO("image_paste_alpha_color end");       pop_n_elems(args);    ref_push_object(THISOBJ);   }