TweetFollow Us on Twitter

File Package
Volume Number:1
Issue Number:7
Column Tag:MacPascal

Standard File Package, unpack & copy the bits

By Alan Wootton

Here is a program to view MacPaint documents from MacPascal (like “showpage”). I use the File Manager, the Standard File Package, the toolbox procedure UnPackBits, and QuickDraw’s CopyBits to display the image loaded in memory.

In MacTutor #5, Gary Palmer asked about loading MacPaint files in MacPascal. Well, I remembered once running across a description of MacPaint docs by Bill Atkinson. After some digging, I found the description in part of a mailing from tech support to developers dated Dec. 1983.

Atkinson explains that the resource fork is not used and that a 512 byte header precedes the packed bitmap data. The header is an array of patterns and some empty space. To simply view the picture we can ignore the header. The MacPaint ‘page’ is 720 lines of 72 bytes each. This is almost 51K and since we wish to load this into memory, a FatMac will probably be required. Atkinson gives a short routine to decode the data into a page. He uses UnpackBits, which is on page 7 of ToolBox Utilities.

Reading the data was a problem. I tried to use the file I/O routines provided with MacPascal. One must either read data byte-at-a-time, which takes forever, or, if you read large blocks, you can never get the last partially filled block. At this point I decided that Real Programmers use the low level File Manager routines. In other words PBOpen, PBRead, and then PBClose.

Another MacPascal shortcoming is the GetOldFile function. Once again, Real Programmers go straight past the limitations of the language and consult the bible (Inside Mac). We shall use the Standard File Package.

The Standard File Package

MacPascal’s function GetOldName will only select files of type TEXT. To select MacPaint docs it will be necessary to call the Standard File Package directly (OldPaintName in following program). On page 30 of Packages in “Inside Macintosh”, the procedure SFGetFile is described. To call it, use trap $A9EA, which is Pack3, and be sure to push a 2, the ‘selector’, on the stack last. You must type in the SFReply record on page 25. [For a complete description of SFReply, see the Assembly Column in this issue.] Since our type list will just be one type (PNTG) it will not be necessary to declare an array. Pass nil for filefilter and dlghook. There is an argument to pass a prompt string that Inside Mac says is historical only. I find that it works fine.

Low Level File Manager

The filecall interface in the following program simulates the PB calls of the File Manager. This is an example of the OS call I presented in MacTutor #6. [Please refer to issue #6 for details on calling OS routines from Pascal.] Before you can use the the PB routines, you must type the lengthy Parameter Block declarations. There are four variant parts to this record. I have only used the ioParam part here because that is all that was needed in this example. You should type the whole thing and save it somewhere for later use. Note that 8 bit types do not come out right, so one must take care that the declaration has the correct length. The routine descriptions (starting on page 31 of the File Manager) give a nice list of those parameters that must be set before calling (assembly programmers note: in some places ioRefNum is listed as 22, but the correct number is 24). You should carefully check that each parameter is set correctly before calling. The exception is ioCompletion, which can be ignored since we will be making synchronous calls. If ioNamePtr is not nil then the File Manager assumes that its value is the address of a string. This could badly mess up memory. In MacPascal, declared variables are cleared to 0’s so you can get away with this, but later, when you compile, nasty bugs pop up (this actually happened to me).

UnPackBits

UnPackBits is actually very simple. I have complicated things by not reading all the data into memory at once. The scheme used is to read 1024 bytes into memory (skipping the header) and when the first 512 bytes are used I slide the upper 512 bytes down and add 512 bytes onto the end. The program unpacks 72 bytes at a time for 720 lines. For faster operation you can unpack 4 lines at a time. Change 720 to 180 and 72 to 288.

After the page array is filled, it would be nice to take a look at it. Copybits is used to move the image onto the drawing window. The nice thing about copybits is that it will scale the image to fit. Simply setup a Quickdraw Bitmap record for page, set destrect to the desired size, and call copybits as shown. If you change 144 to 576 and 180 to 720 you can see the painting full size.

