Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Pixel-based Plotting in Perl?

by pat_mc (Pilgrim)
on Sep 02, 2008 at 19:16 UTC ( [id://708577]=perlquestion: print w/replies, xml ) Need Help??

pat_mc has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Monks -

I remember that many, many years ago I had lots of fun plotting graphs and fractals on my computer screen in what otherwise was a rather limited dialect of BASIC. Now, years later, I am wondering if there is a suitable module to do just that in Perl. Basically, I would like to have functions like plot, draw and fill at hand to draw various elements on my screen programmatically, maybe in different colours, too.

Can anybody please suggest suitable modules to do that?

Thanks in advance and regards -

Pat

Replies are listed 'Best First'.
Re: Pixel-based Plotting in Perl?
by mr_mischief (Monsignor) on Sep 02, 2008 at 20:55 UTC
    There are a few options, none of which are AFAICT perfect for what you're wanting.

    GD, GD::Simple and Image::Magick will let you work with graphics files and save them. Then you'll need something to display those files.

    Tk, The GIMP (perhaps with Gimp::Fu managing it), a browser, Prima (particularly Prima::Image perhaps), wxWidgets (with Wx, or something could take care of the display.

    Some of those graphics loaders and GUI toolkits let you draw point by point in them, too. See Prima::Drawable and Tk::Canvas.

    You can also tie into SDL and OpenGL (perhaps OpenGL::Simple). Those will give you even more graphics control than you're probably looking to use. You can even use SDL n conjunction with OpenGL or WxWidgets in conjunction with OpenGL.

    There are Gtk and Qt, which are the graphics toolkits for Gnome and KDE.

    Perhaps you're looking for something like Graphics::Simple, which is much like what some languages close to the metal of their platforms used to handle. It's marked as alpha and uses GTK or Postscript back ends.

    You don't really say whether you want to show the drawing in progress on the screen or to show the result. Some of these options only really fit well into the completed image work flow. Others work well with updating the graphics on screen as you add or change elements. SDL, OpenGL, Tk, Prima, Gtk, Qt, and Graphics::Simple seem to be among those.

    Portability is another issue. Tk, SDL, OpenGL, Qt, and Gtk can be used on most platforms but require the relevant libraries to be there. Prima is an option in Perl, but the others have bindings for other languages.

    Your main issue here is that Perl is widget/toolkit/canvas ambivalent. The dialect of Basic you mentioned was tied tightly to the nature of the computer that drew the pixels for it. Once you pick a way to display your pixels, your application will be tied to some library unless you go the extra mile to make a handle of them interchangeable back ends. (That would be a neat trick, and many people would appreciate a module that wrapped Qt, Gtk, Prima, Tk, and SDL's graphics with a unified API. I doubt you're interested in doing that much work on it, though).

      Good list. Of all of them, GD is probably the easiest to get started with, the easiest to install and the documentation is quite good (though many of the others are more powerful and/or have better image quality).

      I would like to add a plug for Cairo: it has excellent image quality and can export to all kinds of file formats (and to Gnome/Gtk widgets, if you have Gtk2 installed).

      mr_mischief - Thanks for your comprehensive post which outlines a whole spectrum of options!

      Can I just ask a newbie question: How exactly does Perl handle the display of graphics? As far as I understand, essentially, I have a Perl programme determine the position, size and colour of a pixel. And then? What exactly is a canvas? Is it a separate programme with an API? ... Sorry, I am just new to this whole area.

      Thanks again and cheers -

      Pat
        Perl itself has no concept of pixels or screens. All it can do is call on libraries that manage displays or abstractions of displays. Most current operating systems will also not allow direct access to the display anyway - usually a program requests a window, which may be a MS Windows type window with title bar, menu etc, or may take up the whole available screen as a drawable map of pixels.

        For many applications it's more useful to have the more abstract notion of a canvas, which usually means an area on which you can place a combination of vector (lines, curves, circles, polygons etc) and possibly bitmap primitives with varying levels op transparency, height (IOW place one primitive on top of another), rotations, scaling, translations and so on and let the canvas deal with rendering all those primitives to the screen/window or to some file format (and some file formats like postscript, pdf and svg can store the primitives directly, which is very useful for printing, since you can get a more or less unlimited resolution out of vector primitives)

        And this is all just 2D imaging. 3D rendering libraries can be a lot more complex. :-) See OpenGL.

        Basically, Perl doesn't do graphics directly. In order to do so, it would need to be tied to the specifics of the host device. The language itself is more than sufficient to hold and manipulate the data structures involved, although in some complicated cases it might be a little slow.

        What is needed is a way to get the shape and color data (or pixel data) into hardware for it to be rendered. That's where these libraries come in.

        GD, ImageMagick, and Cairo take instructions about shapes, colors, lines, and such and put out graphics formats other systems already understand. They are libraries that are used by your application (C/C++ libraries with Perl wrappers in this case).

        Tk, Gtk, Qt, SDL, WxWidgets, and OpenGL are APIs for external libraries that are built to actually draw into the windows on various OSes. They each allow you to write to their API and they write to the Windows, X11, Cocoa, or whatever API on the actual OS and device. Each of them is a separate API apart from the others on the list except that a couple of them can use OpenGL as an intermediate back end. All of them AFAIK work on Windows and OS X as well as Linux, Irix, and Solaris. Tk, Gtk, Gtk2, Qt, and WxWidgets are "graphical toolkits". They're made for bringing applications to GUIs without having to write all the windowing code and such. SDL and OpenGL are more general graphics things. There are implementations of those for framebuffer devices and for windowed GUIs (I think Qt has a framebuffer backend for embedded devices, too).

        In order to just write to the pixels being shown, you'd have to have fairly direct (driver level or lower) access to the video subsystem. In a protected, multi-user OS that's a bad idea. That's why these libraries exist, aside from also making many things easier and more uniform than everyone reinventing their own wheels with incompatible hubs and tires. On a sufficiently single-user system with a smallish single-user OS, one could write to video memory or to the framebuffer device provided by the OS directly still today. The trick is finding such a device.

Re: Pixel-based Plotting in Perl?
by zentara (Archbishop) on Sep 02, 2008 at 20:44 UTC
    Use the various canvas widgets...Tk, Gtk2, Zinc, etc. For something complex, using the excellent gnuplot as the engine, see SuperFormula with gnuplot and Tk. For simple straight forward plotting, see Tk Realtime data aquisition or look at the Tk "widget" demo for the canvas 2-d plot.

    You will find however, that pixel plotting (single pixels) is VERY intensive, so you really want to use some sort of lines( actually curves, a line is just a straight curve :-))

    You also might like to look at Tk Patio/Office layout designer for circles, polygons, etc. that are draggable.

    Here is a sample of styles.


    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Pixel-based Plotting in Perl?
by oko1 (Deacon) on Sep 02, 2008 at 22:26 UTC

    I recall doing the same thing as you: drawing, say, a bunch of diagonal lines on a screen using BASIC, and people coming up to me and going, "Whoa! What's that?" But that was in 1978 or so. :) These days, most people don't plot stuff like that on the console - although you still can, if you want to, at least in Linux (that's what the 'svga' library is for.) Assuming that you are running Linux, and want to do this with Perl, you could use Linux::Svgalib:

    # From the module docs use Linux::Svgalib; my $svga = Linux::Svgalib->new(); $svga->init(); $svga->setmode(G640x480x16); # Set the color for the subsequent ops $svga->setcolor($color); # Place a pixel $svga->drawpixel($x, $y); # Draw a line $svga->drawline($x1, $y1, $x2, $y2); # ...and so on; see the other methods in the documentation $svga->setmode(TEXT);

    This may be as close as you'll get to the old BASIC experience. :)

    Update: Linux::Svgalib doesn't compile on my system (an error in the XS portion, which I'm too lazy to troubleshoot); however, vga seems to compile just fine, and appears (per the docs) to handle the same functions.

    
    -- 
    Human history becomes more and more a race between education and catastrophe. -- HG Wells
    
      oko1 -

      Thanks for your input ... and the shared bit of history which sounds similar to what I recall (even if my memory only takes me back as far as the late 80s ;-).
      Must try the library you suggest on my Linux machine. From the code snippets you provide I suspect this may be close to what I was looking for.
      Thanks again!

      Cheers -

      Pat
