Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SDL::Surface::new_from and PLot question

12 views
Skip to first unread message

Kartik Thakore

unread,
Nov 16, 2009, 2:54:18 PM11/16/09
to dcme...@gmail.com, sdl-...@perl.org, pdl-p...@jach.hawaii.edu
Hi,

So I fixed new_from to prevent it from segfaulting. Now as for direct
write from PLot to Surface, where can I see the how the raw data is
stored and made acessible in PDL.

Kartik Thakore

Kartik Thakore

unread,
Nov 16, 2009, 5:17:37 PM11/16/09
to David Mertens, sdl-...@perl.org, pdl-p...@jach.hawaii.edu
Hi David,

Can you give a small script ( a sin function ) that is ready for me to
grab get_dataref? Some to test SV to Surface pixels conversions.

Kartik Thakore

On 16-Nov-09, at 4:39 PM, David Mertens <dcme...@gmail.com> wrote:

> Kartik -
>
> I've never looked too closely at the internals of PDL to know
> precisely how it stores the data, but as I understand, the data is
> typically stored in a packed scalar. The proper way to get at this
> data is using the get_dataref and upd_data member functions. These
> functions are discussed here: http://search.cpan.org/~chm/PDL-2.4.5/Basic/Core/Core.pm.PL#get_dataref
>
> For the PDL folks, who probably don't know what's going on, I am
> trying to make PLplot play with SDL. I would like to quickly draw
> animations of plots. Doug wrote an option into PLplot that allows
> it to plot to a memory location, and PDL::Graphics::PLplot is
> capable of specifying a piddle for this purpose. His original
> intention was to allow him to load an image into a piddle and
> overlay a graph. My intention is to make an SDL surface that uses
> the same memory location as a PLplot output, so that I can use SDL's
> quick redrawing mechanisms for fast interactive plotting. I don't
> actually need to get PDL involved in this - SDL would do well to
> interact directly with PLplot - but it's probably a good idea to
> interface SDL::Surfaces with PDL while we're at it.
>
> David

Kartik Thakore

unread,
Nov 16, 2009, 5:51:21 PM11/16/09
to David Mertens, sdl-...@perl.org, pdl-p...@jach.hawaii.edu
How do I update this? like animation?

while(1)
{

$pdl_memory->get_dataref();
}

On Mon, Nov 16, 2009 at 5:48 PM, David Mertens <dcme...@gmail.com> wrote:

> On Mon, Nov 16, 2009 at 4:17 PM, Kartik Thakore <thakore...@gmail.com>wrote:
>
>> Hi David,
>>
>> Can you give a small script ( a sin function ) that is ready for me to
>> grab get_dataref? Some to test SV to Surface pixels conversions.
>>
>> Kartik Thakore
>>
>

> If all you're looking for is a 2-d piddle with something nontrivial, try
> this:
>
> --------%<--------
>
> use PDL;
>
> # Allocate a 10x10 array for rgb. I think that only the zeroes function
> allows
> # you to declare a piddle with a specific data type
> my $pdl_memory = zeroes(byte, 3, 10, 10);
>
> # Set values. Use in place to be sure we don't get another allocation,
> which may be the wrong type.
> # Note that the sequence wraps back to zero after hitting 256, because
> they're bytes!
> $pdl_memory->inplace->sequence;
>
> # I believe we can get at the actual IV like so:
> my $iv = $pdl_memory->get_dataref();
>
> --------%<--------
>
> I believe that will do it. PDL wizards please correct me if I'm wrong.
>
> David
>

Kartik Thakore

unread,
Nov 16, 2009, 7:01:32 PM11/16/09
to David Mertens, sdl-...@perl.org, pdl-p...@jach.hawaii.edu
Well this does something ... But I don't think thats what we want ... I
still don't know how to get color data from dataref.

use SDL;
BEGIN{
delete $main::{in};

}
use PDL;
use PDL::NiceSlice;
use SDL::Video;
use SDL::Event;
use SDL::Surface;
use SDL::Constants;
use Carp;


# Allocate a 10x10 array for rgb. I think that only the zeroes function
allows

# # you to declare a piddle with a specific data type
my $pdl = zeroes(byte, 3, 10, 10);
#
#
# # Set values. Use in place to be sure we don't get another allocation,


which may be the wrong type.

# # Note that the sequence wraps back to zero after hitting 256, because
they're bytes!
$pdl->inplace->sequence;
#
# # Now for a really silly animation - turn all the pixels white


