/*
 * A Drawing Mode for ImageFX 1.5
 *
 * It works like this:
 *
 *    Find maximum of background R, G, B.
 *    Subtract a tiny bit so we darken more each time we overwrite.
 *    Adjust new R, G, B value by this percentage.
 *    Store it.
 *
 *    (Kinda like a colorize.)
 *
 */

#include <exec/types.h>
#include <scan/modall.h>

#define MAX(a,b)     ((a) > (b) ? (a) : (b))

/**********************************************************************\

                                Library Vectors

\**********************************************************************/

int __asm DM_PutInit (void)
{
   return(1);
}

void __asm DM_PutCleanup (void)
{
}

void __asm DM_PutColor (register __a0 short *destr,
                        register __a1 short *destg,
                        register __a2 short *destb,
                        register __d0 LONG oldr,
                        register __d1 LONG oldg,
                        register __d2 LONG oldb,
                        register __d3 LONG newr,
                        register __d4 LONG newg,
                        register __d5 LONG newb,
                        register __d6 LONG x,
                        register __d7 LONG y)
{
   LONG v;

   v = MAX(MAX(oldr,oldg),oldb) - 10;
   if (v < 0) v = 0;

   *destr = (newr * v) / 255;
   *destg = (newg * v) / 255;
   *destb = (newb * v) / 255;
}

void __asm DM_PutGrey  (register __a0 short *destg,
                        register __d0 LONG oldg,
                        register __d1 LONG newg,
                        register __d6 LONG x,
                        register __d7 LONG y)
{
   *destg = (newg * (oldg-10)) / 255;
}

/*
 * You cannot rely on this being called after DM_PutInit(), or
 * vice versa.
 *
 */
void __asm DM_NewPoint (register __d0 int n,
                        register __d1 int total,
                        register __d2 int x,
                        register __d3 int y,
                        register __d4 int z)
{
   /* This is called at every point along a user-drawn curve. */
}

/**********************************************************************\

                         Library Initialization Stuff

\**********************************************************************/

/*
 * This is the table of all the functions that can be called in this
 * module.  The first four (Open, Close, Expunge, and Null) are reserved
 * for system use and MUST be specified in the order shown.  The actual
 * functions are in the standard module startup code.
 */
ULONG FuncTable[] = {
   /* These four MUST be present in this order */
   (ULONG) LibOpen,
   (ULONG) LibClose,
   (ULONG) LibExpunge,
   (ULONG) LibNull,

   /* Specific to the module */
   (ULONG) DM_PutInit,
   (ULONG) DM_PutCleanup,
   (ULONG) DM_PutColor,
   (ULONG) DM_PutGrey,
   (ULONG) DM_NewPoint,

   /* End with -1L */
   (ULONG) -1L
};

/*
 * These are used by the standard module startup code.
 * LibraryName is the name of the library, and LibraryID is a short
 * description of the library.  Both of these are largely irrelavent,
 * but they are included just for completeness.
 */
UBYTE LibraryID[]    = "$VER: Felt Tip Drawing Mode 1.04.21 (12.6.93)";
UBYTE LibraryType    = NT_DRAWMODE;

/*
 * This is called by the standard module startup code when Image Scan
 * first opens the module.  Here we should fill in the NumGads,
 * NewGad, Language, and LangCount fields of the provided ModuleBase
 * structure if necessary.
 */
long  __asm UserOpen (register __a6 struct ModuleBase *modbase)
{
   return(TRUE);
}

/*
 * This is called by the standard module startup code when Image Scan
 * closes the module.  It should cleanup anything allocated or obtained
 * in the UserOpen() function.
 */
long  __asm UserClose (register __a6 struct ModuleBase *modbase)
{
   return(TRUE);
}

