ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/Development/ray/src/common/abitmap.h
Revision: 2.3
Committed: Fri Sep 12 00:44:38 2025 UTC (13 days, 4 hours ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.2: +1 -4 lines
Log Message:
chore: Removed copyrights as these moved from panlib into Radiance distro.

File Contents

# Content
1 /* RCSid $Id: abitmap.h,v 2.2 2024/08/24 23:25:24 greg Exp $ */
2 /*
3 * abitmap.h
4 *
5 * General bitmap class (mostly inline)
6 *
7 * Created by gward on Tue May 15 2001.
8 */
9 #ifndef _ABITMAP_H_
10 #define _ABITMAP_H_
11
12 #ifndef _TIFF_
13 #include "tiff.h" /* needed for uint32 type */
14 #endif
15
16 #define ABMend 0xffffffff // terminal return
17
18 // Inline bitmap class
19 class ABitMap {
20 private:
21 uint32 * bmap; // bitmap storage
22 uint32 len; // bitmap size
23 public:
24 ABitMap() { len=0; bmap=0; }
25 ABitMap(uint32 n, bool clrset=false) {
26 bmap=0; len=0; NewBitMap(n,clrset);
27 }
28 ABitMap(const ABitMap &orig) {
29 bmap=0; len=0; *this=orig;
30 }
31 ~ABitMap() {
32 delete [] bmap;
33 }
34 // Access to raw word data
35 uint32 * base() {
36 return bmap;
37 }
38 // Read-only word access
39 const uint32 * base() const {
40 return bmap;
41 }
42 // Number of words from #bits
43 static int32 bmlen(uint32 nbits) {
44 return (nbits+0x1f)>>5;
45 }
46 // Total number of words
47 int32 bmlen() const {
48 return bmlen(len);
49 }
50 // Reallocate and clear bitmap
51 bool NewBitMap(uint32 n, bool clrset=false);
52 // Clear bitmap to all 0's or 1's
53 void ClearBitMap(bool clrset=false);
54 // Steal another bitmap's contents
55 bool Take(ABitMap *srcp) {
56 if (srcp == this) return false;
57 delete [] bmap; bmap=0; len=0;
58 if (!srcp) return false;
59 bmap=srcp->bmap; len=srcp->len;
60 srcp->bmap=0; srcp->len=0;
61 return len;
62 }
63 // Return number of bits in bitmap
64 uint32 Length() const {
65 return len;
66 }
67 // Get original length if RLE (or 0)
68 uint32 RLength() const;
69 // Compress into a run-length encoded bitmap
70 bool GetRLE(ABitMap *rlep) const;
71 // Reconstitute bits from RLE encoding
72 bool SetFromRLE(const ABitMap &rle);
73 // Extract bitmap section (true if some overlap)
74 bool GetBits(ABitMap *dp, uint32 i) const;
75 // Extract bitmap section (fill if past end)
76 ABitMap GetBits(uint32 i, uint32 n, bool fill=false) const {
77 ABitMap bm(n, fill);
78 GetBits(&bm, i);
79 return bm;
80 }
81 // Overlay bitmap section (ignores bits past end)
82 bool AssignBits(uint32 i, const ABitMap &src);
83 // Apply operation to bitmap section
84 bool OpBits(uint32 i, char op, const ABitMap &src);
85 // Clear bitmap section
86 void ClearBits(uint32 i, uint32 n, bool clrset=false);
87 // Clear bits set in second bitmap
88 bool ClearBitsFrom(const ABitMap &src);
89 // Count number of bits set in bitmap
90 uint32 SumTotal(bool bit2cnt = true) const;
91 // Return the next bit position matching val (or ABMend)
92 uint32 Find(uint32 i = 0, bool val = true) const;
93 uint32 Find(int i, bool val = true) const {
94 return Find((uint32)i, val);
95 }
96 uint32 Find(long i, bool val = true) const {
97 return Find((uint32)i, val);
98 }
99 // Different interface to find the next bit matching val
100 bool Find(uint32 *ip, bool val = true) const {
101 if (!ip) return false;
102 if (*ip >= len) return false;
103 *ip = Find(*ip, val);
104 return (*ip < len);
105 }
106 // Access word for i'th bit
107 uint32 & Word(uint32 i) {
108 static uint32 dummy;
109 if (i >= len) return dummy;
110 return bmap[i>>5];
111 }
112 // Get word value for i'th bit (0 if out of range)
113 uint32 WordV(uint32 i) const {
114 if (i >= len) return 0;
115 return bmap[i>>5];
116 }
117 // Index corresponding bit in word
118 static uint32 Bit(uint32 i) {
119 return 1 << (i & 0x1f);
120 }
121 // Return value of i'th bit in bitmap
122 bool Check(uint32 i) const {
123 return (WordV(i) & Bit(i));
124 }
125 // Set i'th bit
126 void Set(uint32 i) {
127 Word(i) |= Bit(i);
128 }
129 // Reset i'th bit
130 void Reset(uint32 i) {
131 Word(i) &= ~Bit(i);
132 }
133 // Set i'th bit explicitly on or off
134 void Set(uint32 i, bool switchon) {
135 uint32 b = Bit(i);
136 uint32 & w = Word(i);
137 if (switchon) w |= b;
138 else w &= ~b;
139 }
140 // Toggle i'th bit
141 void Toggle(uint32 i) {
142 Word(i) ^= Bit(i);
143 }
144 // Set i'th bit if it's clear (or fail)
145 bool TestAndSet(uint32 i) {
146 if (i >= len) return false;
147 uint32 b = Bit(i);
148 uint32 & w = Word(i);
149 uint32 wasset = w & b;
150 w |= b;
151 return !wasset;
152 }
153 // Clear i'th bit if it's set (or fail)
154 bool TestAndReset(uint32 i) {
155 if (i >= len) return false;
156 uint32 b = Bit(i);
157 uint32 & w = Word(i);
158 uint32 wasset = w & b;
159 w &= ~b;
160 return wasset;
161 }
162 // Set i'th bit on/off (fail if no change)
163 bool TestAndSet(uint32 i, bool switchon) {
164 return switchon ? TestAndSet(i) : TestAndReset(i);
165 }
166 // Invert the entire bitmap
167 void Invert();
168 // Downward shift operator, zero fill
169 ABitMap & operator>>=(uint32 nbits);
170 // Upward shift operator, zero fill
171 ABitMap & operator<<=(uint32 nbits);
172 // Copy operator
173 ABitMap & operator=(const ABitMap &src);
174 // Bitwise OR-copy operator
175 ABitMap & operator|=(const ABitMap &src);
176 // Bitwise AND-assign operator
177 ABitMap & operator&=(const ABitMap &src);
178 // Bitwise XOR-assign operator
179 ABitMap & operator^=(const ABitMap &src);
180 // Subtraction operator, synonym for ClearBitsFrom()
181 ABitMap & operator-=(const ABitMap &src) {
182 ClearBitsFrom(src);
183 return *this;
184 }
185 // Compare two bitmaps for equality
186 bool operator==(const ABitMap &that) const;
187 };
188
189 inline bool
190 operator!=(const ABitMap &bm1, const ABitMap &bm2)
191 {
192 return !(bm1 == bm2);
193 }
194
195 inline ABitMap
196 operator>>(ABitMap bmLeft, uint32 nbr)
197 {
198 return bmLeft >>= nbr;
199 }
200
201 inline ABitMap
202 operator<<(ABitMap bmLeft, uint32 nbl)
203 {
204 return bmLeft <<= nbl;
205 }
206
207 inline ABitMap
208 operator|(ABitMap bmLeft, const ABitMap &bmRight)
209 {
210 return bmLeft |= bmRight;
211 }
212
213 inline ABitMap
214 operator&(ABitMap bmLeft, const ABitMap &bmRight)
215 {
216 return bmLeft &= bmRight;
217 }
218
219 inline ABitMap
220 operator^(ABitMap bmLeft, const ABitMap &bmRight)
221 {
222 return bmLeft ^= bmRight;
223 }
224
225 inline ABitMap
226 operator-(ABitMap bmLeft, const ABitMap &bmRight)
227 {
228 return bmLeft -= bmRight;
229 }
230
231 inline ABitMap
232 operator~(ABitMap bmUnary)
233 {
234 bmUnary.Invert();
235 return bmUnary;
236 }
237
238 // 2-dimensional bitmap class
239 class ABitMap2 : protected ABitMap {
240 private:
241 int width, height; // bitmap dimensions
242 protected:
243 uint32 bmi(int x, int y) const {
244 if (OffBitMap(x, y)) return ABMend;
245 return (uint32)y*width + x;
246 }
247 public:
248 ABitMap2() { width=height=0; }
249 ABitMap2(int w, int h, bool clrset=false) :
250 ABitMap((w>0)&(h>0)&&(w <= ABMend/h)
251 ? (uint32)w*h : (uint32)0, clrset) {
252 if (Length()) {
253 width=w; height=h;
254 } else { width=height=0; }
255 }
256 ABitMap2(const ABitMap2 &orig) {
257 *this=orig;
258 }
259 // Access to raw word data
260 uint32 * base() {
261 return ABitMap::base();
262 }
263 // Read-only word access
264 const uint32 * base() const {
265 return ABitMap::base();
266 }
267 // Total number of words
268 int32 bmlen() const {
269 return ABitMap::bmlen();
270 }
271 // Return bitmap width
272 int Width() const {
273 return width;
274 }
275 // Return bitmap height
276 int Height() const {
277 return height;
278 }
279 // Is the indicated bit off our bitmap?
280 bool OffBitMap(int x, int y) const {
281 return ((x < 0) | (x >= width) |
282 (y < 0) | (y >= height));
283 }
284 // Count number of bits set in bitmap
285 uint32 SumTotal(bool bit2cnt = true) const {
286 return ABitMap::SumTotal(bit2cnt);
287 }
288 // Reallocate and clear bitmap
289 bool NewBitMap(int w, int h, bool clrset=false) {
290 if ((w <= 0) | (h <= 0) || w > ABMend/h)
291 w = h = 0;
292 width=w; height=h;
293 return ABitMap::NewBitMap((uint32)w*h, clrset);
294 }
295 // Clear bitmap to all 0's or 1's
296 void ClearBitMap(bool clrset=false) {
297 ABitMap::ClearBitMap(clrset);
298 }
299 // Compress with run-length encoding into a 1-D bitmap
300 bool GetRLE(ABitMap *rlep) const {
301 return ABitMap::GetRLE(rlep);
302 }
303 // Reconstitute bits from RLE encoding (size must match)
304 bool SetFromRLE(int w, int h, const ABitMap &rle);
305 // Steal another bitmap's contents
306 bool Take(ABitMap2 *srcp) {
307 if (srcp == this) return false;
308 width=height=0;
309 if (!ABitMap::Take(srcp)) return false;
310 width=srcp->width; height=srcp->height;
311 srcp->width=srcp->height=0;
312 return true;
313 }
314 // Extract bitmap section (true if some overlap)
315 bool GetRect(ABitMap2 *dp, int sx, int sy) const;
316 // Extract bitmap section (fill outside overlap)
317 ABitMap2 GetRect(int sx, int sy, int w, int h, bool fill=false) const {
318 ABitMap2 bm2(w, h, fill);
319 GetRect(&bm2, sx, sy);
320 return bm2;
321 }
322 // Assign bitmap section (ignores anything past edges)
323 bool AssignRect(int dx, int dy, const ABitMap2 &src);
324 // Apply operation to bitmap section
325 bool OpRect(int dx, int dy, char op, const ABitMap2 &src);
326 // Clear a rectangle
327 void ClearRect(int x, int y, int w, int h, bool clrset=false);
328 // Find the next bit matching val (scanline order)
329 bool Find(int *xp, int *yp, bool val=true) const {
330 if (!xp | !yp) return false;
331 if (width <= 0) return false;
332 if ((*xp < 0) | (*yp < 0)) *xp = *yp = 0;
333 else if (*xp >= width) { *xp=0; ++(*yp); }
334 uint32 i = ABitMap::Find(bmi(*xp,*yp), val);
335 if (i == ABMend) { *yp = height; return false; }
336 *yp = int(i / width);
337 *xp = int(i - *yp*width);
338 return true;
339 }
340 // Get bounds of assigned region
341 bool GetBoundRect(int xymin[2], int wh[2], bool val=true) const;
342 // Return value of bit in bitmap
343 bool Check(int x, int y) const {
344 return ABitMap::Check(bmi(x,y));
345 }
346 // Set bit
347 void Set(int x, int y) {
348 ABitMap::Set(bmi(x,y));
349 }
350 // Reset bit
351 void Reset(int x, int y) {
352 ABitMap::Reset(bmi(x,y));
353 }
354 // Set bit explicitly on or off
355 void Set(int x, int y, bool switchon) {
356 ABitMap::Set(bmi(x,y), switchon);
357 }
358 // Toggle bit
359 void Toggle(int x, int y) {
360 ABitMap::Toggle(bmi(x,y));
361 }
362 // Set bit if it's clear (or fail)
363 bool TestAndSet(int x, int y) {
364 return ABitMap::TestAndSet(bmi(x,y));
365 }
366 // Clear bit if it's set (or fail)
367 bool TestAndReset(int x, int y) {
368 return ABitMap::TestAndReset(bmi(x,y));
369 }
370 // Set bit on/off (fail if no change)
371 bool TestAndSet(int x, int y, bool switchon) {
372 return ABitMap::TestAndSet(bmi(x,y), switchon);
373 }
374 // Invert the entire bitmap
375 void Invert() {
376 ABitMap::Invert();
377 }
378 // Shift bitmap, filling uncovered area as indicated
379 void Shift(int dx, int dy, int fill=0);
380 // Dilate (or erode) selection by given radius
381 void Expand(double rad, bool val=true);
382 // Clear bits set in second map
383 bool ClearBitsFrom(const ABitMap2 &src) {
384 if (width != src.width)
385 return false;
386 return ABitMap::ClearBitsFrom(src);
387 }
388 // Copy operator
389 ABitMap2 & operator=(const ABitMap2 &src) {
390 ABitMap::operator=(src);
391 width=src.width; height=src.height;
392 return *this;
393 }
394 // Bitwise OR-copy operator
395 ABitMap2 & operator|=(const ABitMap2 &src) {
396 ABitMap::operator|=(src);
397 width=src.width; height=src.height;
398 return *this;
399 }
400 // Bitwise AND-assign operator
401 ABitMap2 & operator&=(const ABitMap2 &src) {
402 if ((width!=src.width)|(height!=src.height))
403 return *this;
404 ABitMap::operator&=(src);
405 return *this;
406 }
407 // Bitwise XOR-assign operator
408 ABitMap2 & operator^=(const ABitMap2 &src) {
409 if ((width!=src.width)|(height!=src.height))
410 return *this;
411 ABitMap::operator^=(src);
412 return *this;
413 }
414 // Subtraction operator, synonym for ClearBitsFrom()
415 ABitMap2 & operator-=(const ABitMap2 &src) {
416 ClearBitsFrom(src);
417 return *this;
418 }
419 // Compare two bitmaps for equality
420 bool operator==(const ABitMap2 &that) const {
421 if (width != that.width)
422 return false;
423 return ABitMap::operator==(that);
424 }
425 };
426
427 inline bool
428 operator!=(const ABitMap2 &bm1, const ABitMap2 &bm2)
429 {
430 return !(bm1 == bm2);
431 }
432
433 inline ABitMap2
434 operator|(ABitMap2 bmLeft, const ABitMap2 &bmRight)
435 {
436 return bmLeft |= bmRight;
437 }
438
439 inline ABitMap2
440 operator&(ABitMap2 bmLeft, const ABitMap2 &bmRight)
441 {
442 return bmLeft &= bmRight;
443 }
444
445 inline ABitMap2
446 operator^(ABitMap2 bmLeft, const ABitMap2 &bmRight)
447 {
448 return bmLeft ^= bmRight;
449 }
450
451 inline ABitMap2
452 operator-(ABitMap2 bmLeft, const ABitMap2 &bmRight)
453 {
454 return bmLeft -= bmRight;
455 }
456
457 inline ABitMap2
458 operator~(ABitMap2 bmUnary)
459 {
460 bmUnary.Invert();
461 return bmUnary;
462 }
463
464 // Function to write a bitmap to a BMP file
465 extern bool WriteBitMap(const ABitMap &bm, const char *fname);
466
467 // Function to write a 2-D bitmap to a BMP file
468 extern bool WriteBitMap2(const ABitMap2 &bm2, const char *fname);
469
470 // Function to read a bitmap from a BMP file
471 extern bool ReadBitMap(ABitMap *bmp, const char *fname);
472
473 // Function to read a 2-D bitmap from a BMP file
474 extern bool ReadBitMap2(ABitMap2 *bm2p, const char *fname);
475
476 #endif // ! _ABITMAP_H_