carp 'Unable to init SDL: '.SDL::get_error() if( SDL::init(SDL_INIT_VIDEO) <
0);

my $screen = SDL::Video::set_video_mode( 640, 480, 32, SDL_SWSURFACE);

carp 'Unable to set 640x480x32 video'.SDL::get_error() if(!$screen);

my $offset = $screen->pitch / 4;

sub putpixel
{
my($x, $y, $color) = @_;
my $lineoffset = $y * ($screen->pitch / 4);
$screen->set_pixels( $lineoffset+ $x, $color);
}
sub render
{
if( SDL::Video::MUSTLOCK( $screen) )
{
return if (SDL::Video::lock_surface( $screen ) < 0)
}


for (my $i = 0; $i < 300; $i++) {

$pdl()->clump(-1)->($i) = 255;
$iv = $pdl->get_dataref();
putpixel( $i, $i, $iv );
}

SDL::Video::unlock_surface($screen) if (SDL::Video::MUSTLOCK($screen));

SDL::Video::update_rect($screen, 0, 0, 640, 480);

}


while(1)
{
render();

my $event = SDL::Event->new();

while( SDL::Events::poll_event($event) )
{
my $type = $event->type;

if( $type == SDL_QUIT)
{
SDL::quit();
exit();
}


}

SDL::Events::pump_events();

}

On Mon, Nov 16, 2009 at 6:36 PM, David Mertens <dcme...@gmail.com> wrote:

> On Mon, Nov 16, 2009 at 4:51 PM, Kartik Thakore <thakore...@gmail.com>wrote:
>
>> How do I update this? like animation?
>>
>> while(1)
>> {
>>
>> $pdl_memory->get_dataref();
>>
>> }
>>
>

> Yeah, you know, I used a terrible variable name for that piddle. Let's try


> this:
>
> --------%<--------
>
> use PDL;

> use PDL::NiceSlice;


>
>
> # Allocate a 10x10 array for rgb. I think that only the zeroes function
> allows
> # you to declare a piddle with a specific data type

> my $pdl = zeroes(byte, 3, 10, 10);


>
>
> # Set values. Use in place to be sure we don't get another allocation,
> which may be the wrong type.
> # Note that the sequence wraps back to zero after hitting 256, because
> they're bytes!

> $pdl->inplace->sequence;
>
> # Now for a really silly animation - turn all the pixels white
> for (my $i = 0; $i < 300; $i++) {
> $pdl()->clump(-1)->($i) = 255;
> # Do something with piddle data here.
> }
>
> --------%<--------
>
> Is that a bit clearer? Does anybody have a better sample?
>
> David
>

Kartik Thakore

unread,
Nov 16, 2009, 7:54:31 PM11/16/09
to Chris Marshall, David Mertens, sdl-...@perl.org, pdl-p...@jach.hawaii.edu
Um how do I get the x,y coords and the data you want me to put there from
$sdl?

On Mon, Nov 16, 2009 at 7:13 PM, Chris Marshall <c...@alum.mit.edu> wrote:

> David Mertens wrote:


>
> On Mon, Nov 16, 2009 at 4:51 PM, Kartik Thakore <thakore...@gmail.com<mailto:
>> thakore...@gmail.com>> wrote:
>>
>> How do I update this? like animation?
>>
>> while(1)
>> {
>>
>> $pdl_memory->get_dataref();
>>
>> }
>>
>>

>> Yeah, you know, I used a terrible variable name for that piddle. Let's

>> try this:
>> --------%<--------
>>
>> use PDL;

>> use PDL::NiceSlice;
>>
>> # Allocate a 10x10 array for rgb. I think that only the zeroes function #
>> allows you to declare a piddle with a specific data type
>>
>
>
> perldl> ?sequence
> Module PDL::Basic
> sequence
> Create array filled with a sequence of values
>
> $a = sequence($b); $a = sequence [OPTIONAL TYPE], @dims;
>
> etc. see zeroes.
>
> perldl> p sequence(10)
> [0 1 2 3 4 5 6 7 8 9]
> perldl> p sequence(3,4)
> [
> [ 0 1 2]
> [ 3 4 5]
> [ 6 7 8]
> [ 9 10 11]
> ]
>
> Docs from /c/site/perl/lib_pdl/cygwin-thread-multi-64int/PDL/Basic.pm
>
> So you can do:
>
> perldl> $a = sequence(byte, 3, 10, 10);
>
> perldl> p $a->info
> PDL: Byte D [3,10,10]
>
>
> > my $pdl = zeroes(byte, 3, 10, 10);


