From 2d87fbd0d9ec5b747dca47c206727b1a27c5c986 Mon Sep 17 00:00:00 2001
From: "Mirar (Pontus Hagland)" <pike@sort.mirar.org>
Date: Mon, 12 Apr 1999 16:55:57 +0200
Subject: [PATCH] cast to array of Image.image, colors can be used as mapping
 index

Rev: src/modules/Image/colors.c:1.11
Rev: src/modules/Image/colortable.c:1.63
Rev: src/modules/Image/image.c:1.125
Rev: src/modules/Image/x.c:1.20
---
 src/modules/Image/colors.c     | 121 ++++++++++++++-------------------
 src/modules/Image/colortable.c |  51 +++++++-------
 src/modules/Image/image.c      |  56 ++++++++++++++-
 src/modules/Image/x.c          |  21 +-----
 4 files changed, 133 insertions(+), 116 deletions(-)

diff --git a/src/modules/Image/colors.c b/src/modules/Image/colors.c
index 09bc9210a1..2d129f662a 100644
--- a/src/modules/Image/colors.c
+++ b/src/modules/Image/colors.c
@@ -1,7 +1,7 @@
 /*
 **! module Image
 **! note
-**!	$Id: colors.c,v 1.10 1999/04/10 02:02:01 mirar Exp $
+**!	$Id: colors.c,v 1.11 1999/04/12 14:55:48 mirar Exp $
 **! submodule color
 **!
 **!	This module keeps names and easy handling 
@@ -97,7 +97,7 @@
 #include "global.h"
 #include <config.h>
 
-RCSID("$Id: colors.c,v 1.10 1999/04/10 02:02:01 mirar Exp $");
+RCSID("$Id: colors.c,v 1.11 1999/04/12 14:55:48 mirar Exp $");
 
 #include "config.h"
 
@@ -258,13 +258,10 @@ void _img_nct_map_to_flat_cubicles(rgb_group *s,
 static void try_find_name(struct color_struct *this)
 {
    rgb_group d;
-   static struct nct_dither dith = { NCTD_NONE,NULL,NULL,NULL,NULL };
+   static struct nct_dither dith = { NCTD_NONE,NULL,NULL,NULL,NULL,-1 };
 
    if (!colors)
-   {
-      fprintf(stderr,"make colors\n");
       make_colors();
-   }
 
    if (this->name) 
       fatal("try_find_name called twice\n");
@@ -752,6 +749,13 @@ static void image_color_equal(INT32 args)
    push_int(0);
 }
 
+static void image_color___hash(INT32 args)
+{
+   pop_n_elems(args);
+   push_int(((THIS->rgb.r<<16)+(THIS->rgb.g<<8)+THIS->rgb.b)
+	    +(THIS->rgbl.r+THIS->rgbl.g+THIS->rgbl.b));
+}
+
 
 /*
 **! method object light()
@@ -1422,76 +1426,55 @@ void init_image_colors(void)
 
    /* color info methods */
 
