home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / utilities / utilsd / dithering / CDBox_Gen (.txt) < prev    next >
RISC OS BBC BASIC V Source  |  1994-02-08  |  13KB  |  467 lines

  1.  >CDBox_Gen
  2.  : Cy Booker
  3.  : 86 Church View
  4.  : Main Road
  5.  : Crockenhill
  6.  : Swanley
  7.  : Kent
  8.  : BR8 8 JW
  9.      this file contains code to do the following
  10.              1. Generate external file for dither look up table
  11.              2. Compress this file into a form that can be used by ColourDBox code
  12.              3. Tests a BASIC multi-tasking RGB cube slice using ColourTrans
  13.              4. Tests a BASIC multi-tasking RGB cube slice using LUT
  14.      NOTE
  15.              current colourdbox code can only handle nbits in {4, 5, 6}
  16.              (if nbits = 7 then need over 4Mb!)
  17.  When run, does demo of multi-tasking rgb cube using lut
  18.  This file also contains code to generate a lut for dither patterns
  19.  in 4bpp modes for (currently) the current palette (ie the standard
  20.  wimp palette).
  21.  The way this is done is to use the vdu variable GcolOraEorAddr
  22.  which contains 64 bytes of raw screen data set up as an ecf pattern
  23.  when ColourTrans_SetGCOL.
  24.  the PRM gives very poor documentation about this, but It seems my code
  25.  to extract the 4 (x4bit) colour numbers used in the dither pattern
  26.  works
  27.  Obviously can't do 8-bit colour, so use 4-bit colour
  28.              = 0, &11, &22, ..., &ee, &ff
  29.  or 5-bit
  30.              = &00, &08, &10, .., &7b, &84, ..., &f7, &ff
  31.  We then try and compact this table down
  32.  Currently we do this by observing that many MANY dither patterns repeat
  33.  due to the limited number of colours in a palette.  In fact, there
  34.  is usually less than 1024 different patterns generated, and so instead
  35.  of storing the 16-bits for each element, we store only (say) 10 bits
  36.  and use this as an offset into an actual 16-bit table of actual dither
  37.  patterns.
  38.  How to extend this to 8bpp and 16bpp?
  39.  Well 8bpp would require 32 bits (2x2 pixels) in the ancillary array,
  40.  and a btree to store the different patterns.  Actually, I would assume
  41.  that instead of storing as tl.tr.bl.br we store as tl:tl^tr.tl^bl.tl^br
  42.  (ie 8 bits + aux index) we may well reduce the table considerably.  The
  43.  reason for this is that the top 6 bits of the colour number are the
  44.  top 6 bits of a colour, and these woul very rarely change by much.
  45.  Note that this doesn't work for 4bpp, because I tried!
  46.  For 16bpp, well it probably isn't worth it as I assume the colourtrans
  47.  routine is very fast, just some shifts
  48. Debug% = 
  49. 91FilePrefixUncompressed$ = "<d>.Tmp.colourlut"
  50. :+FilePrefixCompressed$   = "<d>.Tmp.lut"
  51. ;0FilePrefixStatistics$   = "<d>.Tmp.lutstats"
  52. _TestCube_ColourTrans
  53. PROC_Create_Uncompressed_LUT(5)
  54. PROC_Compress_LUT(5)
  55. PROC_TestCube_LUT(5)
  56. _TestCube_ColourTrans
  57.  uses colourtrans to generate cube
  58.   NCells% = 13
  59.  MapXYZToRGB%(2)
  60.  MapRGBToXYZ%(2)
  61.  MapXYZToShift%(2)
  62.   ToDo% = 
  63. malloc(64*64+1)
  64. IJ  B% = 
  65. malloc(64)                             :
  66.  GcolOraEorAddr 1-705
  67.  Cells%(NCells%-1, 1)
  68.  512, 512
  69. ct_orientate(2)
  70.   Z% = 0
  71.  300, 0, 0, 516
  72.  x%, y%, b%
  73. R5    
  74.  &444 
  75. ct_orientate(Orientation% + 2)
  76. S5    
  77.  &111 
  78. ct_orientate(Orientation% 
  79. T#    z% = (y% * &ff / 512) + 0.5
  80.     z% = 
  81. min(&ff, z%)
  82. V$    Z% = z% << MapXYZToShift%(2)
  83.  Z%<>oZ% 
  84.       oZ% = Z%
  85.       
  86. redo(32)
  87. Z>      
  88. 1, 8); 
  89. "Z= "+ 
  90. (z%*100/&ff)+ "%"+ 
  91. 10, " "), 10)
  92. [        
  93. \/    
  94.  NToDo%=0 
  95.  Step%>4 
  96. redo(Step% 
  97. ]"    n% = 
  98. min(NToDo%, NCells%)
  99.  n% > 0
  100. `#      o% = ToDo% + 
  101. (Size%) - 1
  102.       
  103.  ?o%:o%+=1:
  104. b-      
  105.  o%=End% 
  106.  o%=ToDo%:
  107.  ?o%:o%+=1:
  108. c@      
  109.  o%>=End% 
  110.  1, "Bad NToDo "+ 
  111. (NToDo%)+ ", "+ 
  112.       ?o% = 1
  113.       o% -= ToDo%
  114. f%      x% = (o% 
  115.  Length%) * Step%
  116. g%      y% = (o% 
  117.  Length%) * Step%
  118. h-      X% = (x% + H%) << MapXYZToShift%(0)
  119. i-      Y% = (y% + H%) << MapXYZToShift%(1)
  120. j;      
  121.  "ColourTrans_SetGCOL", Z% 
  122.  X%,,, &100, 0
  123.       
  124.  x%, y%, Step%-1
  125.       NToDo% -= 1
  126.       n% -= 1
  127. n        
  128. ct_orientate(o%)
  129.  z%, i%, s$()
  130.  s$(2, 1)
  131. u;  s$() = "X= ", "Red  ", "Y= ", "Green", "Z= ", "Blue "
  132. v#  Orientation% = 
  133. modulo(o%, 6)
  134.   z% = o% >> 1
  135.  d% = +1 
  136.  d% = -1
  137.  i%= 0 
  138. z5    MapXYZToRGB%(i%) = 
  139. modulo(z% + (2-i%)*d%, 3)
  140. {+    MapRGBToXYZ%(MapXYZToRGB%(i%)) = i%
  141. |7    MapXYZToShift%(i%) = (MapXYZToRGB%(i%) * 8) + 8
  142. }7    
  143. 1, i%+1); s$(i%, 0); s$(MapXYZToRGB%(i%), 1)
  144. redo(32)
  145.   oZ% = -1
  146.  ***************************************************************************
  147.  ***************************************************************************
  148.  ***************************************************************************
  149. _TestCube_LUT(nbits%)
  150.  uses lut of nbits size to show multi-tasking cube generation
  151.   NCells% = 13
  152. =  N% = 8 - nbits%                       :
  153.  #bits not used
  154. ?  lut = 
  155. _load_file(FilePrefixCompressed$+ 
  156. (nbits%), &ffd)
  157.  lut=0 
  158.  1, "Can't load lut"
  159.  MapXYZToRGB%(2)
  160.  MapRGBToXYZ%(2)
  161.  MapXYZToShift%(2)
  162.   Array% = lut + 12
  163.   Table% = lut + lut!0
  164.   NBits% = lut!4
  165.    Mask%  = (1 << NBits%) - 1
  166.   ToDo% = 
  167. malloc(64*64+1)
  168. J  B% = 
  169. malloc(64)                             :
  170.  GcolOraEorAddr 1-705
  171.  Cells%(NCells%-1, 1)
  172.  512, 512
  173. orientate(2)
  174.   Z% = 0
  175.  300, 0, 0, 516
  176.  x%, y%, b%
  177. 2    
  178.  &444 
  179. orientate(Orientation% + 2)
  180. 2    
  181.  &111 
  182. orientate(Orientation% 
  183. #    z% = (y% * &ff / 512) + 0.5
  184.     z% = 
  185. min(&ff, z%)
  186. ,    Z% = (z% >> N%) << MapXYZToShift%(2)
  187.  Z%<>oZ% 
  188.       oZ% = Z%
  189.       
  190. redo(32)
  191. >      
  192. 1, 8); 
  193. "Z= "+ 
  194. (z%*100/&ff)+ "%"+ 
  195. 10, " "), 10)
  196.         
  197. /    
  198.  NToDo%=0 
  199.  Step%>4 
  200. redo(Step% 
  201. "    n% = 
  202. min(NToDo%, NCells%)
  203.  n% > 0
  204. #      o% = ToDo% + 
  205. (Size%) - 1
  206.       
  207.  ?o%:o%+=1:
  208. -      
  209.  o%=End% 
  210.  o%=ToDo%:
  211.  ?o%:o%+=1:
  212. @      
  213.  o%>=End% 
  214.  1, "Bad NToDo "+ 
  215. (NToDo%)+ ", "+ 
  216.       ?o% = 1
  217.       o% -= ToDo%
  218. %      x% = (o% 
  219.  Length%) * Step%
  220. %      y% = (o% 
  221.  Length%) * Step%
  222. 5      X% = ((x% + H%) >> N%) << MapXYZToShift%(0)
  223. 5      Y% = ((y% + H%) >> N%) << MapXYZToShift%(1)
  224. &      b% = (X% 
  225.  Z%) * NBits%
  226. 7      i% = ((Array%!(b% >> 3)) >> (b% 
  227.  7)) 
  228.  Mask%
  229. <      w% = Table%!(i%*2)                        :
  230.  &cdab
  231. 0      o% = (w% 
  232.  &FF) * &010101 
  233.  (w% << 24)
  234. >      e% = ((w% >> 8) 
  235.  &FF) * &010101 
  236.  ((w% >> 8) << 24)
  237.       
  238.  i%= 0 
  239.         B%!i% = o%
  240.         B%!(i%+4) = e%
  241.       
  242. $      
  243.  "OS_SetColour", &20, B%
  244.       
  245.  x%, y%, Step%-1
  246.       NToDo% -= 1
  247.       n% -= 1
  248.         
  249. orientate(o%)
  250.  z%, i%, s$()
  251.  s$(2, 1)
  252. ;  s$() = "X= ", "Red  ", "Y= ", "Green", "Z= ", "Blue "
  253. #  Orientation% = 
  254. modulo(o%, 6)
  255.   z% = o% >> 1
  256.  d% = +1 
  257.  d% = -1
  258.  i%= 0 
  259. 5    MapXYZToRGB%(i%) = 
  260. modulo(z% + (2-i%)*d%, 3)
  261. +    MapRGBToXYZ%(MapXYZToRGB%(i%)) = i%
  262. 6    MapXYZToShift%(i%) = MapXYZToRGB%(i%) * nbits%
  263. 7    
  264. 1, i%+1); s$(i%, 0); s$(MapXYZToRGB%(i%), 1)
  265. redo(32)
  266.   oZ% = -1
  267. redo(s%)
  268.   Step% = s%
  269.   H% = Step% / 2
  270.   Length% = 256 / Step%
  271.   Size% = Length% * Length%
  272.   NToDo% = Size%  
  273. memset(ToDo%, 0, Size%+1)
  274.   End% = ToDo% + Size%
  275.  ***************************************************************************
  276.  ***************************************************************************
  277.  ***************************************************************************
  278. _Compress_LUT(nbits%)
  279.  creates a file of format
  280.    +0 = offset to auxillary table (word-aligned)
  281.    +4 = number of bits used for each element in array { <= 10}
  282.    +8 = size of each dimension = 2^{4,5,6}
  283.    +12 = 3-d array [b,g,r] of n-bit elements
  284.    [+0] = array of dither half-words stored as &cdab
  285.                    +---+---+
  286.                    | a | b |
  287.                    +---+---+
  288.                    | c | d |
  289.                    +---+---+
  290.   length% = 1 << nbits%
  291. )  size% = length% * length% * length%
  292.  array size%*2
  293.  output 12 + (size%*10/8) + 3 + (2^10*2)           :
  294.  maximum buffer needed
  295.  used%(&ffff)
  296.   used%() = 0
  297.  "OS_File", 16, FilePrefixUncompressed$+ 
  298. (nbits%), array, 0     :
  299.  load it
  300.   a% = array
  301.  "Finding which dither words are used"
  302.  i%= 0 
  303.  size% - 1
  304. '    used%(?a% 
  305.  ((a%?1) << 8)) += 1
  306.     a% += 2
  307.  "Counting number of different dither words used"
  308.   n% = 0
  309.  i%= 0 
  310.  &ffff
  311.  used%(i%) 
  312.  n% += 1
  313.   bits% = 1
  314.  n% >= (1 << bits%)
  315.     bits% += 1
  316.  "There are "; n%; " different dither words, requiring "; bits%; " bits "
  317.  "Writing dither word table"
  318. A  aux% = ((size%*bits%+31) 
  319.  32) * 4          :
  320.  word-aligned
  321. C  aux% += 12                                    :
  322.  skip header!
  323.   output!0 = aux%
  324.   output!4 = bits%
  325.   output!8 = 1 << nbits%
  326.   aux% += output
  327.   index% = 0
  328.  i%= 0 
  329.  &ffff
  330.  used%(i%) 
  331.       used%(i%) = index%
  332.       index% += 1
  333.       !aux% = i%
  334.       aux% += 2
  335.         
  336.  "Compressing table"
  337.   index% =