***************************************************************************
*                                                                         *
*  Project: RiscOS                                                        *
*                                                                         *
*  Module:  Colours                                                       *
*                                                                         *
*  Created: Wed 24-Oct-90      By: Ran Mokady                             *
*                                                                         *
*  First version: 0.62                                                    *
*                                                                         *
*    Copyright:    (C) 1990, Acorn Computers Ltd., Cambridge, England.    *
*                                                                         *
***************************************************************************

Purpose:
========

Colour translation module.

***************************************************************************


Change Log:
===========


---------------------------------------------------------------------------

Version: 0.62 Wed 24-Oct-90        Ran Mokady                    


---------------------------------------------------------------------------

Version: 0.64 Thu 17-Jan-91        David De Vorchik              

* New CMYK and HSV conversion SWIs added, all accepting 16.16 fixed point
  values and returning in the same format.


---------------------------------------------------------------------------

Version: 0.65 Tue 12-Mar-91        David De Vorchik              

* Changes to produce 'true colours' when using the Font Managers Switch
  Output to buffer SWIs.

  ColourTrans_SetFontColours / ReturnFontColours improved for speed.

* Use of PaletteV to get colours, if this fails then it reverts back to 
  using OS_ReadPalette.


---------------------------------------------------------------------------

Version: 0.66 Mon 25-Mar-91        Owen Smith                    

* Internationalised.

---------------------------------------------------------------------------

Version: 0.67 Mon 01-Apr-91        Owen Smith                    

* Changed to use some generic error tokens.

---------------------------------------------------------------------------

Version: 0.80 Wed 10-Apr-91        David De Vorchik              

* Version number increased to give room for SerialPorts version (0.70)
  not to clash.

* Removed the Read/Write Palette SWIs.

---------------------------------------------------------------------------

Version: 0.81 Sat 13-Apr-91        David De Vorchik              

* Bug fix; when calling PaletteV now does not try to treat it as
  brain damaged with r4 returned as zero (claimed).

---------------------------------------------------------------------------

Version: 0.82 Wed 17-Apr-91        Owen Smith                    

* Fixed R1 corruption on internationalised error lookups.

---------------------------------------------------------------------------

Version: 0.83 Sun 28-Apr-91        Owen Smith                    

* Fixed minor internationalisation bug.

---------------------------------------------------------------------------

Version: 0.84 Sun 02-Jun-91        David De Vorchik              

* Added new Read/WritePalette SWIs.
* Added ColourTrans_SelectTable to accept sprite pointer to 
  translate without having to extract the palette from the sprite.
---------------------------------------------------------------------------

Version: 0.85 Wed 05-Jun-91        David De Vorchik              

* Changed so that when output redirected at a sprite the
  read function works correctly and returns either the palette
  entries or the default for that mode.

* WritePalette when output is switched into a sprite is faulted.


---------------------------------------------------------------------------

Version: 0.86 Thu 13-Jun-91        David De Vorchik              

* Improved so calibration not performed when not required.

* Cache invalidation performed when output switched sprite and read or
  write performed.

* Bug fix; Log2BPP returned correctly when dithering performed.
---------------------------------------------------------------------------

Version: 0.87 Wed 03-Jul-91        Neil Raine

* Added font colour cacheing

---------------------------------------------------------------------------

Version: 0.88 Mon 22-Jul-91        David De Vorchik              

* Removed clever palette handling when output switched to sprite that has
  no palette, gives speed up for PC Emulator aswell (fixes white cursor
  blocks).

---------------------------------------------------------------------------

Version: 0.89 Mon 05-Aug-91        David De Vorchik              

* Fixed R6,R7,R8 corruption on ColourTrans_SetFontColours.
* Removed Neil's change for font colour caching as it did not work correctly.

---------------------------------------------------------------------------

Version: 0.90 Mon 02-Sep-91        David De Vorchik              

 07-Aug-91      DDV Added ColourTrans_SetColour
 19-Aug-91      DDV Re-read the Log2BPP on switching output to a sprite; fixes printing problems.
 21-Aug-91      DDV Added ColourTrans_MiscOp
 22-Aug-91      DDV Introduced John Bowlers new colour matching routines
 22-Aug-91      DDV Introduced MiscOp(0) to modify the error loadings, defaults set to 1,3,10
 23-Aug-91      DDV Some restructuring tidying of certain files
 23-Aug-91      DDV Bug fix: Changing the error loadings invalidates the cache
 25-Aug-91      DDV Extensive modifications to build_colours routine
 26-Aug-91      DDV SelectTable new specification implemented to allow current palette aswell as default
 26-Aug-91      DDV Now uses scratch space rather than assuming that space on SVC stack
 26-Aug-91      DDV Read/WritePalette now comply with new specification for R0,R1 and sprite pointers
 26-Aug-91      DDV Added error about reserved bits on Read/WritePalette
 27-Aug-91      DDV Rationalised the tokens
 27-Aug-91      DDV Bug fix: ColourTrans_SetColour in 8BPP modes - wrong tint altered
 27-Aug-91      DDV Optimised to keep palette and use fast routines where needed for default palette
 27-Aug-91      DDV New implementation of ColourTrans_SetFontColours added
 30-Aug-91      DDV Modified title string
 30-Aug-91      DDV Modified macros internally used to accept condition codes
 30-Aug-91      DDV Bug fixed and optimised in some areas, now copes correctly with modifable error loadings
 02-Sep-91      DDV Optimised for speed moved error calculation out of main loop.
 02-Sep-91 0.90 DDV Bug fix: ColourTrans_SetFontColours gets it right with strange palettes.

