/*
**  rgb.library
**
**  This file is part of sFilter.
**
**  Copyright © 1997 by Thomas and Hans-Joerg Frieden
**
**  Make buffer red, green or blue by setting the other
**  color compontens to zero
**  This file is also a model implementation of a sFilter plugin
**
*/

/*  Version string. Please supply one */
static char *VersionString = "$VER: rgb.library 1.0 (13.7.1997) © 1997 Thomas & Hans-Joerg Frieden";

/*  Command string. This is the command supplied by this plugin */
#define COMMAND  "RGB\0"
/*  Template string. The description of the parameters for this command. See dos/ReadArgs autodoc */
#define TEMPLATE "R=RED/S,G=GREEN/S,B=BLUE/S\0"

#include <math.h>
#include <strings.h>

#include <dos/dos.h>
#include <dos/rdargs.h>

#include <clib/dos_protos.h>
#include <pragmas/dos_pragmas.h>
#include <clib/exec_protos.h>
#include <pragmas/exec_pragmas.h>

#include "sFilter.h"

/*
**  GetTemplate
**  This function simply allocates space for the template and returns a pointer to it.
**  It`s unlikely that you have to change anything in this function
*/

__saveds __asm char *GetTemplate(register __a0 APTR DataPool) {

    char *new = AllocPooled(DataPool, sizeof(TEMPLATE) + 1);

    strcpy(new, TEMPLATE);
    return(new);
}


/*
**  GetCommand
**  This function allocates and copies the command word and returns a pointer to it.
**  As above, you can just leave this untouched.
*/

__saveds __asm char *GetCommand(register __a0 APTR DataPool) {

    char *new = AllocPooled(DataPool, sizeof(COMMAND) + 1);

    strcpy(new, COMMAND);
    return(new);
}


/*
**  GetVersion
**  This function returns the API version number of this library.
**  At this moment, only one is supported, so always return 1 for now
*/

__saveds __asm int GetVersion(void) {

    return(1);
}


/*
**  Filter
**  This implements the functionality of the plugin.
**  DataPool is a pointer to an exec memory pool. All "permanent" storage should be taken
**      from this pool.
**  buffer is a pointer to the current buffer. The plugin should operate on this data.
**  second is a pointer to a secondary buffer. It is used for operations that require
**      it (mixing and stuff).
**  result is a pointer to an array of LONGs. It is filled with data by ReadArgs and
**      represents the parameters for the plugin. See dos/ReadArgs for more information
**      on ReadArgs. (NOTE: Read only!)
**  r is a pointer to a FilterRect structure. See sFilter.h for more information on this
**      structure
**
**  The function returns a boolean result, indicating success with TRUE, and error conditions
**      with FALSE
**
**  Notes:
**    - Please obey the rectangle gicen in the struct FilterRect *r. This example shows how
**      to do it. Also, alpha channel  support would be great (where possible, of course)
**    - Check the values you get. If you need a secondary buffer, or an alpha
**      channel, check to see if one is there.
**    - In the normal case, secondary buffer and alpha channel are read-only.
**      Operations should be carried out on the primary buffer. However,
**      the secondary buffer and alpha channel can be written to if needed.
**
*/

__saveds __asm BOOL Filter(register __a3 APTR DataPool,
                           register __a0 APTR buffer,
                           register __a4 APTR second,
                           register __a1 LONG *result,
                           register __a2 struct FilterRect *r) {

    int i,j;
    UBYTE *buf, *obuf;
    UBYTE rc, gc, bc;

    rc = (result[0] == DOSTRUE) ? 255 : 0;
    gc = (result[1] == DOSTRUE) ? 255 : 0;
    bc = (result[2] == DOSTRUE) ? 255 : 0;


    buf = (UBYTE *)buffer + (r->x * r->rw * 3 + r->y * 3);      //  Start address of r->x,r->y

    for(i = 0; i < r->h; i++) {                                 //  loop over lines
        obuf = buf;
        for(j = 0; j < r->w; j++) {                             //  loop over pixels
            *buf     &= rc;                                     //  carry out operation
            *(buf+1) &= gc;
            *(buf+2) &= bc;
            buf += 3;
        }
        buf = obuf + (r->rw * 3);                               //  goto next line
    }

    return(TRUE);

}



