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

version» Context lines:

pike.git/src/modules/Image/colortable.c:30:   #include "stralloc.h"   #include "pike_macros.h"   #include "object.h"   #include "interpret.h"   #include "svalue.h"   #include "mapping.h"   #include "builtin_functions.h"   #include "pike_error.h"   #include "module_support.h"   #include "operators.h" - #include "dmalloc.h" +    #include "bignum.h" -  + #include "pike_types.h" + #include "constants.h" + #include "sprintf.h"      #include "image.h"   #include "colortable.h"   #include "initstuff.h"         #define sp Pike_sp      #define WEIGHT_NEEDED (nct_weight_t)(0x10000000)   #define WEIGHT_REMOVE (nct_weight_t)(0x10000001)
pike.git/src/modules/Image/colortable.c:66:   #define CUBICLE_DEFAULT_R 10   #define CUBICLE_DEFAULT_G 10   #define CUBICLE_DEFAULT_B 10   #define CUBICLE_DEFAULT_ACCUR 4      #define RIGID_DEFAULT_R 16   #define RIGID_DEFAULT_G 16   #define RIGID_DEFAULT_B 16      #define SQ(x) ((x)*(x)) - static INLINE ptrdiff_t sq(ptrdiff_t x) { return x*x; } - static INLINE int sq_int(int x) { return x*x; } + static inline ptrdiff_t sq(ptrdiff_t x) { return x*x; } + static inline int sq_int(int x) { return x*x; }      #ifdef THIS   #undef THIS /* Needed for NT */   #endif   #define THIS ((struct neo_colortable *)(Pike_fp->current_storage))   #define THISOBJ (Pike_fp->current_object)    - static struct pike_string *s_array; - static struct pike_string *s_string; - static struct pike_string *s_mapping; -  +    /***************** init & exit *********************************/         static void colortable_free_lookup_stuff(struct neo_colortable *nct)   {    switch (nct->lookup_mode)    {    case NCT_CUBICLES:    if (nct->lu.cubicles.cubicles)    {
pike.git/src/modules/Image/colortable.c:341:    if (n>=target_size) return n;       switch (type)    {    case NCT_REDUCE_MEAN:    /* get the mean color */       if (src_size > 10240) {    mmul = 10240;    } else { -  mmul = DO_NOT_WARN((nct_weight_t)src_size); +  mmul = (nct_weight_t)src_size;    }       for (i=0; i<src_size; i++)    {    nct_weight_t mul = src[i].weight;       sum.r += src[i].color.r*mul;    sum.g += src[i].color.g*mul;    sum.b += src[i].color.b*mul;    tot += mul;
pike.git/src/modules/Image/colortable.c:386:    if (min.r>(INT32)src[i].color.r) min.r=src[i].color.r;    if (min.g>(INT32)src[i].color.g) min.g=src[i].color.g;    if (min.b>(INT32)src[i].color.b) min.b=src[i].color.b;    if (max.r<(INT32)src[i].color.r) max.r=src[i].color.r;    if (max.g<(INT32)src[i].color.g) max.g=src[i].color.g;    if (max.b<(INT32)src[i].color.b) max.b=src[i].color.b;    tot+=src[i].weight;    }       dest->color.r = -  DO_NOT_WARN((COLORTYPE)(max.r*position.r+min.r*(1-position.r))); +  (COLORTYPE)(max.r*position.r+min.r*(1-position.r));    dest->color.g = -  DO_NOT_WARN((COLORTYPE)(max.g*position.g+min.g*(1-position.g))); +  (COLORTYPE)(max.g*position.g+min.g*(1-position.g));    dest->color.b = -  DO_NOT_WARN((COLORTYPE)(max.b*position.b+min.b*(1-position.b))); +  (COLORTYPE)(max.b*position.b+min.b*(1-position.b));    dest->weight=tot;    dest->no=-1;   #ifdef COLORTABLE_REDUCE_DEBUG    fprintf(stderr, "COLORTABLE%*s min=%d,%d,%d max=%d,%d,%d position=%g,%g,%g\n",    level, "", min.r, min.g, min.b, max.r, max.g, max.b,    position.r, position.g, position.b);    fprintf(stderr, "COLORTABLE%*s dest=%d,%d,%d weight=%d no=%d\n",    level, "", dest->color.r, dest->color.g, dest->color.b,    dest->weight, dest->no);   #endif
pike.git/src/modules/Image/colortable.c:459:   #endif       for (i=0; i<src_size; i++)    {    nct_weight_t mul;    if ((mul=src[i].weight)==WEIGHT_NEEDED)    mul=mmul;    diff.r += (sq(src[i].color.r-(INT32)sum.r)/8)*mul;    diff.g += (sq(src[i].color.g-(INT32)sum.g)/8)*mul;    diff.b += (sq(src[i].color.b-(INT32)sum.b)/8)*mul; -  gdiff += (sq(src[i].color.r*sf.r+src[i].color.g*sf.g+ -  src[i].color.b*sf.b-g)/8)*mul; +  gdiff += (sq(((INT32)src[i].color.r)*sf.r+ +  ((INT32)src[i].color.g)*sf.g+ +  ((INT32)src[i].color.b)*sf.b-g)/8)*mul;    tot+=mul;    }      #ifdef COLORTABLE_REDUCE_DEBUG    fprintf(stderr, "COLORTABLE%*s pure diff=%d,%d,%d,%d sort=?\n",    level, "", diff.r, diff.g, diff.b, gdiff);   #endif       diff.r*=DIFF_R_MULT;    diff.g*=DIFF_G_MULT;
pike.git/src/modules/Image/colortable.c:607: Inside #if defined(COLORTABLE_REDUCE_DEBUG)
     #ifdef COLORTABLE_REDUCE_DEBUG    fprintf(stderr, "COLORTABLE%*s try i=%d/%d - %d+%d=%d from %d+%d=%d\n",    level+1, "", i, target_size, i, target_size-i, target_size,    left, src_size-left, src_size);   #endif       n=reduce_recurse(src,dest,left,i,level+2,sf,newpos1,space,type);    if (n!=oldn) {    if (n<oldn) /* i certainly hope so */ -  MEMMOVE(dest+n,dest+oldn,sizeof(struct nct_flat_entry)*m); +  memmove(dest+n,dest+oldn,sizeof(struct nct_flat_entry)*m);    else /* huh? */    /* this is the only case we don't have them already */    m=reduce_recurse(src+left,dest+n,src_size-left,    target_size-n,level+2,sf,newpos2,space,type);    }   #ifdef COLORTABLE_REDUCE_DEBUG    fprintf(stderr, "COLORTABLE%*s ->%d+%d=%d (retried for %d+%d=%d)\n",    level, "", n, m, n+m, i, target_size-i, target_size);   #endif    }
pike.git/src/modules/Image/colortable.c:632:   static struct nct_flat _img_reduce_number_of_colors(struct nct_flat flat,    unsigned long maxcols,    rgbl_group sf)   {    ptrdiff_t i,j;    struct nct_flat_entry *newe;    rgbd_group pos={0.5,0.5,0.5},space={0.5,0.5,0.5};       if ((size_t)flat.numentries <= (size_t)maxcols) return flat;    -  newe=malloc(sizeof(struct nct_flat_entry)*flat.numentries); +  newe=calloc(sizeof(struct nct_flat_entry), flat.numentries);    if (!newe) { return flat; }       i = reduce_recurse(flat.entries, newe, flat.numentries, maxcols, 0, sf,    pos, space, NCT_REDUCE_WEIGHT);    if (!i)    {    free(newe);    return flat;    }       free(flat.entries);       flat.entries=realloc(newe,i*sizeof(struct nct_flat_entry));    flat.numentries=i;    if (!flat.entries) {    free(newe); -  resource_error(NULL, 0, 0, "memory", 0, "Out of memory.\n"); +  out_of_memory_error(NULL, -1, 0);    }       for (j=0; j<i; j++)    flat.entries[j].no=j;       return flat;   }      /*** scanning ***/      #define DEFAULT_COLOR_HASH_SIZE 8192   #define MAX_COLOR_HASH_SIZE 32768      struct color_hash_entry   {    rgb_group color;    unsigned long pixels;    ptrdiff_t no;   };    - static INLINE struct color_hash_entry *insert_in_hash(rgb_group rgb, + static inline struct color_hash_entry *insert_in_hash(rgb_group rgb,    struct color_hash_entry *hash,    size_t hashsize)   {    size_t j, k;    j=(rgb.r*127+rgb.b*997+rgb.g*2111)%hashsize;    k=100;    if (j+100>=hashsize)    while (--k && hash[j].pixels &&    (hash[j].color.r!=rgb.r ||    hash[j].color.g!=rgb.g ||
pike.git/src/modules/Image/colortable.c:695:    (hash[j].color.r!=rgb.r ||    hash[j].color.g!=rgb.g ||    hash[j].color.b!=rgb.b))    j++;    if (!k) return NULL; /* no space */    hash[j].pixels++;    hash[j].color=rgb;    return hash+j;   }    - static INLINE struct color_hash_entry *insert_in_hash_nd(rgb_group rgb, + static inline struct color_hash_entry *insert_in_hash_nd(rgb_group rgb,    struct color_hash_entry *hash,    size_t hashsize)   {    size_t j, k;    j=(rgb.r*127+rgb.b*997+rgb.g*2111)%hashsize;    k=100;    if (j+100>=hashsize)    while (--k && hash[j].pixels &&    (hash[j].color.r!=rgb.r ||    hash[j].color.g!=rgb.g ||
pike.git/src/modules/Image/colortable.c:719:    while (--k && hash[j].pixels &&    (hash[j].color.r!=rgb.r ||    hash[j].color.g!=rgb.g ||    hash[j].color.b!=rgb.b))    j++;    if (!k) return NULL; /* no space */    hash[j].color=rgb;    return hash+j;   }    - static INLINE struct color_hash_entry *insert_in_hash_mask(rgb_group rgb, + static inline struct color_hash_entry *insert_in_hash_mask(rgb_group rgb,    struct color_hash_entry *hash,unsigned long hashsize,rgb_group mask)   {    unsigned long j,k;    rgb.r&=mask.r;    rgb.g&=mask.g;    rgb.b&=mask.b;    j=(rgb.r*127+rgb.b*997+rgb.g*2111)%hashsize;    k=100;    if (j+100>=hashsize)    while (--k && hash[j].pixels &&
pike.git/src/modules/Image/colortable.c:746:    (hash[j].color.r!=rgb.r ||    hash[j].color.g!=rgb.g ||    hash[j].color.b!=rgb.b))    j++;    if (!k) return NULL; /* no space */    hash[j].pixels++;    hash[j].color=rgb;    return hash+j;   }    - static INLINE rgb_group get_mask_of_level(int level) + static inline rgb_group get_mask_of_level(int level)   {    static const unsigned char strip_r[24]=    { 0xff, 0xfe, 0xfe, 0xfe, 0xfc, 0xfc, 0xfc, 0xf8, 0xf8, 0xf8, 0xf0,    0xf0, 0xf0, 0xe0, 0xe0, 0xe0, 0xc0, 0xc0, 0xc0, 0x80, 0x80, 0x80 };    static const unsigned char strip_g[24]=    { 0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfc, 0xfc, 0xfc, 0xf8, 0xf8, 0xf8,    0xf0, 0xf0, 0xf0, 0xe0, 0xe0, 0xe0, 0xc0, 0xc0, 0xc0, 0x80, 0x80, 0x80 };    static const unsigned char strip_b[24]=    { 0xfe, 0xfe, 0xfe, 0xfc, 0xfc, 0xfc, 0xf8, 0xf8, 0xf8, 0xf0, 0xf0,    0xf0, 0xe0, 0xe0, 0xe0, 0xc0, 0xc0, 0xc0, 0x80, 0x80, 0x80 };
pike.git/src/modules/Image/colortable.c:802:    hashsize*=2;    if (hashsize>MAX_COLOR_HASH_SIZE)    {    hashsize/=2;    break; /* try again, with mask */    }   #ifdef COLORTABLE_DEBUG    fprintf(stderr,"COLORTABLE %ld pixels left; hashsize=%ld\n",i,hashsize);   #endif    -  hash=malloc(sizeof(struct color_hash_entry)*hashsize); +  hash=calloc(sizeof(struct color_hash_entry), hashsize);    if (!hash)    {    free(oldhash); -  resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); +  out_of_memory_error(NULL, -1, 0);    }    k=hashsize;    while (k--) hash[k].pixels=0;       while (j--)    if (oldhash[j].pixels)    {    mark=insert_in_hash(oldhash[j].color,hash,hashsize);    if (!mark) {    free(hash);
pike.git/src/modules/Image/colortable.c:848:    rgb_mask=get_mask_of_level(mask_level);    mask_level++;   #ifdef COLORTABLE_DEBUG    fprintf(stderr,"COLORTABLE mask level=%d mask=%02x%02x%02x\n",    mask_level,rgb_mask.r,rgb_mask.g,rgb_mask.b);   #endif       oldhash=hash;    j=hashsize;    -  hash=malloc(sizeof(struct color_hash_entry)*hashsize); +  hash=calloc(sizeof(struct color_hash_entry), hashsize);    if (!hash)    {    free(oldhash); -  resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); +  out_of_memory_error(NULL, -1, 0);    }       k=hashsize;    while (k--) hash[k].pixels=0;       while (j--)    if (oldhash[j].pixels)    {    mark=insert_in_hash_mask(oldhash[j].color,hash,hashsize,rgb_mask);    if (!mark) {
pike.git/src/modules/Image/colortable.c:894:    j=0;    while (i--)    if (hash[i].pixels) j++;    /* j is now the number of colors */      #ifdef COLORTABLE_DEBUG    fprintf(stderr,"COLORTABLE %ld colors found in image; hashsize=%ld\n",j,hashsize);   #endif       flat.numentries=j; -  flat.entries=malloc(sizeof(struct nct_flat_entry)*j); +  flat.entries=calloc(sizeof(struct nct_flat_entry), j);    if (!flat.entries)    {    free(hash); -  resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); +  out_of_memory_error(NULL, -1, 0);    }    j=0;    i=hashsize;    while (i--)    if (hash[i].pixels)    {    flat.entries[j].color=hash[i].color;    flat.entries[j].no=j;    flat.entries[j].weight=hash[i].pixels;    j++;
pike.git/src/modules/Image/colortable.c:920:       if (((int)j)!=flat.numentries) abort();       free(hash);       return flat;   }      static struct nct_flat _img_get_flat_from_array(struct array *arr)   { - #if 0 -  struct svalue s,s2; - #endif +     struct nct_flat flat;    int i,n=0;       flat.numentries=arr->size;    flat.entries=(struct nct_flat_entry*)    xalloc(flat.numentries*sizeof(struct nct_flat_entry));    - #if 0 -  SET_SVAL_TYPE(s, T_INT); -  SET_SVAL_TYPE(s2, T_INT); - #endif +     for (i=0; i<arr->size; i++)    {    if (TYPEOF(arr->item[i]) == T_INT && !arr->item[i].u.integer)    continue;       if (!image_color_svalue(arr->item+i,    &(flat.entries[n].color))) -  bad_arg_error("Colortable", -  0,0, 1, "array of colors or 0", 0, -  "Colortable(): bad element %d of colorlist\n",i); +  bad_arg_error("create", +  0, 1, "array of colors or 0", 0, +  "Bad element %d of colorlist.\n",i);      #if DEBUG    fprintf(stderr,"got %d: %02x%02x%02x\n",n,    flat.entries[n].color.r,    flat.entries[n].color.g,    flat.entries[n].color.b);   #endif       flat.entries[n].weight=1;    flat.entries[n].no=i;
pike.git/src/modules/Image/colortable.c:968:    return flat;   }      static struct nct_flat _img_get_flat_from_string(struct pike_string *str)   {    struct nct_flat flat;    int i;       flat.numentries = str->len/3;    if (flat.numentries<1) -  Pike_error("Can't make a colortable with less then one (1) color.\n"); +  Pike_error("Can't make a colortable with less than one (1) color.\n");       flat.entries=(struct nct_flat_entry*)    xalloc(flat.numentries*sizeof(struct nct_flat_entry));       for (i=0; i<flat.numentries; i++)    {    flat.entries[i].color.r=str->str[i*3];    flat.entries[i].color.g=str->str[i*3+1];    flat.entries[i].color.b=str->str[i*3+2];    flat.entries[i].weight=1;
pike.git/src/modules/Image/colortable.c:992:    return flat;   }      static struct nct_flat _img_get_flat_from_bgr_string(struct pike_string *str)   {    struct nct_flat flat;    int i;       flat.numentries=str->len/3;    if (flat.numentries<1) -  Pike_error("Can't make a colortable with less then one (1) color.\n"); +  Pike_error("Can't make a colortable with less than one (1) color.\n");       flat.entries=(struct nct_flat_entry*)    xalloc(flat.numentries*sizeof(struct nct_flat_entry));       for (i=0; i<flat.numentries; i++)    {    flat.entries[i].color.r=str->str[i*3+2];    flat.entries[i].color.g=str->str[i*3+1];    flat.entries[i].color.b=str->str[i*3];    flat.entries[i].weight=1;
pike.git/src/modules/Image/colortable.c:1016:    return flat;   }      static struct nct_flat _img_get_flat_from_bgrz_string(struct pike_string *str)   {    struct nct_flat flat;    int i;       flat.numentries=str->len/4;    if (flat.numentries<1) -  Pike_error("Can't make a colortable with less then one (1) color.\n"); +  Pike_error("Can't make a colortable with less than one (1) color.\n");       flat.entries=(struct nct_flat_entry*)    xalloc(flat.numentries*sizeof(struct nct_flat_entry));       for (i=0; i<flat.numentries; i++)    {    flat.entries[i].color.r=str->str[i*4+2];    flat.entries[i].color.g=str->str[i*4+1];    flat.entries[i].color.b=str->str[i*4];    flat.entries[i].weight=1;    flat.entries[i].no=i;    }       return flat;   }    - static INLINE void _find_cube_dist(struct nct_cube cube,rgb_group rgb, + static inline void _find_cube_dist(struct nct_cube cube,rgb_group rgb,    int *dist,int *no,    rgbl_group sf)   {    int mindist;    struct nct_scale *s;    int nc;       *no=-1;       if (cube.r&&cube.g&&cube.b)
pike.git/src/modules/Image/colortable.c:1076:       while (s)    {    rgbl_group b;    int n;       b.r=((INT32)rgb.r)-s->low.r;    b.g=((INT32)rgb.g)-s->low.g;    b.b=((INT32)rgb.b)-s->low.b;    -  n = DOUBLE_TO_INT((s->steps*(b.r*s->vector.r+ +  n = (int)((s->steps*(b.r*s->vector.r+    b.g*s->vector.g+    b.b*s->vector.b))*s->invsqvector);       if (n<0) n=0; else if (n>=s->steps) n=s->steps-1;       if (s->no[n]>=nc)    {    int steps=s->steps;    int ldist =    sf.r*sq_int(rgb.r-((s->high.r*n+s->low.r*(steps-n-1))/(steps-1)))+
pike.git/src/modules/Image/colortable.c:1153:       while (args>=ap+3)    {    rgb_group low,high;    int steps;    int isteps;    int mdist;    int c;       if (!image_color_arg(-args,&low)) -  SIMPLE_BAD_ARG_ERROR("colortable",1,"color"); +  SIMPLE_ARG_TYPE_ERROR("create",1,"color");    if (!image_color_arg(1-args,&high)) -  SIMPLE_BAD_ARG_ERROR("colortable",2,"color"); +  SIMPLE_ARG_TYPE_ERROR("create",2,"color");    if (TYPEOF(sp[2+ap-args]) != T_INT)    Pike_error("illegal argument(s) %d, %d or %d\n",ap,ap+1,ap+2);       steps=isteps=sp[2+ap-args].u.integer;    ap+=3;       if (steps<2) continue; /* no idea */       c=0;    o2steps=osteps=-1;
pike.git/src/modules/Image/colortable.c:1366:    if (!(mark=insert_in_hash_nd(en->color,hash,hashsize)))    {    struct color_hash_entry *oldhash=hash;    size_t oldhashsize = hashsize;      rerun_rehash_add_1:    j = oldhashsize;       hashsize*=2;    -  hash=malloc(sizeof(struct color_hash_entry)*hashsize); +  hash=calloc(sizeof(struct color_hash_entry), hashsize);    if (!hash)    {    free(oldhash);    free_colortable_struct(&tmp1);    free_colortable_struct(&tmp2); -  resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); +  out_of_memory_error(NULL, -1, 0);    }    k=hashsize;    while (k--) hash[k].pixels=0;       while (j--)    if (oldhash[j].pixels)    {    mark=insert_in_hash_nd(oldhash[j].color,hash,hashsize);    if (!mark) {    free(hash);
pike.git/src/modules/Image/colortable.c:1394:    }    mark->no=oldhash[i].no;    mark->pixels=oldhash[i].pixels;    }       free(oldhash);    continue;    }       mark->no=en->no; -  mark->pixels += DO_NOT_WARN((unsigned long)en->weight); +  mark->pixels += (unsigned long)en->weight;       i--;    en++;    }       i=src->u.flat.numentries;    en=src->u.flat.entries;    no=dest->u.flat.numentries;       while (i)
pike.git/src/modules/Image/colortable.c:1416:    if (!(mark=insert_in_hash_nd(en->color,hash,hashsize)))    {    struct color_hash_entry *oldhash=hash;    size_t oldhashsize = hashsize;      rerun_rehash_add_2:    j = oldhashsize;       hashsize*=2;    -  hash=malloc(sizeof(struct color_hash_entry)*hashsize); +  hash=calloc(sizeof(struct color_hash_entry), hashsize);    if (!hash)    {    free(oldhash);    free_colortable_struct(&tmp1);    free_colortable_struct(&tmp2); -  resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); +  out_of_memory_error(NULL, -1, 0);    }    i=hashsize;    while (i--) hash[i].pixels=0;       while (j--)    if (oldhash[j].pixels)    {    mark=insert_in_hash_nd(oldhash[j].color,hash,hashsize);    if (!mark) {    free(hash);
pike.git/src/modules/Image/colortable.c:1449:       free(oldhash);    continue;    }       if (mark->pixels==WEIGHT_NEEDED || en->weight==WEIGHT_NEEDED)    mark->pixels=WEIGHT_NEEDED;    else    {    if (!mark->pixels) mark->no=no++; -  mark->pixels += DO_NOT_WARN((unsigned long)en->weight); +  mark->pixels += (unsigned long)en->weight;    }       i--;    en++;    }       /* convert to flat */       i=hashsize;    j=0;    while (i--)    if (hash[i].pixels) j++;    /* j is now the number of colors */    flat.numentries=j; -  flat.entries=malloc(sizeof(struct nct_flat_entry)*j); +  flat.entries=calloc(sizeof(struct nct_flat_entry), j);    if (!flat.entries)    {    free(hash); -  resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); +  out_of_memory_error(NULL, -1, 0);    }    j=0;    i=hashsize;    while (i--)    if (hash[i].pixels)    {    flat.entries[j].color=hash[i].color;    flat.entries[j].no=j;    flat.entries[j].weight=hash[i].pixels;    j++;
pike.git/src/modules/Image/colortable.c:1549:   #endif       i=dest->u.flat.numentries;    en=dest->u.flat.entries;       while (i)    {    if (!(mark=insert_in_hash_nd(en->color,hash,hashsize)))    {    struct color_hash_entry *oldhash=hash; -  j=hashsize; +  size_t oldhashsize = hashsize;      rerun_rehash_add_1: -  +  j = oldhashsize;       hashsize*=2;    -  hash=malloc(sizeof(struct color_hash_entry)*hashsize); +  hash=calloc(sizeof(struct color_hash_entry), hashsize);    if (!hash)    {    free(oldhash);    free_colortable_struct(&tmp1);    free_colortable_struct(&tmp2); -  resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); +  out_of_memory_error(NULL, -1, 0);    } -  k=hashsize; -  while (k--) hash[k].pixels=0; +        while (j--)    if (oldhash[j].pixels)    {    mark=insert_in_hash_nd(oldhash[j].color,hash,hashsize); -  if (!mark) goto rerun_rehash_add_1; -  mark->no=oldhash[i].no; -  mark->pixels=oldhash[i].pixels; +  if (!mark) { +  free(hash); +  goto rerun_rehash_add_1;    } -  +  mark->no=oldhash[j].no; +  mark->pixels=oldhash[j].pixels; +  }       free(oldhash);    continue;    }       mark->no=en->no; -  mark->pixels += DO_NOT_WARN((unsigned long)en->weight); +  mark->pixels += (unsigned long)en->weight;       i--;    en++;    }       i=src->u.flat.numentries;    en=src->u.flat.entries;    no=dest->u.flat.numentries;       while (i)    {    if (!(mark=insert_in_hash_nd(en->color,hash,hashsize)))    {    struct color_hash_entry *oldhash=hash; -  j=hashsize; +  size_t oldhashsize = hashsize;      rerun_rehash_add_2: -  +  j = oldhashsize;       hashsize*=2;    -  hash=malloc(sizeof(struct color_hash_entry)*hashsize); +  hash=calloc(sizeof(struct color_hash_entry), hashsize);    if (!hash)    {    free(oldhash);    free_colortable_struct(&tmp1);    free_colortable_struct(&tmp2); -  resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); +  out_of_memory_error(NULL, -1, 0);    } -  i=hashsize; -  while (i--) hash[i].pixels=0; +        while (j--)    if (oldhash[j].pixels)    {    mark=insert_in_hash_nd(oldhash[j].color,hash,hashsize); -  if (!mark) goto rerun_rehash_add_2; +  if (!mark) { +  free(hash); +  goto rerun_rehash_add_2; +  }    if (mark->pixels!=1) -  mark->no=oldhash[i].no; -  mark->pixels=oldhash[i].pixels; +  mark->no=oldhash[j].no; +  mark->pixels=oldhash[j].pixels;    }       free(oldhash);    continue;    }       mark->pixels=WEIGHT_REMOVE;       i--;    en++;    }       /* convert to flat */       i=hashsize;    j=0;    while (i--)    if (hash[i].pixels && hash[i].pixels!=WEIGHT_REMOVE) j++;    /* j is now the number of colors */    flat.numentries=j; -  flat.entries=malloc(sizeof(struct nct_flat_entry)*j); +  flat.entries=calloc(sizeof(struct nct_flat_entry), j);    if (!flat.entries)    {    free(hash); -  resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); +  out_of_memory_error(NULL, -1, 0);    }    j=0;    i=hashsize;    while (i--)    if (hash[i].pixels && hash[i].pixels!=WEIGHT_REMOVE)    {    flat.entries[j].color=hash[i].color;    flat.entries[j].no=j;    flat.entries[j].weight=hash[i].pixels;    j++;
pike.git/src/modules/Image/colortable.c:1680:   static rgbl_group dither_floyd_steinberg_encode(struct nct_dither *dith,    int rowpos,    rgb_group s)   {    rgbl_group rgb;    int i;    rgbd_group *err=dith->u.floyd_steinberg.errors+rowpos;    if (err->r>255) err->r=255; else if (err->r<-255) err->r=-255;    if (err->g>255) err->g=255; else if (err->g<-255) err->g=-255;    if (err->b>255) err->b=255; else if (err->b<-255) err->b=-255; -  i = DOUBLE_TO_INT((int)s.r-err->r+0.5); +  i = (int)((int)s.r-err->r+0.5);    rgb.r=i<0?0:(i>255?255:i); -  i = DOUBLE_TO_INT((int)s.g-err->g+0.5); +  i = (int)((int)s.g-err->g+0.5);    rgb.g=i<0?0:(i>255?255:i); -  i = DOUBLE_TO_INT((int)s.b-err->b+0.5); +  i = (int)((int)s.b-err->b+0.5);    rgb.b=i<0?0:(i>255?255:i);    return rgb;   }      static void dither_floyd_steinberg_got(struct nct_dither *dith,    int rowpos,    rgb_group s,    rgb_group d)   {    int cd = dith->u.floyd_steinberg.currentdir;       rgbd_group *ner=dith->u.floyd_steinberg.nexterrors;    rgbd_group *er=dith->u.floyd_steinberg.errors;    rgbd_group err;    -  err.r = DO_NOT_WARN((float)((DOUBLE_TO_INT(d.r)-DOUBLE_TO_INT(s.r)) + -  er[rowpos].r+0.5)); -  err.g = DO_NOT_WARN((float)((DOUBLE_TO_INT(d.g)-DOUBLE_TO_INT(s.g)) + -  er[rowpos].g+0.5)); -  err.b = DO_NOT_WARN((float)((DOUBLE_TO_INT(d.b)-DOUBLE_TO_INT(s.b)) + -  er[rowpos].b+0.5)); +  err.r = (float)(((int)d.r-(int)s.r) + er[rowpos].r+0.5); +  err.g = (float)(((int)d.g-(int)s.g) + er[rowpos].g+0.5); +  err.b = (float)(((int)d.b-(int)s.b) + er[rowpos].b+0.5);       ner[rowpos].r+=err.r*dith->u.floyd_steinberg.down;    ner[rowpos].g+=err.g*dith->u.floyd_steinberg.down;    ner[rowpos].b+=err.b*dith->u.floyd_steinberg.down;       if (rowpos+cd>=0 && rowpos+cd<dith->rowlen)    {    ner[rowpos+cd].r+=err.r*dith->u.floyd_steinberg.downforward;    ner[rowpos+cd].g+=err.g*dith->u.floyd_steinberg.downforward;    ner[rowpos+cd].b+=err.b*dith->u.floyd_steinberg.downforward;
pike.git/src/modules/Image/colortable.c:1801:    int *rowpos,    rgb_group **s,    rgb_group **drgb,    unsigned char **d8bit,    unsigned short **d16bit,    unsigned INT32 **d32bit,    int *cd)   {    rgbd_group *er;    int i; +  unsigned INT16 *rnd;       er=dith->u.floyd_steinberg.errors; -  +  push_random_string(dith->rowlen*2*3); +  rnd = (unsigned INT16*)Pike_sp[-1].u.string->str;    for (i=0; i<dith->rowlen; i++)    { -  er[i].r = DO_NOT_WARN((float)((my_rand()&65535)*(1.0/65536)-0.49999)); -  er[i].g = DO_NOT_WARN((float)((my_rand()&65535)*(1.0/65536)-0.49999)); -  er[i].b = DO_NOT_WARN((float)((my_rand()&65535)*(1.0/65536)-0.49999)); +  er[i].r = (float)(rnd[i*3+0]*(1.0/65536)-0.49999); +  er[i].g = (float)(rnd[i*3+1]*(1.0/65536)-0.49999); +  er[i].b = (float)(rnd[i*3+2]*(1.0/65536)-0.49999);    } -  +  pop_stack();       er=dith->u.floyd_steinberg.nexterrors;    for (i=0; i<dith->rowlen; i++) er[i].r=er[i].g=er[i].b=0.0;       if (dith->u.floyd_steinberg.dir>=0)    {    dith->u.floyd_steinberg.currentdir=(*cd)=1;    *rowpos=0;    }    else
pike.git/src/modules/Image/colortable.c:1830:    dith->u.floyd_steinberg.currentdir=(*cd)=-1;    (*rowpos)=dith->rowlen-1;    (*s)+=dith->rowlen-1;    if (drgb) (*drgb)+=dith->rowlen-1;    if (d8bit) (*d8bit)+=dith->rowlen-1;    if (d16bit) (*d16bit)+=dith->rowlen-1;    if (d32bit) (*d32bit)+=dith->rowlen-1;    }   }    + static int randomcube_rnd(struct nct_dither *dith, int limit) + { +  int value; +  push_int(limit); +  apply_svalue(dith->u.randomcube.rnd, 1); +  if(TYPEOF(Pike_sp[-1])!=T_INT) +  Pike_error("Couldn't generate random number.\n"); +  value = Pike_sp[-1].u.integer; +  pop_stack(); +  return value; + } +    static rgbl_group dither_randomcube_encode(struct nct_dither *dith,    int UNUSED(rowpos),    rgb_group s)   {    rgbl_group rgb;    int i; -  i=(int)(s.r-(my_rand()%(dith->u.randomcube.r*2-1))+dith->u.randomcube.r+1); +  i=(int)(s.r-randomcube_rnd(dith, dith->u.randomcube.r*2-1)+ +  dith->u.randomcube.r+1);    rgb.r=i<0?0:(i>255?255:i); -  i=(int)(s.g-(my_rand()%(dith->u.randomcube.g*2-1))+dith->u.randomcube.g+1); +  i=(int)(s.g-randomcube_rnd(dith, dith->u.randomcube.g*2-1)+ +  dith->u.randomcube.g+1);    rgb.g=i<0?0:(i>255?255:i); -  i=(int)(s.b-(my_rand()%(dith->u.randomcube.b*2-1))+dith->u.randomcube.b+1); +  i=(int)(s.b-randomcube_rnd(dith, dith->u.randomcube.b*2-1)+ +  dith->u.randomcube.b+1);    rgb.b=i<0?0:(i>255?255:i);    return rgb;   }      static rgbl_group dither_randomgrey_encode(struct nct_dither *dith,    int UNUSED(rowpos),    rgb_group s)   {    rgbl_group rgb;    int i; -  int err = -(int)((my_rand()%(dith->u.randomcube.r*2-1))+ +  int err = -(int)(randomcube_rnd(dith, dith->u.randomcube.r*2-1)+    dith->u.randomcube.r+1);    i=(int)(s.r+err);    rgb.r=i<0?0:(i>255?255:i);    i=(int)(s.g+err);    rgb.g=i<0?0:(i>255?255:i);    i=(int)(s.b+err);    rgb.b=i<0?0:(i>255?255:i);    return rgb;   }   
pike.git/src/modules/Image/colortable.c:1975:    dith->u.floyd_steinberg.dir=    nct->du.floyd_steinberg.dir;    return 1;       case NCTD_RANDOMCUBE:   #ifdef COLORTABLE_DEBUG    fprintf(stderr, "COLORTABLE image_colortable_initiate_dither: "    "RANDOMCUBE\n");   #endif /* COLORTABLE_DEBUG */    dith->u.randomcube=THIS->du.randomcube; -  +     dith->encode=dither_randomcube_encode; -  +  { +  struct svalue *rnd = +  simple_mapping_string_lookup(get_builtin_constants(), "random"); +  if(!rnd || (TYPEOF(*rnd) != T_FUNCTION)) +  Pike_error("Unable to resolve random function.\n"); +  ++*rnd->u.refs; +  dith->u.randomcube.rnd = rnd; +  }    return 1;       case NCTD_RANDOMGREY:   #ifdef COLORTABLE_DEBUG    fprintf(stderr, "COLORTABLE image_colortable_initiate_dither: "    "RANDOMGREY\n");   #endif /* COLORTABLE_DEBUG */    dith->u.randomcube=THIS->du.randomcube; -  +     dith->encode=dither_randomgrey_encode; -  +  { +  struct svalue *rnd = +  simple_mapping_string_lookup(get_builtin_constants(), "random"); +  if(!rnd || (TYPEOF(*rnd) != T_FUNCTION)) +  Pike_error("Unable to resolve random function.\n"); +  ++*rnd->u.refs; +  dith->u.randomcube.rnd = rnd; +  }    return 1;       case NCTD_ORDERED:   #ifdef COLORTABLE_DEBUG    fprintf(stderr, "COLORTABLE image_colortable_initiate_dither: "    "ORDERED\n");   #endif /* COLORTABLE_DEBUG */    /* copy it all */    dith->u.ordered=nct->du.ordered;   
pike.git/src/modules/Image/colortable.c:2015:    nct->du.ordered.ys);    if (!dith->u.ordered.rdiff||    !dith->u.ordered.gdiff||    !dith->u.ordered.bdiff)    {    if (dith->u.ordered.rdiff) free(dith->u.ordered.rdiff);    if (dith->u.ordered.gdiff) free(dith->u.ordered.gdiff);    if (dith->u.ordered.bdiff) free(dith->u.ordered.bdiff);    return 0;    } -  MEMCPY(dith->u.ordered.rdiff,nct->du.ordered.rdiff, +  memcpy(dith->u.ordered.rdiff,nct->du.ordered.rdiff,    sizeof(int)*nct->du.ordered.xs*nct->du.ordered.ys); -  MEMCPY(dith->u.ordered.gdiff,nct->du.ordered.gdiff, +  memcpy(dith->u.ordered.gdiff,nct->du.ordered.gdiff,    sizeof(int)*nct->du.ordered.xs*nct->du.ordered.ys); -  MEMCPY(dith->u.ordered.bdiff,nct->du.ordered.bdiff, +  memcpy(dith->u.ordered.bdiff,nct->du.ordered.bdiff,    sizeof(int)*nct->du.ordered.xs*nct->du.ordered.ys);       dith->u.ordered.row=0;       if (nct->du.ordered.same)    {    dith->encode=dither_ordered_encode_same;    dith->u.ordered.xa=dith->u.ordered.xs-1;    dith->u.ordered.ya=dith->u.ordered.ys-1;    }
pike.git/src/modules/Image/colortable.c:2052:    switch (dith->type)    {    case NCTD_NONE:    break;    case NCTD_FLOYD_STEINBERG:    free(dith->u.floyd_steinberg.errors);    free(dith->u.floyd_steinberg.nexterrors);    break;    case NCTD_RANDOMCUBE:    case NCTD_RANDOMGREY: +  --*dith->u.randomcube.rnd->u.refs;    break;    case NCTD_ORDERED:    free(dith->u.ordered.rdiff);    free(dith->u.ordered.gdiff);    free(dith->u.ordered.bdiff);    break;    }   }      /***************** global methods ******************************/      /*   **! method void create()   **! method void create(array(array(int)) colors) -  + **! method void create(object(Image.Colortable)) colortable)   **! method void create(object(Image.Image) image,int number)   **! method void create(object(Image.Image) image,int number,array(array(int)) needed)   **! method void create(int r,int g,int b)   **! method void create(int r,int g,int b, array(int) from1,array(int) to1,int steps1, ..., array(int) fromn,array(int) ton,int stepsn)   **! method object add(array(array(int)) colors) -  + **! method object add(object(Image.Colortable)) colortable)   **! method object add(object(Image.Image) image,int number)   **! method object add(object(Image.Image) image,int number,array(array(int)) needed)   **! method object add(int r,int g,int b)   **! method object add(int r,int g,int b, array(int) from1,array(int) to1,int steps1, ..., array(int) fromn,array(int) ton,int stepsn)   **   ** developers note:   ** method object add|create(string "rgbrgbrgb...")   **!   **! <ref>create</ref> initiates a colortable object.   **! Default is that no colors are in the colortable.
pike.git/src/modules/Image/colortable.c:2106:   **!   **! ct=colortable(({({0,0,0}),({255,255,255})})); // black and white   **!   **! ct=colortable(6,7,6); // a colortable of 252 colors   **! ct=colortable(7,7,5, ({0,0,0}),({255,255,255}),11);   **! // a colorcube of 245 colors, and a greyscale of the rest -> 256   **! </pre>   **!   **! arg array(array(int)) colors   **! list of colors + **! arg object(Image.Colortable) colortable + **! colortable to copy colors from.   **! arg object(Image.Image) image   **! source image   **!   **! note: you may not get all colors from image,   **! max hash size is (probably, set by a <tt>#define</tt>)   **! 32768 entries, giving   **! maybe half that number of colors as maximum.   **! arg int number   **! number of colors to get from the image   **!
pike.git/src/modules/Image/colortable.c:2161:    return;    }       if (THIS->type!=NCT_NONE)    {    struct object *o;       if (TYPEOF(sp[-args]) == T_OBJECT)    {    struct neo_colortable *ct2; -  ct2=(struct neo_colortable*) -  get_storage(sp[-args].u.object,image_colortable_program); +  ct2=get_storage(sp[-args].u.object,image_colortable_program);    if (ct2)    {   #ifdef COLORTABLE_DEBUG    fprintf(stderr,"COLORTABLE added %p and %p to %p (args=%d)\n",    THIS,ct2,THIS,args);   #endif    _img_add_colortable(THIS,ct2);    pop_n_elems(args);    ref_push_object(THISOBJ);    return;
pike.git/src/modules/Image/colortable.c:2202: Inside #if defined(COLORTABLE_DEBUG)
  #ifdef COLORTABLE_DEBUG    fprintf(stderr, "COLORTABLE %p created with %d args, sp-args=%p\n",    THIS, args, sp-args);   #endif       if (TYPEOF(sp[-args]) == T_OBJECT)    {    struct neo_colortable *ct2;    struct image *img;    -  if ((ct2=(struct neo_colortable*) -  get_storage(sp[-args].u.object,image_colortable_program))) +  if ((ct2=get_storage(sp[-args].u.object,image_colortable_program)))    {    /* just copy that colortable */    _img_copy_colortable(THIS,ct2);    } -  else if ((img=(struct image*) -  get_storage(sp[-args].u.object,image_program))) +  else if ((img=get_storage(sp[-args].u.object,image_program)))    {    /* get colors from image */    if (args>=2)    if (TYPEOF(sp[1-args]) == T_INT)    {    int numcolors=sp[1-args].u.integer;    if (args>2)    {    struct object *o;    struct neo_colortable *nct;       o=clone_object(image_colortable_program,args-2);    -  nct=(struct neo_colortable*) -  get_storage(o,image_colortable_program); +  nct=get_storage(o,image_colortable_program);       if (!nct) abort();       if (nct->type==NCT_CUBE)    nct->u.cube.weight=WEIGHT_NEEDED;    else if (nct->type==NCT_FLAT)    {    size_t i;    i = nct->u.flat.numentries;    while (i--)
pike.git/src/modules/Image/colortable.c:2266:    {    THIS->u.flat=_img_get_flat_from_image(img,numcolors);    THIS->type=NCT_FLAT;    THIS->u.flat=    _img_reduce_number_of_colors(THIS->u.flat,    numcolors,    THIS->spacefactor);    }    }    else -  bad_arg_error("Image",sp-args,args,2,"",sp+2-1-args, -  "Bad argument 2 to Image()\n"); +  bad_arg_error("add",args,2,"",sp+2-1-args, +  "Bad argument 2 to add.\n");    else    {    THIS->u.flat=_img_get_flat_from_image(img,256);    if (THIS->u.flat.numentries>256)    THIS->u.flat=_img_reduce_number_of_colors(THIS->u.flat,256,    THIS->spacefactor);    THIS->type=NCT_FLAT;    }    } -  else bad_arg_error("Image",sp-args,args,1,"",sp+1-1-args, -  "Bad argument 1 to Image()\n"); +  else bad_arg_error("add",args,1,"",sp+1-1-args, +  "Bad argument 1 to add.\n");    }    else if (TYPEOF(sp[-args]) == T_ARRAY)    {    THIS->u.flat=_img_get_flat_from_array(sp[-args].u.array);    THIS->type=NCT_FLAT;    }    else if (TYPEOF(sp[-args]) == T_STRING)    {    if (args>1)    {    if (TYPEOF(sp[1-args]) != T_INT) -  SIMPLE_BAD_ARG_ERROR("Image.Colortable",2,"int"); +  SIMPLE_ARG_TYPE_ERROR("add",2,"int");    switch (sp[1-args].u.integer)    {    case 0: /* rgb */    THIS->u.flat=_img_get_flat_from_string(sp[-args].u.string);    break;    case 1: /* bgr */    THIS->u.flat=_img_get_flat_from_bgr_string(sp[-args].u.string);    break;    case 2: /* bgrz */    THIS->u.flat=    _img_get_flat_from_bgrz_string(sp[-args].u.string);    break;    default: -  SIMPLE_BAD_ARG_ERROR("Image.Colortable",2,"int(0..2)"); +  SIMPLE_ARG_TYPE_ERROR("add",2,"int(0..2)");    }    }    else    THIS->u.flat=_img_get_flat_from_string(sp[-args].u.string);    THIS->type=NCT_FLAT;    }    else if (TYPEOF(sp[-args]) == T_INT)    {    THIS->u.cube=_img_get_cube_from_args(args);    THIS->type=NCT_CUBE;    } -  else bad_arg_error("Image",sp-args,args,0,"",sp-args, -  "Bad arguments to Image()\n"); +  else bad_arg_error("add",args,0,"",sp-args, +  "Bad arguments to add.\n");    pop_n_elems(args);    ref_push_object(THISOBJ);      #ifdef COLORTABLE_DEBUG    fprintf(stderr, "COLORTABLE done (%p created, %d args was left, sp-1=%p)\n",    THIS, args, sp-1);   #endif      }      void image_colortable_create(INT32 args)   {    if (args) /* optimize */ -  +  {    image_colortable_add(args); -  +  pop_stack(); +  }    else    push_undefined();   }      /*   **! method object reduce(int colors)   **! method object reduce_fs(int colors)   **! reduces the number of colors   **!   **! All needed (see <ref>create</ref>) colors are kept.
pike.git/src/modules/Image/colortable.c:2371:   **/      void image_colortable_reduce(INT32 args)   {    struct object *o;    struct neo_colortable *nct;    int numcolors=0;       if (args)    if (TYPEOF(sp[-args]) != T_INT) -  SIMPLE_BAD_ARG_ERROR("Image.Colortable->reduce",1,"int"); +  SIMPLE_ARG_TYPE_ERROR("reduce",1,"int");    else    numcolors=sp[-args].u.integer;    else    numcolors=1293791; /* a lot */       o=clone_object_from_object(THISOBJ,0); -  nct=(struct neo_colortable*)get_storage(o,image_colortable_program); +  nct=get_storage(o,image_colortable_program);       switch (nct->type = THIS->type)    {    case NCT_NONE: pop_n_elems(args); push_object(o); return;    case NCT_CUBE:    nct->type=NCT_FLAT;    nct->u.flat=_img_nct_cube_to_flat(THIS->u.cube);    break;    case NCT_FLAT:    _img_copy_colortable(nct,THIS);
pike.git/src/modules/Image/colortable.c:2412:      void image_colortable_reduce_fs(INT32 args)   {    int numcolors = 1293791; /* a lot */    int i;    struct object *o;    struct neo_colortable *nct;       if (args) {    if (TYPEOF(sp[-args]) != T_INT) -  SIMPLE_BAD_ARG_ERROR("Image.Colortable->reduce",1,"int"); +  SIMPLE_ARG_TYPE_ERROR("reduce_fs",1,"int");    else    numcolors=sp[-args].u.integer;    }       if (numcolors<2) -  SIMPLE_BAD_ARG_ERROR("Image.Colortable->reduce",1,"int(2..)"); +  SIMPLE_ARG_TYPE_ERROR("reduce_fs",1,"int(2..)");       pop_n_elems(args);    image_colortable_corners(0);       if (numcolors<8)    {    push_int(0);    push_int(1);    f_index(3);    }    push_object(o=clone_object(image_colortable_program,1)); -  nct=(struct neo_colortable*)get_storage(o,image_colortable_program); +  nct=get_storage(o,image_colortable_program);       for (i=0; i<nct->u.flat.numentries; i++)    nct->u.flat.entries[i].weight=WEIGHT_NEEDED;       image_colortable_add(1);    pop_stack();    push_int(numcolors);    image_colortable_reduce(1);   }   
pike.git/src/modules/Image/colortable.c:2460:      void image_colortable_operator_plus(INT32 args)   {    struct object *o,*tmpo=NULL;    struct neo_colortable *dest,*src=NULL;       int i;       ref_push_object(THISOBJ);    o=clone_object_from_object(THISOBJ,1); -  dest=(struct neo_colortable*)get_storage(o,image_colortable_program); +  dest=get_storage(o,image_colortable_program);       for (i=0; i<args; i++)    {    if (TYPEOF(sp[i-args]) == T_OBJECT && -  (src=(struct neo_colortable*) -  get_storage(sp[i-args].u.object,image_colortable_program))) +  (src=get_storage(sp[i-args].u.object,image_colortable_program)))    {    tmpo=NULL;    }    else if (TYPEOF(sp[i-args]) == T_ARRAY ||    TYPEOF(sp[i-args]) == T_OBJECT)    {    struct svalue *sv=sp+i-args;    push_svalue(sv);    tmpo=clone_object(image_colortable_program,1); -  src=(struct neo_colortable*) -  get_storage(tmpo,image_colortable_program); +  src=get_storage(tmpo,image_colortable_program);    if (!src) abort();    }    else { -  bad_arg_error("Image-colortable->`+",sp-args,args,0,"",sp-args, -  "Bad arguments to Image-colortable->`+()\n"); -  /* Not reached, but keeps the compiler happy. */ -  src = NULL; +  bad_arg_error("`+",args,0,"",sp-args, +  "Bad arguments to `+.\n"); +  UNREACHABLE(src = NULL);    }       _img_add_colortable(dest,src);       if (tmpo) free_object(tmpo);    }    pop_n_elems(args);    push_object(o);   }   
pike.git/src/modules/Image/colortable.c:2513:      void image_colortable_operator_minus(INT32 args)   {    struct object *o;    struct neo_colortable *dest,*src=NULL;       int i;       ref_push_object(THISOBJ);    o=clone_object_from_object(THISOBJ,1); -  dest=(struct neo_colortable*)get_storage(o,image_colortable_program); +  dest=get_storage(o,image_colortable_program);       for (i=0; i<args; i++)    if (TYPEOF(sp[i-args]) == T_OBJECT)    { -  src=(struct neo_colortable*) -  get_storage(sp[i-args].u.object,image_colortable_program); +  src=get_storage(sp[i-args].u.object,image_colortable_program);    if (!src)    {    free_object(o); -  bad_arg_error("Image",sp-args,args,i+2,"",sp+i+2-1-args, -  "Bad argument %d to Image()\n",i+2); +  bad_arg_error("`-",args,i+2,"",sp+i+2-1-args, +  "Bad argument %d to `-\n",i+2);    }    _img_sub_colortable(dest,src);    }    else    {    free_object(o); -  bad_arg_error("Image",sp-args,args,i+2,"",sp+i+2-1-args, -  "Bad argument %d to Image()\n",i+2); +  bad_arg_error("`-",args,i+2,"",sp+i+2-1-args, +  "Bad argument %d to `-.\n",i+2);    }    pop_n_elems(args);    push_object(o);   }      void image_colortable_cast_to_array(struct neo_colortable *nct)   {    struct nct_flat flat;    int i;    int n=0;
pike.git/src/modules/Image/colortable.c:2721:    image_colortable_cast_to_string( THIS );   }      static void image_colortable__decode( INT32 args )   {    image_colortable_create( args );   }         /* - **! method object cast(string to) + **! method array|mapping cast(string to)   **! cast the colortable to an array or mapping,   **! the array consists of <ref>Image.Color</ref> objects   **! and are not in index order. The mapping consists of   **! index:<ref>Image.Color</ref> pairs, where index is   **! the index (int) of that color.   **!   **! example: <tt>(mapping)Image.Colortable(img)</tt>   **!   **! arg string to   **! must be "string", "array" or "mapping".   **/      void image_colortable_cast(INT32 args)   { -  +  struct pike_string *type; +     if (!args) -  SIMPLE_TOO_FEW_ARGS_ERROR("Image.Colortable->cast",1); -  if (TYPEOF(sp[-args]) == T_STRING || sp[-args].u.string->size_shift) -  { -  if (!strncmp(sp[-args].u.string->str,"array",5)) -  { -  pop_n_elems(args); +  SIMPLE_WRONG_NUM_ARGS_ERROR("Image.Colortable->cast",1); +  +  type = Pike_sp[-args].u.string; +  pop_n_elems(args); /* type have at least one more reference. */ +  +  if (type == literal_array_string)    image_colortable_cast_to_array(THIS); -  return; -  } -  if (!strncmp(sp[-args].u.string->str,"string",6)) -  { -  pop_n_elems(args); +  else if (type == literal_string_string)    image_colortable_cast_to_string(THIS); -  return; -  } -  if (!strncmp(sp[-args].u.string->str,"mapping",7)) -  { -  pop_n_elems(args); +  else if (type == literal_mapping_string)    image_colortable_cast_to_mapping(THIS); -  return; +  else +  push_undefined();   } -  } -  SIMPLE_BAD_ARG_ERROR("Image.Colortable->cast",1, -  "string(\"mapping\"|\"array\"|\"string\")"); - } +       /*   **! method object full()   **! Set the colortable to use full scan to lookup the closest color.   **!   **! example: <tt>colors=Image.Colortable(img)->full();</tt>   **!   **! algorithm time: O[n*m], where n is numbers of colors   **! and m is number of pixels   **!
pike.git/src/modules/Image/colortable.c:2822:   **! note   **! Not applicable to colorcube types of colortable.   **/      void image_colortable_rigid(INT32 args)   {    INT_TYPE r,g,b;       if (args)    { -  get_all_args("Image.Colortable->rigid()",args,"%i%i%i",&r,&g,&b); +  get_all_args(NULL,args,"%i%i%i",&r,&g,&b);    }    else    {    r=RIGID_DEFAULT_R;    g=RIGID_DEFAULT_G;    b=RIGID_DEFAULT_B;    }       if (!(THIS->lookup_mode==NCT_RIGID &&    THIS->lu.rigid.r == r &&    THIS->lu.rigid.g == g &&    THIS->lu.rigid.b == b))    {    colortable_free_lookup_stuff(THIS);    THIS->lookup_mode=NCT_RIGID;    -  if (r<1) SIMPLE_BAD_ARG_ERROR("Image.Colortable->rigid",1,"integer >0"); -  if (g<1) SIMPLE_BAD_ARG_ERROR("Image.Colortable->rigid",2,"integer >0"); -  if (b<1) SIMPLE_BAD_ARG_ERROR("Image.Colortable->rigid",3,"integer >0"); +  if (r<1) SIMPLE_ARG_TYPE_ERROR("rigid",1,"int(1..)"); +  if (g<1) SIMPLE_ARG_TYPE_ERROR("rigid",2,"int(1..)"); +  if (b<1) SIMPLE_ARG_TYPE_ERROR("rigid",3,"int(1..)");       THIS->lu.rigid.r=r;    THIS->lu.rigid.g=g;    THIS->lu.rigid.b=b;    THIS->lu.rigid.index=NULL;    }       pop_n_elems(args);    ref_push_object(THISOBJ);   }
pike.git/src/modules/Image/colortable.c:2948:    THIS->lu.cubicles.r=MAXIMUM(sp[-args].u.integer,1);    THIS->lu.cubicles.g=MAXIMUM(sp[1-args].u.integer,1);    THIS->lu.cubicles.b=MAXIMUM(sp[2-args].u.integer,1);    if (args>=4 &&    TYPEOF(sp[3-args]) == T_INT)    THIS->lu.cubicles.accur=MAXIMUM(sp[3-args].u.integer,1);    else    THIS->lu.cubicles.accur=CUBICLE_DEFAULT_ACCUR;    }    else -  bad_arg_error("colortable->cubicles",sp-args,args,0,"",sp-args, -  "Bad arguments to colortable->cubicles()\n"); +  bad_arg_error("cubicles",args,0,"",sp-args, +  "Bad arguments to cubicles.\n");    else    {    THIS->lu.cubicles.r=CUBICLE_DEFAULT_R;    THIS->lu.cubicles.g=CUBICLE_DEFAULT_G;    THIS->lu.cubicles.b=CUBICLE_DEFAULT_B;    THIS->lu.cubicles.accur=CUBICLE_DEFAULT_ACCUR;    }       THIS->lookup_mode=NCT_CUBICLES;       pop_n_elems(args);    ref_push_object(THISOBJ);   }    - static int _cub_find_2cub_add(int *i,int *p, -  int *p2,int n2, -  struct nct_flat_entry *fe, -  rgbl_group sf, -  int r,int g,int b) - { -  int mindist=256*256*100; /* max dist is 256²*3 */ -  int c=0; -  int *p1=p; -  int n=*i; -  int k=1; -  -  while (n--) -  { -  int dist=sf.r*SQ(fe[*p1].color.r-r)+ -  sf.g*SQ(fe[*p1].color.g-g)+ -  sf.b*SQ(fe[*p1].color.b-b); -  -  if (dist<mindist) -  { -  c=*p1; -  mindist=dist; -  if (!dist) break; -  } -  -  p1++; -  } -  if (mindist) while (n2--) -  { -  int dist=sf.r*SQ(fe[*p2].color.r-r)+ -  sf.g*SQ(fe[*p2].color.g-g)+ -  sf.b*SQ(fe[*p2].color.b-b); -  -  if (dist<mindist) -  { -  c=*p2; -  k=0; -  if (!dist) break; -  mindist=dist; -  } -  -  p2++; -  } -  -  if (!k) -  { -  n=*i; -  while (n--) -  if (*p==c) return c; else p++; -  -  *p=c; -  (*i)++; -  } -  -  return c; - } -  - static void _cub_add_cs_2cub_recur(int *i,int *p, -  int *p2,int n2, -  struct nct_flat_entry *fe, -  int rp,int gp,int bp, -  int rd1,int gd1,int bd1, -  int rd2,int gd2,int bd2, -  INT32 *a, INT32 *b, -  INT32 *c, INT32 *d, -  rgbl_group sf, -  int accur) - { -  - /* a-h-b -> 2 -  | | | -  v e f g -  1 | | -  c-j-d */ -  -  INT32 e=-1,f=-1,g=-1,h=-1,j=-1; -  int rm1,gm1,bm1; -  int rm2,gm2,bm2; -  -  if (*a==-1) *a=_cub_find_2cub_add(i,p,p2,n2,fe,sf, rp,gp,bp); /* 0,0 */ -  if (*b==-1) *b=_cub_find_2cub_add(i,p,p2,n2,fe,sf, -  rp+rd2,gp+gd2,bp+bd2); /* 0,1 */ -  if (*c==-1) *c=_cub_find_2cub_add(i,p,p2,n2,fe,sf, -  rp+rd1,gp+gd1,bp+bd1); /* 1,0 */ -  if (*d==-1) *d=_cub_find_2cub_add(i,p,p2,n2,fe,sf, -  rp+rd2+rd1,gp+gd2+gd1,bp+bd2+bd1); /* 1,1 */ -  -  if (rd1+gd1+bd1<=accur && rd2+gd2+bd2<=accur) return; -  -  if (*a==*b) h=*a; -  if (*c==*d) j=*c; -  -  if (h!=-1 && h==j) return; /* all done */ -  -  if (*a==*c) e=*a; -  if (*b==*d) g=*b; -  if (*a==*d) f=*a; -  if (*b==*c) f=*b; -  -  rm1=rd1-(rd1>>1); rd1>>=1; -  gm1=gd1-(gd1>>1); gd1>>=1; -  bm1=bd1-(bd1>>1); bd1>>=1; -  rm2=rd2-(rd2>>1); rd2>>=1; -  gm2=gd2-(gd2>>1); gd2>>=1; -  bm2=bd2-(bd2>>1); bd2>>=1; -  -  _cub_add_cs_2cub_recur(i,p,p2,n2,fe, rp,gp,bp, rd1,gd1,bd1, rd2,gd2,bd2, a,&h,&e,&f,sf,accur); -  _cub_add_cs_2cub_recur(i,p,p2,n2,fe, rp+rd2,gp+gd2,bp+bd2, rd2?rm1:rd1,gd2?gm1:gd1,bd2?bm1:bd1, rd2?rm2:rd2,gd2?gm2:gd2,bd2?bm2:bd2, &h,b,&f,&g,sf,accur); -  _cub_add_cs_2cub_recur(i,p,p2,n2,fe, rp+rd1,gp+gd1,bp+bd1, rd1?rm1:rd1,gd1?gm1:gd1,bd1?bm1:bd1, rd1?rm2:rd2,gd1?gm2:gd2,bd1?bm2:bd2, &e,&f,c,&j,sf,accur); -  _cub_add_cs_2cub_recur(i,p,p2,n2,fe, rp+rd2+rd1,gp+gd2+gd1,bp+bd2+bd1, rm1,gm1,bm1, rm2,gm2,bm2, &f,&g,&j,d,sf,accur); - } -  - static INLINE ptrdiff_t _cub_find_full_add(int **pp, int *i, int *p, + static inline ptrdiff_t _cub_find_full_add(int **pp, int *i, int *p,    ptrdiff_t n,    struct nct_flat_entry *fe,    int r,int g,int b,    rgbl_group sf)   {    int mindist=256*256*100; /* max dist is 256²*3 */    int c = 0;       while (n--)    if (fe->no==-1) fe++;
pike.git/src/modules/Image/colortable.c:3179:    rm2=rd2>>1; rd2-=rm2;    gm2=gd2>>1; gd2-=gm2;    bm2=bd2>>1; bd2-=bm2;       _cub_add_cs_full_recur(pp,i,p,n,fe, rp,gp,bp, rd1,gd1,bd1, rd2,gd2,bd2, a,&h,&e,&f,sf,accur);    _cub_add_cs_full_recur(pp,i,p,n,fe, rp+rd2,gp+gd2,bp+bd2, rd2?rm1:rd1,gd2?gm1:gd1,bd2?bm1:bd1, rd2?rm2:rd2,gd2?gm2:gd2,bd2?bm2:bd2, &h,b,&f,&g,sf,accur);    _cub_add_cs_full_recur(pp,i,p,n,fe, rp+rd1,gp+gd1,bp+bd1, rd1?rm1:rd1,gd1?gm1:gd1,bd1?bm1:bd1, rd1?rm2:rd2,gd1?gm2:gd2,bd1?bm2:bd2, &e,&f,c,&j,sf,accur);    _cub_add_cs_full_recur(pp,i,p,n,fe, rp+rd2+rd1,gp+gd2+gd1,bp+bd2+bd1, rm1,gm1,bm1, rm2,gm2,bm2, &f,&g,&j,d,sf,accur);   }    - static INLINE void _cub_add_cs(struct neo_colortable *nct, + static inline void _cub_add_cs(struct neo_colortable *nct,    struct nctlu_cubicle *UNUSED(cub),    int **pp,int *i,int *p,    int ri,int gi,int bi,    int red,int green,int blue,    int rp,int gp,int bp,    int rd1,int gd1,int bd1,    int rd2,int gd2,int bd2)   {    INT32 a=-1,b=-1,c=-1,d=-1;   #ifdef CUBICLE_DEBUG
pike.git/src/modules/Image/colortable.c:3220:   #endif    _cub_add_cs_full_recur(pp,i,p,    nct->u.flat.numentries,    nct->u.flat.entries,    rp,gp,bp, rd1,gd1,bd1, rd2,gd2,bd2,    &a,&b,&c,&d,    nct->spacefactor,    nct->lu.cubicles.accur);   }    - static INLINE void _build_cubicle(struct neo_colortable *nct, + static inline void _build_cubicle(struct neo_colortable *nct,    int r,int g,int b,    int red,int green,int blue,    struct nctlu_cubicle *cub)   {    int rmin,rmax;    int gmin,gmax;    int bmin,bmax;       struct nct_flat_entry *fe=nct->u.flat.entries;    INT32 n = nct->u.flat.numentries;
pike.git/src/modules/Image/colortable.c:3255:    pp=p;       while (n--)    if (fe->no==-1) fe++;    else    {    if ((int)fe->color.r>=rmin && (int)fe->color.r<=rmax &&    (int)fe->color.g>=gmin && (int)fe->color.g<=gmax &&    (int)fe->color.b>=bmin && (int)fe->color.b<=bmax)    { -  *pp = DO_NOT_WARN((int)fe->no); +  *pp = (int)fe->no;    pp++; i++;    }       fe++;    }       /* add closest to sides */    _cub_add_cs(nct,cub,&pp,&i,p,r-1,g,b,red,green,blue,rmin,gmin,bmin,0,gmax-gmin,0,0,0,bmax-bmin);    _cub_add_cs(nct,cub,&pp,&i,p,r,g-1,b,red,green,blue,rmin,gmin,bmin,rmax-rmin,0,0,0,0,bmax-bmin);    _cub_add_cs(nct,cub,&pp,&i,p,r,g,b-1,red,green,blue,rmin,gmin,bmin,rmax-rmin,0,0,0,gmax-gmin,0);
pike.git/src/modules/Image/colortable.c:3313:       if (nct->lu.rigid.index) Pike_fatal("rigid is initialized twice.\n");       index=malloc(sizeof(int)*r*g*b);    dist=malloc(sizeof(int)*r*g*b);       if (!index||!dist)    {    if (index) free(index);    if (dist) free(dist); -  resource_error(NULL,0,0,"memory",r*g*b*sizeof(int),"Out of memory.\n"); +  out_of_memory_error(NULL, -1, r*g*b*sizeof(int));    }       for (i=0; i<nct->u.flat.numentries; i++)    {    rc=nct->u.flat.entries[i].color.r;    gc=nct->u.flat.entries[i].color.g;    bc=nct->u.flat.entries[i].color.b;       ddist=dist;    dindex=index;
pike.git/src/modules/Image/colortable.c:3422:   **!   **! note   **! Flat (not cube) colortable and not '<ref>full</ref>' method:   **! this method does figure out the data needed for   **! the lookup method, which may take time the first   **! use of the colortable - the second use is quicker.   **!   **! see also: cubicles, full   **/    - /* Some functions to avoid warnings about losss of precision. */ - #ifdef __ECL - static inline unsigned char TO_UCHAR(ptrdiff_t val) - { -  return DO_NOT_WARN((unsigned char)val); - } - static inline unsigned short TO_USHORT(ptrdiff_t val) - { -  return DO_NOT_WARN((unsigned short)val); - } - static inline unsigned INT32 TO_UINT32(ptrdiff_t val) - { -  return DO_NOT_WARN((unsigned INT32)val); - } - #else /* !__ECL */ - #define TO_UCHAR(x) ((unsigned char)x) - #define TO_USHORT(x) ((unsigned short)x) - #define TO_UINT32(x) ((unsigned INT32)x) - #endif /* __ECL */ -  +    /* begin instantiating from colortable_lookup.h */   /* instantiate map functions */      #define NCTLU_DESTINATION rgb_group   #define NCTLU_CACHE_HIT_WRITE *d=lc->dest;   #define NCTLU_DITHER_GOT *d   #define NCTLU_FLAT_CUBICLES_NAME _img_nct_map_to_flat_cubicles   #define NCTLU_FLAT_FULL_NAME _img_nct_map_to_flat_full   #define NCTLU_CUBE_NAME _img_nct_map_to_cube   #define NCTLU_FLAT_RIGID_NAME _img_nct_map_to_flat_rigid
pike.git/src/modules/Image/colortable.c:3493:   /* instantiate 8bit functions */      #define NCTLU_DESTINATION unsigned char   #define NCTLU_CACHE_HIT_WRITE *d=((unsigned char)(lc->index))   #define NCTLU_DITHER_GOT lc->dest   #define NCTLU_FLAT_CUBICLES_NAME _img_nct_index_8bit_flat_cubicles   #define NCTLU_FLAT_FULL_NAME _img_nct_index_8bit_flat_full   #define NCTLU_CUBE_NAME _img_nct_index_8bit_cube   #define NCTLU_FLAT_RIGID_NAME _img_nct_index_8bit_flat_rigid   #define NCTLU_LINE_ARGS (dith,&rowpos,&s,NULL,&d,NULL,NULL,&cd) - #define NCTLU_RIGID_WRITE (d[0] = TO_UCHAR(feprim[i].no)) + #define NCTLU_RIGID_WRITE (d[0] = (unsigned char)feprim[i].no)   #define NCTLU_DITHER_RIGID_GOT (feprim[i].color)   #define NCTLU_SELECT_FUNCTION image_colortable_index_8bit_function   #define NCTLU_EXECUTE_FUNCTION image_colortable_index_8bit_image      #define NCTLU_CUBE_FAST_WRITE(SRC) \    *d=(unsigned char) \    ((int)((SRC)->r*red+hred)>>8)+ \    (((int)((SRC)->g*green+hgreen)>>8)+ \    ((int)((SRC)->b*blue+hblue)>>8)*green)*red;   
pike.git/src/modules/Image/colortable.c:3541:   /* instantiate 16bit functions */      #define NCTLU_DESTINATION unsigned short   #define NCTLU_CACHE_HIT_WRITE *d=((unsigned short)(lc->index))   #define NCTLU_DITHER_GOT lc->dest   #define NCTLU_FLAT_CUBICLES_NAME _img_nct_index_16bit_flat_cubicles   #define NCTLU_FLAT_FULL_NAME _img_nct_index_16bit_flat_full   #define NCTLU_CUBE_NAME _img_nct_index_16bit_cube   #define NCTLU_FLAT_RIGID_NAME _img_nct_index_16bit_flat_rigid   #define NCTLU_LINE_ARGS (dith,&rowpos,&s,NULL,NULL,&d,NULL,&cd) - #define NCTLU_RIGID_WRITE (d[0] = TO_USHORT(feprim[i].no)) + #define NCTLU_RIGID_WRITE (d[0] = (unsigned short)feprim[i].no)   #define NCTLU_DITHER_RIGID_GOT (feprim[i].color)   #define NCTLU_SELECT_FUNCTION image_colortable_index_16bit_function   #define NCTLU_EXECUTE_FUNCTION image_colortable_index_16bit_image      #define NCTLU_CUBE_FAST_WRITE(SRC) \    *d=(unsigned short) \    ((int)((SRC)->r*red+hred)>>8)+ \    (((int)((SRC)->g*green+hgreen)>>8)+ \    ((int)((SRC)->b*blue+hblue)>>8)*green)*red;   
pike.git/src/modules/Image/colortable.c:3589:   /* instantiate 32bit functions */      #define NCTLU_DESTINATION unsigned INT32   #define NCTLU_CACHE_HIT_WRITE *d=((unsigned INT32)(lc->index))   #define NCTLU_DITHER_GOT lc->dest   #define NCTLU_FLAT_CUBICLES_NAME _img_nct_index_32bit_flat_cubicles   #define NCTLU_FLAT_FULL_NAME _img_nct_index_32bit_flat_full   #define NCTLU_CUBE_NAME _img_nct_index_32bit_cube   #define NCTLU_FLAT_RIGID_NAME _img_nct_index_32bit_flat_rigid   #define NCTLU_LINE_ARGS (dith,&rowpos,&s,NULL,NULL,NULL,&d,&cd) - #define NCTLU_RIGID_WRITE (d[0] = TO_UINT32(feprim[i].no)) + #define NCTLU_RIGID_WRITE (d[0] = (unsigned INT32)feprim[i].no)   #define NCTLU_DITHER_RIGID_GOT (feprim[i].color)   #define NCTLU_SELECT_FUNCTION image_colortable_index_32bit_function   #define NCTLU_EXECUTE_FUNCTION image_colortable_index_32bit_image      #define NCTLU_CUBE_FAST_WRITE(SRC) \    *d=(unsigned INT32) \    ((int)((SRC)->r*red+hred)>>8)+ \    (((int)((SRC)->g*green+hgreen)>>8)+ \    ((int)((SRC)->b*blue+hblue)>>8)*green)*red;   
pike.git/src/modules/Image/colortable.c:3636:      /* done instantiating from colortable_lookup.h */      void image_colortable_map(INT32 args)   {    struct image *src=NULL;    struct image *dest;    struct object *o;       if (args<1) -  SIMPLE_TOO_FEW_ARGS_ERROR("colortable->map",1); +  SIMPLE_WRONG_NUM_ARGS_ERROR("map",1);       if (TYPEOF(sp[-args]) == T_STRING)    {    struct object *o;    struct pike_string *ps=sp[-args].u.string;    struct image *img;    ptrdiff_t n;    struct neo_colortable *nct=THIS;    rgb_group *d;       if (args!=3)    Pike_error("illegal number of arguments to colortable->map()\n");    o=clone_object(image_program,2); -  img=(struct image*)get_storage(o,image_program); +  img=get_storage(o,image_program);    d=img->img;       n=img->xsize*img->ysize;    if (n>ps->len) n=ps->len;       switch (ps->size_shift)    {    case 0:    {    p_wchar0 *s=(p_wchar0*)ps->str;
pike.git/src/modules/Image/colortable.c:3709:    }    }       pop_stack(); /* pops the given string */    push_object(o); /* pushes the image object */       return;    }       if (TYPEOF(sp[-args]) != T_OBJECT || -  ! (src=(struct image*)get_storage(sp[-args].u.object,image_program))) -  bad_arg_error("colortable->map",sp-args,args,1,"",sp+1-1-args, -  "Bad argument 1 to colortable->map()\n"); +  ! (src=get_storage(sp[-args].u.object,image_program))) +  bad_arg_error("map",args,1,"",sp+1-1-args, +  "Bad argument 1 to map.\n");       if (!src->img)    Pike_error("Called Image.Image object is not initialized\n");;       o=clone_object(image_program,0);    dest=(struct image*)(o->storage);    *dest=*src;       dest->img=malloc(sizeof(rgb_group)*src->xsize*src->ysize+RGB_VEC_PAD);    if (!dest->img)    {    free_object(o); -  resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); +  out_of_memory_error(NULL, -1, 0);    }       if (!image_colortable_map_image(THIS,src->img,dest->img,    src->xsize*src->ysize,src->xsize))    {    free_object(o);    Pike_error("colortable->map(): called colortable is not initiated\n");    }       pop_n_elems(args);    push_object(o);   }      void image_colortable_index_32bit(INT32 args)   {    struct image *src=NULL;    struct pike_string *ps;       if (args<1) -  SIMPLE_TOO_FEW_ARGS_ERROR("Colortable.index",1); +  SIMPLE_WRONG_NUM_ARGS_ERROR("index",1);    if (TYPEOF(sp[-args]) != T_OBJECT || -  ! (src=(struct image*)get_storage(sp[-args].u.object,image_program))) -  SIMPLE_BAD_ARG_ERROR("Colortable.index",1,"image object"); +  ! (src=get_storage(sp[-args].u.object,image_program))) +  SIMPLE_ARG_TYPE_ERROR("index",1,"Image.Image");       if (!src->img) -  SIMPLE_BAD_ARG_ERROR("Colortable.index",1,"non-empty image object"); +  SIMPLE_ARG_TYPE_ERROR("index",1,"non-empty image object");    -  +  /* FIXME: Use B4_t instead? */ + #ifdef PIKE_DEBUG    if (sizeof(unsigned INT32)!=4)    Pike_fatal("INT32 isn't 32 bits (sizeof is %ld)\n", -  (long)TO_UINT32(sizeof(unsigned INT32))); +  (long)sizeof(unsigned INT32)); + #endif       ps=begin_wide_shared_string(src->xsize*src->ysize,2);       if (!image_colortable_index_32bit_image(THIS,src->img,    (unsigned INT32 *)ps->str,    src->xsize*src->ysize,src->xsize))    {    do_free_unlinked_pike_string (ps); -  SIMPLE_BAD_ARG_ERROR("Colortable.index",1,"non-empty image object"); +  SIMPLE_ARG_TYPE_ERROR("index",1,"non-empty image object");    return;    }       pop_n_elems(args);       push_string(end_shared_string(ps));   }      /*   **! method object spacefactors(int r,int g,int b)
pike.git/src/modules/Image/colortable.c:3794:   **! This has no sanity check. Some functions may bug   **! if the factors are to high - color reduction functions   **! sums grey levels in the image, this could exceed maxint   **! in the case of high factors. Negative values may   **! also cause strange effects. *grin*   **/      void image_colortable_spacefactors(INT32 args)   {    if (args<3) -  SIMPLE_TOO_FEW_ARGS_ERROR("colortable->spacefactors",1); +  SIMPLE_WRONG_NUM_ARGS_ERROR("spacefactors",1);       if (TYPEOF(sp[0-args]) != T_INT ||    TYPEOF(sp[1-args]) != T_INT ||    TYPEOF(sp[2-args]) != T_INT) -  bad_arg_error("colortable->spacefactors",sp-args,args,0,"",sp-args, -  "Bad arguments to colortable->spacefactors()\n"); +  bad_arg_error("spacefactors",args,0,"",sp-args, +  "Bad arguments to spacefactors.\n");       THIS->spacefactor.r=sp[0-args].u.integer;    THIS->spacefactor.g=sp[1-args].u.integer;    THIS->spacefactor.b=sp[2-args].u.integer;       pop_n_elems(args);    ref_push_object(THISOBJ);   }      /*
pike.git/src/modules/Image/colortable.c:3854:   **/      void image_colortable_floyd_steinberg(INT32 args)   {    double forward=7.0,downforward=1.0,down=5.0,downback=3.0,sum;    double factor=0.95;    THIS->dither_type=NCTD_NONE;       if (args>=1)    if (TYPEOF(sp[-args]) != T_INT) -  bad_arg_error("colortable->spacefactors",sp-args,args,0,"",sp-args, -  "Bad arguments to colortable->spacefactors()\n"); +  bad_arg_error("floyd_steinberg",args,0,"",sp-args, +  "Bad arguments to floyd_steinberg.\n");    else    THIS->du.floyd_steinberg.dir=sp[-args].u.integer;    else    THIS->du.floyd_steinberg.dir=0;    if (args>=6) {    if (TYPEOF(sp[5-args]) == T_FLOAT)    factor = sp[5-args].u.float_number;    else if (TYPEOF(sp[5-args]) == T_INT)    factor = (double)sp[5-args].u.integer;    else -  bad_arg_error("colortable->spacefactors",sp-args,args,0,"",sp-args, -  "Bad arguments to colortable->spacefactors()\n"); +  bad_arg_error("floyd_steinberg",args,0,"",sp-args, +  "Bad arguments to floyd_steinberg.\n");    }    if (args>=5)    {    if (TYPEOF(sp[1-args]) == T_FLOAT)    forward = sp[1-args].u.float_number;    else if (TYPEOF(sp[1-args]) == T_INT)    forward = (double)sp[1-args].u.integer;    else -  bad_arg_error("colortable->spacefactors",sp-args,args,0,"",sp-args, -  "Bad arguments to colortable->spacefactors()\n"); +  bad_arg_error("floyd_steinberg",args,0,"",sp-args, +  "Bad arguments to floyd_steinberg.\n");    if (TYPEOF(sp[2-args]) == T_FLOAT)    downforward = sp[2-args].u.float_number;    else if (TYPEOF(sp[2-args]) == T_INT)    downforward = (double)sp[2-args].u.integer;    else -  bad_arg_error("colortable->spacefactors",sp-args,args,0,"",sp-args, -  "Bad arguments to colortable->spacefactors()\n"); +  bad_arg_error("floyd_steinberg",args,0,"",sp-args, +  "Bad arguments to floyd_steinberg.\n");    if (TYPEOF(sp[3-args]) == T_FLOAT)    down = sp[3-args].u.float_number;    else if (TYPEOF(sp[3-args]) == T_INT)    down = (double)sp[3-args].u.integer;    else -  bad_arg_error("colortable->spacefactors",sp-args,args,0,"",sp-args, -  "Bad arguments to colortable->spacefactors()\n"); +  bad_arg_error("floyd_steinberg",args,0,"",sp-args, +  "Bad arguments to floyd_steinberg.\n");    if (TYPEOF(sp[4-args]) == T_FLOAT)    downback = sp[4-args].u.float_number;    else if (TYPEOF(sp[4-args]) == T_INT)    downback = (double)sp[4-args].u.integer;    else -  bad_arg_error("colortable->spacefactors",sp-args,args,0,"",sp-args, -  "Bad arguments to colortable->spacefactors()\n"); +  bad_arg_error("floyd_steinberg",args,0,"",sp-args, +  "Bad arguments to floyd_steinberg.\n");    }       sum=forward+downforward+down+downback;    if (fabs(sum)<1e-10) sum=1.0;    sum/=factor;    -  THIS->du.floyd_steinberg.forward = DO_NOT_WARN((float)(forward/sum)); -  THIS->du.floyd_steinberg.downforward = DO_NOT_WARN((float)(downforward/sum)); -  THIS->du.floyd_steinberg.down = DO_NOT_WARN((float)(down/sum)); -  THIS->du.floyd_steinberg.downback = DO_NOT_WARN((float)(downback/sum)); +  THIS->du.floyd_steinberg.forward = (float)(forward/sum); +  THIS->du.floyd_steinberg.downforward = (float)(downforward/sum); +  THIS->du.floyd_steinberg.down = (float)(down/sum); +  THIS->du.floyd_steinberg.downback = (float)(downback/sum);       THIS->dither_type = NCTD_FLOYD_STEINBERG;       pop_n_elems(args);    ref_push_object(THISOBJ);   }      /* called by GIF encoder */   void image_colortable_internal_floyd_steinberg(struct neo_colortable *nct)   { -  nct->du.floyd_steinberg.forward = DO_NOT_WARN((float)(0.95*(7.0/16))); -  nct->du.floyd_steinberg.downforward = DO_NOT_WARN((float)(0.95*(1.0/16))); -  nct->du.floyd_steinberg.down = DO_NOT_WARN((float)(0.95*(5.0/16))); -  nct->du.floyd_steinberg.downback = DO_NOT_WARN((float)(0.95*(3.0/16))); +  nct->du.floyd_steinberg.forward = (float)(0.95*(7.0/16)); +  nct->du.floyd_steinberg.downforward = (float)(0.95*(1.0/16)); +  nct->du.floyd_steinberg.down = (float)(0.95*(5.0/16)); +  nct->du.floyd_steinberg.downback = (float)(0.95*(3.0/16));       nct->dither_type=NCTD_FLOYD_STEINBERG;   }      /*   **! method object nodither()   **! Set no dithering (default).   **!   **! returns the object being called   **/
pike.git/src/modules/Image/colortable.c:4011:   **/      void image_colortable_randomcube(INT32 args)   {    THIS->dither_type=NCTD_NONE;       if (args>=3)    if (TYPEOF(sp[-args]) != T_INT||    TYPEOF(sp[1-args]) != T_INT||    TYPEOF(sp[2-args]) != T_INT) -  bad_arg_error("Image.Colortable->randomcube",sp-args,args,0,"",sp-args, -  "Bad arguments to Image.Colortable->randomcube()\n"); +  bad_arg_error("randomcube",args,0,"",sp-args, +  "Bad arguments to randomcube.\n");    else    {    THIS->du.randomcube.r=sp[-args].u.integer;    THIS->du.randomcube.g=sp[1-args].u.integer;    THIS->du.randomcube.b=sp[2-args].u.integer;    }    else if (THIS->type==NCT_CUBE && THIS->u.cube.r &&    THIS->u.cube.g && THIS->u.cube.b)    {    THIS->du.randomcube.r=256/THIS->u.cube.r;
pike.git/src/modules/Image/colortable.c:4045:    pop_n_elems(args);    ref_push_object(THISOBJ);   }      void image_colortable_randomgrey(INT32 args)   {    THIS->dither_type=NCTD_NONE;       if (args)    if (TYPEOF(sp[-args]) != T_INT) -  bad_arg_error("Image.Colortable->randomgrey",sp-args,args,0,"",sp-args, -  "Bad arguments to Image.Colortable->randomgrey()\n"); +  bad_arg_error("randomgrey",args,0,"",sp-args, +  "Bad arguments to randomgrey.\n");    else    THIS->du.randomcube.r=sp[-args].u.integer;    else if (THIS->type==NCT_CUBE && THIS->u.cube.r)    THIS->du.randomcube.r=256/THIS->u.cube.r;    else    THIS->du.randomcube.r=32;       THIS->dither_type=NCTD_RANDOMGREY;       pop_n_elems(args);
pike.git/src/modules/Image/colortable.c:4076:    static const int errors2x2[4]={0,2,3,1};    static const int errors3x1[3]={1,0,2};    static const int errors3x2[6]={4,0,2,1,5,3};    static const int errors3x3[9]={6,8,4,1,0,3,5,2,7};       const int *errs;    int szx,szy,sz,*d,*s;    int xf,yf;    int x,y;    -  src=malloc(sizeof(int)*dxs*dys); -  dest=malloc(sizeof(int)*dxs*dys); +  src=calloc(sizeof(int),dxs*dys); +  dest=calloc(sizeof(int),dxs*dys);       if (!src||!dest)    {    if (src) free(src);    if (dest) free(dest);    return NULL;    }       *src=0;    sxs=sys=1; -  MEMSET(src,0,sizeof(int)*dxs*dys); -  MEMSET(dest,0,sizeof(int)*dxs*dys); +        for (;;)    {    if (dxs==sxs) xf=1;    else if (((dxs/sxs)%2)==0) xf=2;    else if (((dxs/sxs)%3)==0) xf=3;    else break;    if (dys==sys) yf=1;    else if (((dys/sys)%2)==0) yf=2;    else if (((dys/sys)%3)==0) yf=3;
pike.git/src/modules/Image/colortable.c:4174:      static int *ordered_make_diff(int *errors,int sz,int err)   {    /* ok, i want errors between -err and +err from 0 and sz-1 */       int *dest;    int *d;    int n=sz;    double q;    -  d=dest=(int*)malloc(sizeof(int)*sz); +  d=dest=calloc(sizeof(int), sz);    if (!d) return d;    -  if (sz!=1) q = DO_NOT_WARN(1.0/(sz-1)); else q=1.0; +  if (sz!=1) q = 1.0/(sz-1); else q=1.0;       while (n--) -  *(d++) = DOUBLE_TO_INT((*(errors++)*q-0.5)*2*err); +  *(d++) = (int)((*(errors++)*q-0.5)*2*err);       return dest;   }      /*   **! method object ordered()   **! method object ordered(int r,int g,int b)   **! method object ordered(int r,int g,int b,int xsize,int ysize)   **! method object ordered(int r,int g,int b,int xsize,int ysize,int x,int y)   **! method object ordered(int r,int g,int b,int xsize,int ysize,int rx,int ry,int gx,int gy,int bx,int by)
pike.git/src/modules/Image/colortable.c:4278:    int xsize,ysize;       colortable_free_dither_union(THIS);    THIS->dither_type=NCTD_NONE;       if (args>=3)    if (TYPEOF(sp[-args]) != T_INT ||    TYPEOF(sp[1-args]) != T_INT ||    TYPEOF(sp[2-args]) != T_INT)    { -  bad_arg_error("Image.Colortable->ordered",sp-args,args,0,"",sp-args, -  "Bad arguments to Image.Colortable->ordered()\n"); -  /* Not reached, but keep the compiler happy */ -  r = 0; -  g = 0; -  b = 0; +  bad_arg_error("ordered",args,0,"",sp-args, +  "Bad arguments to ordered.\n"); +  UNREACHABLE(r=g=b=0);    }    else    {    r=sp[-args].u.integer;    g=sp[1-args].u.integer;    b=sp[2-args].u.integer;    }    else if (THIS->type==NCT_CUBE && THIS->u.cube.r &&    THIS->u.cube.g && THIS->u.cube.b)    {
pike.git/src/modules/Image/colortable.c:4318:    THIS->du.ordered.ry=    THIS->du.ordered.gx=    THIS->du.ordered.gy=    THIS->du.ordered.bx=    THIS->du.ordered.by=0;       if (args>=5)    {    if (TYPEOF(sp[3-args]) != T_INT ||    TYPEOF(sp[4-args]) != T_INT) -  bad_arg_error("Image.Colortable->ordered",sp-args,args,0,"",sp-args, -  "Bad arguments to Image.Colortable->ordered()\n"); +  bad_arg_error("ordered",args,0,"",sp-args, +  "Bad arguments to ordered.\n");    else    {    xsize=MAXIMUM(sp[3-args].u.integer,1);    ysize=MAXIMUM(sp[4-args].u.integer,1);    }    }       if (args>=11)    {    if (TYPEOF(sp[5-args]) != T_INT ||    TYPEOF(sp[6-args]) != T_INT ||    TYPEOF(sp[7-args]) != T_INT ||    TYPEOF(sp[8-args]) != T_INT ||    TYPEOF(sp[9-args]) != T_INT ||    TYPEOF(sp[10-args]) != T_INT) -  bad_arg_error("Image.Colortable->ordered",sp-args,args,0,"",sp-args, -  "Bad arguments to Image.Colortable->ordered()\n"); +  bad_arg_error("ordered",args,0,"",sp-args, +  "Bad arguments to ordered.\n");    else    {    THIS->du.ordered.rx=sp[5-args].u.integer;    THIS->du.ordered.ry=sp[6-args].u.integer;    THIS->du.ordered.gx=sp[7-args].u.integer;    THIS->du.ordered.gy=sp[8-args].u.integer;    THIS->du.ordered.bx=sp[9-args].u.integer;    THIS->du.ordered.by=sp[10-args].u.integer;    }    }    else if (args>=7)    {    if (TYPEOF(sp[5-args]) != T_INT||    TYPEOF(sp[6-args]) != T_INT) -  bad_arg_error("Image.Colortable->ordered",sp-args,args,0,"",sp-args, -  "Bad arguments to Image.Colortable->ordered()\n"); +  bad_arg_error("ordered",args,0,"",sp-args, +  "Bad arguments to ordered.\n");    else    {    THIS->du.ordered.rx=    THIS->du.ordered.gx=    THIS->du.ordered.bx=sp[5-args].u.integer;    THIS->du.ordered.ry=    THIS->du.ordered.gy=    THIS->du.ordered.by=sp[6-args].u.integer;    }    }       errors=ordered_calculate_errors(xsize,ysize);    if (!errors)    { -  resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); +  out_of_memory_error(NULL, -1, 0);    return;    }       THIS->du.ordered.rdiff=ordered_make_diff(errors,xsize*ysize,r);    THIS->du.ordered.gdiff=ordered_make_diff(errors,xsize*ysize,g);    THIS->du.ordered.bdiff=ordered_make_diff(errors,xsize*ysize,b);       if (r==g && g==b &&    THIS->du.ordered.rx==THIS->du.ordered.gx && THIS->du.ordered.gx==THIS->du.ordered.bx)    {
pike.git/src/modules/Image/colortable.c:4392:       free(errors);       if (!THIS->du.ordered.rdiff||    !THIS->du.ordered.gdiff||    !THIS->du.ordered.bdiff)    {    if (THIS->du.ordered.rdiff) free(THIS->du.ordered.rdiff);    if (THIS->du.ordered.gdiff) free(THIS->du.ordered.gdiff);    if (THIS->du.ordered.bdiff) free(THIS->du.ordered.bdiff); -  resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); +  out_of_memory_error(NULL, -1, 0);    return;    }       THIS->du.ordered.xs=xsize;    THIS->du.ordered.ys=ysize;       THIS->dither_type=NCTD_ORDERED;       pop_n_elems(args);    ref_push_object(THISOBJ);
pike.git/src/modules/Image/colortable.c:4431:       pop_n_elems(args);    push_int64(image_colortable_size(THIS));    push_int(1);    o=clone_object(image_program,2);    push_object(o);       if (THIS->type==NCT_NONE)    return;    -  img=(struct image*)get_storage(o,image_program); +  img=get_storage(o,image_program);    dest=img->img;       if (THIS->type==NCT_CUBE)    flat=_img_nct_cube_to_flat(THIS->u.cube);    else    flat=THIS->u.flat;       /* sort in number order? */       for (i=0; i<flat.numentries; i++)
pike.git/src/modules/Image/colortable.c:4552:    free(flat.entries);       pop_n_elems(args);    push_int(bw);   }      static void image_colortable__sprintf( INT32 args )   {    int x;    if (args != 2 ) -  SIMPLE_TOO_FEW_ARGS_ERROR("_sprintf",2); +  SIMPLE_WRONG_NUM_ARGS_ERROR("_sprintf",2);    if (TYPEOF(sp[-args]) != T_INT) -  SIMPLE_BAD_ARG_ERROR("_sprintf",0,"integer"); +  SIMPLE_ARG_TYPE_ERROR("_sprintf",0,"int");    if (TYPEOF(sp[1-args]) != T_MAPPING) -  SIMPLE_BAD_ARG_ERROR("_sprintf",1,"mapping"); +  SIMPLE_ARG_TYPE_ERROR("_sprintf",1,"mapping");       x = sp[-2].u.integer;       pop_n_elems( 2 );    switch( x )    {    case 't': -  push_constant_text("Image.Colortable"); +  push_static_text("Image.Colortable");    return;    case 'O': -  push_constant_text( "Image.Colortable( %d, m=%s, d=%s )" ); +  push_static_text( "Image.Colortable( %d, m=%s, d=%s )" );    push_int64( image_colortable_size( THIS ) );    switch( THIS->type )    { -  case NCT_NONE: push_constant_text( "none" ); break; -  case NCT_FLAT: push_constant_text( "flat" ); break; -  case NCT_CUBE: push_constant_text( "cube" ); break; +  case NCT_NONE: push_static_text( "none" ); break; +  case NCT_FLAT: push_static_text( "flat" ); break; +  case NCT_CUBE: push_static_text( "cube" ); break;    }    switch( THIS->dither_type )    { -  case NCTD_NONE: push_constant_text( "none" ); break; -  case NCTD_FLOYD_STEINBERG:push_constant_text( "floyd-steinberg" );break; -  case NCTD_RANDOMCUBE: push_constant_text( "randomcube" ); break; -  case NCTD_RANDOMGREY: push_constant_text( "randomgrey" ); break; -  case NCTD_ORDERED: push_constant_text( "ordered" ); break; +  case NCTD_NONE: push_static_text( "none" ); break; +  case NCTD_FLOYD_STEINBERG:push_static_text( "floyd-steinberg" );break; +  case NCTD_RANDOMCUBE: push_static_text( "randomcube" ); break; +  case NCTD_RANDOMGREY: push_static_text( "randomgrey" ); break; +  case NCTD_ORDERED: push_static_text( "ordered" ); break;    }    f_sprintf( 4 );    return;    default:    push_int(0);    return;    }   }      /***************** global init etc *****************************/      void init_image_colortable(void)   { -  s_array=make_shared_string("array"); -  s_string=make_shared_string("string"); -  s_mapping=make_shared_string("mapping"); -  +     ADD_STORAGE(struct neo_colortable);       set_init_callback(init_colortable_struct);    set_exit_callback(exit_colortable_struct);       /* function(void:void)|"    "function(array(array(int)|string|object):void)|"    "function(object,void|int,mixed ...:void)|"    "function(int,int,int,void|int ...:void) */    ADD_FUNCTION("create",image_colortable_create,tOr4(tFunc(tVoid,tVoid),tFunc(tOr(tArr(tColor),tStr),tVoid),tFuncV(tObj tOr(tVoid,tInt),tMix,tVoid),tFuncV(tInt tInt tInt,tOr(tVoid,tInt),tVoid)),0);
pike.git/src/modules/Image/colortable.c:4630:       /* function(int:object) */    ADD_FUNCTION("reduce",image_colortable_reduce,tFunc(tInt,tObj),0);    ADD_FUNCTION("reduce_fs",image_colortable_reduce_fs,tFunc(tInt,tObj),0);       /* operators */    ADD_FUNCTION("`+",image_colortable_operator_plus,tFunc(tObj,tObj),0);    ADD_FUNCTION("``+",image_colortable_operator_plus,tFunc(tObj,tObj),0);       /* cast to array */ -  ADD_FUNCTION("cast",image_colortable_cast,tFunc(tStr,tArray),0); +  ADD_FUNCTION("cast",image_colortable_cast,tFunc(tStr,tOr(tArray,tMapping)),ID_PROTECTED);       /* info */    ADD_FUNCTION("_sizeof",image_colortable__sizeof,tFunc(tNone,tInt),0);    ADD_FUNCTION("greyp",image_colortable_greyp,tFunc(tNone,tInt01),0);       /* lookup modes */    ADD_FUNCTION("cubicles",image_colortable_cubicles,tOr(tFunc(tNone,tObj),tFunc(tInt tInt tInt tOr(tVoid,tInt),tObj)),0);    ADD_FUNCTION("rigid",image_colortable_rigid,tOr(tFunc(tNone,tObj),tFunc(tInt tInt tInt,tObj)),0);    ADD_FUNCTION("full",image_colortable_full,tFunc(tNone,tObj),0);   
pike.git/src/modules/Image/colortable.c:4682:    /* tuning image */    /* function(int,int,int:object) */    ADD_FUNCTION("spacefactors",image_colortable_spacefactors,tFunc(tInt tInt tInt,tObj),0);       ADD_FUNCTION("corners",image_colortable_corners,tFunc(tNone,tArray),0);      }      void exit_image_colortable(void)   { -  free_string(s_array); -  free_string(s_mapping); -  free_string(s_string); +    }