[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Graphics generation



>If not, is there anyone who has ported something else like this over?
>
>Although I have worked on ENUCH machines, I need a solution for the Mac and
>don't know where to begin.
>
>Byron Palmer


Byron,

I too had a need to generate graphics with MacPerl and have found a
solution that works rather nicely.  There is a small, and very elegant
graphics utility on the Mac called Clip2Gif.  If you're not familiar with
it, it is generally used as a drag and drop application to convert graphics
files between PICT/GIF/JPEG.  It also supports interlacing and transparancy
for GIFs and thus has found a home for net folks.  What is NOT widely known
is Clip2Gifs extensive AppleScripting capabilities.  Not only can you
convert image files but you can manipulate them...even from scratch...with
a simple set of tools.  Thus allowing you to "draw" a graphic image from
scratch.

Now you ask, but AppleScript doesnt help me...

Using MacPerl's DoAppleScript as in the following example:

# Send apple event to WebSTAR to resume logging...
MacPerl'DoAppleScript(<<EOF);
tell application "$WEBSTAR_NAME"
     Suspend Logging FALSE
end tell
EOF


This is an example I use from a perl script that process WebSTAR logs.  The
log file is always busy so I cannot read it until I disable logging.  This
code snippet simply sends the appropriate applescript to the application
WebSTAR.


Here is a more elaborate Perl script that comes with the Clip2Gif
distribution illustrating a graphic creation from MacPerl with using
applescript.

This may sound very "ugly" but actually it is quite nice and takes
advantage of really nice software.  Perl just ends up sending a text block
as an AppleEvent and Poof!  It's also very, very fast...
==================================== Cut Here ===============================
#!/usr/bin/perl
# Copyright 1995, Yves Piguet. All Rights Reserved.

# History:
#  9/95: 1st release
#  12/95: numeric sort instead of ascii sort; cleaned to run under Perl 5
(thanks to Philippe Chartrand)

# file names

$logfile = "MacHTTP.log";
$loghtml = "stat.html";

### processes the log file

open(inFile, $logfile);

for ($i = 0; $i < 24; $i++)
{
        $nHour[$i] = 0;
}

while (<inFile>)
{
        SWITCH_ext:
        {
                if (/\.html\b/) { $nExt{"html"}++; last SWITCH_ext; }
                if (/\.gif\b/) { $nExt{"gif"}++; last SWITCH_ext; }
                if (/\.a?cgi\b/) { $nExt{"cgi"}++; last SWITCH_ext; }
                $nExt{"other"}++;
        }

        /\b(\d\d):\d\d/ && $nHour[$1]++;

        $total++;
}

### creates the graphics

$pieData = sprintf("{%.3f,red,%.3f,green,%.3f,blue,%.3f,yellow}",       #
sum of data should be 360 to have a full pie
        360 * $nExt{"html"} / $total,
        360 * $nExt{"gif"} / $total,
        360 * $nExt{"cgi"} / $total,
        360 * $nExt{"other"} / $total);

$pie = "{chart data:" . $pieData . ", chart style:pie, position:{0,0,100,100}}";

$deflabelhtml = 'set html_label to {{rectangle:{110,15,120,25},color:red},
{drawn text:"html", position:{125,25}}}';
$deflabelgif  = 'set gif_label to {{rectangle:{110,35,120,45},color:green},
{drawn text:"gif", position:{125,45}}}';
$deflabelcgi  = 'set cgi_label to {{rectangle:{110,55,120,65},color:blue},
{drawn text:"cgi", position:{125,65}}}';
$deflabelothr = 'set other_label to
{{rectangle:{110,75,120,85},color:yellow}, {drawn text:"other",
position:{125,85}}}';

($maxHour) = reverse(sort({$b <=> $a} @nHour)); # rescales data in the
range 0-100
for ($i = 0; $i < 24; $i++)
{
        $nHour[$i] = int($nHour[$i]*100/$maxHour);
}
$bars = '{chart data:{' . join(',', @nHour) . '}, chart style:bars,
thickness:5, position:{0,0,200,100}}';

&MacPerl'DoAppleScript(<<eof);  # executes what follows as an AppleScript script
set red to {65535,0,0}
set green to {0,65535,0}
set blue to {0,0,65535}
set yellow to {65535,65535,0}
tell app \"clip2gif\"
        $deflabelhtml
        $deflabelgif
        $deflabelcgi
        $deflabelothr
 set thePie to $pie
        set theDrawingPie to {thePie} & html_label & gif_label & cgi_label
& other_label
        save {150,100} as GIF drawing theDrawingPie depth 4 colors palette
system colors transparency first pixel in file \"extstats.gif\"
        set theBars to $bars
        save {200,100} as GIF drawing {theBars} depth 1 transparency first
pixel in file \"hourlystats.gif\"
end
eof

### writes the html doc

open(outFile, ">" . $loghtml);

print outFile <<eof     # writes what follows in the html doc
<html>
<head>
<title>Stats</title>
</head>

<body>

<h1>Stats</h1>

Total number of files: $total <p>

<h2>File Types Transmission Stats</h2>

<img src=extstats.gif><p>

<h2>Hourly Transmission Stats</h2>

<img src=hourlystats.gif><p>

</body>
</html>
eof
==================================== Cut Here ===============================

Again, I urge you to check out this method and let me know what you think.
It works quite well for my needs.  Keep in mind that since the graphics
tools in Clip2Gif can work on any image, you could even load in a custom
"backround" bitmat and then draw on top of that!

Regards,

Andrew Tennant
Cyberneering, Inc.
atennant@cyberneering.com