chiark / gitweb /
undo broken deletion
[trains.git] / layout / segcmap.h
1 /*
2  *
3  * Encodings in segment encodings are as follows:
4  *  firstly, assemble the R, G, B bytes like this:
5  *       blue << 16  |  green << 8  |  red
6  *
7  * Now interpret the resulting 24-bit datum as a bit sequence with the
8  * following parts (LSbit first):
9  *     bits
10  *       6*     moveable feature and position                  } blue
11  *      10*     segment number                                 } blue/green
12  *       1*     reserved - always 0                            } red
13  *       1      `edge'(1) or `core'(0) - currently always 0    } red
14  *       6*     angle                                          } red
15  *
16  * Each moveable feature has an unambiguous prefix the remainder of the
17  * moveable feature bits are the positions of that feature (LSbit is
18  * position 0, etc.).  The feature all-bits-0 (0b000000) is for fixed track
19  * (so every moveable feature prefix must contain at least one 1).
20  *
21  * The unknown/unidentified segment is all-bits-0, with feature
22  * all-bits-0 for fixed parts and feature 0b1.... for moveable parts.
23  * Background is white (all bits set).
24  *
25  * Items marked * can have the number of bits allocated to them
26  * adjusted - see below.
27  */
28
29 #ifndef SEGMAP_H
30 #define SEGMAP_H
31
32 /*---------- bit widths of various datum fields ----------*/
33
34 /* Be careful editing this bit: you may change the number of bits
35  * within reason, but preserve the format of the file and the ordering
36  * of info in the datum.  Both segcmapassign and layout have knowledge
37  * of the datum format that doesn't come from here, although there
38  * are arrangements to read the widths of the fields from here. */
39
40 #define ANGLE_BITS        6
41 #define MOVFEATPOS_BITS   6
42 #define SEGNUM_BITS      10
43 /* do not make these add up to more than 23 */
44
45 /*---------- datum assembly/disassembly macros ----------*/
46
47 /* It wouldn't be adviseable to change things beyond this point
48  * without considering editing segcmapassign, layout and various
49  * comments and associated machinery. */
50
51 #define BACKGROUND    0x00ffffffUL
52
53 #define MOVFEATPOS_BASEBIT (24-MOVFEATPOS_BITS)
54 #define SEGNUM_BASEBIT (MOVFEATPOS_BASEBIT-SEGNUM_BITS)
55 #define ANGLE_BASEBIT 0
56 #define RESERVED_BITS (SEGNUM_BASEBIT-ANGLE_BITS-1)
57
58 #define MKMASK(v) (((1UL)<<(v))-1)
59 #define DATUM2(q,l) (((l) >> q##_BASEBIT) & MKMASK(q##_BITS))
60 #define RESERVED_MASK (MKMASK(RESERVED_BITS+1) << ANGLE_BITS)
61
62 /*---------- useful utility macros ----------*/
63                        
64 #define RGB2DATUM(r,g,b) (((b)<<16) | ((g)<<8) | (r))
65 #define ARY2DATUM(a) RGB2DATUM((a)[0],(a)[1],(a)[2])
66 #define DATUM2RGB(r,g,b,l) ((r) = (l)&0xff,             \
67                             (g) = ((l)>>8)&0xff,        \
68                             (b) = ((l)>>16)&0xff)
69 #define DATUM2ARY(a,l) DATUM2RGB((a)[0],(a)[1],(a)[2],(l))
70
71 /*---------- consequences of the datum format ----------*/
72
73 #define ANGLE_TOPBIT (1<<(ANGLE_BITS-1))
74 #define NANGLES (1<<ANGLE_BITS)
75 #define ANGLE_MAX (NANGLES-1)
76
77 #define SEGNUM_MAX ((1<<SEGNUM_BITS)-1)
78 #define MOVFEATPOS_MAX ((1<<MOVFEATPOS_BITS)-1)
79
80
81 #endif /*SEGMAP_H*/