TweetFollow Us on Twitter

Reading Paint Files
Volume Number:1
Issue Number:10
Column Tag:Basic School

"Reading Paint Files"

By Dave Kelly, Hybrids Engineer, MacTutor Editorial Board

This month we will explore how to read MacPaint files via MSBASIC. We will start by generally dissecting a paint file.

The 8"X10" MacPaint pictures that we are used to seeing are represented by 576 X 720 pixels. That's 414,720 pixels which would require 51,840 bytes to store directly to disk. The first 512 bytes (first block) contain the brush and fill pattern information. Fortunately, the bit map is compressed a row at a time in order to conserve disk space or we really wouldn't have much room to do anything else. Therefore there is a block of encoded pixels for each of the 720 rows. There are two flavors of encoding these entries, a straight bitmap and a run-length encoded sequence.

PATTERN BYTES

The first 512 bytes start with 00 00 00 02. Then after these four bytes are the 38 patterns that you see when you open your MacPaint document. The patterns may be edited with the pattern editor provided in MacPaint and your custom patterns may then be used as needed. Each of the 38 patterns are represented by 8 bytes each where each byte represents a row in the pattern grid. The pattern rows are mapped from left to right and top to bottom. The order of the patterns is the same as appear at the bottom of the MacPaint screen. The patterns are not encoded. The remainder of the first 512 bytes are filled with zeros.

BITMAP FLAVORS

Both flavors start with a one byte count. In the bitmap flavor the count indicates the number of pixel bytes which will follow minus one. As an example of this see figure 1 (Flavor 1) below:

Fig. 2 Program Output

In the example, the pattern 0100101011010010 (where a 1 represents a black pixel and a 0 represents a white pixel) would be encoded as 014AD2. The first byte (01) is the count minus one, therefore the count is 2 bytes. The following 2 bytes represent the data (4AD2).