-   /* function(string:array|string) */
-  ADD_FUNCTION("cast",image_color_cast,tFunc(tStr,tOr(tArray,tStr)),/* opt */0);
-   /* function(string|int:int|function) */
-  ADD_FUNCTION("`[]",image_color_index,tFunc(tOr(tStr,tInt),tOr(tInt,tFunction)),/* opt */0);
-   /* function(string|int:int|function) */
-  ADD_FUNCTION("`->",image_color_index,tFunc(tOr(tStr,tInt),tOr(tInt,tFunction)),/* opt */0);
-   /* function(object|int:int) */
-  ADD_FUNCTION("`==",image_color_equal,tFunc(tOr(tObj,tInt),tInt),/* opt */0);
-
-   /* function(:string) */
-  ADD_FUNCTION("name",image_color_name,tFunc(,tStr),/* opt */0);
-   /* function(:string) */
-  ADD_FUNCTION("hex",image_color_hex,tFunc(,tStr),/* opt */0);
-   /* function(:string) */
-  ADD_FUNCTION("html",image_color_html,tFunc(,tStr),/* opt */0);
-
-  ADD_FUNCTION("rgb",image_color_rgb,tFunc(,tArr(tInt)),/* opt */0);
-  ADD_FUNCTION("rgbf",image_color_rgbf,tFunc(,tArr(tFlt)),/* opt */0);
-  ADD_FUNCTION("hsv",image_color_hsv,tFunc(,tArr(tInt)),/* opt */0);
-  ADD_FUNCTION("hsvf",image_color_hsvf,tFunc(,tArr(tFlt)),/* opt */0);
-  ADD_FUNCTION("cmyk",image_color_cmyk,tFunc(,tArr(tFlt)),/* opt */0);
-  ADD_FUNCTION("greylevel",image_color_greylevel,tOr(tFunc(,tInt),tFunc(tInt tInt tInt,tInt)),/* opt */0);
+   ADD_FUNCTION("cast",image_color_cast,tFunc(tStr,tOr(tArray,tStr)),0);
+   ADD_FUNCTION("`[]",image_color_index,tFunc(tOr(tStr,tInt),tOr(tInt,tFunction)),0);
+   ADD_FUNCTION("`->",image_color_index,tFunc(tOr(tStr,tInt),tOr(tInt,tFunction)),0);
+   ADD_FUNCTION("`==",image_color_equal,tFunc(tOr(tObj,tInt),tInt),0);
+   ADD_FUNCTION("__hash",image_color___hash,tFunc(,tInt),0);
+
+   ADD_FUNCTION("name",image_color_name,tFunc(,tStr),0);
+   ADD_FUNCTION("hex",image_color_hex,tFunc(,tStr),0);
+   ADD_FUNCTION("html",image_color_html,tFunc(,tStr),0);
+
+   ADD_FUNCTION("rgb",image_color_rgb,tFunc(,tArr(tInt)),0);
+   ADD_FUNCTION("rgbf",image_color_rgbf,tFunc(,tArr(tFlt)),0);
+   ADD_FUNCTION("hsv",image_color_hsv,tFunc(,tArr(tInt)),0);
+   ADD_FUNCTION("hsvf",image_color_hsvf,tFunc(,tArr(tFlt)),0);
+   ADD_FUNCTION("cmyk",image_color_cmyk,tFunc(,tArr(tFlt)),0);
+   ADD_FUNCTION("greylevel",image_color_greylevel,tOr(tFunc(,tInt),tFunc(tInt tInt tInt,tInt)),0);
 
    /* color conversion methods */
 
-   /* function(:object)|function(int,int,int:object) */
-  ADD_FUNCTION("grey",image_color_grey,tOr(tFunc(,tObj),tFunc(tInt tInt tInt,tObj)),
-		/* opt */0);
-
-   /* function(:object) */
-  ADD_FUNCTION("light",image_color_light,tFunc(,tObj),/* opt */0);
-   /* function(:object) */
-  ADD_FUNCTION("dark",image_color_dark,tFunc(,tObj),/* opt */0);
-   /* function(:object) */
-  ADD_FUNCTION("neon",image_color_neon,tFunc(,tObj),/* opt */0);
-   /* function(:object) */
-  ADD_FUNCTION("bright",image_color_bright,tFunc(,tObj),/* opt */0);
-   /* function(:object) */
-  ADD_FUNCTION("dull",image_color_dull,tFunc(,tObj),/* opt */0);
-
-   /* function(float:object) */
-  ADD_FUNCTION("`*",image_color_mult,tFunc(tFlt,tObj),/* opt */0);
-   /* function(object:object) */
-  ADD_FUNCTION("`+",image_color_add,tFunc(tObj,tObj),/* opt */0);
+   ADD_FUNCTION("grey",image_color_grey,
+		tOr(tFunc(,tObj),tFunc(tInt tInt tInt,tObj)),0);
+
+   ADD_FUNCTION("light",image_color_light,tFunc(,tObj),0);
+   ADD_FUNCTION("dark",image_color_dark,tFunc(,tObj),0);
+   ADD_FUNCTION("neon",image_color_neon,tFunc(,tObj),0);
+   ADD_FUNCTION("bright",image_color_bright,tFunc(,tObj),0);
+   ADD_FUNCTION("dull",image_color_dull,tFunc(,tObj),0);
+
+   ADD_FUNCTION("`*",image_color_mult,tFunc(tFlt,tObj),0);
+   ADD_FUNCTION("`+",image_color_add,tFunc(tObj,tObj),0);
 
    image_color_program=end_program();
    
    start_new_program();
