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

Purpose:
========

Transformed sprite plotting.

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


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

Version: 0.30 Tue 05-Feb-91        David De Vorchik              

 * Wacky new SpriteReason_CreateRemovePalette operation.  This allows the
   caller to create, remove and enquire about palettes in association
   with sprites.

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

Version: 0.31 Mon 01-Apr-91        Owen Smith                    

 * Internationalised.

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

Version: 0.32 Tue 02-Apr-91        Owen Smith                    

 * Added caching of "Sprite doesn't exist"  translated error. This is
   necessary as this error is provoked extensively by the Filer and the Wimp.

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

Version: 0.33 Fri 19-Apr-91        Owen Smith                    

* Fixed a couple of internationalisation bugs.

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

Version: 0.34 Fri 10-May-91        Owen Smith                    

* Fixed service entry - the service reset code was not being executed on
  a soft reset (bug introduced during internationalisation).

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

Version: 0.36 Thu 15-Aug-91        David De Vorchik              

 10-May-91    0.34    Fixed Service_Reset code
 14-Aug-91    0.35    Fix bug: Transforming transparent sprites work again
 14-Aug-91            Optimised for 1:1 mapping in BPP for transformed sprite masks
 14-Aug-91    0.36    Fix bug: Insert/delete columns works when rapping off a word boundary

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

Version: 0.37 Fri 13-Sep-91        Jonathan Roach                

 02-Sep-91    0.37    Fix bug: Insert columns doesn't barf when there's exactly enough room
 13-Sep-91            Fix bug: revist insert columns and apply crow bar where appropriate
                               (used to scramble sprite quite often).

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

Version: 0.38 Tue 10-Dec-91        Steve Cormie                  

 10-Dec-91    0.38    Removed comments from Messages file and shortened tokens.

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

Version: 0.39 Mon 17-Feb-92        ENevill                       

 Bug fix: Rotate sprite incorrectly calculated the amount to move the sprite
          pool up to make room for the larger sprite.

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

Version: 0.40 Thu 20-Feb-92        ENevill                       

 Bug fix: Fixed out of memory error when inserting 0 rows/columns

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

Version: 0.41 Wed 18-Mar-92        ENevill                       

 Bug fix: Fixed insert columns

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

Version: 0.42 Wed 18-Mar-92        ENevill                       

 Removed debugging OS_Confirm call

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

Version: 0.43 Mon 30-Mar-92        ENevill                       

 Fixed insert column bug (another one)
 Fixed garbage fill when inserting rows

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

Version: 0.44 Thu 02-Apr-92        ENevill                       

RP-2050: Deleting 1 column in a single column mode 0 sprite lead to a
32 bit wide sprite.
"Sprite not found" error now cached on Service_ResourceFSStarted instead of
Service_MessageFileClosed.

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

Version: 0.45 Thu 23-Apr-92        ENevill                       

RP-2503 Fix boundary condition when inserting rows. Previously the test for
        space in sprite area did BGE error, EQ is OK, changed to BGT.

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

Version: 0.46 Thu 23-Apr-92        Tim Dobson                    

RP-2540 Corrected start and end addresses of block to copy in CreatePalette.

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

Version: 0.47 Tue 22-Sep-92        David De Vorchik              

 24-Jul-92            Removed the conditional code - made it more readable
 24-Jul-92            Bodge OS code removed - can no longer build Sprite Extend for Arthur - shame
 24-Jul-92            Seperated off Insert/Delete Rows/Columns into seperate file
  3-Aug-92            Support for rendering into higher depth modes implemented
  3-Aug-92            Incorporated Tims wacky bit reordering function
  5-Aug-92            Handling of masked sprites tidied to work with greater depths
  5-Aug-92            Modified pixel translation code, now works for all depths upto 32 bit per pixel
 11-Aug-92            PutSpriteScaled now faults sprites of depth > 8 bit per pixel
 17-Aug-92            Generalised copying and other functions for translation tables
 18-Aug-92            Transformed sprites into 16 or 32 bit per pixel now working (must test 32 bit per pixel)
 18-Aug-92            PutSpriteTransformed now errors if input depth > 8 bit per pixel
 28-Aug-92            PutSpriteScaled now copes with sprite containing new header (needs new kernel)
  7-Sep-92            Support for VIDC 20 added (conditional between two boards)
 11-Sep-92            Implemented a crude bit munging function to generate 32 bit per pixel sprites

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