Re: Pixel-based Plotting in Perl?
by binf-jw (Monk) on Sep 02, 2008 at 19:35 UTC
    Not entirely sure what you'd need it for. But you can do some drawing with CGI modules.
    but to be honest if it's for graphs etc you're better of with the CAD::* modules.
    More specifically the CAD::Drawing. module could be helpful.
    Update: just read the title, YMMV with pixel based plotting. But CAD will sort you for vector graphics.
    Hope that helps,
    John
      Thanks, John -

      I was hoping to draw points, lines, circles and maybe some other geometric shapes in different locations on my screen. Essentially, the decisions on which shape is to be drawn where will depend on the individual use case and should not be of importance to the module recommendation requested here. The graphics thus produced will be the product of individual pixel decisions and do not need to be scalable (hence vector graphics may not be what I am looking for).

      Thanks again for your input!

      Best regards -

      Pat
Re: Pixel-based Plotting in Perl?
by jbert (Priest) on Sep 02, 2008 at 23:14 UTC

    I'd echo SDL for the nearest thing to old-school BASIC graphics from perl.

    And - to be a little heretical - I'd say that the closest+easiest thing today is javascript in a browser which supports the 'canvas' HTML element (e.g. firefox).

    That allows you to run in an interactive way, use moveto/lineto as you may be used to and also easily allows you to share your fun with other people (since you can serve the javascript from a static HTML file hosted somewhere.

      Hi, jbert -

      This sounds very interesting. How exactly do you do this?

      Thanks for your post!

      Pat

        You need to be able to:

        As noted in the canvas link above, you'll need a browser which supports canvas (e.g. firefox). You can just open the .html file in the browser and you should see your code running. If you put your HTML and javascript on a web host somewhere, other people can see it too.

        Have fun :-)

