1 /*
2 
3 mapIndex.h
4 
5 These are included from engine.c and imageSynth.c so they can be inlined.
6 This is a simple, portable model for inlining: Declared/defined in one place.
7 If the compiler does not inline, may be separate instances of the function in separate compilation units.
8 
9 Indexing into a Map
10 
11 This is dictated by use of glib GArray
12 instead of conventional malloc and pointer arithmetic.
13 Presumably glib does good error checking for malloc.
14 Here we also do arithmetic for indexing a dynamic multi-dimensional array.
15 Here we use static inline rather than a macro.
16 !!! g_array_index is a macro that casts types.
17 !!! g_array_index returns an element, we return address of.
18 
19   Copyright (C) 2010, 2011  Lloyd Konneker
20 
21   This program is free software; you can redistribute it and/or modify
22   it under the terms of the GNU General Public License as published by
23   the Free Software Foundation; either version 2 of the License, or
24   (at your option) any later version.
25 
26   This program is distributed in the hope that it will be useful,
27   but WITHOUT ANY WARRANTY; without even the implied warranty of
28   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29   GNU General Public License for more details.
30 
31   You should have received a copy of the GNU General Public License
32   along with this program; if not, write to the Free Software
33   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
34 */
35 
36 
37 
38 /*
39 !!! Note in this case the 3rd dimension, depth, varies.
40 i.e. a Pixel is a variable-length array of Pixelels.
41 */
42 static inline Pixelel*
pixmap_index(const Map * const map,const Coordinates coords)43 pixmap_index(
44   const Map * const map,
45   const Coordinates coords
46   )
47 {
48   guint index = (coords.x + coords.y * map->width) * map->depth;
49   return &g_array_index(map->data, Pixelel, index);
50 }
51 
52 /* Return pointer to guint at coordinates in map. */
53 static inline guint*
intmap_index(Map * map,const Coordinates coords)54 intmap_index(
55   Map* map,
56   const Coordinates coords
57   )
58 {
59   guint index = coords.x + coords.y * map->width;
60   return &g_array_index(map->data, guint, index);
61 }
62 
63 /* Return pointer to coordinates at coordinates in map. */
64 static inline Coordinates*
coordmap_index(Map * map,const Coordinates coords)65 coordmap_index(
66   Map* map,
67   const Coordinates coords
68   )
69 {
70   guint index = coords.x + coords.y * map->width;
71   return &g_array_index(map->data, Coordinates, index);
72 }
73 
74 /* Return pointer to boolean at coordinates in Pixmap. */
75 /* Use guchar as boolean, which corresponds to current use of bytes for masks in Gimp. */
76 static inline guchar*
bytemap_index(Map * map,const Coordinates coords)77 bytemap_index(
78   Map* map,
79   const Coordinates coords
80   )
81 {
82   guint index = coords.x + coords.y * map->width;
83   return &g_array_index(map->data, guchar, index);
84 }
85 
86 
87