home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / lang / dial_cde.hqx / bitmap.c < prev    next >
Text File  |  1988-09-05  |  3KB  |  176 lines

  1. /*
  2.  * resizable bitmaps
  3.  */
  4.  
  5. #include "bitmap.h"
  6.  
  7. PBMap
  8. BMNew()
  9. {
  10. PBMap p;
  11.  
  12.     if (p = (PBMap)NewPtr(sizeof(Bmap))) {
  13.         if (p->data = NewHandle(0)) {
  14.             p->bm.baseAddr = 0;
  15.             p->bm.rowBytes = 0;
  16.             SetRect(&p->bm.bounds,0,0,0,0);
  17.         } else {
  18.             DisposPtr(p);
  19.             p = nil;
  20.         }
  21.     }
  22.     return p;
  23. }
  24.  
  25. void
  26. BMDispose(p)
  27. PBMap p;
  28. {
  29.     if (p) {
  30.         if (p->data)
  31.             DisposHandle(p->data);
  32.         DisposPtr(p);
  33.     }
  34. }
  35.  
  36. /*
  37.  * expand bitmap to fit rect.  do not shrink bitmap unless rect has a zero width
  38.  * or height, in which case zero it.
  39.  * black and white bitmaps only.
  40.  * if rect has just translated, just change the bounds record of the bitmap.
  41.  */
  42. void
  43. BMSize(p,r)
  44. PBMap p;
  45. Rect *r;
  46. {
  47. int ht,wid,oht,owid,rb;
  48. long int len;
  49.  
  50.     if (p) {
  51.         ht = r->bottom - r->top;
  52.         wid = r->right - r->left;
  53.         oht = p->bm.bounds.bottom - p->bm.bounds.top;
  54.         owid = p->bm.bounds.right - p->bm.bounds.left;
  55.         if ((ht == oht) && (wid == owid)) {
  56.             p->bm.bounds = *r;
  57.         } else {
  58.             if ((ht>0) && (wid>0)) {
  59.                 rb = ((wid+15) >> 3) & 0xfffe;
  60.                 len = rb * ht;
  61.                 if (len>GetHandleSize(p->data))
  62.                     SetHandleSize(p->data,len);
  63.                 if (!MemError()) {
  64.                     p->bm.bounds = *r;
  65.                     p->bm.rowBytes = rb;
  66.                 }
  67.             } else {
  68.                 SetHandleSize(p->data,0);
  69.                 SetRect(&p->bm.bounds,0,0,0,0);
  70.                 p->bm.rowBytes = 0;
  71.             }
  72.         }
  73.     }
  74. }
  75.  
  76. /*
  77.  * just offset the bitmap
  78.  */
  79. void 
  80. BMMoveTo(p,x,y)
  81. PBMap p;
  82. int x,y;
  83. {
  84.     if (p) {
  85.         OffsetRect(&p->bm.bounds,x-p->bm.bounds.left,y-p->bm.bounds.top);
  86.     }
  87. }
  88.  
  89. /*
  90.  * basically, call copybits with this bitmap.  all copybits calls should go through
  91.  * here since here is where we ensure that the bitmap address is that of
  92.  * our locked data.
  93.  */
  94. void
  95. BMCopyBits(p,pdbm,sr,dr,m,c)
  96. PBMap p;
  97. BitMap *pdbm;
  98. Rect *sr;
  99. Rect *dr;
  100. int m;
  101. RgnHandle c;
  102. {
  103. char state;
  104.  
  105.     if (p) {
  106.         state = HGetState(p->data);
  107.         HLock(p->data);
  108.         
  109.         p->bm.baseAddr = *p->data;
  110.         CopyBits(&p->bm,pdbm,sr,dr,m,c);
  111.         
  112.         HSetState(p->data,state);
  113.     }
  114. }
  115.  
  116. /*
  117.  * clear all the data
  118.  */
  119. void 
  120. BMErase(p)
  121. PBMap p;
  122. {
  123. long int len;
  124. register int *s,*t;
  125.  
  126.     if (p && (len = GetHandleSize(p->data))) {
  127.         for (s = (int *)(*p->data), t = s + len/sizeof(int); s != t; *s++ = 0){};
  128.     }
  129. }
  130.  
  131. /*
  132.  * copy the whole bitmap
  133.  */
  134. void
  135. BMDraw(p)
  136. PBMap p;
  137. {
  138. GrafPtr thePort;
  139.  
  140.     if (p) {
  141.         GetPort(&thePort);
  142.         BMCopyBits(p,&thePort->portBits,&p->bm.bounds,&p->bm.bounds,srcCopy,0L);
  143.     }
  144. }
  145.  
  146. /*
  147.  * draw something in the bitmap.
  148.  * the function gets called with the pointer d.
  149.  */
  150. void
  151. BMInBitMapDo(p,f,d)
  152. PBMap p;
  153. void (*f)();
  154. void *d;
  155. {
  156. GrafPtr temPort;
  157. GrafPort port;
  158. char state;
  159.  
  160.     if (p) {
  161.         GetPort(&temPort);
  162.         OpenPort(&port);
  163.         if (!MemError()) {
  164.             state = HGetState(p->data);
  165.             HLock(p->data);
  166.             p->bm.baseAddr = *p->data;
  167.             SetPortBits(&p->bm);
  168.             
  169.             (*f)(d);
  170.             
  171.             HSetState(p->data,state);
  172.         }
  173.         ClosePort(&port);
  174.         SetPort(temPort);
  175.     }
  176. }