Map.Mesa
Mik Lamming - September 30, 1982 11:37 am
DIRECTORY
Inline   USING [BITAND, BITSHIFT],
Rope   USING [ROPE];
Map: DEFINITIONS
IMPORTS Inline = BEGIN
ColorMapSize: TYPE = [0..256);
ColorRange: TYPE = REAL;
ColorList: TYPE = LIST OF Triplet;
Triplet: TYPE = REF TripletRec;
TripletRec: TYPE = RECORD [r, g, b: ColorRange];
PalTable: TYPE = REF PalTableRec;
PalTableRec: TYPE = RECORD [
size:CARDINAL,
data:SEQUENCE ix:CARDINAL OF TripletRec];
ColorTable: TYPE = REF ColorTableRec;
ColorTableRec: TYPE = RECORD [
size:CARDINAL,
data:SEQUENCE ix:CARDINAL OF ColorMapSize];
This module provides a mechanism for reducing a 24 bit color to an 8 bit index into a color palette. The goal is to allow a user to fill up a device's color palette (hardware) with any small subset of 24-bit colors. Given this set of colors, this module will generate a mapping table from the complete set of colors (16777216) to the subset contained in the color palette. 24 bit colors are mapped down to 8 bit indexes by separating the 24 bit color into 8 bit R,G,B and truncating each of these to 4 bits then combining the result to give a 12 bit index into a 4096 element table - the ColorTable. Each element in the color table contains the index of the 'nearest' color in the palette. During creation of the color map determination the 'nearest' color is the responsibility of a client procedure 'ErrorFunc'. The module also provides facilities for storing and retrieving [table, palette] pairs and a function for doing the actual mapping using the ColorTable.
ErrorFunc: TYPE = PROCEDURE [r1,g1,b1,r2,g2,b2: ColorRange] RETURNS [error:REAL] ;
A user supplied routine used by ListToTable to determine hte 'difference' between two colours
GetIndex: PUBLIC PROC [r, g, b:INTEGER, table:ColorTable] RETURNS [palix:CARDINAL] = INLINE {
Returns the index of the 'nearest' color in the color palette to [r,g,b]. r,g and b are supposed to lie in the range [0..256) and will be rounded if they don't
r ← MAX[0, MIN[r+7,255]]; g ← MAX[0, MIN[g+7,255]]; b ← MAX[0, MIN[b+7,255]];
palix ← table[Inline.BITSHIFT[Inline.BITAND[r, 360B], 4] + Inline.BITSHIFT[Inline.BITAND[g, 360B],0] + Inline.BITSHIFT[Inline.BITAND[b, 360B],-4]];
};
InsertColor: PUBLIC PROCEDURE [r,g,b: ColorRange, list: ColorList ← NIL] RETURNS [ColorList] = INLINE {RETURN [CONS[NEW[TripletRec ← [r, g, b]], list]]};
Generates a list of colors, later to be used by ListToTable to create the colormap and palette tables
NullList: SIGNAL [];
A signal raised by ListToTable if it doesnt have anything sensible to work on
ListToTable: PUBLIC PROCEDURE [list: ColorList, error: ErrorFunc ← Vector3D] RETURNS [colorTable: ColorTable, pal:PalTable] ;
Creates a PalTable and a 4096 element ColorTable of indexes into a PalTable. The PalTable is a tabular version of the ColorList.
MakeStandardList: PUBLIC PROCEDURE [redMax, greenMax, blueMax, grayMax:CARDINAL] RETURNS [l:ColorList] ;
Generates a list of colors containing:
a) every combination of colors that can be generated using redMax equally spaced reds, greenMax equally spaced greens, and blueMax equally spaced blues.
b) grayMax equally spaced grays excluding black and white
Restore: PUBLIC PROCEDURE [fileName:Rope.ROPE] RETURNS [table:ColorTable, pal:PalTable] ;
Restore a color table and palette from given file
Save: PUBLIC PROCEDURE [fileName:Rope.ROPE, table:ColorTable←NIL, pal:PalTable] ;
Saves a color table and palette on a file
Vector3D: PUBLIC ErrorFunc;
A default error function which uses Cartesian distance to determine the nearness of two colors
END.