On Oct 27, 2004, at 7:19 PM, Dmitriy Kuvshinov wrote:

How i can return packed data into perl script?

newSVpv() copies a character array into a scalar; but it doesn't have to be a character string.


      SV * sv = newSVpv ((char*) data, n*sizeof(data[0]));

that will allocate a scalar with n*sizeof(data[0]) bytes of storage, and copy that many bytes of the contents of data into it. in perl, you can unpack that normally. this also works for structs and pretty much anything else; the key is to specify the length explicitly, rather than letting newSVpv() find the length with strlen().


 $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file ($ARGV[0]);
 $pixels = $pixbuf->get_pixels();
 #$pixels="\x00\x00\xf0\x00\x00\xff\x00\x00\xff";

in fact, Gtk2::Gdk::Pixbuf::get_pixels() is implemented like this:

SV *
gdk_pixbuf_get_pixels (pixbuf)
        GdkPixbuf *pixbuf
    PREINIT:
        guchar * pixels;
    CODE:
        pixels = gdk_pixbuf_get_pixels (pixbuf);
        RETVAL = newSVpv ((gchar *) pixels,
                          gdk_pixbuf_get_height (pixbuf)
                          * gdk_pixbuf_get_rowstride (pixbuf));
    OUTPUT:
        RETVAL



--
Examples really shouldn't include unexploded ordnance.
  -- Joe Smith, referring to an example program i wrote.



Reply via email to