To write a MacPaint document (not done in this program) it is necessary to know the format of the header. The header is a 4 byte version number (default = 2) and then 38*8 = 304 bytes of patterns and then 204 unused bytes for a total of 512. However, Atkinson’s example writes 512 zeros for a header so I guess that would work. To pack the data you use the reverse of the unpack procedure. Things can be made simpler though because you can use one destination buffer and then write it out after every PackBits.

It is my contention that everything in Inside Mac can be demonstrated and used from MacPascal (except things like Vertical Retrace routines). If there is anything that you have trouble with and would like to see in MacTutor, write a letter to MacTutor and I will see if there isn’t a way to do it.


program Read_MacPaint_doc;{by Alan Wootton 4/85}
           { This program reads a MacPaint file and shows            
        the picture 1/4 size in Drawing }
 uses
  Quickdraw2;
 type
  ptr = ^integer;
  OStype = longint;

  SFReply = record { this is used by the standard file               
                           package, see Packages }
    good : integer;
    ftype : OSType;
    vrefNum : integer;
    version : integer;
    fname : string[63];
   end;

{ Parameter Block information contained in File Manager 
chapter of Inside Macintosh.  Note that MacPascal won’t do 8 bit fields 
right }

  ParamBlkPtr = ^ParamBlockRec;
  ParamBlockRec = record
{ data structure of File Manager }
    qLink : Ptr;
    qType : integer;
    ioTrap : integer;
    ioCmdAddr : ptr;
    ioCompletion : ptr;
    ioResult : integer;
    ioNamePtr : ^str255;
    ioVrefNum : integer;
{  case ParamBlkType of  ...  ioParam:  }
    ioRefNum : integer;
{       ioVersNum : byte;  }
    ioPermssn : integer;{ byte }
    ioMisc : ptr;
    ioBuffer : ptr;
    ioReqCount : longint;
    ioActCount : longint;
    ioPosMode : integer;
    ioPosOffset : longint;
   end;

  diskBlock = packed array[0..511] of char;

  paintPage = array[0..35, 0..719] of integer;
{  576*720 bitmap  = 72 bytes * 720 lines =  51,840           bytes }




 var
  reply : SFReply;{ used by OldPaintName, declared here              
                   to access vRefNum }
  filename : str255;
  fileblock : ParamBlockRec;
  buff : array[0..1] of diskblock;{ 1K }

  Page : paintPage;{ Huge variable, almost 52K  }

  srcPnt : ^diskBlock;
  destPnt : ^paintPage;

  lines, err : integer;

  curport : Grafptr;
  pageBits : bitmap;
  destRect : Rect;


{ common OS trap code, could be done with ‘Generic’ call}
{see MacTutor Vol. 1 No. 6 for assembly source code}
 function filecall (Pb : ParamBlkPtr;
         trap : integer) : integer;
  var
   access : array[0..12] of integer;
   d0, a0 : longint;
 begin
  stuffHex(@access, ‘2848548C41FA000C309F245F265F20522013FFFF224826804ED4’);
  a0 := ord(pb);
  inlineP($4E75, @d0, @a0, trap, @access);
  filecall := loword(d0);
 end;

{ The following File Manager calls work just like   }
{ those described in Inside Macintosh for the Lisa Pascal Workshop.  
}
{ Except that the async parameter is a dummy; all calls are sync }

 function PBOpen (Pb : ParamBlkPtr;
         async : boolean) : integer;
 begin
  PBOpen := filecall(pb, $A000);
 end;

 function PBClose (Pb : ParamBlkPtr;
         async : boolean) : integer;
 begin
  PBClose := filecall(pb, $A001);
 end;

 function PBRead (Pb : ParamBlkPtr;
         async : boolean) : integer;
 begin
  PBRead := filecall(pb, $A002);
 end;