Re: Pixel-based Plotting in Perl?
by John M. Dlugosz (Monsignor) on Sep 03, 2008 at 00:16 UTC
    Last time I needed to plot some things, I used GD.

    More recently, I just wrote Perl to output SVG, then rendered using inkscape and converted to PDF.

    --John

Re: Pixel-based Plotting in Perl?
by Perlbotics (Archbishop) on Sep 02, 2008 at 21:47 UTC
    Hi, maybe a little bit OT, but the various PDF APIs (e.g. PDF::API2::Lite) support basic plotting (e.g. circles, filled polygons, etc.). Although you cannot watch the creation process, printing and archiving the results might be easier...
Re: Pixel-based Plotting in Perl?
by hda (Chaplain) on Sep 03, 2008 at 11:34 UTC
    Hi, Perhaps you want to take a look at Perl Data language (PDL) at http://pdl.perl.org/
Re: Pixel-based Plotting in Perl?
by smiffy (Pilgrim) on Sep 04, 2008 at 02:08 UTC

    SVG has been mentioned, so I will just throw in this suggestion to keep things interesting: PostScript.

    I'm not aware of any modules that can help you produce PostScript, but it's not really that hard to use if you are prepared to do a little reading - there are plenty of tutorials online.

    What I like about PostScript (and SVG) is that it is not a binary format - just plain text commands.

Re: Pixel-based Plotting in Perl?
by zebedee (Pilgrim) on Sep 04, 2008 at 04:13 UTC
    Maybe if you enjoyed that BASIC variant you can find an emulator for it or the machine it ran on? Way OT, but who knows?

    http://en.wikipedia.org/wiki/Emulator

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://708577]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found