home *** CD-ROM | disk | FTP | other *** search
RISC OS BBC BASIC V Source | 1994-02-08 | 12.5 KB | 467 lines |
- >CDBox_Gen
- : Cy Booker
- : 86 Church View
- : Main Road
- : Crockenhill
- : Swanley
- : Kent
- : BR8 8 JW
- this file contains code to do the following
- 1. Generate external file for dither look up table
- 2. Compress this file into a form that can be used by ColourDBox code
- 3. Tests a BASIC multi-tasking RGB cube slice using ColourTrans
- 4. Tests a BASIC multi-tasking RGB cube slice using LUT
- NOTE
- current colourdbox code can only handle nbits in {4, 5, 6}
- (if nbits = 7 then need over 4Mb!)
- When run, does demo of multi-tasking rgb cube using lut
- This file also contains code to generate a lut for dither patterns
- in 4bpp modes for (currently) the current palette (ie the standard
- wimp palette).
- The way this is done is to use the vdu variable GcolOraEorAddr
- which contains 64 bytes of raw screen data set up as an ecf pattern
- when ColourTrans_SetGCOL.
- the PRM gives very poor documentation about this, but It seems my code
- to extract the 4 (x4bit) colour numbers used in the dither pattern
- works
- Obviously can't do 8-bit colour, so use 4-bit colour
- = 0, &11, &22, ..., &ee, &ff
- or 5-bit
- = &00, &08, &10, .., &7b, &84, ..., &f7, &ff
- We then try and compact this table down
- Currently we do this by observing that many MANY dither patterns repeat
- due to the limited number of colours in a palette. In fact, there
- is usually less than 1024 different patterns generated, and so instead
- of storing the 16-bits for each element, we store only (say) 10 bits
- and use this as an offset into an actual 16-bit table of actual dither
- patterns.
- How to extend this to 8bpp and 16bpp?
- Well 8bpp would require 32 bits (2x2 pixels) in the ancillary array,
- and a btree to store the different patterns. Actually, I would assume
- that instead of storing as tl.tr.bl.br we store as tl:tl^tr.tl^bl.tl^br
- (ie 8 bits + aux index) we may well reduce the table considerably. The
- reason for this is that the top 6 bits of the colour number are the
- top 6 bits of a colour, and these woul very rarely change by much.
- Note that this doesn't work for 4bpp, because I tried!
- For 16bpp, well it probably isn't worth it as I assume the colourtrans
- routine is very fast, just some shifts
- Debug% =
- 91FilePrefixUncompressed$ = "<d>.Tmp.colourlut"
- :+FilePrefixCompressed$ = "<d>.Tmp.lut"
- ;0FilePrefixStatistics$ = "<d>.Tmp.lutstats"
- _TestCube_ColourTrans
- PROC_Create_Uncompressed_LUT(5)
- PROC_Compress_LUT(5)
- PROC_TestCube_LUT(5)
- _TestCube_ColourTrans
- uses colourtrans to generate cube
- NCells% = 13
- MapXYZToRGB%(2)
- MapRGBToXYZ%(2)
- MapXYZToShift%(2)
- ToDo% =
- malloc(64*64+1)
- IJ B% =
- malloc(64) :
- GcolOraEorAddr 1-705
- Cells%(NCells%-1, 1)
- 512, 512
- ct_orientate(2)
- Z% = 0
- 300, 0, 0, 516
- x%, y%, b%
- R5
- &444
- ct_orientate(Orientation% + 2)
- S5
- &111
- ct_orientate(Orientation%
- T# z% = (y% * &ff / 512) + 0.5
- z% =
- min(&ff, z%)
- V$ Z% = z% << MapXYZToShift%(2)
- Z%<>oZ%
- oZ% = Z%
-
- redo(32)
- Z>
- 1, 8);
- "Z= "+
- (z%*100/&ff)+ "%"+
- 10, " "), 10)
- [
- \/
- NToDo%=0
- Step%>4
- redo(Step%
- ]" n% =
- min(NToDo%, NCells%)
- n% > 0
- `# o% = ToDo% +
- (Size%) - 1
-
- ?o%:o%+=1:
- b-
- o%=End%
- o%=ToDo%:
- ?o%:o%+=1:
- c@
- o%>=End%
- 1, "Bad NToDo "+
- (NToDo%)+ ", "+
- ?o% = 1
- o% -= ToDo%
- f% x% = (o%
- Length%) * Step%
- g% y% = (o%
- Length%) * Step%
- h- X% = (x% + H%) << MapXYZToShift%(0)
- i- Y% = (y% + H%) << MapXYZToShift%(1)
- j;
- "ColourTrans_SetGCOL", Z%
- X%,,, &100, 0
-
- x%, y%, Step%-1
- NToDo% -= 1
- n% -= 1
- n
- ct_orientate(o%)
- z%, i%, s$()
- s$(2, 1)
- u; s$() = "X= ", "Red ", "Y= ", "Green", "Z= ", "Blue "
- v# Orientation% =
- modulo(o%, 6)
- z% = o% >> 1
- d% = +1
- d% = -1
- i%= 0
- z5 MapXYZToRGB%(i%) =
- modulo(z% + (2-i%)*d%, 3)
- {+ MapRGBToXYZ%(MapXYZToRGB%(i%)) = i%
- |7 MapXYZToShift%(i%) = (MapXYZToRGB%(i%) * 8) + 8
- }7
- 1, i%+1); s$(i%, 0); s$(MapXYZToRGB%(i%), 1)
- redo(32)
- oZ% = -1
- ***************************************************************************
- ***************************************************************************
- ***************************************************************************
- _TestCube_LUT(nbits%)
- uses lut of nbits size to show multi-tasking cube generation
- NCells% = 13
- = N% = 8 - nbits% :
- #bits not used
- ? lut =
- _load_file(FilePrefixCompressed$+
- (nbits%), &ffd)
- lut=0
- 1, "Can't load lut"
- MapXYZToRGB%(2)
- MapRGBToXYZ%(2)
- MapXYZToShift%(2)
- Array% = lut + 12
- Table% = lut + lut!0
- NBits% = lut!4
- Mask% = (1 << NBits%) - 1
- ToDo% =
- malloc(64*64+1)
- J B% =
- malloc(64) :
- GcolOraEorAddr 1-705
- Cells%(NCells%-1, 1)
- 512, 512
- orientate(2)
- Z% = 0
- 300, 0, 0, 516
- x%, y%, b%
- 2
- &444
- orientate(Orientation% + 2)
- 2
- &111
- orientate(Orientation%
- # z% = (y% * &ff / 512) + 0.5
- z% =
- min(&ff, z%)
- , Z% = (z% >> N%) << MapXYZToShift%(2)
- Z%<>oZ%
- oZ% = Z%
-
- redo(32)
- >
- 1, 8);
- "Z= "+
- (z%*100/&ff)+ "%"+
- 10, " "), 10)
-
- /
- NToDo%=0
- Step%>4
- redo(Step%
- " n% =
- min(NToDo%, NCells%)
- n% > 0
- # o% = ToDo% +
- (Size%) - 1
-
- ?o%:o%+=1:
- -
- o%=End%
- o%=ToDo%:
- ?o%:o%+=1:
- @
- o%>=End%
- 1, "Bad NToDo "+
- (NToDo%)+ ", "+
- ?o% = 1
- o% -= ToDo%
- % x% = (o%
- Length%) * Step%
- % y% = (o%
- Length%) * Step%
- 5 X% = ((x% + H%) >> N%) << MapXYZToShift%(0)
- 5 Y% = ((y% + H%) >> N%) << MapXYZToShift%(1)
- & b% = (X%
- Z%) * NBits%
- 7 i% = ((Array%!(b% >> 3)) >> (b%
- 7))
- Mask%
- < w% = Table%!(i%*2) :
- &cdab
- 0 o% = (w%
- &FF) * &010101
- (w% << 24)
- > e% = ((w% >> 8)
- &FF) * &010101
- ((w% >> 8) << 24)
-
- i%= 0
- B%!i% = o%
- B%!(i%+4) = e%
-
- $
- "OS_SetColour", &20, B%
-
- x%, y%, Step%-1
- NToDo% -= 1
- n% -= 1
-
- orientate(o%)
- z%, i%, s$()
- s$(2, 1)
- ; s$() = "X= ", "Red ", "Y= ", "Green", "Z= ", "Blue "
- # Orientation% =
- modulo(o%, 6)
- z% = o% >> 1
- d% = +1
- d% = -1
- i%= 0
- 5 MapXYZToRGB%(i%) =
- modulo(z% + (2-i%)*d%, 3)
- + MapRGBToXYZ%(MapXYZToRGB%(i%)) = i%
- 6 MapXYZToShift%(i%) = MapXYZToRGB%(i%) * nbits%
- 7
- 1, i%+1); s$(i%, 0); s$(MapXYZToRGB%(i%), 1)
- redo(32)
- oZ% = -1
- redo(s%)
- Step% = s%
- H% = Step% / 2
- Length% = 256 / Step%
- Size% = Length% * Length%
- NToDo% = Size%
- memset(ToDo%, 0, Size%+1)
- End% = ToDo% + Size%
- ***************************************************************************
- ***************************************************************************
- ***************************************************************************
- _Compress_LUT(nbits%)
- creates a file of format
- +0 = offset to auxillary table (word-aligned)
- +4 = number of bits used for each element in array { <= 10}
- +8 = size of each dimension = 2^{4,5,6}
- +12 = 3-d array [b,g,r] of n-bit elements
- [+0] = array of dither half-words stored as &cdab
- +---+---+
- | a | b |
- +---+---+
- | c | d |
- +---+---+
- length% = 1 << nbits%
- ) size% = length% * length% * length%
- array size%*2
- output 12 + (size%*10/8) + 3 + (2^10*2) :
- maximum buffer needed
- used%(&ffff)
- used%() = 0
- "OS_File", 16, FilePrefixUncompressed$+
- (nbits%), array, 0 :
- load it
- a% = array
- "Finding which dither words are used"
- i%= 0
- size% - 1
- ' used%(?a%
- ((a%?1) << 8)) += 1
- a% += 2
- "Counting number of different dither words used"
- n% = 0
- i%= 0
- &ffff
- used%(i%)
- n% += 1
- bits% = 1
- n% >= (1 << bits%)
- bits% += 1
- "There are "; n%; " different dither words, requiring "; bits%; " bits "
- "Writing dither word table"
- A aux% = ((size%*bits%+31)
- 32) * 4 :
- word-aligned
- C aux% += 12 :
- skip header!
- output!0 = aux%
- output!4 = bits%
- output!8 = 1 << nbits%
- aux% += output
- index% = 0
- i%= 0
- &ffff
- used%(i%)
- used%(i%) = index%
- index% += 1
- !aux% = i%
- aux% += 2
-
- "Compressing table"
- index% = 0
- a% = array
- o% = output + 12
- b% = 0
- m% = (1 << bits%) - 1
- i%= 0
- size% - 1
- !F !o% = (!o%
- (m% << b%))
- (used%(?a%
- ((a%?1) << 8)) << b%)
- b% += bits%
- o% += b% >> 3
- b% = b%
- a% += 2
- "OS_File", 10, FilePrefixCompressed$+
- (nbits%), &ffd,, output, aux%
- ***************************************************************************
- ***************************************************************************
- ***************************************************************************
- _Create_Uncompressed_LUT(nbits%)
- length% = 1 << nbits%
- 0) size% = length% * length% * length%
- vars 8
- output size%*2
- count%(256*256)
- m%(length%)
- i%= 0
- length%-1
- 6/ m%(i%) = ((i%*&ff) / (length%-1)) + 0.5
- "XHourglass_On"
- "XHourglass_Off":
- e+ " [rgb= "+
- ~(cg%
- (m%(r%) << 8))+ " ]"
- !vars = 171
- vars!4 = -1
- >,
- "OS_ReadVduVariables", vars, vars
- e%= !vars
- o% = output
- count%() = 0
- b%= 0
- length% - 1
- cb% = m%(b%) << 24
-
- g%= 0
- length% - 1
- E& cg% = cb%
- (m%(g%) << 16)
- FM
- "XHourglass_Percentage", (b%*length%+g%)*100/(length%*length%)
- G!
- r%= 0
- length% - 1
- HF
- "ColourTrans_SetGCOL", cg%
- (m%(r%) << 8),,, &100, 0
- ?o% = ?e%
- e%?4
- J! o%?1 = e%?8
- e%?12
- K. count%(?o%
- ((o%?1) << 8)) += 1
- o% += 2
-
-
- "XHourglass_Off"
- "OS_File", 10, FilePrefixUncompressed$+
- (nbits%), &ffd,, output, o%
- max% = 0
- i%= 0
- 65535
- T/
- max% < count%(i%)
- max% = count%(i%)
- h%(max%)
- i%= 0
- 65535
- h%(count%(i%)) += 1
- Z- H%=
- (FilePrefixStatistics$+
- (nbits%))
- i%= 0
- max%
- ^2
- h%(i%)
- #H%,
- (i%)+ " : "+
- (h%(i%))
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Debug%
- e$ = ", ["+
- )+ "]"
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- returns pointer to malloc()'ed block, or zero
- if not a file, not correct filetype, or not enough memory
- _load_file(file$, ftype%)
- siz%, t%, type%, f%
- "OS_File", 23, file$
- t%,,,, siz%,, type%
- t%=1
- type%=ftype%
- f% =
- malloc(siz%)
- z-
- "OS_File", 16, file$, f%, 0
- memset(dest%, byte%, siz%)
- i%, word%
- byte% = byte%
- # word% = byte%
- (byte% << 16)
- " word% = word%
- (word% << 8)
- siz% > 3
- !dest% = word%
- dest% += 4
- siz% -= 4
- siz% > 0
- ?dest% = byte%
- dest% += 1
- siz% -= 1
- SYS "CAssembler_Memory", 1, dest%, byte%, siz%
- min(a, b):
- a>b
- modulo(a%, b%)
- a%<0 : a%+=b% :
- a%>=b% : a%-=b% :
- malloc(x%)
- x% x%
-