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

Re: [MacPerl] How to read PICT files



Philippe de Rochambeau writes 30th June 1998:

>I would like to tranform a b/w PICT file into an array of 1s and 0s to be used
>latter to generate a bitmap in PocketC and CASL for the PalmPilot.

Brian Matthews and Andre Leclerc both suggest drawing the picture into an
offscreen part of memory and digging the pixels out there.

The script below suggests one way of doing it.

The first part of the script uses 'qd.pl' to draw a very simple pict
consisting of just a vertical black bar. This is saved as a file so it can
be viewed.

In 'draw_offscreen()', the current graphics port and graphics device are
saved as $origP and $origD. Then a new gWorld is created with NewGWorld(),
and this is set as the currrent graphics port.

Next the handle to the gWorld pixMap is obtained from GetGWorldPixMap() and
locked so that the pixels do not move about during drawing. The port
rectangle in the gWorld is erased to clear the drawing area. The picture is
then drawn as it would be in a visible window by DrawPicture().

Since it is a black and white piture it seems to me the simplest approach
to retrieving the data is to use GetPixel(). But is that adequate in all
cases? Just for the experiment the pixels (0 or 1) are packed into an
array. This is printed out at the end of the script, and th pattern of '1's
can be compared to the PICT itself.

Finally the graphics world is restored to what it was, the ofscreen are
unlocked allowing disposal of the offscreen gWorld.

Offscreen.pm hs not been discussed here before I think, and I hope this
short description will be of interest. As I said in my previous note
offscreen needs the revised versions of both QDOffscreen and QuickDraw
dated 19th May.

This is a good moment to thank Matthias for his efforts in May to get
offscreen to work and his even greater efforts to get me to understand it
without which these notes would not have been possible.

Alan Fry

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#!perl

use Mac::QuickDraw;
use Mac::Windows;
use Mac::Events;
use Mac::QDOffscreen;
require "qd.pl";

qd::SetRect(*win_r,0,0,100,100);
qd::SetRect(*bar_r,40,0,60,100);

qd::OpenPicture($win_r);
qd::ClipRect($win_r);
qd::PaintRect($bar_r);
$data = qd::ClosePicture();

open(OUT, ">:myPic");
print OUT $data;
closeOUT;
MacPerl::SetFileInfo('R*ch', 'PICT', ':myPic');

$x = substr($data, 512);
$pic = PicHandle->new($x);
$bnd = Rect->new(0, 0, 100, 100);
draw_offscreen();
foreach (@map) { print $_, "\n" }

sub draw_offscreen {
    ($origP, $origD) = GetGWorld();
    $gWorld = NewGWorld(0, $bnd);
    SetGWorld($gWorld);

    $pixMap = GetGWorldPixMap($gWorld);
    LockPixels($pixMap);
    EraseRect($gWorld->portRect);

    DrawPicture($pic, $bnd);

    for ($i = 0; $i < 99; $i++) {
        for ($j = 0; $j < 99; $j++) {
            $map[$i] .= GetPixel($j, $i)
        }
    }

    SetGWorld($origP, $origD);
    UnlockPixels($pixMap);
    DisposeGWorld($gWorld);
}

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch