in reply to Open CV problem

Will this also work on non-Windows? Your code is very Windows oriënted.

Most character or data pointers in C cannot be used just as such in perl. They will have to be converted to a SCALAR value, in case of a string this will be a PV.

You will have to read the Inline::C man-page and the perlguts manpage. Good luck!


Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: Open CV problem
by Anonyrnous Monk (Hermit) on Dec 09, 2010 at 16:41 UTC
    Most character or data pointers in C cannot be used just as such in perl. They will have to be converted to a SCALAR value

    The nice thing about Inline::C is that it should take care of this, i.e. the necessary .xs (and .c) code doing the conversions is created behind the scenes - at least for simple types such as char*.

    For example, the following function returning a char* is mapped to a normal scalar on the Perl side:

    #!/usr/bin/perl use Inline C => Config => BUILD_NOISY => 1, CLEAN_AFTER_BUILD => 0; use Inline C => <<'END_OF_C_CODE'; char* GetImage() { char *data = "test-test-test"; return(data); } END_OF_C_CODE my $data = GetImage(); print "$data\n"; # "test-test-test"

    (btw, the CLEAN_AFTER_BUILD => 0 option I've specified above is very useful when you want to look at the auto-generated XS/C wrapper code for debugging purposes)

    So, if the data as returned by the OP's GetImage() function is in fact char*, things should just work fine in this regard. In other words, I suspect the problem lies elsewhere...

      When I do a printf(data); in C, I get the same out put from Perl when I do print("$data\n"); Sample out put is below.

      This look like binray data to me. But not sure if Gtk2::Pixfuf new_from_data can use this directly. I.E is their a header it looks for or somthing..... Not sure.

        I've never used Gtk2::Gdk::Pixbuf myself, so this is just speculation...
        What might be a problem is that binary data may normally contain \0, which terminates a regular C char* string (as opposed to Perl strings, which may contain \0 without problems). In other words, if you cast the binary blob to char*, you're (likely) cutting off any data after the first \0... (which would explain why it works in some cases)

        There are Perl API functions that allow you to specifiy the number of bytes to be copied into a scalar (e.g. sv_setpvn(), see perlapi), but I have no idea how to tell Inline::C to do custom conversions... So, what I would try is to convert the data to a scalar (SV*) myself on the C side, and then specify that as the return type of GetImage().  Also, you'd have to figure out where to get the proper length from... (header?)