home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Blitting Class Library / Image Copier / ImageCopier.cp next >
Encoding:
Text File  |  1995-10-21  |  3.7 KB  |  118 lines  |  [TEXT/CWIE]

  1. // ImageCopier.cp, use the declared copying objects (CopyGrafs, DynamicCopy, and
  2. //    CopyBuffers) in your code to copy images instead of using CopyBits. Each
  3. //    operator() guarantees to set the port and the addressing mode back to their
  4. //    previous states when the function returns. Use CopyGrafs as you would CopyBits,
  5. //    i.e. the port must be set to the destination first.
  6.  
  7. // copyright © 1995, Macneil Shonle. All rights reserved.
  8.  
  9. #ifndef __IMAGECOPIER__
  10. #include <ImageCopier.h>
  11. #endif
  12.  
  13. #ifndef __GWORLDSETTER__
  14. #include <GWorldSetter.h>
  15. #endif
  16.  
  17. CGrafPortCopier CopyGrafs;
  18. BufferCopier CopyBuffers;
  19.  
  20. // sets the cache to an invalid state. Validate the cache by setting it to two legal ports,
  21. // the "use" member-function can do this
  22. CGrafPortCopier::CGrafPortCopier()
  23.     : source(0), dest(0)
  24. {
  25. }
  26.  
  27. // sets the cache to the parameters
  28. CGrafPortCopier::CGrafPortCopier(CGrafPtr srcPort, CGrafPtr dstPort)
  29.     : source(srcPort), dest(dstPort)
  30. {
  31. }
  32.  
  33. // copies the images within the given rectangles, using the cached source and destination
  34. // ports. Throws invalidargument if the cached source or destination port is invalid
  35. //    may throw: invalidargument
  36. void CGrafPortCopier::operator()(const Rect& srcR, const Rect& dstR)
  37.     throw(invalidargument) const
  38. {
  39.     if (!source || !dest)
  40.         throw invalidargument("source or dest not cached in", "GrafPortCopier::operator()");
  41.     
  42.     ::CopyBits(&GrafPtr(source)->portBits, &GrafPtr(dest)->portBits,
  43.         &srcR, &dstR, srcCopy, nil);
  44. }
  45.  
  46. // caches the ports to the parameters, and copies the images within the given rectangles
  47. void CGrafPortCopier::operator()(CGrafPtr src, CGrafPtr dest, const Rect& srcR,
  48.     const Rect& destR)
  49. {
  50.     this->use(src, dest);
  51.     this->operator()(srcR, destR);
  52. }
  53.  
  54. // caches the ports to the parameters
  55. void CGrafPortCopier::use(CGrafPtr srcPort, CGrafPtr dstPort)
  56. {
  57.     source = srcPort;
  58.     dest = dstPort;
  59. }
  60.  
  61. // sets the cache to an invalid state. Validate the cache by setting it to two legal buffers,
  62. // the "use" member-function can do this
  63. BufferCopier::BufferCopier()
  64.     : source(0), dest(0)
  65. {
  66. }
  67.  
  68. // sets the cache to the parameters
  69. BufferCopier::BufferCopier(BufferAccessor& srcBuff, BufferAccessor& destBuff)
  70.     : source(&srcBuff), dest(&destBuff)
  71. {
  72. }
  73.  
  74. // copies the images within the given rectangles, using the cached source and destination
  75. // buffers. Throws invalidargument if the cached source or destination buffer is invalid
  76. //    may throw: invalidargument
  77. void BufferCopier::operator()(const Rect& srcR, const Rect& dstR) throw(invalidargument) const
  78. {
  79.     if (!source || !dest)
  80.         throw invalidargument("source or dest not cached in", "BufferCopier::operator()");
  81.     
  82.     FourPixelsPtr srcPtr = (FourPixelsPtr)source->pixelAddr(srcR.left, srcR.top);
  83.     FourPixelsPtr destPtr = (FourPixelsPtr)dest->pixelAddr(dstR.left, dstR.top);
  84.     PixelCord numRows = srcR.bottom - srcR.top;
  85.     PixelCord copyWidth = srcR.right - srcR.left;
  86.     PixelCord copyWidthDiv4 = copyWidth / 4;
  87.     RowBytes srcRowSkip = source->rowBytes() - copyWidth;
  88.     RowBytes destRowSkip = dest->rowBytes() - copyWidth;
  89.     
  90.     do {
  91.         for (PixelCord x=0; x<copyWidthDiv4; x++)
  92.             *destPtr++ = *srcPtr++;
  93.         
  94.         if (copyWidth & 0x2)
  95.             *TwoPixelsPtr(destPtr)++ = *TwoPixelsPtr(srcPtr)++;
  96.         
  97.         if (copyWidth & 0x1)
  98.             *PixelPtr(destPtr)++ = *PixelPtr(srcPtr)++;
  99.         
  100.         PixelPtr(srcPtr) += srcRowSkip;
  101.         PixelPtr(destPtr) += destRowSkip;
  102.     } while (--numRows > 0);
  103. }
  104.  
  105. // caches the buffers to the parameters, and copies the images within the given rectangles
  106. void BufferCopier::operator()(BufferAccessor& src, BufferAccessor& dest,
  107.     const Rect& srcR, const Rect& destR)
  108. {
  109.     this->use(src, dest);
  110.     this->operator()(srcR, destR);
  111. }
  112.  
  113. // caches the buffers to the parameters
  114. void BufferCopier::use(BufferAccessor& srcPort, BufferAccessor& dstPort)
  115. {
  116.     source = &srcPort;
  117.     dest = &dstPort;
  118. }