        Subject: The Complete And Essential Guide To Bitmaps
       Part One: Bitplane Basics
             By: James Collett (Professor)
    A Member Of: VIRTUAL INFINITY
           Date: January 1994
         E-mail: s6005146@brookes.ac.uk
        Address: Park View Farm, Carlton, Nr Nuneaton, Warks, CV13 ODA
    Mono account: bcc


    The ST`s Screens
    ================
    Starting  with something that is known,   the resolutions & colours  of
    the ST compatible screens:

               Screen   | Resolution| Max Colours| => bits/pixel
             -----------+-----------+------------+---------------
                LOW     | 320 * 200 |     16     |       4
               MEDIUM   | 640 * 200 |      4     |       2
              HI (MONO) | 640 * 400 |      2     |       1

    From this the screen length in  bits  can be calculated,  and from that
    the length in bytes can be calculated:

                    LOW = (320 * 200 * 4) / 8 = 32000 bytes
                 MEDIUM = (640 * 200 * 2) / 8 = 32000 bytes
                   MONO =   (640 * 400) / 8   = 32000 bytes


    Introducing Bitplanes On The ST
    ===============================
    Starting with MEDIUM res, each pixel(P) needs two bits(B) as follows:-

           B1a  B1b             B31a   B31b
             \  /                  \   /
          P0, P1, P2 > P15;   P16 > P31;   P32 > P47;   and so on...
         /  \         /   \
       B0a  B0b    B15a   B15b

    These bits are not stored  consecutively.    Instead they are stored in
    groups of 16, 16 being the ST`s WORD SIZE, as follows :-

          +-----Make up P1------+             +-Make up P16--+
          |                     |             |              |
    B0a,B1a,B2a > B15a;   B0b,B1b > B15b;   B16a > B31a;   B16b > B31b;
    ~~~~~~~~~~~~~~~~~~    ~~~~~~~~~~~~~~    ~~~~~~~~~~~    ~~~~~~~~~~~
     Bitplane 0            Plane 1         Plane 0        Plane 1
      (Block 0)           (Block 0)       (Block 1)      (Block 1)

    A group of 16 pixels lower  bits  are all stored together,  followed by
    that groups  higher  bits,   and  each  'group  of  bits'  is  called a
    BITPLANE.  Each  group  of  pixels  (called  a  BLOCK) consists of  two
    bitplanes  in  Medium res.

    Moving onto LOW res, and again starting with the pixels:

                    P0 > P15;   P16 > P31;   and so on...

    For this case each pixel needs 4  bits(B),   namely a > d,  and thus  4
    bitplanes are needed as follows:

                                  All  make  up  P15
                       +-------------+-------------+-------------+
                       |             |             |             |
    (Block 0)  B0a > B15a;   B0b > B15b;   B0c > B15c;   B0d > B15d;
    (Block 1)  B16a > B31a;  B16b > B31b;  B16c > B31c;  B16d > B31d;  ...
               ~~~~~~~~~~~   ~~~~~~~~~~~   ~~~~~~~~~~~   ~~~~~~~~~~~
                 Plane 0       Plane 1       Plane 2       Plane 3

    Each  group of 16 pixels (called  a  BLOCK) consists of four  bitplanes
    in Low res.

    In MONO,  as there is only 1  bitplane  (i.e. 1 bit/pixel), coding is a
    lot less  complex  as  "P0>P15;   P16>P31"  is  simply  represented  by
    "B0>B15;  B16>B31".   Each  block  consists   of only one  bitplane  in
    Mono,  and  therefore a block is identical to a bitplane.


    Bitmaps and Blocks In General
    =============================
    On a machine with a PLANE SIZE of  16  bits,  such as the ST, the first
    16  pixels  (P0 to P15)  are  stored  in  the first  block  (block  0),
    the  second  16 pixels  (16  to  32)  are  stored  in  the second block
    (block  1),  and so on.   On a  machine  with  a plane size of 16 bits,
    the Pth pixel is  in block P DIV  16.    On a machine with a plane size
    of S bits,  the  Pth  pixel is in block P DIV S.

    The total number of blocks can  be  found by dividing the total  bitmap
    length (in bits) by the size of each block (in bits):

                     Bitmap length   Total pixels * Number of planes
      Total blocks = ------------- = -------------------------------
                      Block size      Plane size * Number of planes

    The number of blocks for the three ST compatible screens are:

                   LOW = 256000 / (16 * 4) =  4000 blocks
                MEDIUM = 256000 / (16 * 2) =  8000 blocks
                  MONO = 256000 / (16)     = 16000 blocks


    Pixels And Planes In General
    ============================
    In order to write to or read  from  a pixel,  the block where the pixel
    is stored must be determined.  As  already  discussed, the Pth pixel is
    in  block P DIV S on a machine with a plane size of S bits.

    A pixel`s colour is determined by one  bit in every plane, where planes
    count from 0 to N-1 (inclusive) on a machine with N planes:

                               All  make  up  P63
                  +-------------+-------------+-------------+
                  |             |             |             |
          B48a > B63a;  B48b > B63b;  B48c > B63c;  B48d > B63d;
          ~~~~~~~~~~~   ~~~~~~~~~~~   ~~~~~~~~~~~   ~~~~~~~~~~~
            Plane 0       Plane 1       Plane 2       Plane 3

    On a machine with a plane size of  S  bits, the POSITION (of the bit in
    each  plane) for the Pth pixel  would  be  the  (P MOD S-1)th  bit,  if
    the  machine started counting  from  the  least  significant bit (right
    end).  As  the  machine  starts  from the most  significant  bit  (left
    end),  the  POSITION  for the Pth pixel is actually the (S-P MOD S-1)th
    bit in  each  plane, on a machine with a plane size of S bits.


    And Finally: Feedback
    =====================
    All comments,  queries,  suggestions,   ideas,  error / incompatibility
    reports,  etc are welcome  -  both  positive  and negative.   My E-mail
    and  postal addresses are previously listed.


                                   ---END---