{  same as OldFileName, only for MacPaint docs }
 function OldPaintName (prompt : str255) : str255;
  var
   where : point;
   typelist : longint;
 begin
  reply.good := 0;
  typelist := $504E5447;{ ‘PNTG’ }
  setPT(where, 100, 100);
  inlineP($A9EA, where, @prompt, nil, 1, @typelist, nil, @reply, 2);
{ $A9EA is pack3 ,  2 is SFGetFile, see Packages  }
  if reply.good <> 0 then
   OldPaintName := reply.Fname
  else
   OldPaintName := ‘’;
 end;


{  This procedure moves buff[1] down onto buff[0], adjusts srcPnt to 
point at it’s same data and tacks more data on end in buff[1].     }
 procedure Refill_Buffer;
 begin
  blockmove(@buff[1], @buff[0], 512);
  srcPnt := pointer(ord(srcPnt) - 512);
  fileBlock.ioBuffer := @buff[1];
  fileBlock.ioReqCount := 512;
  fileBlock.ioPosMode := 0;
  err := PBRead(@fileBlock, false);{ fill buff[1] }
 end;


begin { main }
 ShowDrawing;
 fileName := OldPaintName(‘ Select Painting to examine’);
 while filename <> ‘’ do
  begin
   fileBlock.ioVrefNum := reply.vrefnum;
   fileBlock.ioNamePtr := @fileName;
   fileBlock.ioPermssn := 1;{ read only, version number              
                                   is in upper byte and is 0 }
   fileBlock.ioMisc := nil;
   if PBOpen(@fileBlock, false) = 0 then
    begin
     refill_buffer;{  first block is skipped }
     refill_buffer;{   fill 1 }
     refill_buffer;{ 1 onto 0, refill 1 }

     srcPnt := @buff[0];{ <-- pointer to start of data }
     destPnt := @Page;{ <----  pointer to Page bitmap }

{------> This is the part that actually decodes the packed MacPaint data 
<------}
{ $A8D0 is UnPackBits, see page 7 in Toolbox Utilities   }
{ srcPnt and destPnt are updated by UnPackBits so use @}
     for lines := 1 to 720 do{ 720 lines in paint doc }
      begin
       inlineP($A8D0, @srcPnt, @destPnt, 72);
                                     { UnPackBits for 72 bytes }
       if ord(srcpnt) >= ord(@buff[1]) then
        Refill_Buffer; { if src no longer in buff[0] }
      end;{ of 720 lines }

     if PBClose(@fileBlock, false) <> 0 then
      writeln(‘close error’);

{  now show drawing  in current port  (drawing window) }
     GetPort(curPort);

 
{  set up bitmap record }
    pagebits.baseaddr := pointer(ord(@page));
    pagebits.rowbytes := 72;
    setRect(pagebits.bounds, 0, 0, 576, 720);

     setRect(destRect, 0, 0, 144, 180);
{ 144,180 =1/4 normal, image will be squashed to fit }

     copyBits(pagebits, curport^.portbits, pagebits.bounds,          
           destRect, srcCopy, nil);
     moveto(4, 190);
     drawstring(‘Click to open another MacPainting’);
     repeat
     until button;

    end;{ if no Open error }
   fileName := OldPaintName(‘ Select another Painting ?’);
  end;{ if name<>’’ loop }
end.
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Whitethorn Games combines two completely...
If you have ever gone fishing then you know that it is a lesson in patience, sitting around waiting for a bite that may never come. Well, that's because you have been doing it wrong, since as Whitehorn Games now demonstrates in new release Skate... | Read more »
Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »

Price Scanner via MacPrices.net

B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for up to $150 off Apple’s new MSRP, starting at only $849. Free 1-2 day delivery is available to most US... Read more
M2 Mac minis on sale for $100-$200 off MSRP,...
B&H Photo has Apple’s M2-powered Mac minis back in stock and on sale today for $100-$200 off MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $... Read more
Mac Studios with M2 Max and M2 Ultra CPUs on...
B&H Photo has standard-configuration Mac Studios with Apple’s M2 Max & Ultra CPUs in stock today and on Easter sale for $200 off MSRP. Their prices are the lowest available for these models... Read more
Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.