[Cython] Mapping a really strange typedef in Cython

Robert Bradshaw robertwb at math.washington.edu
Wed Jul 16 09:15:55 CEST 2008


On Jun 29, 2008, at 4:16 AM, Johannes Wienke wrote:

> Hi again,
>
> I'm struggling to get a really strange typedef working in Cython. The
> original C (preprocessor) code looks like this:
>
> #define IW_CTAB_SIZE	256
> typedef guchar (*iwColtab)[3];
>
> extern iwColtab iw_def_col_tab;
>
> /* Color formats of the data for prev_render...() */
> #define IW_RGB			((iwColtab)0)
>
> // some more formats
>
> #define IW_BW			((iwColtab)7)
> #define IW_GRAY			IW_BW
> #define IW_INDEX		(iw_def_col_tab)
>
> #define IW_COLFORMAT_MAX	IW_BW	/* last 'special' definition */
>
> The purpose of this strange construct was that one can use normal  
> color
> spaces without struggling, but one is also able to use a custom  
> color table.
>
> This definition was originally used like this:
>
> void prev_colConvert (iwColtab ctab, gint r, gint g, gint b,
>                       gint *rd, gint *gd, gint *bd)
> {
> 	if (ctab == IW_RGB) {
> 		*rd = r;
> 		*gd = g;
> 		*bd = b;
> 	} else if (ctab == IW_BGR) {
> 		*rd = b;
> 		*gd = g;
> 		*bd = r;
> 	} else if (ctab > IW_COLFORMAT_MAX && r>=0 && r < IW_CTAB_SIZE){
> 		*rd = ctab[r][0];
> 		*gd = ctab[r][1];
> 		*bd = ctab[r][2];
> 	}
>
> }
>
> I need to be able to do the same thing in cython with this definition.
> My current approach was simply using a enum to emulate it:
>
> cdef extern from "gui/Gimage.h":
>
>     ctypedef enum iwColtab:
>         IW_RGB
>         # ...
>         IW_GRAY
>         IW_CTAB_SIZE
>         IW_COLFORMAT_MAX
>
> The normal color modes work like this, but using the color table  
> doesn't
> work:
>
> Error converting Pyrex file to C:
> ------------------------------------------------------------
> ...
>     elif ctab == IW_YUV:
>         return Color(1.164 * (r - 16) + 1.596 * (b - 128),
>                      1.164 * (r - 16) - 0.813 * (b - 128) - 0.391 *  
> (g -
> 128),
>                      1.164 * (r - 16) + 2.018 * (g - 128))
>     elif (ctab > IW_COLFORMAT_MAX and r >= 0 and r < IW_CTAB_SIZE):
>         return Color(ctab[r][0], ctab[r][1], ctab[r][2])
>                         ^
> ------------------------------------------------------------
>
> image_utils.pyx:144:25: Attempting to index non-array type 'iwColtab'
>
> I really have no idea how to solve this. Has anyone got a solution  
> for this?
>
> Thanks for the help
> Johannes

I'm not sure--what is the definition of iwColtab? An enum won't work  
here because one can't index into it (in Cython or in C) so I think  
the only thing to do here is to have two separate types (the enum,  
and iwColtab) and do some casting.

- Robert



-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
Url : http://codespeak.net/pipermail/cython-dev/attachments/20080716/faba004e/attachment.pgp 


More information about the Cython-dev mailing list