Re: [gtk-list] glist, GValue, GTypes?



On 16 Sep 1998, Dirk Luetjens wrote:

> Hello,
> 
> I would like to use the glib as a foundation for some image processing
> routines. The fundamental problem of imageprocessing is, that you have 
> to deal with different data types. So I defined the following structs
> for an image
> 
> typedef struct _Image Image;
> struct _Image {
>   int ndim;
>   int veclen;
>   int type;
>   int *dimension;
>   int size;
>   
>   union {
>     ubyte	*data_ubyte;
>     float	*data_float;
>     double	*data_double;
>     int		*data_int;
>     short	*data_short;
>     complex	*data_complex;
>     pointer	*data_pointer;
>   } data_union;
> 
>   union {
>     ubyte	data_ubyte;
>     float	data_float;
>     double	data_double;
>     int		data_int;
>     short	data_short;
>     complex	data_complex;
>     pointer	data_pointer;
>   } data_union_thresh;
> };
> 
> typedef struct _Image_double Image_double;
> struct _Image_double {
>   int ndim;
>   int veclen;
>   int type;
>   int *dimension;
>   int size;
> 
>   double *data;
>   double thresh;
> };
> 
> If I know the image type I can cast a generic Image to the appropriate 
> type and can extract the elements from the image.

why do you want to cast the image pointer if the original structure
contains all meningfull members?
in this case it seems better to just code like:

if (image->type == IMG_DOUBLE)
  return image->data_union_thresh.data_double;
else if (iamge->type == IMG_FLOAT)
  return image->data_union_thresh.data_float;

but that's of course just a guess, since i don't know the whole
problem ;)

> To extract a value from a compile time unknown image type I have a
> functions that extract a data_union based on the type field. 
> 
> Now I want to switch to glib, and I thought to use the GValue union as 
> my data_union type. But the GValue field defines v_float as a gdouble
> or a v_int as a gulong. I know most compilers handle floats as doubles 
> and the on most systems the size of int is the same as the size of a
> long. Can you explain the reason for this to me. 
> 
> Second, I would like to use defines or enums like GINT, GDOUBLE,
> GFLOAT, ... to switch between the different cases. Shouldn´t there be
> a GTypes type in glib.h which enums all types declared in glib.h, like
>

losts some text there? 

regarding GValue, it was not intended to serve as a generic multiple value
storage structure, but is actually just a helper structure for GScanner, and
thus it caries such odd members as gdouble v_float; gchar *v_comment; or
gpointer v_symbol; since GScanner attempts to simplify the parsed stuffs as
much as possible.
maybe i should rename that to GScannerValue, i guess we would even keep
source capability with that.
anyways, since what you're aiming at is just a generic union and associated
enum definitions, and thus have to write all code that's going to handle this
yourself anyways, i guess you're better be off with defining your own

typedef enum
{
  MY_VAL_CHAR,
  MY_VAL_UCHAR,
  MY_VAL_INT,
  MY_VAL_UINT,
  MY_VAL_FLOAT,
  MY_VAL_DOUBLE,
  MY_VAL_STRING
} MyValueType;

union   _MyValue
{
  gchar         v_char;
  guchar        v_uchar;
  gint          v_int;
  guint		v_uint;
  gfloat	v_float;
  gdouble	v_double;
  gchar	       *v_string;
};

and code away with that.

> 
> Dirk
> 

---
ciaoTJ



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]