-   /* function(string:object) */
-  ADD_FUNCTION("`[]",image_get_color,tFunc(tStr,tObj),/* opt */0);
-   /* function(string|int...:object) */
-  ADD_FUNCTION("`()",image_make_color,tFuncV(,tOr(tStr,tInt),tObj),/* opt */0);
-   /* function(int,int,int:object) */
-  ADD_FUNCTION("rgb",image_make_rgb_color,tFunc(tInt tInt tInt,tObj),/* opt */0);
-   /* function(int,int,int:object)|"
-		"function(float,float,float:object) */
-  ADD_FUNCTION("hsv",image_make_hsv_color,tOr(tFunc(tInt tInt tInt,tObj),tFunc(tFlt tFlt tFlt,tObj)),/* opt */0);
-   /* function(int|float,int|float,int|float,int|float:object) */
-  ADD_FUNCTION("cmyk",image_make_cmyk_color,tFunc(tOr(tInt,tFlt) tOr(tInt,tFlt) tOr(tInt,tFlt) tOr(tInt,tFlt),tObj),
-		/* opt */0);
-   /* function(string:object) */
-  ADD_FUNCTION("html",image_make_html_color,tFunc(tStr,tObj),/* opt */0);
-   /* function(string:object) */
-  ADD_FUNCTION("guess",image_guess_color,tFunc(tStr,tObj),/* opt */0);
-   /* function(int:object) */
-  ADD_FUNCTION("greylevel",image_make_greylevel_color,tFunc(tInt,tObj),/* opt */0);
-   /* function(:array(string)) */
-  ADD_FUNCTION("_indices",image_colors_indices,tFunc(,tArr(tStr)),/* opt */0);
-   /* function(:array(object)) */
-  ADD_FUNCTION("_values",image_colors_values,tFunc(,tArr(tObj)),/* opt */0);
+   ADD_FUNCTION("`[]",image_get_color,tFunc(tStr,tObj),0);
+   ADD_FUNCTION("`()",image_make_color,tFuncV(,tOr(tStr,tInt),tObj),0);
+   ADD_FUNCTION("rgb",image_make_rgb_color,tFunc(tInt tInt tInt,tObj),0);
+   ADD_FUNCTION("hsv",image_make_hsv_color,
+		tOr(tFunc(tInt tInt tInt,tObj),
+		    tFunc(tFlt tFlt tFlt,tObj)) ,0);
+   ADD_FUNCTION("cmyk",image_make_cmyk_color,tFunc(tOr(tInt,tFlt) 
+						   tOr(tInt,tFlt) 
+						   tOr(tInt,tFlt) 
+						   tOr(tInt,tFlt),tObj), 0);
+   ADD_FUNCTION("html",image_make_html_color,tFunc(tStr,tObj),0);
+   ADD_FUNCTION("guess",image_guess_color,tFunc(tStr,tObj),0);
+   ADD_FUNCTION("greylevel",image_make_greylevel_color,tFunc(tInt,tObj),0);
+   ADD_FUNCTION("_indices",image_colors_indices,tFunc(,tArr(tStr)),0);
+   ADD_FUNCTION("_values",image_colors_values,tFunc(,tArr(tObj)),0);
 
    add_program_constant("color",image_color_program,0);
 
