home *** CD-ROM | disk | FTP | other *** search
/ TCE Demo 2 / TCE_DEMO_CD2.iso / demo_cd_.2 / mags / stosser / stoser05.arj / stoser05.msa / A / 7.PNE < prev    next >
Text File  |  1987-04-22  |  7KB  |  304 lines

  1.           STOS BITS 'N PIECES
  2.  
  3.                    by
  4.  
  5.         Martin Cubitt July 1993
  6.  
  7.  
  8.       The PACK command explained.
  9.  
  10.  The PACK and UNPACK commands must be
  11. two of the most commonly used commands
  12. from any extension. Issues 5 and 6 of
  13. the STOS Newsletter featured an article
  14. by Francois Lionet explaining how they
  15. work.
  16.  
  17.  
  18.  This article extracts most of the
  19. information from that article. No copy-
  20. right is expressed. Besides, Francois 
  21. would probably like you to know how
  22. clever (and suprisingly simple) the com-
  23. paction method is.
  24.  
  25.  How many parameter does the PACK com-
  26. mand have?
  27.  
  28.  How many of you said two? Well, yes but
  29. that is not the whole story.
  30.  
  31.  You may use two parameters (for the
  32. source and destination banks) or nine 
  33. parameters!
  34.  
  35. The STOS compactor accessory uses all 
  36. nine to get the best results of compact-
  37. ion.
  38.  
  39.  The full syntax of the pack command is:
  40.  
  41.  pic_length=PACK source, destination, 
  42.  resolution, flags, height of block,
  43.  start in x, start in y, size of x, size
  44.  of y
  45.  
  46.  Wow!
  47.  
  48.  So what do they all do?
  49.  
  50.  SOURCE is the address of the screen to
  51. be packed, as per normal.
  52.  
  53.  DESTINATION is the bank or address
  54. where the compacted data should be
  55. stored.
  56.  
  57.  RESOLUTION tells STOS the mode the pic-
  58. ture has been created in. Obviously this
  59. should be what ever MODE you are in. 
  60. This can be retrieved by M=mode.
  61.  
  62.  FLAGS set up information about the
  63. colour palette.
  64.  
  65.  Bit 0: If on, the entire palette will
  66. be set to zero before the picture is 
  67. unpacked. If the bit is clear the pal-
  68. ette will not be altered, ideal when un-
  69. packing to a bank. Ideally this flag 
  70. would have been given in the UNPACK com-
  71. mand so you can decode this at UNPACK
  72. time. It is possible to change the flag 
  73. of a compacted picture, details later.
  74.  
  75.  Bit 1: If on, the palette will change
  76. to that of the screen to be unpacked. If
  77. the bit is clear then the palette will
  78. be unaltered.
  79.  
  80.  The default for both of these bits is 1
  81. (on).
  82.  
  83.  HEIGHT OF BLOCK is covered later, it
  84. sets the packing block size in Y lines.
  85.  
  86.  START IN X states the top left x co-
  87. ordinate from where the picture will be
  88. packed. The value given is the number of 
  89. words (2 bytes), so for low resolution 0
  90. would be far left, 10 the middle and 19
  91. the far right.
  92.  
  93.  START IN Y states the top y co-ordinate
  94. of the picture.
  95.  
  96.  SIZE OF X indicates how many words (2
  97. bytes) across from the start x will be
  98. packed, ie. the width.
  99.  
  100.  SIZE OF Y states the number of packing
  101. squares in Y to process. The full length
  102. of the picture area to pack is therefore
  103. square_size * size_in_y.
  104.  
  105.  When using the more detailed version of
  106. the command you must set ALL nine param-
  107. eters, not just the ones you want. 
  108. Otherwise the command will be in error.
  109.  
  110.  
  111.  The compaction method used by STOS is
  112. reasonably simple. I will not go in at
  113. length about some of the more technical 
  114. details of how, but give you a basic
  115. idea. I will try to keep it to a level
  116. where most of us get the picture (no pun 
  117. intended).
  118.  
  119. For each byte of the screen (32,000 of
  120. them) we have a single corresponding bit
  121. in an index table. Therefore the index
  122. table will be 32,000 bits long. That
  123. works out to be 4,000 bytes. Another
  124. table is required to store the bytes.
  125.  
  126.  The compaction routine starts off by
  127. examining the bytes from the original
  128. picture systematically. If the current 
  129. byte is different from the previous one,
  130. or it is the first byte, it sets the 
  131. corresponding bit within the index table
  132. to 1 and stores the current byte into
  133. the bytes table. If however the current
  134. byte is equal to the previously examined
  135. byte then the compactor sets the bit in 
  136. the index to 0. Okay so far?
  137.  
  138.  So for the following bytes...
  139.  
  140.  10
  141.  10
  142.  12
  143.  12
  144.  12
  145.  13
  146.  12
  147.  10
  148.  10
  149.  
  150.  The bits table would be 
  151.  
  152.  101001110
  153.  
  154.  and the bytes table
  155.  
  156.  10
  157.  12
  158.  13
  159.  12
  160.  10
  161.  
  162.  Okay. So how is this unpacked?
  163.  
  164.  It firsts read the current bit from the
  165. index. If set to 1 then the next byte is
  166. pulled off the bytes table, in my exam-
  167. ple this would be 10. If the bit is not
  168. set, then the previous value is used.
  169.  
  170.  The retrieved byte is poked into the
  171. picture and the loop continues until the
  172. last bit is reached.
  173.  
  174.  If the screen was blank then the bit
  175. table would start with a 1 and then have
  176. 31,999 0's.
  177.  
  178.  The byte table would have 0 and that is
  179. all.
  180.  
  181.  So the compacted picture would be 4,000
  182. + 1 = 4,001 bytes.
  183.  
  184.  That is not so good. An empty picture
  185. should not pack into 4K!
  186.  
  187.  So Francois' routine cleverly packs the
  188. index! This is not packed at bit level
  189. but at byte level. So the new bit index 
  190. table will be 4,000 / 8 = 500 bytes. So
  191. the blank picture will be 500 bytes plus 
  192. 2 bytes for the second bytes tables ($80
  193. and $00) and one byte for the original
  194. bytes table, giving 503 bytes. A lot 
  195. better than 4,000 bytes.
  196.  
  197.  Francois did try packing the second
  198. index table but there was generally no
  199. saving. Besides, the process was slowed
  200. down.
  201.  
  202.  So the unpacker actually has to first
  203. rebuild the first index table using the
  204. second index table! Then the first one
  205. can be unpacked.
  206.  
  207.  This principle can be used for non-
  208. screen banks. It is probably the most
  209. simple (without being rubbish) compact-
  210. ion methods around. For this reason it
  211. is fast.
  212.  
  213.  
  214.  As you may know, the ST's screen is
  215. divided into what is called bit planes.
  216. In low resolution there are four bit 
  217. planes. The combination of all four
  218. gives a four bit value which represents
  219. the colour of the pixel at that position
  220.  So if 
  221.  
  222.  plane 0 - 0
  223.  plane 1 - 0
  224.  plane 2 - 1
  225.  plane 3 - 0
  226.  
  227.  the colour is %0100 = 4
  228.  
  229.  The compactor explores memory plane by
  230. plane. Therefore it loops four times in
  231. low resolution (2 in medium and 1 in
  232. high).
  233.  
  234.  The compactor accessory tries the block
  235. sizes 1,2,4,8,16 and 32. The height of
  236. the block contains the size in Y of 
  237. blocks (1 being line by line).
  238.  
  239.  
  240.  A packed bank creates a header as
  241. follows:
  242.  
  243.  Offset Size Value Description
  244.   0     L $06071963 ID (Francois' DOB)
  245.   4     W  0/1/2  Resolution
  246.   6     W         X start (in words)
  247.   8     W         Y start (in lines)
  248.  10     W         X size  (in words)
  249.  12     W         No. blocks
  250.  14     W         - Unused -
  251.  16     W         Y size of squares
  252.                    (line)
  253.  18     W         Flags
  254.  20     L         Offset to bytes(2)
  255.                    table
  256.  24     L         Offset to index(2)
  257.                    table
  258.  28     W W W...  palette
  259.  70               start of bytes(1)
  260.                    table
  261.  
  262.                   start of index(2)
  263.                    table
  264.  
  265.                   start of bytes(2)
  266.                    table
  267.  
  268.  
  269.  So you could actually doke (not poke as
  270. it is as double byte value) the flags to 
  271. change them. So to change bit 0 to 1 you
  272. may do
  273.  
  274.  doke start(bank)+18, 
  275.  deek(start(bank)+18) or 
  276.  %01
  277.  
  278.  and to set bit 1 to 1 use
  279.  
  280.  doke start(bank)+18,
  281.  deek(start(bank)+18) or
  282.  %10
  283.  
  284.  To set off bit 0 use
  285.  
  286.  a=deek(start(bank)+18)
  287.  a=bclr(a,0)
  288.  doke start(bank)+18,a
  289.  
  290.  and to set off bit 1 use
  291.  
  292.  a=deek(start(bank)+18)
  293.  a=bclr(a,1)
  294.  doke start(bank)+18,a
  295.  
  296.  
  297. That's about it. A little confusing but
  298. the main thing is that it works and
  299. works well!
  300.  
  301.  
  302.  CUBITTM/STOS0005/190793
  303.  
  304.