>
>> # Set values. Use in place to be sure we don't get another allocation,
>> which may be the wrong type.
>> # Note that the sequence wraps back to zero after hitting 256, because
>> they're bytes!

>> $pdl->inplace->sequence;
>>
>> # Now for a really silly animation - turn all the pixels white
>> for (my $i = 0; $i < 300; $i++) {
>> $pdl()->clump(-1)->($i) = 255;
>> # Do something with piddle data here.
>> }
>>
>> --------%<--------
>>
>> Is that a bit clearer? Does anybody have a better sample?
>>
>

> You could use the PDL life example of threading that Matt posted
> earlier today. Just make an empty $sdl piddle with dims [3,N,M],
> a $life piddle with shape [N,M] for the life updates, init $life,
> then copy each update from the $life to the $sdl, maybe like:
>
> $sdl .= $life(*3);
>

Chris Marshall

unread,
Nov 16, 2009, 7:13:23 PM11/16/09
to David Mertens, Kartik Thakore, sdl-...@perl.org, pdl-p...@jach.hawaii.edu
David Mertens wrote:
> On Mon, Nov 16, 2009 at 4:51 PM, Kartik Thakore
> <thakore...@gmail.com <mailto:thakore...@gmail.com>> wrote:
>
> How do I update this? like animation?
>
> while(1)
> {
>
> $pdl_memory->get_dataref();
>
> }
>
>
> Yeah, you know, I used a terrible variable name for that piddle. Let's
> try this:
>
> --------%<--------
>
> use PDL;
> use PDL::NiceSlice;

>
> # Allocate a 10x10 array for rgb. I think that only the zeroes function
> # allows you to declare a piddle with a specific data type


perldl> ?sequence
Module PDL::Basic
sequence
Create array filled with a sequence of values

$a = sequence($b); $a = sequence [OPTIONAL TYPE], @dims;

etc. see zeroes.

perldl> p sequence(10)
[0 1 2 3 4 5 6 7 8 9]
perldl> p sequence(3,4)
[
[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]
]

Docs from /c/site/perl/lib_pdl/cygwin-thread-multi-64int/PDL/Basic.pm

So you can do:

perldl> $a = sequence(byte, 3, 10, 10);

perldl> p $a->info
PDL: Byte D [3,10,10]

> my $pdl = zeroes(byte, 3, 10, 10);


> # Set values. Use in place to be sure we don't get another allocation,
> which may be the wrong type.
> # Note that the sequence wraps back to zero after hitting 256, because
> they're bytes!

David Mertens

unread,
Nov 16, 2009, 5:48:12 PM11/16/09
to Kartik Thakore, sdl-...@perl.org, pdl-p...@jach.hawaii.edu
On Mon, Nov 16, 2009 at 4:17 PM, Kartik Thakore <thakore...@gmail.com>wrote:

> Hi David,
>
> Can you give a small script ( a sin function ) that is ready for me to grab
> get_dataref? Some to test SV to Surface pixels conversions.
>
> Kartik Thakore
>

If all you're looking for is a 2-d piddle with something nontrivial, try
this:

--------%<--------

use PDL;

# Allocate a 10x10 array for rgb. I think that only the zeroes function
allows
# you to declare a piddle with a specific data type
my $pdl_memory = zeroes(byte, 3, 10, 10);

# Set values. Use in place to be sure we don't get another allocation, which
may be the wrong type.
# Note that the sequence wraps back to zero after hitting 256, because
they're bytes!

David Mertens

unread,
Nov 16, 2009, 6:36:18 PM11/16/09
to Kartik Thakore, sdl-...@perl.org, pdl-p...@jach.hawaii.edu
On Mon, Nov 16, 2009 at 4:51 PM, Kartik Thakore <thakore...@gmail.com>wrote:

> How do I update this? like animation?
>
> while(1)
> {
>
> $pdl_memory->get_dataref();
>
> }
>

Yeah, you know, I used a terrible variable name for that piddle. Let's try
this:

--------%<--------

use PDL;
use PDL::NiceSlice;

# Allocate a 10x10 array for rgb. I think that only the zeroes function
allows
# you to declare a piddle with a specific data type

my $pdl = zeroes(byte, 3, 10, 10);

# Set values. Use in place to be sure we don't get another allocation, which
may be the wrong type.
# Note that the sequence wraps back to zero after hitting 256, because
they're bytes!

$pdl->inplace->sequence;