---------------------------------------------------------------------------

Version: 0.91 Wed 04-Sep-91        David De Vorchik              

 04-Sep-91 0.91 DDV Correct colour weights

---------------------------------------------------------------------------

Version: 0.92 Mon 09-Sep-91        David De Vorchik              

 05-Sep-91      DDV Added support for Service_InvalidateCache
 05-Sep-91      DDV Check for calibration table in the build table function
 06-Sep-91      DDV New monochrome dithering implemented
 06-Sep-91      DDV Find correct background colour for monochrome dithering routine
 07-Sep-91      DDV CTransMiscOp_ReturnPattern added
 07-Sep-91      DDV Added caching of patterns (increased the cache entry size)
 07-Sep-91      DDV Fixed caching problems within ReturnColourNumber and ReturnGCOL
 09-Sep-91 0.92 DDV New colour dithering routines added
     
---------------------------------------------------------------------------

Version: 0.94 Wed 11-Sep-91        David De Vorchik              

 09-Sep-91 0.93 DDV Corrected dither routines to not multiply by 4
 09-Sep-91      DDV New colour error loadings to sort out matching to greys
 09-Sep-91      DDV Ordering of pixels within 4x4 grid sorted out
 10-Sep-91      DDV Some optimsiations to the pixel ordering routines
 11-Sep-91 0.94 DDV Bug fix: ColourTrans_ReturnFontColours uses new forwards/backwards algorithm

---------------------------------------------------------------------------

Version: 0.95 Fri 13-Sep-91        David De Vorchik              
                    
 13-Sep-91 0.95 DDV Bug fix: ReturnOpp<foo> now works again - setup wrong vector to call

---------------------------------------------------------------------------

Version: 0.96 Thu 19-Sep-91        David De Vorchik              

 19-Sep-91 0.96 DDV Bug fix: Fixed read of palette when mode not -1 to use screen vector

---------------------------------------------------------------------------

Version: 0.98 Thu 17-Oct-91        David De Vorchik              

 22-Sep-91      DDV Fixed the non-internationalised version to use correct Bad SWI error string.
 22-Sep-91 0.97 DDV Bug fix: Modifying error loadings does not always invalidate cache, ie. when reading
 16-Oct-91      DDV Added ColourTrans_SelectTable with R4 =0 returns size of table generated
 17-Oct-91      DDV Improved matching in ReturnFontColours (speed up aswell)
 17-Oct-91      DDV Read/Write error loadings for desktop saving
 17-Oct-91 0.98 DDV ColourTrans_WriteLoadingsToFile SWI added 

---------------------------------------------------------------------------

Version: 0.99 Wed 23-Oct-91        David De Vorchik              

 21-Oct-91      DDV File debugging option added
 21-Oct-91      DDV Luminance matching replaced by error comparison
 21-Oct-91 0.99 DDV Removed bonus push in middle of return font colours

---------------------------------------------------------------------------

Version: 1.00 Mon 04-Nov-91        David De Vorchik              

 01-Nov-91      DDV Bug fix: If used on old Font Manager falls back to old method of setting colours
 02-Nov-91      DDV Fixed some more internationalisation messages for compiling the RISC OS 2.00 version
 02-Nov-91 1.00 DDV Bug fix: Can always save calibration even if default

---------------------------------------------------------------------------

Version: 1.01 Wed 11-Dec-91        David De Vorchik              

 30-Nov-91      DDV Implement ColourTrans_SetTextColour and SetOppTextColour
 30-Nov-91 1.01 DDV Added set text colour bit to ColourTrans_SetColour

---------------------------------------------------------------------------

Version: 1.03 Wed 15-Jan-92        David De Vorchik              

 08-Jan-92 1.02 DDV Added support for OS_SetColour gives better ECF handling for foreground / background
 13-Jan-92      DDV Added the concept of a transfer function to remap the R,G and B on select table calls
 14-Jan-92      DDV Added ColourTrans_ProcessTable
 14-Jan-92      DDV Bug fix: Bogus calling of transfer function
 15-Jan-92 1.03 DDV Changed ColourTrans_ProcessTable to ColourTrans_GenerateTable