Version: 0.48 Tue 04-May-93        Alan Glover                   

 03-Mar-93            AMG: Added multiple buffer code 
    Mar-93            AMG: Make SpriteScaled cope with input from/output to 16/32bpp  
    Apr-93            AMG: Make SpriteTransformed cope with input from/output to 16/32bpp
    Apr-93            AMG: Optimise the bit mungeing functions from 11-Sep-92
    Apr-93            AMG: When plotting a 8bpp or lower sprite with a full palette to
                           16bpp or above, ignore the translation table and use the
                           palette entries directly. NB will NOT work for braindamaged
                           8bpp 16 or 64 entry palettes. Flag 'ignore_ttr' controls this.
 04-Apr-93    0.48    AMG: New version number for return to source filer

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

Version: 0.49 Tue 11-May-93        Alan Glover

;; 06-Apr-93                 Bug fix on sprext plotting via 32K tables
;;                           Bug fix on plotting 32bpp sprites to 8bpp
;; 11-May-93    0.49    AMG: Bounce mask/palette operations on 16 & 32bpp new format sprites


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

Version: 0.51 Wed 14-Jul-93        Alan Glover                   

;; 21-May-93    0.50    AMG: Fix bug in PutSpriteTransformed (causing an extra plot of first
;;                           pixel on each row of sprite)

(Source Filer swallowed it!!!)

;; 23-Jun-93    0.51    AMG: Introduce 1bpp masks on sprites (only for putspritescaled &
;;                           transformed - NOT greyscaled) 


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

Version: 0.52 Thu 15-Jul-93        Alan Glover                   

Wrap up mask work and fix bugs.

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

Version: 0.53 Mon 09-Aug-93        Alan Glover                   

Fix bug in PutSprite|MaskScaled when plotting masked sprites with the LH
edge obscured.

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

Version: 0.54 Thu 16-Sep-93        Alan Glover                   

Fix bug in PutMaskScaled when plotting new mask sprites across depths

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

Version: 0.55 Tue 28-Sep-93        Alan Glover                   

Fix bug in PutMaskScaled due to calls where outoffset exceeded the amount
representable in an immediate LDR ra,[rb],#-rc instruction.

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

Version: 0.56 Thu 28-Oct-93        Alan Glover                   
                              
Bugfixes:

Put back CR at end of messages file - repairs BadDepth token bug.
Improve handling of CS returns from ReadModeVariable

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

Version: 0.57 Wed 03-Nov-93        Alan Glover                   

Fix bug MED-00879 in PlotMaskScaled 16bpp

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

Version: 0.58 Fri 19-Nov-93        Alan Glover                   

Fix bug MED-01156 - createremovepalette wasn't complaining when it should 
have done. Tidy up handling of any T>6 sprites passing through
---------------------------------------------------------------------------

Version: 0.59 Tue 23-Nov-93        Alan Glover                   

Fix bug MED-01281 - ensure errors returned for operations were new format
sprites with masks are prohibited

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

Version: 0.60 Wed 22-Dec-93        Alan Glover                   

Fix bug MED-01867 - restrict ignore_ttr behaviour to 8bpp full palette only -
ie only when it's really needed.

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

Version: 0.61 Wed 02-Feb-94        Alan Glover                   

Fix bugs MED-02291 & MED-02611. Translation table expansion into 16 and 32
bit values was going wrong; noticably for 16 (causing greys instead of white)
and more subtly for 32 (losing some low order bits of blue).

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

Purpose:
========



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


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


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

Version: 0.61 Wed 22-Jun-94        Aideen McConville             

Moved to new source tree.

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

Version: 0.62 Thu 26-May-94        Alan Glover                   

Begin Black implementation. Wide Translation tables now supported on
PutSpriteScaled and PlotSpriteTransformed. Direct plot from palette
behaviour removed totally. William Turner's new 1bpp mask routines
incorporated.

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