diff --git a/src/modules/Image/colortable.c b/src/modules/Image/colortable.c
index 7faadaaab6..3c8bce26b7 100644
--- a/src/modules/Image/colortable.c
+++ b/src/modules/Image/colortable.c
@@ -1,12 +1,12 @@
 #include "global.h"
 #include <config.h>
 
-/* $Id: colortable.c,v 1.62 1999/04/12 13:43:40 mirar Exp $ */
+/* $Id: colortable.c,v 1.63 1999/04/12 14:55:50 mirar Exp $ */
 
 /*
 **! module Image
 **! note
-**!	$Id: colortable.c,v 1.62 1999/04/12 13:43:40 mirar Exp $
+**!	$Id: colortable.c,v 1.63 1999/04/12 14:55:50 mirar Exp $
 **! class colortable
 **!
 **!	This object keeps colortable information,
@@ -21,7 +21,7 @@
 #undef COLORTABLE_DEBUG
 #undef COLORTABLE_REDUCE_DEBUG
 
-RCSID("$Id: colortable.c,v 1.62 1999/04/12 13:43:40 mirar Exp $");
+RCSID("$Id: colortable.c,v 1.63 1999/04/12 14:55:50 mirar Exp $");
 
 #include <math.h> /* fabs() */
 
@@ -2633,30 +2633,31 @@ void image_colortable_cast_to_string(struct neo_colortable *nct)
 
 void image_colortable_cast(INT32 args)
 {
-   if (!args ||
-       sp[-args].type!=T_STRING) 
-      error("Illegal argument 1 to Image.colortable->cast\n");
-
-   if (sp[-args].u.string==s_array)
-   {
-      pop_n_elems(args);
-      image_colortable_cast_to_array(THIS);
-   }
-   else if (sp[-args].u.string==s_string)
-   {
-      pop_n_elems(args);
-      image_colortable_cast_to_string(THIS);
-   }
-   else if (sp[-args].u.string==s_mapping)
-   {
-      pop_n_elems(args);
-      image_colortable_cast_to_mapping(THIS);
-   }
-   else
+   if (!args)
+      SIMPLE_TOO_FEW_ARGS_ERROR("Image.colortable->cast",1);
+   if (sp[-args].type==T_STRING||sp[-args].u.string->size_shift)
    {
-      error("Image.colortable->cast: can't cast to %s\n",
-	    sp[-args].u.string->str);
+      if (strncmp(sp[-args].u.string->str,"array",5)==0)
+      {
+	 pop_n_elems(args);
+	 image_colortable_cast_to_array(THIS);
+	 return;
+      }
+      if (strncmp(sp[-args].u.string->str,"string",6)==0)
+      {
+	 pop_n_elems(args);
+	 image_colortable_cast_to_string(THIS);
+	 return;
+      }
+      if (strncmp(sp[-args].u.string->str,"mapping",7)==0)
+      {
+	 pop_n_elems(args);
+	 image_colortable_cast_to_mapping(THIS);
+	 return;
+      }
    }
+   SIMPLE_BAD_ARG_ERROR("Image.colortable->cast",1,
+			"string(\"mapping\"|\"array\"|\"string\")");
 }
 
 /*
diff --git a/src/modules/Image/image.c b/src/modules/Image/image.c
index b0f41fa72e..52f71b72ad 100644
--- a/src/modules/Image/image.c
+++ b/src/modules/Image/image.c
@@ -1,9 +1,9 @@
-/* $Id: image.c,v 1.124 1999/04/12 10:30:14 per Exp $ */
+/* $Id: image.c,v 1.125 1999/04/12 14:55:52 mirar Exp $ */
 
 /*
 **! module Image
 **! note
-**!	$Id: image.c,v 1.124 1999/04/12 10:30:14 per Exp $
+**!	$Id: image.c,v 1.125 1999/04/12 14:55:52 mirar Exp $
 **! class image
 **!
 **!	The main object of the <ref>Image</ref> module, this object
@@ -97,7 +97,7 @@
 
 #include "stralloc.h"
 #include "global.h"
-RCSID("$Id: image.c,v 1.124 1999/04/12 10:30:14 per Exp $");
+RCSID("$Id: image.c,v 1.125 1999/04/12 14:55:52 mirar Exp $");
 #include "pike_macros.h"
 #include "object.h"
 #include "constants.h"
@@ -3452,6 +3452,56 @@ void image_read_lsb_grey(INT32 args)
    push_string(end_shared_string(ps));
 }
 
+/*
+**! method string cast(string type)
+**!	Cast the image to another datatype. Currently supported
+**!	are string ("rgbrgbrgb...") and array (double array 
+**!	of <ref>Image.color</ref> objects).
+**! see also: Image.color,Image.X
+*/
+
+void image_cast(INT32 args)
+{
+   if (!args)
+      SIMPLE_TOO_FEW_ARGS_ERROR("Image.image->cast",1);
+   if (sp[-args].type==T_STRING||sp[-args].u.string->size_shift)
+   {
+      if (strncmp(sp[-args].u.string->str,"array",5)==0)
+      {
+	 int i,j;
+	 rgb_group *s=THIS->img;
+
+	 if (!THIS->img) error("no image\n");
+	 
+	 pop_n_elems(args);
+
+	 for (i=0; i<THIS->ysize; i++)
+	 {
+	    for (j=0; j<THIS->xsize; j++)
+	    {
+	       _image_make_rgb_color(s->r,s->g,s->b);
+	       s++;
+	    }
+	    f_aggregate(THIS->xsize);
+	 }
+	 f_aggregate(THIS->ysize);
+
+	 return;
+      }
+      if (strncmp(sp[-args].u.string->str,"string",6)==0)
+      {
+	 if (!THIS->img) error("no image\n");
+
+	 pop_n_elems(args);
+	 push_string(make_shared_binary_string((char *)THIS->img,
+					       THIS->xsize*THIS->ysize
+					       *sizeof(rgb_group)));
+	 return;
+      }
+      
+   }
+   SIMPLE_BAD_ARG_ERROR("Image.image->cast",1,"string(\"array\"|\"string\")");
+}
 
 /***************** global init etc *****************************/
 
