home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / gfx / misc / imagefx_sdk / sas / examples / xstyle / common.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-09  |  11.9 KB  |  389 lines

  1. /*
  2.  * Common routines used in all drawing styles.
  3.  *
  4.  */
  5.  
  6. #include <scan/modall.h>
  7. #include <scan/drawinfo.h>
  8. #include <string.h>
  9.  
  10.  
  11. #define Channel      (ScanBase->sb_Channel)
  12.  
  13.  
  14. #ifdef USE_BUFFERS
  15.  
  16.  
  17. BOOL __regargs GetFromBuf (struct Buffer *buf, struct IDrawInfo *di)
  18. {
  19.    UBYTE *sr, *sg, *sb;
  20.    UBYTE *dr, *dg, *db;
  21.    UBYTE *red, *grn, *blu;
  22.    int i, j;
  23.    int w, h;
  24.    int x, y;
  25. // int xo, xl;
  26.  
  27.    if (!buf) return(FALSE);
  28.  
  29.    /*
  30.     * Only copy if space has been allocated for us.  Some drawing
  31.     * modes may not require this information, so the buffers may
  32.     * not be allocated to save time & space.
  33.     */
  34.  
  35.    if (di->BBuf)
  36.    {
  37.  
  38.       w = di->BufW;
  39.       h = di->BufH;
  40.  
  41.       for (j = 0, y = di->TE - di->BufYO; j < h; j++, y++)
  42.       {
  43.          GetBufLine(di->BBuf, &dr, &dg, &db, j);
  44.  
  45.          if ((y >= 0) && (y < buf->Height))
  46.          {
  47.             if (GetBufLine(buf, &red, &grn, &blu, y))
  48.             {
  49.                sr = red + di->LE - di->BufXO;
  50.                sg = grn + di->LE - di->BufXO;
  51.                sb = blu + di->LE - di->BufXO;
  52.                for (x = di->LE - di->BufXO, i = 0; i < w; i++, x++)
  53.                {
  54.                   if ((x >= 0) && (x < buf->Width))
  55.                   {
  56.                      *dr++ = *sr++;
  57.                      *dg++ = *sg++;
  58.                      *db++ = *sb++;
  59.                   }
  60.                   else
  61.                   {
  62.                      *dr++ = 0;
  63.                      *dg++ = 0;
  64.                      *db++ = 0;
  65.                      sr++; sg++; sb++;
  66.                   }
  67.                }
  68.                #if 0
  69.                memcpy(dr, sr, w);
  70.                memcpy(dg, sg, w);
  71.                memcpy(db, sb, w);
  72.                if (di->LE - di->BufXO < 0)
  73.                {
  74.                   /* outside bounds of image is black */
  75.                   memset(dr, 0, -(di->LE - di->BufXO));
  76.                   memset(dg, 0, -(di->LE - di->BufXO));
  77.                   memset(db, 0, -(di->LE - di->BufXO));
  78.                }
  79.                if (di->LE - di->BufXO + w >= buf->Width)
  80.                {
  81.                   /* outside bounds of image is black */
  82.                   xo = di->LE - di->BufXO + w - buf->Width + 1;
  83.                   xl = w - xo;
  84.                   memset(dr + xo, 0, xl);
  85.                   memset(dg + xo, 0, xl);
  86.                   memset(db + xo, 0, xl);
  87.                }
  88.                #endif
  89.  
  90.             }
  91.          }
  92.          else
  93.          {
  94.             /* outside bounds of image is black */
  95.             memset(dr, 0, w);
  96.             memset(dg, 0, w);
  97.             memset(db, 0, w);
  98.          }
  99.  
  100.          PutBufLine(di->BBuf);
  101.       }
  102.  
  103.    }
  104.  
  105.    return(TRUE);
  106. }
  107.  
  108.  
  109. BOOL __regargs PutToBuf (struct Buffer *buf, struct IDrawInfo *di)
  110. {
  111.    UBYTE *sr, *sg, *sb;
  112.    UBYTE *dr, *dg, *db;
  113.    UBYTE *red, *grn, *blu;
  114.    UBYTE *mask;
  115.    int i, j, x, y, mx, my;
  116.    int left, top;
  117.    short drawpart = (ScanBase->sb_DrawBlend * 255 / 100);
  118.    int mix;
  119.    short r, g, b;
  120.    UBYTE *alphapix;
  121.    struct Buffer *abuf = ScanBase->Alpha;
  122.  
  123.    if (!buf) return(FALSE);
  124.  
  125.    left = di->LE;
  126.    top = di->TE;
  127.  
  128.    /*
  129.     * Put the pixels into the buffer, applying blending and masking.
  130.     * Also clip to buffer boundaries.
  131.     */
  132.    for (y = top, j = 0; j < di->Height; j++, y++) {
  133.       if (di->Flags & IDIF_StatusBar)
  134.       {
  135.          if (Bar(j)) ReturnError(ERR_UserCancel, FALSE);
  136.       }
  137.       if (y >= 0 && y < buf->Height) {
  138.          GetBufLine(di->BOut, &sr, &sg, &sb, j);
  139.          GetBufLine(di->BMask, &mask, NULL, NULL, j);
  140.          GetBufLine(buf, &red, &grn, &blu, y);
  141.          if (buf->Mask) my = y - buf->Mask->OffsetY;
  142.          dr = red + left;
  143.          dg = grn + left;
  144.          db = blu + left;
  145.          if (abuf && ScanBase->sb_DrawAlpha)
  146.          {
  147.             GetBufLine(abuf, &alphapix, NULL, NULL, y % abuf->Height);
  148.          }
  149.          for (x = left, i = 0; i < di->Width; i++, x++) {
  150.             mix = *mask;
  151.             if (buf->Mask) mx = x - buf->Mask->OffsetX;
  152.             if (x >= 0 && x < buf->Width) {
  153.                if (CheckMask(buf->Mask, mx, my)) {
  154.                   if (mix && !CheckCloseness(*dr,*dg,*db)) {
  155.                      switch(ScanBase->sb_DrawAlpha)
  156.                      {
  157.                         case 0 :    /* alpha = off */
  158.                            r = mixer(*sr, *dr, mixer(drawpart,0,mix));
  159.                            g = mixer(*sg, *dg, mixer(drawpart,0,mix));
  160.                            b = mixer(*sb, *db, mixer(drawpart,0,mix));
  161.                            break;
  162.                         case 1 :    /* alpha = frisket */
  163.                            //GetBufLine(abuf, &alphapix, NULL, NULL, y % abuf->Height);
  164.                            if (abuf)
  165.                            {
  166.                               mix = mixer(mix, 0, alphapix[x % abuf->Width]);
  167.                            }
  168.                            r = mixer(*sr, *dr, mixer(drawpart,0,mix));
  169.                            g = mixer(*sg, *dg, mixer(drawpart,0,mix));
  170.                            b = mixer(*sb, *db, mixer(drawpart,0,mix));
  171.                            break;
  172.                         case 2 :    /* alpha = texture */
  173.                            //GetBufLine(abuf, &alphapix, NULL, NULL, y % abuf->Height);
  174.                            if (abuf)
  175.                            {
  176.                               r = *sr + alphapix[x % abuf->Width] - 128; if (r < 0) r = 0; else if (r > 255) r = 255;
  177.                               g = *sg + alphapix[x % abuf->Width] - 128; if (g < 0) g = 0; else if (g > 255) g = 255;
  178.                               b = *sb + alphapix[x % abuf->Width] - 128; if (b < 0) b = 0; else if (b > 255) b = 255;
  179.                            }
  180.                            else
  181.                            {
  182.                               r = *sr;
  183.                               g = *sg;
  184.                               b = *sb;
  185.                            }
  186.                            r = mixer(r, *dr, mixer(drawpart,0,mix));
  187.                            g = mixer(g, *dg, mixer(drawpart,0,mix));
  188.                            b = mixer(b, *db, mixer(drawpart,0,mix));
  189.                            break;
  190.                      }
  191.                      if (buf->Depth == 1 || (Channel & CHAN_RED)) { *dr = r; }
  192.                      if (buf->Depth > 1)
  193.                      {
  194.                         if (Channel & CHAN_GRN) { *dg = g; }
  195.                         if (Channel & CHAN_BLU) { *db = b; }
  196.                      }
  197.                   }
  198.                }
  199.             }
  200.             dr++; dg++; db++;
  201.             sr++; sg++; sb++;
  202.             mask++;
  203.          }
  204.          PutBufLine(buf);
  205.       }
  206.    }
  207.  
  208.    return(TRUE);
  209. }
  210.  
  211.  
  212. #else
  213.  
  214.  
  215. BOOL __regargs GetFromBuf (struct Buffer *buf, struct IDrawInfo *di)
  216. {
  217.    UBYTE *sr, *sg, *sb;
  218.    UBYTE *dr, *dg, *db;
  219.    UBYTE *red, *grn, *blu;
  220.    int j;
  221.    int w, h;
  222.    int xo, xl, y;
  223.  
  224.    if (!buf) return(FALSE);
  225.  
  226.    /*
  227.     * Only copy if space has been allocated for us.  Some drawing
  228.     * modes may not require this information, so the buffers may
  229.     * not be allocated to save time & space.
  230.     */
  231.  
  232.    if (di->BufR && di->BufG && di->BufB)
  233.    {
  234.  
  235.       w = di->BufW;
  236.       h = di->BufH;
  237.  
  238.       dr = di->BufR - di->BufOffs;
  239.       dg = di->BufG - di->BufOffs;
  240.       db = di->BufB - di->BufOffs;
  241.  
  242.       for (j = 0, y = di->TE - di->BufYO; j < h; j++, y++)
  243.       {
  244.  
  245.          if ((y >= 0) && (y < buf->Height))
  246.          {
  247.             if (GetBufLine(buf, &red, &grn, &blu, y))
  248.             {
  249.                sr = red + di->LE - di->BufXO;
  250.                sg = grn + di->LE - di->BufXO;
  251.                sb = blu + di->LE - di->BufXO;
  252.                memcpy(dr, sr, w);
  253.                memcpy(dg, sg, w);
  254.                memcpy(db, sb, w);
  255.                if (di->LE - di->BufXO < 0)
  256.                {
  257.                   /* outside bounds of image is black */
  258.                   memset(dr, 0, -(di->LE - di->BufXO));
  259.                   memset(dg, 0, -(di->LE - di->BufXO));
  260.                   memset(db, 0, -(di->LE - di->BufXO));
  261.                }
  262.                if (di->LE - di->BufXO + w >= buf->Width)
  263.                {
  264.                   /* outside bounds of image is black */
  265.                   xo = di->LE - di->BufXO + w - buf->Width + 1;
  266.                   xl = w - xo;
  267.                   memset(dr + xo, 0, xl);
  268.                   memset(dg + xo, 0, xl);
  269.                   memset(db + xo, 0, xl);
  270.                }
  271.  
  272.             }
  273.          }
  274.          else
  275.          {
  276.             /* outside bounds of image is black */
  277.             memset(dr, 0, w);
  278.             memset(dg, 0, w);
  279.             memset(db, 0, w);
  280.          }
  281.  
  282.          dr += di->BufW;
  283.          dg += di->BufW;
  284.          db += di->BufW;
  285.  
  286.       }
  287.  
  288.    }
  289.  
  290.    return(TRUE);
  291. }
  292.  
  293.  
  294. BOOL __regargs PutToBuf (struct Buffer *buf, struct IDrawInfo *di)
  295. {
  296.    UBYTE *sr, *sg, *sb;
  297.    UBYTE *dr, *dg, *db;
  298.    UBYTE *red, *grn, *blu;
  299.    UBYTE *mask;
  300.    int i, j, x, y, mx, my;
  301.    int left, top;
  302.    short drawpart = (ScanBase->sb_DrawBlend * 255 / 100);
  303.    int mix;
  304.    short r, g, b;
  305.    UBYTE *alphapix;
  306.    struct Buffer *abuf = ScanBase->Alpha;
  307.  
  308.    if (!buf) return(FALSE);
  309.  
  310.    left = di->LE;
  311.    top = di->TE;
  312.  
  313.    /*
  314.     * Read pixels from buffer.
  315.     */
  316.    if (!GetBufLines(buf, &red, &grn, &blu, top, di->Height)) return(FALSE);
  317.  
  318.    /*
  319.     * Put the pixels into the buffer, applying blending and masking.
  320.     * Also clip to buffer boundaries.
  321.     */
  322.    sr = di->OutR;
  323.    sg = di->OutG;
  324.    sb = di->OutB;
  325.    mask = di->Mask;
  326.    for (y = top, j = 0; j < di->Height; j++, y++) {
  327.       if (buf->Mask) my = y - buf->Mask->OffsetY;
  328.       if (y >= 0 && y < buf->Height) {
  329.          dr = red + left + (j * buf->BytesPerRow);
  330.          dg = grn + left + (j * buf->BytesPerRow);
  331.          db = blu + left + (j * buf->BytesPerRow);
  332.          for (x = left, i = 0; i < di->Width; i++, x++) {
  333.             mix = *mask;
  334.             if (buf->Mask) mx = x - buf->Mask->OffsetX;
  335.             if (x >= 0 && x < buf->Width) {
  336.                if (CheckMask(buf->Mask, mx, my)) {
  337.                   if (mix && !CheckCloseness(*dr,*dg,*db)) {
  338.                      switch(ScanBase->sb_DrawAlpha)
  339.                      {
  340.                         case 0 :    /* alpha = off */
  341.                            r = mixer(*sr, *dr, mixer(drawpart,0,mix));
  342.                            g = mixer(*sg, *dg, mixer(drawpart,0,mix));
  343.                            b = mixer(*sb, *db, mixer(drawpart,0,mix));
  344.                            break;
  345.                         case 1 :    /* alpha = frisket */
  346.                            GetBufLine(abuf, &alphapix, NULL, NULL, y % abuf->Height);
  347.                            mix = mixer(mix, 0, alphapix[x % abuf->Width]);
  348.                            r = mixer(*sr, *dr, mixer(drawpart,0,mix));
  349.                            g = mixer(*sg, *dg, mixer(drawpart,0,mix));
  350.                            b = mixer(*sb, *db, mixer(drawpart,0,mix));
  351.                            break;
  352.                         case 2 :    /* alpha = texture */
  353.                            GetBufLine(abuf, &alphapix, NULL, NULL, y % abuf->Height);
  354.                            r = *sr + alphapix[x % abuf->Width] - 128; if (r < 0) r = 0; else if (r > 255) r = 255;
  355.                            g = *sg + alphapix[x % abuf->Width] - 128; if (g < 0) g = 0; else if (g > 255) g = 255;
  356.                            b = *sb + alphapix[x % abuf->Width] - 128; if (b < 0) b = 0; else if (b > 255) b = 255;
  357.                            r = mixer(r, *dr, mixer(drawpart,0,mix));
  358.                            g = mixer(g, *dg, mixer(drawpart,0,mix));
  359.                            b = mixer(b, *db, mixer(drawpart,0,mix));
  360.                            break;
  361.                      }
  362.                      if (buf->Depth == 1 || (Channel & CHAN_RED)) { *dr = r; }
  363.                      if (buf->Depth > 1)
  364.                      {
  365.                         if (Channel & CHAN_GRN) { *dg = g; }
  366.                         if (Channel & CHAN_BLU) { *db = b; }
  367.                      }
  368.                   }
  369.                }
  370.             }
  371.             dr++; dg++; db++;
  372.             sr++; sg++; sb++;
  373.             mask++;
  374.          }
  375.       }
  376.       else {
  377.          sr += di->Width;
  378.          sg += di->Width;
  379.          sb += di->Width;
  380.          mask += di->Width;
  381.       }
  382.    }
  383.  
  384.    PutBufLines(buf, -1, -1);
  385.    return(TRUE);
  386. }
  387.  
  388. #endif
  389.