The second flavor is composed of a sequence of 8 bits which is repeated. The count indicates the number of times which the sequence is repeated. For the second flavor the count byte (first byte) is set negative. Negative binary numbers have the first bit set to indicate that it is negative. In this case, the first byte is the absolute value of the of the first byte minus one. For example FDFF would represent 4 bytes of "FF", or "FF FF FF FF". FD is 11111101. The absolute value (negative) is 00000010 = 3. Therefore the count is 3+1 = 4 bytes. A row that is entirely blank is represented by B900 where B9 = -71. The inverse is 71. Add one to get the count (71+1=72 bytes of the 8-bit pattern "00". There are 72 bytes (72X8 = 576 pixels) in one row of a MacPaint document. The pattern may be any combination of ones and/or zeros. This would be most useful in coding patterns of bits which are repeated.

The tricky part is determining which of the flavors to use. In reading the file this is not too much of a problem because we can always look at the first bye. If it is negative then it is of the second flavor. Writing a MacPaint file is not quite as simple but can be done if you plan ahead.

BASIC PROGRAMS

I'm sure that you have probably seen the public domain program which will read the top left corner of a MacPaint document and display it on the screen. The program below called Paint Pokes will read any MacPaint file and display it on the screen using the Prof. Mac Pokes procedures that I have shown before (see Apr. 85 MacTutor, pg. 34). The problem is that this method of printing on the screen is extreamly slow. I would still like to convert the paint file (or a portion of it) into a BASIC PICTURE$ which could be printed to the screen much quicker, but so far I have not been able to understand how PICTURE$ (or the clipboard for that matter) is encoded. The problem is that BASIC does not recognize anything that has been poked onto the screen. Because of this the screen GET and screen PUT won't work for saving the picture via BASIC for later use. Anyone have the answer??

Paint Pokes asks for the paint file to be displayed and then reads the file and pokes it onto the screen. Press the mouse button at the end to continue when the display has finished (or when you have finished looking at it). The program then returns to BASIC.

Pattern Editor is a BASIC program which allows you to edit the patterns used in MacPaint. The pattern editor built into MacPaint is really more useful, but the BASIC pattern editor demonstrates the format of the patterns stored in a paint file. To use the pattern editor run the program and select the desired menus. First load in a Paint document. The entire document is read in so that it can be completely written back to disk again a with modified set of patterns. Select 'Display Pattern' to show the patterns as they are now defined. Choose 'Edit Pattern' to edit a particular pattern. Use the mouse to select the desired pattern and then a grid will appear and allow you to change the bits. To change the pattern as you are editing it choose 'Display Pattern' and the patterns will be re-drawn using your new pattern. If you display the pattern, the pattern will now be changed for good, you can't undo what has been displayed unless you want to re-edit the same pattern back to the original pattern. You may revert back to the original pattern set by loading the paint document again or you may want to save the changes to a new Paint document.

Thanks go to Bob Denny for his analysis of MacPaint file format.

'    Paint Pokes
'    By Dave Kelly
'    ©MACTUTOR 1985

start=108288!     'Set up beginning screen addr.
ending=130175!      'Set up ending screen addr.
mac512=(512-128)*1024    'Set up addr. for 512K Mac
IF FRE(0)>100000! THEN start=start+mac512
ending=ending+mac512
screen=start
WINDOW 1,"",(0,0)-(512,342),3
x$=FILES$(1,"PNTG")  'Get a MacPaint file
IF x$="" THEN quit   'No selection,  quit
HIDECURSOR
OPEN x$ FOR INPUT AS #1
    FOR i%= 1 TO 512  'Disgard the first 512 bytes
        x$=INPUT$(1,#1)
    NEXT i%
    pixel%=0
    WHILE NOT EOF(1)
    count=ASC(INPUT$(1, #1))
    IF count<&H80 THEN GOSUB type1 ELSE GOSUB type2  'Check if high bit 
is set
    WEND
CLOSE #1

wait.for.mouse.click:
    pause:IF MOUSE(0)>0 THEN  quit
    GOTO pause

Quit: MENU RESET:SHOWCURSOR
WINDOW 1,"Output Window",(2,40)-(510,340),1
END

Pokescreen:
    IF pixel%>511 THEN RETURN
    POKE screen,ASC(byte$)
    screen=screen+1
    IF screen >=ending THEN wait.for.mouse.click
    RETURN


type1:  'first flavor
    FOR i% = 1 TO count+1
        byte$=INPUT$(1,#1)  'Read a byte
        GOSUB Pokescreen
        pixel%=pixel%+8 'Count pixels printed
    NEXT i%
    IF pixel%>=576 THEN pixel%=0   'line is full
RETURN

type2:  'second flavor
    byte$=INPUT$(1,#1)    ' Read a byte
    FOR i%=1 TO (&H101-count)
        GOSUB Pokescreen
        pixel%=pixel%+8 'Count pixels printed
    NEXT i%
    IF pixel%>=576 THEN pixel%=0  'line is full
RETURN


***************************************
'    Pattern Editor
'    By Dave Kelly
'    ©MACTUTOR 1985

start=108288! 'Set up beginning screen addr.
ending=130175!      'Set up ending screen addr.
mac512=(512-128)*1024    'Set up address for 512K Mac
IF FRE(0)>100000! THEN start=start+mac512:ending=ending+mac512
screen=start:editor%=0:NewYork=2
Bold=1:Plain=0: DEFINT i,j,k
DIM pattern%(512), k$(1),Bound0%(72),  Bound1%(72),Bound2%(72), 
 Bound3%(72),Bstatus%(72),  
WINDOW 1,"",(2,24)-(510,150),3
FOR i%=1 TO 5
    MENU i%,0,0,""  'Erase old menus
NEXT i%
MENU 1,0,1,"File"
MENU 1,1,1,"Load Paint patterns"
MENU 1,2,0,"Save Paint patterns"
MENU 1,3,1,"Quit"
MENU 2,0,0,"Patterns"
MENU 2,1,1,"Display Patterns"
MENU 2,2,1,"Edit a Pattern"

ON MENU GOSUB menu.selection
MENU ON
pause:GOTO pause

menu.selection:
    menunumber=MENU(0)
    menuitem=MENU(1):MENU:MENU OFF
    IF menunumber=1 THEN ON menuitem GOSUB read.paint.file,    
 write.paint.file, Quit
    IF menunumber=2 THEN ON menuitem GOSUB Display.patterns,   
 Edit.pattern
    MENU ON:RETURN

read.paint.file:
    x$=FILES$(1,"PNTG")    'Get a MacPaint file
    IF x$="" THEN RETURN  'No selection,                        then 
forget it.
    MENU 1,0,0:MENU 2,0,0
    HIDECURSOR
    TEXTFONT(NewYork):TEXTSIZE(14)
    TEXTFACE(Bold)
    CLS:LOCATE 4,1
PRINT"Now Reading Macpaint document.....           Please wait."
    OPEN x$ FOR INPUT AS #1 LEN= 2000
    ERASE k$
    DIM k$(LOF(1)+1)
    FOR i%= 1 TO 512
        k$(i%)=INPUT$(1,#1)
    NEXT i%
    WHILE NOT EOF(1)
        k$(i%)=INPUT$(1,#1)
        i%=i%+1
    WEND
    number.of.bytes=i%
CLOSE #1
SHOWCURSOR:CLS
MENU 2,0,1:MENU 1,0,1:MENU 1,2,1
BEEP:RETURN 

write.paint.file:
TEXTFONT(NewYork):TEXTSIZE(14)
TEXTFACE(Bold)
CLS:LOCATE 4,1
PRINT "Now writing new MacPaint document....                   Please 
wait."
x$=FILES$(0,"Choose a new filename:") 'Get a                         
                  Paint file
IF x$="" THEN RETURN  'No selection, then                            
                                                  forget it.
MENU 2,0,0:MENU 1,0,0
OPEN x$ FOR OUTPUT AS #1
    FOR i1%=1 TO number.of.bytes
        PRINT #1, k$(i1%);
    NEXT i1%
CLOSE #1
NAME x$ AS x$,"PNTG"
MENU 1,0,1:MENU 2,0,1
LOCATE 4,1:PRINT SPACE$(80)
BEEP:RETURN

Quit: MENU RESET:SHOWCURSOR
WINDOW CLOSE 2
WINDOW 1,"Output Window", (2,40)-(510,340),1
END

Display.patterns:
'Poke the display onto the screen
HIDECURSOR:MENU 1,0,0:MENU 2,0,0
a1=start+64*50+3
b1=3
'These loops poke the patterns to the screen
FOR qloop =0 TO 9 STEP 8
FOR pat% = 0 TO 18
    FOR i%=  (pat%*8)+1 TO(8*pat%)+8
        row=i%+4
        POKE a1+((row MOD 8+qloop)*64)+            (b1*pat%), ASC(k$(row))
        POKE a1+((row MOD 8+qloop)*64)+            (b1*pat%)+1, ASC(k$(row))
        POKE a1+((row MOD 8+qloop)*64)+            (b1*pat%)+2, ASC(k$(row))
    NEXT i%
NEXT pat%
NEXT qloop
a1=start+64*70+3
FOR qloop=0 TO 9 STEP 8
FOR pat% = 19 TO 38
    FOR i%= (pat%*8)+1 TO (8*pat%)+8
        row=i%+4
        POKE a1+((row MOD 8+qloop)*64)+            (b1*(pat%-19)), ASC(k$(row))
        POKE a1+((row MOD 8+qloop)*64)+            (b1*(pat%-19))+1, 
ASC(k$(row))
     POKE a1+((row MOD 8+qloop)*64)+ (b1*(pat%-19))+2, ASC(k$(row))
    NEXT i%
NEXT pat%
NEXT qloop
SHOWCURSOR:MENU 1,0,1:MENU 2,0,1
RETURN

Choose.pattern:  'Pattern% = selected pattern #
SHOWCURSOR:WINDOW 1
IF editor%=1 THEN makeselection
GOSUB Display.patterns
makeselection:
LOCATE 5,1:PRINT"Please use mouse to select pattern"
Edit.pattern:
GOSUB Choose.pattern
IF editor%=1 THEN editor
InitEditor:
WINDOW 2,"Pattern Editor",  (10,160)-(500,320),2
x=20:y=20:offsetx=0:offsety=0:editor%=1
TEXTFONT(NewYork):TEXTSIZE(14)
TEXTFACE(Bold)
LOCATE 5,1
PRINT"Please wait.... Initializing Editor."
PICTURE ON
FOR j= 0 TO 7
    FOR k=7 TO 0 STEP -1
        rectangle%(0)=y+offsety
        Bound0%((j*8)+k)=rectangle%(0)
        rectangle%(1)=x+offsetx
        Bound1%((j*8)+k)=rectangle%(1)
        rectangle%(2)=y+offsety+12
        Bound2%((j*8)+k)=rectangle%(2)
        rectangle%(3)=x+offsetx+12
        Bound3%((j*8)+k)=rectangle%(3)
        Bstatus%((j*8)+k)=0:offsetx=11+offsetx
        FRAMERECT(VARPTR(rectangle%(0)))
    NEXT k
    offsety=11+offsety:offsetx=0
NEXT j
PICTURE OFF
grid$=PICTURE$

editor:
    WINDOW 2
    TEXTFONT(NewYork):TEXTSIZE(14)
   TEXTFACE(Bold)
    LOCATE 5,1
   PRINT"Please wait for Setup of Editor."
    GOSUB Bitstatus
    ' set up new cursor
    GOSUB print.pic
    LOCATE 2,26:PRINT"Define New Pattern"
    GOSUB Print.message
    GOSUB Draw.Datapixels
    GOSUB Define
    CLS:BUTTON CLOSE 1
    TEXTFONT(NewYork):TEXTSIZE(14)
    TEXTFACE(Bold)
    LOCATE 5,1:PRINT"Please wait."
    GOSUB Set.pattern
    LOCATE 5,1:PRINT SPACE$(70)
    GOSUB Display.patterns
    RETURN

print.pic: CLS:PICTURE,grid$ 'print the grid 
    TEXTFONT(NewYork)
    TEXTSIZE(14)
    TEXTFACE(Bold)
    RETURN

Print.message:
    TEXTFACE(Plain)
    TEXTSIZE(12)
    LOCATE 4,35
    PRINT"Click              to  continue"                           
                  'This space ^^^^belongs here
    BUTTON 1,1,"OK",(310,40)-(350,80),1
    RETURN

Draw.Datapixels:
    FOR i= 0 TO 63
        IF Bstatus%(i)=1 THEN rectangle%(0)=Bound0%(i):
 rectangle%(1)=Bound1%(i):
 rectangle%(2)=Bound2%(i):
 rectangle%(3)=Bound3%(i):
 PAINTRECT(VARPTR(rectangle%(0))):
 FRAMERECT(VARPTR(rectangle%(0)))
 'end if
    NEXT i
    RETURN

mousepress:
    GOSUB getpixel   'see which pixel is selected
    IF pixel%=64 THEN  RETURN
    IF Bstatus%(pixel%)=0 THEN     Bstatus%(pixel%)=1          ELSE Bstatus%(pixel%)=0
    rectangle%(0)=Bound0%(pixel%)
    rectangle%(1)=Bound1%(pixel%)
    rectangle%(2)=Bound2%(pixel%)
    rectangle%(3)=Bound3%(pixel%)
    INVERTRECT(VARPTR(rectangle%(0)))
    FRAMERECT(VARPTR(rectangle%(0)))
    RETURN

getpixel:
    pixel%=64
    FOR i = 0 TO 63
        IF Bound0%(i)<MOUSE(2) AND Bound2%(i)>MOUSE(2) AND     
 Bound1%(i)<MOUSE(1) AND  Bound3%(i)>MOUSE(1) THEN             pixel%=i:i=64
    NEXT i
    RETURN

Define:
    WHILE DIALOG(0)<>1
    IF MOUSE(0)>0 THEN GOSUB  mousepress
    IF MENU(0)=2 AND MENU(1)=1 THEN  GOSUB Set.pattern:        GOSUB 
Display.patterns
    WEND
    BEEP:RETURN

Bitstatus: 'get status of each grid row
    FOR j=  0 TO 7
        row=j+((pattern%-1)*8)+5
        t%=ASC(k$(row))
        FOR k=7  TO 0 STEP -1
        IF t%<2^k THEN Bstatus%(j*8+k)=0
        IF t%>=2^k THEN   Bstatus%(j*8+k)=1:t%=t%-2^k
        endloop:NEXT k
    NEXT j
RETURN

Set.pattern: 'set bits to match bit status
    FOR j=  0 TO 7
        row=j+((pattern%-1)*8)+5
        t%=0
        FOR k=7  TO 0 STEP -1
            t%=(Bstatus%(j*8+k)*2^k)+t%
        NEXT k
        k$(row)=CHR$(t%)
    NEXT j
RETURN
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
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
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and 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
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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.