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