diff --git a/src/modules/Image/x.c b/src/modules/Image/x.c
index 910ee5b1a1..1188ae5475 100644
--- a/src/modules/Image/x.c
+++ b/src/modules/Image/x.c
@@ -1,4 +1,4 @@
-/* $Id: x.c,v 1.19 1998/04/24 13:50:18 mirar Exp $ */
+/* $Id: x.c,v 1.20 1999/04/12 14:55:57 mirar Exp $ */
 
 /*
 **! module Image
@@ -12,7 +12,7 @@
 
 #include "stralloc.h"
 #include "global.h"
-RCSID("$Id: x.c,v 1.19 1998/04/24 13:50:18 mirar Exp $");
+RCSID("$Id: x.c,v 1.20 1999/04/12 14:55:57 mirar Exp $");
 #include "pike_macros.h"
 #include "object.h"
 #include "constants.h"
@@ -35,23 +35,6 @@ extern struct program *image_colortable_program;
 #define THIS ((struct image *)(fp->current_storage))
 #define THISOBJ (fp->current_object)
 
-/*
-**! method string cast(string type)
-**! returns the image data as a string ("rgbrgbrgb...")
-**! see also: Image.colortable
-**! bugs
-**!	always casts to string...
-*/
-
-void image_cast(INT32 args)
-{
-  /* CHECK TYPE TO CAST TO HERE! FIXME FIXME FIXME! */
-  pop_n_elems(args);
-  push_string(make_shared_binary_string((char *)THIS->img,
-					THIS->xsize*THIS->ysize
-					*sizeof(rgb_group)));
-}
-
 void image_to8bit(INT32 args) /* compat function */
 {
   struct neo_colortable *nct;
-- 
GitLab