Classic Computer Magazine Archive COMPUTE! ISSUE 71 / APRIL 1986 / PAGE 10

Readers Feedback

The Editors and Readers of COMPUTE!



IBM PUT And GET
I own a TI-99/4A and an IBM PCjr. Lately, I've been trying to convert some programs from TI to IBM. I have only one problem: the PUT and GET graphic statements in the IBM system. I really don't understand them. Could you show me a way to make an image and move it?
Billy Mobley

First, be aware that IBM BASIC has two types of GET and PUT statements: one for graphics and another for random files. The syntax for each type is different, so be sure you're using the graphics type. GET grabs the screen image within a specified rectangle and stores a copy of it in an array. PUT does just the opposite, putting the image from an array back onto the screen.
    Several important rules apply to PUT and GET. Before using either command, you must be in a graphics mode (SCREEN 1, for example); neither PUT nor GET works on a text screen. The array that you GET a shape into must be a one-dimensional numeric array dimensioned to the proper size. Finally, you must GET before you can PUT.
    The most difficult task is deciding what size to dimension the array. If the array is too small, it can't hold the graphics image, and the program won't work. The simplest solution is to try a large size like DIM A(500). It won't hurt to dimension it larger than necessary, but this method wastes memory. Here's a more efficient formula that tells you the minimum required size for the array:

INT((4+INT((x*res+7)/8)*y)/prec)

    In this formula, the variable x represents the width of the image in pixels; y is the height of the image; res is 1 for high resolution and 2 for medium resolution; and prec is the precision of the array (2 for integer, 4 for single precision, and 8 for double precision).
    GET must be followed by the screen coordinates of two opposite corners of the rectangular image, and the name of the array. For example, GET (0,0)-(19,29),A grabs a 20 X 30 pixel image at the top-left corner of the screen and stores it in array A. (Of course, you must first have an image on the screen. This can be done with DRAW.) With a high-resolution screen and a single-precision array, the formula above gives 23, so the dimension statement would be DIM A(23).
    PUT is followed by the coordinates of the location on the screen where the top-left corner of the image is placed, then the name of the array, and an optional parameter for special effects. Five special effects are available: PSET, PRESET, AND, OR, and XOR. If no special effect is specified, XOR is assumed.
    PSET displays the image exactly as it appeared when GET was used. PRESET displays a negative image. AND displays only those parts of the image that overlap an image already on the screen. OR superimposes the image onto an image already on the screen. XOR is a combination of AND and PRESET, reversing only those parts of the image that overlap an image already on the screen. The best way to understand exactly what these special effects do is to try them yourself. Using our example, PUT (200,100),A,PSET displays the image stored in the A array in the center of the screen.
    The operation of XOR may seem strange, but it's handy for animation. When you PUT using XOR twice in the same position, the screen is restored unchanged. This allows you to move an image over a background image, giving a 3-D effect. Animation with XOR is a three-step process: PUT the image on the screen with XOR, calculate the new position, PUT the image in the old position a second time to erase it. By performing these steps repeatedly, the image seems to move. The following program moves a ball across the screen.

10 SCREEN 1
20 DIM A(113)
30 CIRCLE (20,20),20
40 PAINT (20,20)
50 GET (0,0)-(40,40),A
60 CLS
70 FOR C=1 TO 100
80 PUT (X1,Y1),A 'display ima
   ge
90 X2=X1+1:Y2=Y1+1 'calculate
    new position
100 PUT (X1,Y1),A 'erase imag
    e
110 X1=X2:Y1=Y2 'old=new
120 NEXT 'repeat