---------------------------------------------------------------------------

Version: 1.04 Wed 04-Mar-92        David De Vorchik              

 17-Feb-92 1.04 DDV Bug fix: RISC OS 2.00 code for SetFontColours does not corrupt R3

---------------------------------------------------------------------------

Version: 1.05 Fri 27-Mar-92        David De Vorchik              

 26-Mar-92 1.05 DDV Bug fix: more choosy in the way it selects the colour
                      matching routine.

---------------------------------------------------------------------------

Version: 1.06 Fri 27-Mar-92        David De Vorchik              

 27-Mar-92 1.06 DDV No change.

---------------------------------------------------------------------------

Version: 1.07 Wed 15-Apr-92        David De Vorchik              

 15-Apr-92 1.07 DDV Bug fix: Cures problems with RISCOS_Lib passing -1 to CT_SetFontColours

---------------------------------------------------------------------------

Version: 1.08 Fri 11-Sep-92        David De Vorchik              

 11-Sep-92 1.08 DDV Bug fix: Colour matching masks for 8 bit per pixel modes

---------------------------------------------------------------------------

Version: 1.09 Fri 11-Sep-92        David De Vorchik              

  4-Jun-92      DDV Uses OS_SetColour rather than VDU sequences to change colour
  6-Jun-92      DDV Changed to colour matching to work in 16 bits per pixel modes
  6-Jun-92      DDV Build colours no longer attempts to generate a table for 16 bit per pixel modes
  6-Jun-92      DDV ColourTrans_SelectTable generates a sensible table for 16 and 32 bit per pixel modes
 15-Jul-92      DDV Dithering at 16 bit-per-pixel implemented
 16-Jul-92      DDV Abused the cache structure to allow for bit colour numbers / colour words
 17-Jul-92      DDV Recoded SetGCOL for new depth modes, more sensible about reading palette etc...
 17-Jul-92      DDV Removed MiscOp to return pattern it was useless anyway
 17-Jul-92      DDV Removed major bottle neck from 16/32 bit modes, now uses cached routine pointers
 17-Jul-92      DDV Sorted out support for 256 entry CLUT modes
 23-Jul-92      DDV Backwards compatibility hack introduced, generate 8 bit-per-pixel tables for deep modes
  3-Aug-92      DDV Changed that backwards compatibilty hack to return GCOL values, not colour numbers
  3-Aug-92      DDV ReturnGCOLTable now a service of SelectTable
 10-Aug-92      DDV Error returned on read / write palette in a depth of mode >= 16 bit per pixel
  7-Sep-92 1.09 DDV Implemented VIDC20 version of 16 bit-per-pixel handling

---------------------------------------------------------------------------

Version: 1.21 Tue 04-May-93        Alan Glover                   

;
;-----------------------------------------------------------------------------
; 19-Nov-92 1.08 AMG Recreate DDV's original bug fix for 1.08
; 27-Nov-92      AMG/DDV Fix for dithering with supremacy bit set
; 14-Dec-92 1.09 amg Build non-development version with above fix
;  3-Jan-93      DDV Integeration of Tony's new calibration code finished
;  3-Jan-93      DDV Fall back to old code if old style table / commands issued - wowzer!
;  3-Jan-93      DDV Added ColourTrans_MiscOp (2) to return calibration table type
;  3-Jan-93      DDV Fixed exactly where calibration is applied - always when matching a colour
;
;(amg 16/3/93 merge divergent sources to bring the new calibration code into the Medusa sources)
; 16-Mar-93 1.20 amg Build development version, so I know which code is where!!
; 17-Mar-93      amg Introduce familiarity with new sprite mode words and
;                    pointers to mode selectors.
; 29-Mar-93      amg Select/GenerateTable don't generate a table for 16/32 to 16/32 (any
;                    combination). Table generation for 16/32 down to 8 or below still to be
;                    done. 
; 29-Apr-93      amg Complete addition of 32K table generation for 16/32 to 8/lower bpp.
;  4-May-93 1.21 amg new release for return of sources to source filer

---------------------------------------------------------------------------

Version: 1.22 Thu 29-Jul-93        Alan Glover                   

Fix r7 corruption in _SetGCol.

---------------------------------------------------------------------------

Version: 1.23 Wed 11-Aug-93        Alan Glover                   

Add a switch for building a RISC OS 3.10 version to allow a version to 
issued to Dev. Support.

Fix bug affecting NCOL=63 8bpp modes.

---------------------------------------------------------------------------

Version: 1.24 Fri 20-Aug-93        Alan Glover                   

Add another TCheal bug fix

---------------------------------------------------------------------------

Version: 1.25 Tue 24-Aug-93        Alan Glover                   

