ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/abitmap.h
Revision: 2.1
Committed: Wed Aug 14 20:05:23 2024 UTC (8 months, 2 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
feat(rxpict): Added new C++ picture rendering tool with multi-processing and spectral output

File Contents

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