Version: 0.63 Fri 12-Aug-94        Graham Simms                  

MAJOR CHANGE.
William Stoye's new Sprite Extend 2 code used. This involves a total rewrite
of the putscaled engine, and the support for JPEG images. This code is a
modification of version 0.53 of SpriteExtend with the changes since then ported
to the new code, it needs thorough testing.

4 JPEG SWIs added. JPEG_Info, JPEG_FileInfo, JPEG_PlotScaled 
and JPEG_PlotFileScaled.
---------------------------------------------------------------------------

Version: 0.64 Fri 12-Aug-94        Graham Simms                  

Changed c.rojpeg so that CFSI-jpeg builds correctly.
---------------------------------------------------------------------------

Version: 0.65 Fri 12-Aug-94        Aideen McConville             

 * Makefile - standardised
   Added directory 'data'  (for CFSI-jpeg)
   Added directory 'utils' (for genpalette etc)

 * Sources.SprExtend - set standalone flag false

 * Version - updated to 0.65

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

Version: 0.66 Fri 12-Aug-94        Aideen McConville             

 * Makefile - fixed typo

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

Version: 0.67 Fri 12-Aug-94        Aideen McConville             

 * Ensure that 'ROM' versions use Messages from Resources:
   Changed Files: Sources.SprExtend
                  Sources.MsgCode
                  Sources.LocalMsgs (new)
                  Makefile
                  Version (0.67)

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

Version: 0.68 Tue 30-Aug-94        Graham Simms                  

Fixed bug in plotmaskscaled, which used an ECF pattern in an obscure way and always
assumed that a sprite would have a mask....

Added three new SWIs to SpriteExtend:
  JPEG_PDriverIntercept - Called by PDriver to set up interception of JPEG plot SWIs
  JPEG_PlotTransformed - Skeleton implementation of proposed SWI.
  JPEG_PlotFileTransformed - "          "        "      "     ".
  
Added extra error to Messages file.
---------------------------------------------------------------------------

Version: 0.69 Fri 02-Sep-94        Graham Simms                  

Fixed bug where registers weren't preserved in JPEG_FileInfo.

Changed JPEG_Plot SWIs so that dithering was switchable by using the
relevant flag.
---------------------------------------------------------------------------

Version: 0.70 Thu 15-Sep-94        Graham Simms  
                
Implemented JPEG_PlotTransformed SWI

Added switch to PutSpriteScaled to allow dithering of 16bpp+ sprites

Fixed bug where word aligned JPEGS were not printed with the correct width.

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

Version: 0.71 Mon 19-Sep-94        Graham Simms                  

Added extra bit to JPEG_PDriverIntercept SWI so tranaslation table is always used when printing.

Fixed bug where plotting sprites with 1bpp masks into 16bpp mode sometimes corrupted with 
non-aligned data.

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

Version: 0.72 Thu 22-Sep-94        Graham Simms                  

Changed behaviour of Plotscaled when a NULL scale factors pointer is passed.

Fixed bug in JPEG_PlotTransformed when x or y co-ordinate was -ve.

Added Error handling for C plotscaled engine. 

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

Version: 0.73 Mon 26-Sep-94        Graham Simms                  

Fixed bug where JPEG_PlotScaled junked r0.

Implemented JPEG_PlotFileTrnasformed SWI.

Corrected scaling in JPEG_PlotTransformed, it now correctly converts a OS units to OS units
magnification into a Pixels to Pixels magnification which is required by JPEG_PlotScaled.

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

Version: 0.74 Thu 29-Sep-94        Graham Simms                  

Impletmented Sprite_Reason CheckSpriteArea

Added extra return parameter to JPEG_Info SWIs. They now return the memory required in R6.

***All BLACK Functionality complete****

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

Version: 0.75 Fri 07-Oct-94        Graham Simms                  

Fixed bug when plotting sprites with 1bpp masks, not word aligned.

Made SpriteExtend putScaled plot from palette when R5:b4 is set.

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

Version: 0.76 Thu 20-Oct-94        Graham Simms                  