# Now for a really silly animation - turn all the pixels white
for (my $i = 0; $i < 300; $i++) {
$pdl()->clump(-1)->($i) = 255;
# Do something with piddle data here.
}

--------%<--------

Is that a bit clearer? Does anybody have a better sample?

David

Chris Marshall

unread,
Nov 16, 2009, 8:44:15 PM11/16/09
to Kartik Thakore, David Mertens, sdl-...@perl.org, pdl-p...@jach.hawaii.edu
Kartik Thakore wrote:
> Um how do I get the x,y coords and the data you want me to put there
> from $sdl?

Here is a more complete program:

use PDL;
use PDL::NiceSlice;

# Uncomment this if you can use TriD to display
# use PDL::Graphics::TriD;

# declare/init variables
my $sdl = zeroes(byte, 3, 10, 11);
my $life = zeroes(byte, 10, 11);
my $n;

# init Life piddle with a "glider"
$life(1:3,1:3) .= pdl( [1,1,1],
[0,0,1],
[0,1,0] );

while (1) {

# calculate the number of neighbors
$n = ($life->range(ndcoords($life)-1,3,3)->reorder(2,3,0,1)->sumover->sumover)-$life;

# propagate the cells that live in the next step
$life = ((($n == 2) + ($n == 3))* $life) + (($n==3) * !$life);

# copy data to an RGB byte array for SDL to access
$sdl .= $life(*3);

# display the timestep (uncomment if you have TriD)
# nokeeptwiddling3d();
# imagrgb $sdl;

# this is a perl reference to a string containing the PDL data
$sdl_dataref = $sdl->get_dataref;
# print "\$sdl data is " . length($$sdl_dataref) . " bytes long\n";
}

The $sdl_dataref is a perl reference to a string value containing
the packed representation of the PDL, that is the contents of the
string is the binary array data of the PDL, in this case a 10x11
array of RGB bytes ordered in memory for each row of 10 pixels as:

Byte#: 0 1 2 3 4 5 6 7 8 9 10 11 27 28 29
R0 B0 G0,R1 B1 G1,R2 B2 G2,R3 B3 G3,...,R9 B9 G9 (row 0)

Byte#: 30 31 32 33 34 35 36 37 38 39 40 41 57 58 59
R0 B0 G0,R1 B1 G1,R2 B2 G2,R3 B3 G3,...,R9 B9 G9 (row 1)

Byte#: 60 61 62 63 64 65 66 67 68 69 70 71 87 88 89
R0 B0 G0,R1 B1 G1,R2 B2 G2,R3 B3 G3,...,R9 B9 G9 (row 2)
.
.
.
Byte#:300301302303304305306307308309310311 327328329
R0 B0 G0,R1 B1 G1,R2 B2 G2,R3 B3 G3,...,R9 B9 G9 (row 10)

--Chris

> On Mon, Nov 16, 2009 at 7:13 PM, Chris Marshall <c...@alum.mit.edu
> <mailto:c...@alum.mit.edu>> wrote:
>
> David Mertens wrote:
>
> On Mon, Nov 16, 2009 at 4:51 PM, Kartik Thakore
> <thakore...@gmail.com <mailto:thakore...@gmail.com>

> <mailto:thakore...@gmail.com

> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.425 / Virus Database: 270.14.68/2507 - Release Date: 11/16/09 19:53:00
>

David Mertens

unread,
Nov 17, 2009, 12:24:30 AM11/17/09
to Chris Marshall, Kartik Thakore, sdl-...@perl.org, pdl-p...@jach.hawaii.edu
Kartik -

I'm sorry I didn't give a better explanation of how to access individual
elements in the bitmap using PDL's notation. After thinking about it for a
bit, I realized that this seems overly obvious to me because Karl and
company use image processing as their demonstration example in the e-boook,
PDL - Scientific Programming in Perl. You can find a link to download the
pdf under the second bullet here:
http://sourceforge.net/apps/mediawiki/pdl/index.php?title=Main_Page#External_Resources

If you read Chapter 1, you'll hopefully have a much clearer idea of how to
use PDL, and since it uses image processing and analysis, I'm sure you'll
see many possible extensions to SDL.

Don't worry, it's a good read. I'd recommend the whole book except that
it's not finished. :)

David

David Mertens

unread,
Nov 16, 2009, 4:39:12 PM11/16/09
to Kartik Thakore, sdl-...@perl.org, pdl-p...@jach.hawaii.edu

Kartik -

0 new messages