home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 114 / macaddict114.cdr / Software / Utilities / macam.0.8.4.dmg / macam sources / utilities / RGBScalerIncluded.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-07-09  |  5.5 KB  |  209 lines

  1. //This file is included multiple times from RGBScaler.m to produce several optimized and functions
  2.  
  3.  
  4. #ifdef RGBSCALER_MACROS
  5. //Common scaler macros - reading, writing etc.
  6.  
  7. #define READ_RGB(buf,into) { into=(buf[0]<<16)+(buf[1]<<8)+buf[2]; buf+=3; }
  8. #define READ_RGBA(buf,into) { into=(*((unsigned long*)buf))>>8; buf+=4; }
  9.  
  10. #define WRITE_RGB(from,buf) { buf[0]=(from>>16)&0xff; buf[1]=(from>>8)&0xff; buf[2]=from&0xff; buf+=3; }
  11. #define WRITE_RGBA(from,buf) { *((unsigned long*)buf)=(from<<8)|0xff; buf+=4; }
  12.  
  13. #define BLEND(c1,c2,w1,w2,ws,d) { d=\
  14.     ((w1*((c1)&0x000000ff)+w2*((c2)&0x000000ff))/ws)+\
  15.     (((w1*((c1)&0x0000ff00)+w2*((c2)&0x0000ff00))/ws)&0x0000ff00)+\
  16.     ((((w1*((c1>>8)&0x0000ff00)+w2*((c2>>8)&0x0000ff00))/ws)&0x0000ff00)<<8); }
  17.     
  18. #endif
  19.  
  20. /*
  21.  the defines:
  22.  
  23.  BLEND_ROWS
  24.  SCALE_ROW
  25.  SCALE_IMAGE
  26.  
  27.  produce function body code that can be customized by defining:
  28.  
  29.  either SRC_RGB or SRC_RGBA - source pixel size / format
  30.  either DST_RGB or DST_RGBA - destination pixel size / format
  31.  
  32.  An exception is BLEND_ROWS which always uses RGBA as source.
  33.  
  34.  Possible future switches (for better performance - not implemented yet)
  35.  
  36.  either SCALE_X or COPY_X - indicates if horizontal scaling is needed
  37.  either SCALE_Y or COPY_Y - indicates if vertical scaling is needed
  38.  
  39. */
  40.  
  41. #ifdef BLEND_ROWS    // Produce row blending function body
  42. /*
  43.  parameters
  44.  r1: pointer to row 1
  45.  r2: pointer to row 2
  46.  w1: weight of row 1
  47.  w2: weight of row 2
  48.  len: length of row - 1
  49.  dst: pointer to destination row
  50.  */
  51. {
  52.     register int i;
  53.     register unsigned long s1;
  54.     register unsigned long s2;
  55.     register unsigned long d;
  56.     register int ws=w1+w2;
  57.     for (i=len;i>=0;i--) {
  58.         READ_RGBA(r1,s1);
  59.         READ_RGBA(r2,s2);
  60.         BLEND(s1,s2,w1,w2,ws,d);
  61. #ifdef DST_RGB
  62.         WRITE_RGB(d,dst);
  63. #endif
  64. #ifdef DST_RGBA
  65.         WRITE_RGBA(d,dst);
  66. #endif
  67.     }
  68. }
  69. #endif
  70.  
  71. #ifdef SCALE_ROW    // Produce row scaling function body
  72. /*
  73.  parameters
  74.  
  75.  src: pointer to src pixels (format determined by SRC_RGB or SRC_RGBA)
  76.  dst: pointer to dst pixels (format determined by DST_RGB or DST_RGBA)
  77.  srcLength: length of src row - 1
  78.  dstLength: length of dst row - 1 
  79. */
  80. {
  81.     register unsigned long last;        //"last" color
  82.     register unsigned long next;        //"next" color
  83.     register unsigned long blend;        //result color - blended form "last" and "next"
  84.     register int i;                //Pixel run
  85.     register int j;                //src pixel skip run
  86.     register int fract=-srcLength;        //bresenham fraction / blending coefficient
  87.     register int otherFract;            //The other fraction
  88.     //read initial "last" and "next"
  89. #ifdef SRC_RGB
  90.     READ_RGB(src,last);
  91.     READ_RGB(src,next);
  92. #endif
  93. #ifdef SRC_RGBA
  94.     READ_RGBA(src,last);
  95.     READ_RGBA(src,next);
  96. #endif
  97.     for (i=dstLength;i>0;i--) {    //Copy all but one pixel
  98.         //go to next pixel - the while loop is probably less expensive than one more memory access
  99.         fract+=srcLength;
  100.         j=fract/dstLength;
  101.         fract=fract%dstLength;
  102.         while (j>0) {
  103.             last=next;
  104. #ifdef SRC_RGB
  105.             READ_RGB(src,next);
  106. #endif
  107. #ifdef SRC_RGBA
  108.             READ_RGBA(src,next);
  109. #endif
  110.             j--;
  111.         }
  112.         //blend
  113.         otherFract=dstLength-fract;
  114.         BLEND(next,last,fract,otherFract,dstLength,blend);
  115.         //write pixel
  116. #ifdef DST_RGB
  117.         WRITE_RGB(blend,dst);
  118. #endif
  119. #ifdef DST_RGBA
  120.         WRITE_RGBA(blend,dst);
  121. #endif
  122.     }
  123.     //Copy the last pixel - it's the last src pixel, which is in "next"
  124. //    next=0xff0000;
  125. #ifdef DST_RGB
  126.     WRITE_RGB(next,dst);
  127. #endif
  128. #ifdef DST_RGBA
  129.     WRITE_RGBA(next,dst);
  130. #endif
  131. }
  132. #endif    //SCALE_ROW
  133.  
  134.  
  135. #ifdef SCALE_IMAGE    //produce image scaling function body
  136. /*
  137.  this body is intended to be included as a RGBScaler method and may access the instance variables
  138.  
  139.  parameters:
  140.  src - pointer to src pixels (format determined by SRC_RGB or SRC_RGBA)
  141.  dst - pointer to destination pixels (format determined by DST_RGB or DST_RGBA)
  142. */
  143. {
  144.     unsigned char* lastRow;
  145.     unsigned char* nextRow;
  146.     unsigned char* srcRun=src;
  147.     unsigned char* dstRun=dst;
  148.     
  149.     int sx=srcWidth-1;
  150.     int sy=srcHeight-1;
  151.     int dx=dstWidth-1;
  152.     int dy=dstHeight-1;
  153.     int i;                //Row counter
  154.     int j;                //Source row skip counter
  155.     int fract=-sy;            //Bresenham fraction / blending coefficient
  156.     
  157.     nextRow=lastRow=tmpRow1;
  158.     //Read first line to tmp line buffer
  159. #ifdef SRC_RGB
  160.     ScaleRowRGBToRGBA(srcRun,nextRow,sx,dx);
  161. #endif
  162. #ifdef SRC_RGBA
  163.     ScaleRowRGBAToRGBA(srcRun,nextRow,sx,dx);
  164. #endif
  165.     srcRun+=srcRB;            //This line is done
  166.     for (i=dy;i>0;i--) {        //Handle all destination lines but the last one
  167.         //Get this line's sources
  168.         fract+=sy;
  169.         j=fract/dy;
  170.         fract=fract%dy;
  171.         while (j>0) {
  172.             lastRow=nextRow;
  173.             nextRow=(lastRow==tmpRow1)?tmpRow2:tmpRow1;    //Set next row to other than last row
  174. #ifdef SRC_RGB
  175.             ScaleRowRGBToRGBA(srcRun,nextRow,sx,dx);
  176. #endif
  177. #ifdef SRC_RGBA
  178.             ScaleRowRGBAToRGBA(srcRun,nextRow,sx,dx);
  179. #endif
  180.             j--;
  181.             srcRun+=srcRB;            //This line is done
  182.         }
  183.         
  184.         //Write one line from the temp buffers to the destination
  185. #ifdef DST_RGB
  186.         BlendRowsToRGB(nextRow,lastRow,fract,dy-fract,dx,dstRun);
  187. #endif
  188. #ifdef DST_RGBA
  189.         BlendRowsToRGBA(nextRow,lastRow,fract,dy-fract,dx,dstRun);
  190. #endif
  191.         //increment to next line
  192.         dstRun+=dstRB;
  193.     }
  194.     //Handle last row
  195. #ifdef DST_RGB
  196.     BlendRowsToRGB(nextRow,nextRow,1,0,dx,dstRun);
  197. #endif
  198. #ifdef DST_RGBA
  199.     BlendRowsToRGBA(nextRow,lastRow,1,0,dx,dstRun);
  200. #endif
  201. }
  202.  
  203. #endif
  204.  
  205.  
  206.  
  207.  
  208.  
  209.