From ca10c8e4e582fea74ab5664d48ca0f2881fe64ca Mon Sep 17 00:00:00 2001 From: "Mirar (Pontus Hagland)" <pike@sort.mirar.org> Date: Tue, 18 Mar 1997 17:19:31 +0100 Subject: [PATCH] to8bit-foo Rev: src/modules/Image/image.c:1.7 --- src/modules/Image/image.c | 103 +++++++++++++++++++++++++++++++++++--- 1 file changed, 96 insertions(+), 7 deletions(-) diff --git a/src/modules/Image/image.c b/src/modules/Image/image.c index 600f159873..043ccfefbd 100644 --- a/src/modules/Image/image.c +++ b/src/modules/Image/image.c @@ -1,4 +1,4 @@ -/* $Id: image.c,v 1.6 1997/03/18 16:00:40 mirar Exp $ */ +/* $Id: image.c,v 1.7 1997/03/18 16:19:31 mirar Exp $ */ #include "global.h" @@ -7,7 +7,7 @@ #include "stralloc.h" #include "global.h" -RCSID("$Id: image.c,v 1.6 1997/03/18 16:00:40 mirar Exp $"); +RCSID("$Id: image.c,v 1.7 1997/03/18 16:19:31 mirar Exp $"); #include "types.h" #include "pike_macros.h" #include "object.h" @@ -1628,6 +1628,41 @@ void image_cast(INT32 args) *sizeof(rgb_group))); } +void image_to8bit_closest(INT32 args) +{ + struct colortable *ct; + struct pike_string *res = begin_shared_string((THIS->xsize*THIS->ysize)); + unsigned long i; + rgb_group *s; + unsigned char *d; + + if(!res) error("Out of memory\n"); + + if (args!=1) + error("Illegal number of arguments to image->to8bit(COLORTABLE);\n"); + if(sp[-args].type != T_ARRAY) + error("Wrong type to image->to8bit(COLORTABLE);\n"); + + ct=colortable_from_array(sp[-args].u.array,"image->to8bit()\n"); + + i=THIS->xsize*THIS->ysize; + s=THIS->img; + d=res->str; + + THREADS_ALLOW(); + while (i--) + { + *d=ct->index[colortable_rgb_nearest(ct,*s)]; + d++; *s++; + } + THREADS_DISALLOW(); + + colortable_free(ct); + + pop_n_elems(args); + push_string(end_shared_string(res)); +} + void image_to8bit(INT32 args) { struct colortable *ct; @@ -1652,17 +1687,67 @@ void image_to8bit(INT32 args) THREADS_ALLOW(); while (i--) { - int x; - *d=ct->index[x=colortable_rgb_nearest(ct,*s)]; + *d=ct->index[colortable_rgb(ct,*s)]; d++; *s++; } THREADS_DISALLOW(); + colortable_free(ct); + pop_n_elems(args); push_string(end_shared_string(res)); } +static void image_to8bit_fs(INT32 args) +{ + struct colortable *ct; + INT32 i,j,xs; + rgb_group *s; + struct object *o; + int *res,w; + unsigned char *d; + rgbl_group *errb; + struct pike_string *sres = begin_shared_string((THIS->xsize*THIS->ysize)); + + if (!THIS->img) error("no image\n"); + if (args<1 + || sp[-args].type!=T_ARRAY) + error("illegal argument to image->map_fs()\n"); + + + res=(int*)xalloc(sizeof(int)*THIS->xsize); + errb=(rgbl_group*)xalloc(sizeof(rgbl_group)*THIS->xsize); + + ct=colortable_from_array(sp[-args].u.array,"image->map_closest()\n"); + pop_n_elems(args); + + for (i=0; i<THIS->xsize; i++) + errb[i].r=(rand()%(FS_SCALE*2+1))-FS_SCALE, + errb[i].g=(rand()%(FS_SCALE*2+1))-FS_SCALE, + errb[i].b=(rand()%(FS_SCALE*2+1))-FS_SCALE; + + i=THIS->ysize; + s=THIS->img; + d=sres->str; + w=0; + xs=THIS->xsize; + THREADS_ALLOW(); + while (i--) + { + image_floyd_steinberg(s,xs,errb,w=!w,res,ct); + for (j=0; j<THIS->xsize; j++) + *(d++)=ct->index[res[j]]; + s+=xs; + } + THREADS_DISALLOW(); + + free(errb); + free(res); + colortable_free(ct); + push_string(end_shared_string(sres)); +} + /***************** global init etc *****************************/ @@ -1718,6 +1803,10 @@ void pike_module_init() add_function("cast",image_cast, "function(string:string)",0); add_function("to8bit",image_to8bit, "function(array(array(int)):string)",0); + add_function("to8bit_closest",image_to8bit_closest, + "function(array(array(int)):string)",0); + add_function("to8bit_fs",image_to8bit_fs, + "function(array(array(int)):string)",0); add_function("copy",image_copy, @@ -1803,11 +1892,11 @@ void pike_module_init() "function(:int)",0); add_function("map_closest",image_map_closest, - "function(:object)",0); + "function(array(array(int)):object)",0); add_function("map_fast",image_map_fast, - "function(:object)",0); + "function(array(array(int)):object)",0); add_function("map_fs",image_map_fs, - "function(:object)",0); + "function(array(array(int)):object)",0); add_function("select_colors",image_select_colors, "function(int:array(array(int)))",0); -- GitLab