Added speed-ups to new plotscaled engine when plotting sprites at
large scales. This was because the performance was significantly
worse than the old engine in version 0.62.
---------------------------------------------------------------------------

Version: 0.77 Tue 25-Oct-94        Graham Simms                  

Made operation of 'dither' flag consistent when error-diffusing JPEGS

Added an error when Sprite 'PutSpriteGreyScaled' is called

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

Version: 0.78 Sat 29-Oct-94        Aideen McConville             

* Added hdr.SprExtend: contains SWI definitions; exported by Makefile

* Makefile: now copies data.CFSIjpeg to !ChangeFSI.CFSIjpeg at install time (disc)

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

Version: 0.79 Mon 31-Oct-94        Steve Cormie                  

* Added directed comments to Messages file for message tokenisation.

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

Version: 0.80 Tue 01-Nov-94        Graham Simms                  

* Fixed bug in Sources.SprExtend where SpriteOp 35 (Append Sprite) was
  junking r0 when returning an error.

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

Version: 0.81 Fri 11-Nov-94        Graham Simms                  

* c.rojpeg: Stopped JPEG_FileInfo from grabbing a 1k buffer and not returning it...

* Sources.SprExtend: Stopped error when trying to plot a JPEG without a translation table
                     which means that it works on pre RO3.5 machines.

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

Version: 0.82 Tue 15-Nov-94        Graham Simms                  

* Sources.SprExtend: Fixed Bug where PlotMaskScaled was only allowing for 8 bits of bg colour
			     

* c.PutScaled      : Fixed Bug where plotting with 'plot action' AND was zeroing out all other
		     pixels in a word.

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

Version: 0.83 Mon 21-Nov-94        Graham Simms                  

* c.SpriteOp       : Fixed bug in OS_SpriteOp CheckSpriteArea where it was being slightly too
		     pessimistic.
* c.rojpeg         : Added compiler flags so that CFSIjpeg compiles correctly.

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

Version: 0.84 Thu 24-Nov-94        Graham Simms                  

* Sources.SWIs     : Fixed bug where JPEG_PlotTransformed didn't forbid -ve scale factors. in Sources.SWIs

* Sources.SprExtend:
  Sources.SWIs
  c.rojpeg
  c.PutScaled      :Made SpriteExtend use a dynamic area to hold the JPEG workspace.

* Sources.CSupport :
  c.PutScaled      : Internationalised name of Dynamic Area. 

* Added 'DynName' token to Messages file as the Name of the Dynamic Area.

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

Fri 25-Nov-94 Aideen McConville: Makefile - use cc410, link411 (no new version made)

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

Version: 0.85 Mon 28-Nov-94        Graham Simms                  

* Sources.CSupport
  Sources.PutScaled : Moved create_dynamic_area function from Sources.Csupport so 
                      that CFSIjpeg compiles.

* Sources.SprExtend
  c.PutScaled       : Fixed bug in SpriteOp PaintCharScaled, where both the colour used 
                      for the plot and the plotting action were being junked.

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

Version: 0.86 Thu 01-Dec-94        Graham Simms                  

* c.SpriteOp        : Fixed small semantic bugs in checks performed by OS_SpriteOp CheckSpriteArea

* Sources.SWIs      : Stopped OS_SpriteOp CheckSpriteArea junking r12. 

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

Version: 0.87 Fri 02-Dec-94        Graham Simms                  

* c.PutScaled       : Fixed bug MED-04026. Where plotting Old Format 1bpp Sprites with a mask was failing.

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

Version: 0.88 Wed 07-Dec-94        Graham Simms                  

* c.Putscaled       : Fixed bug where a non-zero plot action was always plotted as plot action 0. 
                      The speed optimised code for scale factors >4 doesn't work, so the code now 
                      reverts to the non-optimised code in this case. This fixes MED-03952 and MED-03968.

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

Version: 0.89 Fri 16-Dec-94        Graham Simms                  

* c.SpriteOp        : Changed functionality of OS_SpriteOp CheckSpriteArea so that a sprite's mask 
                      has to be either 1bpp for new sprites or the same size as the sprite image.
