home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / gfx / misc / imagefx_sdk / sas / examples / xdraw / disperse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-15  |  7.1 KB  |  329 lines

  1. /*
  2.  * A Disperse Enhanced Drawing Mode for ImageFX 2.0
  3.  *
  4.  * Revision History:
  5.  *
  6.  *    04.01.95 tek   Now remembers setting from one invokation to the next.
  7.  *
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <scan/modall.h>
  12. #include <scan/drawinfo.h>
  13. #include <math.h>
  14. #include <string.h>
  15.  
  16.  
  17. static int density = 1;
  18.  
  19.  
  20. /**********************************************************************\
  21.  
  22.                                 Library Vectors
  23.  
  24. \**********************************************************************/
  25.  
  26. /*
  27.  * XDM_Attr:
  28.  *
  29.  * Return to ImageFX some information about the Drawing Mode (eg.
  30.  * whether Options are needed, whether we work on greyscale or
  31.  * color, etc.).  Called when ImageFX first scans the drawing
  32.  * mode directory.
  33.  *
  34.  */
  35. ULONG __saveds __asm XDM_Attr (register __a0 struct XDrawAttr *da)
  36. {
  37.    da->Flags      = XDMF_NoPen | XDMF_AreOptions;
  38.    da->Priority   = 113;
  39.    da->HorizOffs  = 1;
  40.    da->VertOffs   = 1;
  41.    return(0);
  42. }
  43.  
  44. /*
  45.  * XDM_Init:
  46.  *
  47.  * Initialize the drawing mode.
  48.  *
  49.  */
  50. int __saveds __asm XDM_Init (void)
  51. {
  52.    density = GetIfxVar("DisperseDensity", density);
  53.    return(1);
  54. }
  55.  
  56. /*
  57.  * XDM_Cleanup:
  58.  *
  59.  * Cleanup the drawing mode.
  60.  *
  61.  */
  62. void __saveds __asm XDM_Cleanup (void)
  63. {
  64.    SetIfxVar("DisperseDensity", density);
  65. }
  66.  
  67. /*
  68.  * XDM_Begin:
  69.  *
  70.  * Prepare before a pixel affecting operation.
  71.  *
  72.  */
  73. int __saveds __asm XDM_Begin (register __a0 struct IDrawInfo *di)
  74. {
  75.    return(1);
  76. }
  77.  
  78. /*
  79.  * XDM_End:
  80.  *
  81.  * Cleanup after a pixel affecting operation.
  82.  *
  83.  */
  84. void __saveds __asm XDM_End (register __a0 struct IDrawInfo *di)
  85. {
  86. }
  87.  
  88. /*
  89.  * XDM_Affect:
  90.  *
  91.  * Affect a block of pixels.  We may also affect some of the
  92.  * parameters of the IDrawInfo structure, such as the X & Y
  93.  * location of the block of pixels.
  94.  *
  95.  */
  96. void __saveds __asm XDM_Affect (register __a0 struct IDrawInfo *di)
  97. {
  98. #ifdef USE_BUFFERS
  99.  
  100.    int i, j;
  101.    UBYTE *ored, *ogrn, *oblu;
  102.    UBYTE *ired, *igrn, *iblu;
  103.    int brow = di->BufW;
  104.    int x, y;
  105.    int t, u;
  106.    int c;
  107.  
  108.    for (j = 0; j < di->Height; j++)
  109.    {
  110.       GetBufLines(di->BBuf, &ired, &igrn, &iblu, j, 3);
  111.       ired += di->BufW + 1;
  112.       igrn += di->BufW + 1;
  113.       iblu += di->BufW + 1;
  114.       for (i = 0; i < di->Width; i++)
  115.       {
  116.  
  117.          for (c = 0; c < density; c++)
  118.          {
  119.             x = ((lrand48() >> 11) & 3) - 1; if (x == 2) x = 1;
  120.             y = ((lrand48() >> 13) & 3) - 1; if (y == 2) y = 1;
  121.  
  122.             t = *(ired+(y*brow)+x);
  123.             u = *(ired);
  124.             *(ired+(y*brow)+x) = u;
  125.             *(ired) = t;
  126.  
  127.             t = *(igrn+(y*brow)+x);
  128.             u = *(igrn);
  129.             *(igrn+(y*brow)+x) = u;
  130.             *(igrn) = t;
  131.  
  132.             t = *(iblu+(y*brow)+x);
  133.             u = *(iblu);
  134.             *(iblu+(y*brow)+x) = u;
  135.             *(iblu) = t;
  136.          }
  137.  
  138.          ired++; igrn++; iblu++;
  139.       }
  140.       PutBufLines(di->BBuf, -1, -1);
  141.    }
  142.  
  143.    ored = di->OutR;
  144.    ogrn = di->OutG;
  145.    oblu = di->OutB;
  146.  
  147.    for (j = 0; j < di->Height; j++)
  148.    {
  149.       GetBufLine(di->BBuf, &ired, &igrn, &iblu, j + 1);
  150.       GetBufLine(di->BOut, &ored, &ogrn, &oblu, j);
  151.       memcpy(ored, ired + 1, di->Width);
  152.       memcpy(ogrn, igrn + 1, di->Width);
  153.       memcpy(oblu, iblu + 1, di->Width);
  154.       PutBufLine(di->BOut);
  155.    }
  156.  
  157. #else
  158.  
  159.    int i, j;
  160.    UBYTE *ored, *ogrn, *oblu;
  161.    UBYTE *ired, *igrn, *iblu;
  162.    int brow = di->BufW;
  163.    int x, y;
  164.    int t, u;
  165.    int c;
  166.  
  167.    for (j = 0; j < di->Height; j++)
  168.    {
  169.       ired = di->BufR + (j * di->BufW);
  170.       igrn = di->BufG + (j * di->BufW);
  171.       iblu = di->BufB + (j * di->BufW);
  172.       for (i = 0; i < di->Width; i++)
  173.       {
  174.  
  175.          for (c = 0; c < density; c++)
  176.          {
  177.             x = ((lrand48() >> 11) & 3) - 1; if (x == 2) x = 1;
  178.             y = ((lrand48() >> 13) & 3) - 1; if (y == 2) y = 1;
  179.  
  180.             t = *(ired+(y*brow)+x);
  181.             u = *(ired);
  182.             *(ired+(y*brow)+x) = u;
  183.             *(ired) = t;
  184.  
  185.             t = *(igrn+(y*brow)+x);
  186.             u = *(igrn);
  187.             *(igrn+(y*brow)+x) = u;
  188.             *(igrn) = t;
  189.  
  190.             t = *(iblu+(y*brow)+x);
  191.             u = *(iblu);
  192.             *(iblu+(y*brow)+x) = u;
  193.             *(iblu) = t;
  194.          }
  195.  
  196.          ired++; igrn++; iblu++;
  197.       }
  198.    }
  199.  
  200.    ored = di->OutR;
  201.    ogrn = di->OutG;
  202.    oblu = di->OutB;
  203.  
  204.    for (j = 0; j < di->Height; j++)
  205.    {
  206.       ired = di->BufR + (j * di->BufW);
  207.       igrn = di->BufG + (j * di->BufW);
  208.       iblu = di->BufB + (j * di->BufW);
  209.       for (i = 0; i < di->Width; i++)
  210.       {
  211.          *ored++ = *ired++;
  212.          *ogrn++ = *igrn++;
  213.          *oblu++ = *iblu++;
  214.       }
  215.    }
  216.  
  217. #endif
  218.  
  219. }
  220.  
  221. /*
  222.  * XDM_Options:
  223.  *
  224.  * Present a window to the user allowing him to adjust drawing mode
  225.  * options.  Arguments may optionally be passed from an Arexx command.
  226.  * This function will only be called if the flag XDMF_AreOptions is
  227.  * returned from XDM_Attr().
  228.  *
  229.  */
  230. int __saveds __asm XDM_Options (register __a0 LONG *args)
  231. {
  232.    int d;
  233.  
  234.    d = IntegerRequest("Disperse Density:", 1, 8, density);
  235.    if (d < 1) return(0);
  236.    density = d;
  237.    return(0);
  238. }
  239.  
  240. /*
  241.  * XDM_LoadPrefs:
  242.  *
  243.  * Set preferences according to information loaded from disk.
  244.  *
  245.  */
  246. int __saveds __asm XDM_LoadPrefs (register __a0 void *prefs)
  247. {
  248.    return(1);
  249. }
  250.  
  251. /*
  252.  * XDM_SavePrefs:
  253.  *
  254.  * Request preferences settings that are about to be saved to disk.
  255.  *
  256.  */
  257. int __saveds __asm XDM_SavePrefs (register __a0 void *prefs)
  258. {
  259.    return(1);
  260. }
  261.  
  262.  
  263. /**********************************************************************\
  264.  
  265.                          Library Initialization Stuff
  266.  
  267. \**********************************************************************/
  268.  
  269. /*
  270.  * This is the table of all the functions that can be called in this
  271.  * module.  The first four (Open, Close, Expunge, and Null) are reserved
  272.  * for system use and MUST be specified in the order shown.  The actual
  273.  * functions are in the standard module startup code.
  274.  */
  275. ULONG FuncTable[] = {
  276.    /* These four MUST be present in this order */
  277.    (ULONG) LibOpen,
  278.    (ULONG) LibClose,
  279.    (ULONG) LibExpunge,
  280.    (ULONG) LibNull,
  281.  
  282.    /* Specific to the module */
  283.    (ULONG) XDM_Attr,
  284.    (ULONG) XDM_Begin,
  285.    (ULONG) XDM_End,
  286.    (ULONG) XDM_Affect,
  287.    (ULONG) XDM_Options,
  288.    (ULONG) XDM_LoadPrefs,
  289.    (ULONG) XDM_SavePrefs,
  290.    (ULONG) 0,
  291.    (ULONG) 0,
  292.    (ULONG) XDM_Init,
  293.    (ULONG) XDM_Cleanup,
  294.  
  295.    /* End with -1L */
  296.    (ULONG) -1L
  297. };
  298.  
  299. /*
  300.  * These are used by the standard module startup code.
  301.  * LibraryName is the name of the library, and LibraryID is a short
  302.  * description of the library.  Both of these are largely irrelavent,
  303.  * but they are included just for completeness.
  304.  */
  305. UBYTE LibraryID[]    = "$VER: Disperse Extended Drawing Mode 2.0.9 (15.2.95)";
  306. UBYTE LibraryType    = NT_XDRAWMODE;
  307.  
  308. /*
  309.  * This is called by the standard module startup code when Image Scan
  310.  * first opens the module.  Here we should fill in the NumGads,
  311.  * NewGad, Language, and LangCount fields of the provided ModuleBase
  312.  * structure if necessary.
  313.  */
  314. long  __asm UserOpen (register __a6 struct ModuleBase *modbase)
  315. {
  316.    return(TRUE);
  317. }
  318.  
  319. /*
  320.  * This is called by the standard module startup code when Image Scan
  321.  * closes the module.  It should cleanup anything allocated or obtained
  322.  * in the UserOpen() function.
  323.  */
  324. long  __asm UserClose (register __a6 struct ModuleBase *modbase)
  325. {
  326.    return(TRUE);
  327. }
  328.  
  329.