I was recently discussing methods for taking a Perl scalar filled with some sort of binary data (which I'll call a buffer) and shoving it into a piddle. This works fine if the data is one of the seven registered PDL data types, but obviously trouble arises if the data is some other format. For example, data that is represented as an unsigned char (i.e. unsigned byte) has not corresponding type in PDL, which means a simple copy of the data will not work. What does work is something like this (thanks to zpmorgan's GStreamer work for making this clear):
my $piddle = pdl (unpack('C*',$buf->data))->reshape(@dims);
I wonder if it might be possible to turn this into a function, such as coerce or some such, which basically allows you to specify the Perl unpack type along with the PDL type, so you could say:
my $piddle = coerce("C" => float, $buffer, @dims);
and, obviously, the reverse operation would be helpful, too:
my $buffer = coerce(float => "C", $piddle);
Obviously this could be made faster than using pack and unpack, but I suspect that it's pretty fast as written.
Log in to post a comment.