dom96 / nim-opencv

Nim OpenCV wrapper
MIT License
55 stars 5 forks source link

type mangling error for object types when passing to C #9

Open oskca opened 6 years ago

oskca commented 6 years ago

this might be the right place to do bug reporting :smile:

type mangling error for object types when passing to C

in nim-opencv, there is two types: TPoint and TScalar

type
  TPoint* {.pure, final.} = object
    x*: cint
    y*: cint

type
  TScalar* {.pure, final.} = object
    val*: array[0..4 - 1, cdouble]

and this function

proc circle*(img: ImgPtr; center: TPoint; radius: cint; color: TScalar;
             thickness: cint ; lineType: cint ; shift: cint ) {.cdecl,
    importc: "cvCircle", dynlib: imgprocdll.}

when it's translated to c, it becomes:

typedef N_CDECL_PTR(void, tyProc_X9c6cdy4kb1kGBF9cECAg70A) (tyObject_TIplImage_CDRuqUq40kDTFB8ETZSvWg* img, tyObject_TPoint_eiQIpSY0i0sZHKdptoc1dg center, int radius, tyObject_TScalar_yQFN0CrcD1NsTHZ9b70VduA* color, int thickness, int lineType, int shift);

you can see that center: TPoint is still a struct value, but color: TScalar becomes a pointer , thus the value color is not passed to c side correctly:

nim prints: color:(val: [3.0, 4.0, 255.0, 2.0])
gdb shows: (gdb) info args: color = {val = {0, 2.0746488398152286e-317, 3.1315132724983341e-294, 3}}

and this cause opencv failed to read the correct arguments and failes for all drawing function like cvCircle and cvRectangle etc

oskca commented 6 years ago

as @dom96 suggested, If I add {.bycopy.} pragma to TScalar the problem goes away

dom96 commented 6 years ago

Let's keep this open until it's actually fixed in the repo.