home *** CD-ROM | disk | FTP | other *** search
- /*
- * resizable bitmaps
- */
-
- #include "bitmap.h"
-
- PBMap
- BMNew()
- {
- PBMap p;
-
- if (p = (PBMap)NewPtr(sizeof(Bmap))) {
- if (p->data = NewHandle(0)) {
- p->bm.baseAddr = 0;
- p->bm.rowBytes = 0;
- SetRect(&p->bm.bounds,0,0,0,0);
- } else {
- DisposPtr(p);
- p = nil;
- }
- }
- return p;
- }
-
- void
- BMDispose(p)
- PBMap p;
- {
- if (p) {
- if (p->data)
- DisposHandle(p->data);
- DisposPtr(p);
- }
- }
-
- /*
- * expand bitmap to fit rect. do not shrink bitmap unless rect has a zero width
- * or height, in which case zero it.
- * black and white bitmaps only.
- * if rect has just translated, just change the bounds record of the bitmap.
- */
- void
- BMSize(p,r)
- PBMap p;
- Rect *r;
- {
- int ht,wid,oht,owid,rb;
- long int len;
-
- if (p) {
- ht = r->bottom - r->top;
- wid = r->right - r->left;
- oht = p->bm.bounds.bottom - p->bm.bounds.top;
- owid = p->bm.bounds.right - p->bm.bounds.left;
- if ((ht == oht) && (wid == owid)) {
- p->bm.bounds = *r;
- } else {
- if ((ht>0) && (wid>0)) {
- rb = ((wid+15) >> 3) & 0xfffe;
- len = rb * ht;
- if (len>GetHandleSize(p->data))
- SetHandleSize(p->data,len);
- if (!MemError()) {
- p->bm.bounds = *r;
- p->bm.rowBytes = rb;
- }
- } else {
- SetHandleSize(p->data,0);
- SetRect(&p->bm.bounds,0,0,0,0);
- p->bm.rowBytes = 0;
- }
- }
- }
- }
-
- /*
- * just offset the bitmap
- */
- void
- BMMoveTo(p,x,y)
- PBMap p;
- int x,y;
- {
- if (p) {
- OffsetRect(&p->bm.bounds,x-p->bm.bounds.left,y-p->bm.bounds.top);
- }
- }
-
- /*
- * basically, call copybits with this bitmap. all copybits calls should go through
- * here since here is where we ensure that the bitmap address is that of
- * our locked data.
- */
- void
- BMCopyBits(p,pdbm,sr,dr,m,c)
- PBMap p;
- BitMap *pdbm;
- Rect *sr;
- Rect *dr;
- int m;
- RgnHandle c;
- {
- char state;
-
- if (p) {
- state = HGetState(p->data);
- HLock(p->data);
-
- p->bm.baseAddr = *p->data;
- CopyBits(&p->bm,pdbm,sr,dr,m,c);
-
- HSetState(p->data,state);
- }
- }
-
- /*
- * clear all the data
- */
- void
- BMErase(p)
- PBMap p;
- {
- long int len;
- register int *s,*t;
-
- if (p && (len = GetHandleSize(p->data))) {
- for (s = (int *)(*p->data), t = s + len/sizeof(int); s != t; *s++ = 0){};
- }
- }
-
- /*
- * copy the whole bitmap
- */
- void
- BMDraw(p)
- PBMap p;
- {
- GrafPtr thePort;
-
- if (p) {
- GetPort(&thePort);
- BMCopyBits(p,&thePort->portBits,&p->bm.bounds,&p->bm.bounds,srcCopy,0L);
- }
- }
-
- /*
- * draw something in the bitmap.
- * the function gets called with the pointer d.
- */
- void
- BMInBitMapDo(p,f,d)
- PBMap p;
- void (*f)();
- void *d;
- {
- GrafPtr temPort;
- GrafPort port;
- char state;
-
- if (p) {
- GetPort(&temPort);
- OpenPort(&port);
- if (!MemError()) {
- state = HGetState(p->data);
- HLock(p->data);
- p->bm.baseAddr = *p->data;
- SetPortBits(&p->bm);
-
- (*f)(d);
-
- HSetState(p->data,state);
- }
- ClosePort(&port);
- SetPort(temPort);
- }
- }