256 entry palettes in the RISC OS sprite format

Introduction

The RISC OS sprite format provides for pictures with and without palettes in 1, 2 and 4 bits per pixel. For 8 bits per pixel a picture with no palette is dealt with rationally, but a picture with a palette will end up with 64 (pairs of) palette entries. (The pairs of entries are for the possibly flashing colours). The reader of the format must know how VIDC1's 16 entry palette works in 8bit mode and apply the necessary bit mashing.

By stepping gingerly around the sprite operations and using ColourTrans appropriately, sprites with 256 entry palettes can be created and displayed on all RISC OS machines. Of course, this ability to create and process a sprite with (say) 256 grey levels in it does not magically endow the hardware with the ability to display 256 grey levels! The display will be as close as possible i.e. 16 grey levels with standard hardware; unless the machine has been hardware extended (e.g. the Serial Port's PCATS Graphics Enhancer).

Format of a 256 entry palette sprite

A 256 entry palette sprite is precisely like a 16 entry palette sprite, save that there are 256 palette entries, each one consisting of a pair of "palette entry" words of the form &BBGGRR00 as for ColourTrans. All bits of the entry are significant.

Creating a 256 entry palette sprite

If you use OS_SpriteOp 15 to create a sprite with a palette, the OS will create a 64 entry palette as before. The best thing to do is to create a sprite with no palette and then to add 2048 to the following entries:

word 1: offset to next sprite
word 9: offset to sprite image
word 10: offset to sprite image/transparency mask

of the sprite and then to add 2048 to

word 4: offset to first free word

of the sprite area control block.

Then write the 256 double words of palette entries starting at word 12 of the sprite, keeping both items in a pair identical:

SYS"OS_SpriteOp",&10f,ram%,name$,0,X,Y,spritemode sptr%=ram%+ram%!8
pal%=sptr%+11*4
!(sptr%+8*4)+=2048
!(sptr%+9*4)+=2048
!sptr%+=2048
!(ram%+12)+=2048
FORZ%=0TO255:B%=palette!(Z%<<2)ANDNOT&FF pal%!(Z%*8)=B%:pal%!(Z%*8+4)=B%
NEXT

Manipulating a 256 entry palette sprite

All sprite operations work on the 256 entry palette sprite. One can even switch output to it and generate 256 grey level output into it (with appropriate care over the GCOL and TINT values required by the OS). Sprite areas containing 256 entry palette sprites may be loaded, saved etc.

Testing to see if a sprite is a 256 entry palette sprite

After creation, the 256 entry palette sprite behaves just like all the others, so it's important to be able to distinguish it on the occasions when it's needed (for example, when displaying it on the screen).

A 256 entry palette sprite will have the lowest of words 9 and 10 of the sprite control block equal to 2048+44 (&82C). (If it is already known that the sprite has no transparency mask, then one can test only word 9).

Displaying a 256 entry palette sprite

The call "ColourTrans_SelectTable" takes a 64 entry palette sprite (and 2, 4 and 16) and returns a pixel translation table as needed for OS_SpriteOp 52. For a 256 entry sprite, one needs to build a similar pixel translation table directly. The following code will compute a pixel translation table for any sprite (which hasn't a transparency mask):

IF sptr%!32=44 THEN
palptr%=0
ELSE
FOR grab%=0 TO 2048-8 STEP 8
paltemp%!(grab%>>1)=sptr%!(grab%+44) NEXT
palptr%=paltemp%
ENDIF
FORQ%=0TO255:pixtrans%?Q%=Q%:NEXT
IFsptr%!32=44+2048 THEN
FORQ%=0TO255
SYS"ColourTrans_ReturnColourNumber",palptr%!(Q%<<2) TO pixtrans%?Q% NEXT
ELSE
SYS "ColourTrans_SelectTable",m,palptr%,-1,-1,pixtrans% ENDIF
spx%=-1:FORQ%=0TO255:IFpixtrans%?Q%<>Q% spx%=pixtrans% NEXT

spx% is either -1 (if no translation needs to be done - this speeds up OS_SpriteOp 52 a lot!) or pixtrans%; it is passed to OS_SpriteOp 52 in register 7.

Conclusion

The ability to store, process and display 256 entry palette sprites represents a small but useful gain for the RISC OS desktop. Common formats can be converted to sprites without any loss of information and meaningfully displayed. (Greater fidelity of display can be had with !ChangeFSI (which can read and generate 256 entry palette sprites) or !Translator.)

Its still a good idea to use the default desktop palette whenever possible.