2 * Copyright (C) 2000, Matias Atria
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* Structures and functions to manipulate bitmaps */
25 /* bitmap unit (as in X's docs) */
26 typedef Uint32 BmUnit;
28 /* size (in bytes) of a bitmap atom */
29 #define BITMAP_BYTES 4
31 /* size (in bits) of a bitmap atom */
32 #define BITMAP_BITS (BITMAP_BYTES << 3)
41 #define BM_BYTES_PER_LINE(b) \
42 (ROUND((b)->width, BITMAP_BITS) * BITMAP_BYTES)
43 #define BM_WIDTH(b) (((BITMAP *)(b))->width)
44 #define BM_HEIGHT(b) (((BITMAP *)(b))->height)
46 #define BMBIT(n) ((BmUnit)1 << (n))
48 /* Macros to manipulate individual pixels in a bitmap
49 * (they are slow, don't use them)
52 #define bm_offset(b,o) (BmUnit *)((Uchar *)(b) + (o))
54 #define __bm_unit_ptr(b,x,y) \
55 bm_offset((b)->data, (y) * (b)->stride + \
56 ((x) / BITMAP_BITS) * BITMAP_BYTES)
58 #define __bm_unit(b,x,y) __bm_unit_ptr((b), (x), (y))[0]
60 #define BM_GETPIXEL(b,x,y) __bm_unit((b), (x), (y))
61 #define BM_SETPIXEL(b,x,y) (__bm_unit((b), (x), (y)) |= FIRSTMASKAT(x))
62 #define BM_CLRPIXEL(b,x,y) (__bm_unit((b), (x), (y)) &= ~FIRSTMASKAT(x))
65 * These macros are used to access pixels in a bitmap. They are supposed
66 * to be used like this:
73 /* position `unit' at coordinates (column_number, row_number) */
74 unit = (BmUnit *)((char *)bitmap->data + row_number * bitmap->stride
75 + (column_number / BITMAP_BITS);
76 /* loop over all pixels IN THE SAME ROW */
77 for(i = 0; i < number_of_pixels; i++) {
78 /* to test if a pixel is set */
80 /* yes, it is, do something with it */
82 /* to set/clear a pixel */
84 *unit |= mask; /* now you see it */
86 *unit &= ~mask; /* now you don't */
87 /* move to next pixel */
88 if(mask == LASTMASK) {
93 /* end of sample code */
96 /* bitmaps are stored in native byte order */
97 #ifdef WORD_BIG_ENDIAN
98 #define FIRSTSHIFT (BITMAP_BITS - 1)
100 #define NEXTMASK(m) ((m) >>= 1)
101 #define PREVMASK(m) ((m) <<= 1)
102 #define FIRSTSHIFTAT(c) (BITMAP_BITS - ((c) % BITMAP_BITS) - 1)
105 #define LASTSHIFT (BITMAP_BITS - 1)
106 #define NEXTMASK(m) ((m) <<= 1)
107 #define PREVMASK(m) ((m) >>= 1)
108 #define FIRSTSHIFTAT(c) ((c) % BITMAP_BITS)
111 #define FIRSTMASK BMBIT(FIRSTSHIFT)
112 #define FIRSTMASKAT(c) BMBIT(FIRSTSHIFTAT(c))
113 #define LASTMASK BMBIT(LASTSHIFT)
115 extern BITMAP *bitmap_alloc __PROTO((int, int));
116 extern BITMAP *bitmap_alloc_raw __PROTO((int, int));
117 extern void bitmap_destroy __PROTO((BITMAP *));
120 * set_row(bm, row, col, count, state):
121 * sets `count' pixels to state `onoff', starting from pixel
122 * at position (col, row). All pixels must lie in the same
125 extern void bitmap_set_col __PROTO((BITMAP *, int, int, int, int));
126 extern void bitmap_set_row __PROTO((BITMAP *, int, int, int, int));
128 extern void bitmap_paint_bits __PROTO((BmUnit *, int, int));
129 extern void bitmap_clear_bits __PROTO((BmUnit *, int, int));
131 extern BITMAP *bitmap_copy __PROTO((BITMAP *));
132 extern void bitmap_flip_horizontally __PROTO((BITMAP *));
133 extern void bitmap_flip_vertically __PROTO((BITMAP *));
134 extern void bitmap_flip_diagonally __PROTO((BITMAP *));
135 extern void bitmap_rotate_clockwise __PROTO((BITMAP *));
136 extern void bitmap_rotate_counter_clockwise __PROTO((BITMAP *));
137 extern void bitmap_flip_rotate_clockwise __PROTO((BITMAP *));
138 extern void bitmap_flip_rotate_counter_clockwise __PROTO((BITMAP *));
139 extern BITMAP *bitmap_convert_lsb8 __PROTO((Uchar *, int, int));
140 extern BITMAP *bitmap_convert_msb8 __PROTO((Uchar *, int, int));
143 extern void bitmap_print __PROTO((FILE *, BITMAP *));
145 #endif /* _BITMAP_H */