* c.PutScaled       : Fixed bug introduced into version 0.88 for plotting at 5:1+ scale without a mask. Ooops.

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

Version: 0.90 Wed 01-Feb-95        Graham Simms                  

* Sources.SWIs
  c.Putscaled       : Added extra bit to JPEG_Plot(File)Scaled SWIs to allow error-diffused dithering

* Sources.SWIs      : Made JPEG SWIs error when invalid flag bits are set.

* c.putscaled       : made trace code not divide by immediate constant 10. This stops cc505 using the
                      optimised divide routine.
                     
* Makefile          : Updated to use cc5.05 by running the generated assembler through 'Sed' to make it aasm
                      compatible.
                      
---------------------------------------------------------------------------

Version: 0.91 Thu 09-Feb-95        Graham Simms                  

* c.Putscaled       : Fixed MED-04481 where all plot actions except 'Set' were failing for scaling > 1:1 
                      in 16M colour modes

* Sources.SprExtend : Fixed MED-04508 where an invalid spritearea pointer was causing a crash even when 
                      it was not required in the spriteOp.

* Sources.SWIs      : Made JPEG_PlotFileScaled successfully return when invalid flag bits set.

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

Version: 0.92 Mon 13-Feb-95        Graham Simms                  

* c.Putscaled       : Fixed MED-04131 where JPEG files with pixel sizes of ratio 0:0 were causing a divide by
                      zero error. A 0:0 ratio is now interpreted as 1:1, which returns as 90:90 dpi.

* Makefile          : altered so that CFSIjpeg compiles with new C compiler.

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

Version: 0.93 Wed 15-Feb-95        Graham Simms                  

* Sources.SprExtend
  c.PutScaled       : Fixed MED-04158 where the PutScaled blitter was running out of registers when 
                      you plotted a new format sprite <=8bpp, with a mask into a 16bpp mode plotting 
                      directly from the palette (phew!). SpriteExtend now converts the palette to 16bpp
                      and sticks it in the space resevered for the creation of a translation table. 
                      This avoids the need for conversion on the fly which requires 2 temporary registers.

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

Version: 0.94 Fri 24-Feb-95        Graham Simms                  

* h.jinclude
  h.rojpeg
  c.rofpeg
  c.PutScaled
  Sources.SprExtend
  Sources.diffuse   : Made error diffusion work with an arbitary palette held in 'newtranstable' a 
                      pointer to which is stacked before entering the diffusion code. I Investigated which 
                      palettes were unsuitable for error-diffusion and came to the conclusion that the 
                      error-diffused result was always superior to the ordered-dither.
                      This Fixes bug MED-04727.

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

Version: 0.95 Fri 24-Feb-95        Graham Simms                  

* Sources.SprAdjSize : Checked in changes made in December but overlooked (oops!).
                       Fixes MED-04141, MED-03966 and  MED-03967


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

Version: 0.96 Mon 27-Feb-95        Graham Simms                  

* Sources.SprExtend : Fixed bug where optimised code for scales >4:1 failed with obscure values 
                      eg 575:115 which draw supplied! This is fixed by trying to rationalise 
                      the scale factors near the beginning of a sprite plot.

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

Version: 0.97 Tue 28-Feb-95        Graham Simms                  

* c.PutScaled       : Fixed bug when outputting to 32bpp mode with large output scales.

* c.rojpeg
  sources.diffuse   : Removed warnings and error from CFSIjpeg compilation. Fixes MED-04643

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

Version: 0.98 Fri 17-Mar-95        Graham Simms                  

* c.PutScaled       : Fixed bug MED-05018. r_xcount was not being initialised with 16bpp 
                      source and dither bit set.

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

Version: 0.99 Wed 29-Mar-95        Graham Simms                  

* c.PutScaled
  Sources.SprExtend : Fixed bug MED-05084. Redone the plotmask code to use the ECF AND & 
                      EOR mask in the kernel. This has to be done because when doing a 
                      PaintCharScaled of a delete char. the kernel sneakily replaces the 
                      foreground colour with the background one to make it work.