Add another (different!) TCheal bug fix

---------------------------------------------------------------------------

Version: 1.50 Thu 28-Oct-93        Alan Glover                   

Version number jump is to leave some space for CC's Colourcard ColourTrans.
Fix MED-00294, 00288, 00044. Make bit 4 on R5 work in Generate/SelectTable.

---------------------------------------------------------------------------

Version: 1.51 Tue 02-Nov-93        Alan Glover                   

Fix bug MED-00743 - ReturnFontColours producing wrong R2 returns.

---------------------------------------------------------------------------

Version: 1.52 Thu 04-Nov-93        Alan Glover                   

Fix bug MED-00838 - Fix historical bug to get bit 4 on R5 working fully.

---------------------------------------------------------------------------

Version: 1.53 Fri 05-Nov-93        Alan Glover                   

Fix bug with selecttable and sprites without palettes

---------------------------------------------------------------------------

Version: 1.54 Mon 08-Nov-93        Alan Glover                   

Fix another historical bug on R0=mode, R1=-1 calls to SelectTable

---------------------------------------------------------------------------

Version: 1.55 Wed 10-Nov-93        Alan Glover                   

Fix two more historical bugs, both contributing to ReturnGCOL sometimes
returning a colour number instead of a GCOL in 8bpp.

---------------------------------------------------------------------------

Version: 1.56 Thu 02-Dec-93        Alan Glover                   

And another similar one - ReturnGCOLForMode was missing when the target
mode was an 8bpp VIDC 1 type and returning a colour number when it should
have given a GCOL.

---------------------------------------------------------------------------

Version: 1.57 Mon 06-Dec-93        Alan Glover                   

No new release code, but added several new debugging switches to allow better
watching of input/output values and swi calls. New switches input1, input2,
output and showswis added. A couple of ADRs become ADRLs when these switches
are on to stay in range.

---------------------------------------------------------------------------

Version: 1.58 Fri 07-Jan-94        Alan Glover                   

Fix bug MED-01821. 32K Table generation routine now calls InvalidateCache
as a SWI rather than internally to get R12 set up right.

---------------------------------------------------------------------------

Version: 1.59 Fri 28-Jan-94        Alan Glover                   

Fix bug MED-02212. When ReturnGCOLForMode was rewritten (after 3.10), it
forgot to load R0 with the error block. Also sorted out the BadDepth error
message - using the wrong macro meant that it appeared in the image as
text rather than token.

---------------------------------------------------------------------------

Version: 1.60 Wed 02-Feb-94        Alan Glover                   

Fix bug MED-0????. ColourTrans will now refuse to die while it has 32K
tables in memory. The upshot is that replacing ColourTrans is now best
attempted after a mode change.

---------------------------------------------------------------------------

Version: 1.61 Thu 17-Feb-94        Alan Glover                   

Fix bug MED-02882 (well, the ColourTrans component of it anyway).
Only claim 32K tables moments before they are needed to reduce rma
traffic (won't be claimed at all if table exists in resourcefs).
Ensure tables which are not squeezed in resourcefs are used
directly. 
Replace the squeezed 4desktop precomputed table with an expanded
one.

***************************************************************************
                      MEDUSA - RISC OS 3.50 build
***************************************************************************
*                                                                         *
*  Project: Black                                                         *
*                                                                         *
*  Module:  Colours                                                       *
*                                                                         *
*  Created: Wed 22-Jun-94      By: Aideen McConville                      *
*                                                                         *
*  First version: 1.61                                                    *
*                                                                         *
*    Copyright:    (C) 1994, Acorn Computers Ltd., Cambridge, England.    *
*                                                                         *
***************************************************************************

Purpose:
========



***************************************************************************


Change Log:
===========


---------------------------------------------------------------------------

Version: 1.61 Wed 22-Jun-94        Aideen McConville             

Moved to new source tree.

---------------------------------------------------------------------------

Version: 1.62 Mon 25-Apr-94        Alan Glover                   

Fix bug MED-03007 - a register corruption introduced in the MED-02882
work. This fix was put together during the Medusa freeze, but not
included in the image, hence the difference between the date above and the
date of the version file.

---------------------------------------------------------------------------

Version: 1.63 Fri 28-Oct-94        Steve Cormie                  

* Added directed comments to Messages file for message tokenisation.
* Moved command help/syntax from Global.Messages to Messages file.

---------------------------------------------------------------------------

Version: 1.64 Fri 10-Mar-95        Graham Simms                  

* Changed the way the ROM tables were built. ColourTrans is now used to
  create the tables which gives better results, especially in 4bpp desktop
  palette modes.

Changed:
  Makefile
  MkTables.dosqueeze
Added:
  MkTables.c.maketables
Removed:
  Old Basic files for creating tables in MkTables directory