// sample based on code from:
// http://www.gimp.org/docs/plug-in/plug-in.html

#include <libgimp/gimp.h>

/* Function prototypes */
static void query (void);
static void run (const gchar     *name,
                 gint             n_params,
                 const GimpParam *param,
                 gint            *n_return_vals,
                 GimpParam      **return_vals);



static void query()
{
    static GimpParamDef params[] =
    {
	{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
	{ GIMP_PDB_IMAGE, "image", "(unused)" },
        { GIMP_PDB_DRAWABLE, "drawable", "Drawable to draw on" },
        { GIMP_PDB_COLOR, "fgcolor",  "Color to draw with"}
    };

    gimp_install_procedure(
	"my name",              /* name */
	"blurb ??",             /* blurb */
	"some helptext here",   /* help */
	"that's me",            /* author */
	"and i made it",        /* copyright */
	"what day is it ?",     /* date */
	"<Image>/Just/Simple",  /* menu_label */
	"RGB",                  /* image_types */
	GIMP_PLUGIN,            /* type */
	G_N_ELEMENTS(params),   /* n_params */
	0,                      /* n_return_vals */
	params,                 /* params */
	0                       /* return_vals */
    );
}

static void run (
	const gchar     *name,
	gint             n_params,
	const GimpParam *param,
	gint            *n_return_vals,
	GimpParam      **return_vals)
{
	gint x, y, w, h, n, idx;
	GimpPixelRgn srcrgn, dstrgn;
	guchar *src, *dst;

	/*
	 * provide space for return value (note that this may NOT 
	 * be allocated on the stack(
	 */
	static GimpParam values[2];

	/* the ID of the drawable */
	gint32 drawable_ID;

	/* will hold a pointer to the drawable later on,
	 * the drawable is passed as a parameter in param */
	GimpDrawable *drawable;


	/* set n_return_vals to the number of return values, the status
	 * code is not counted */
	*n_return_vals = 1;
	/* set the pointer to the return values */
	*return_vals = values;
	
	/* return value #0 is the status code and must always be returned */
	values[0].type = GIMP_PDB_STATUS;
/*	values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR; */
	values[0].data.d_status = GIMP_PDB_SUCCESS;


	/* display a greeting */
	gimp_message("Hello world!");

	/* obtain the drawable of the image */
	drawable_ID = param[2].data.d_drawable;
	drawable = gimp_drawable_get(drawable_ID);

	/* obtain width and height */
	w = drawable->width;
	h = drawable->height;

	/* allocate memory for source and destination arrays */
	n = w * h;
	dst = g_malloc(n * 4);
	src = g_malloc(n * 4);
	
	/* setup pixel region for source */
	gimp_pixel_rgn_init(&srcrgn, drawable, 0, 0, w, h, 0, 0);
	gimp_pixel_rgn_get_rect(&srcrgn, src, 0, 0, w, h);

	/* fill the pixels with some data */
	idx = 0;
	for (y = 0; y < h; y++)
	{
		for (x = 0; x < w; x++)
		{
			dst[idx++] = x;
			dst[idx++] = y;
			dst[idx++] = src[idx];
		}
	}

	/* setup pixel region for destination */
	gimp_pixel_rgn_init(&dstrgn, drawable, 0, 0, w, h, 1, 1);
	gimp_pixel_rgn_set_rect(&dstrgn, dst, 0, 0, w, h);

	/* commit changes to the drawable */
	gimp_drawable_flush(drawable);
	gimp_drawable_merge_shadow(drawable->drawable_id, 1);
	gimp_drawable_update(drawable->drawable_id, 0, 0, w, h);

	/* make sure that changes become visible */
	gimp_displays_flush();

	/* release the drawable */
	gimp_drawable_detach(drawable);

	/* release the memory that was allocated with g_malloc */
	g_free(src);
	g_free(dst);
}


/* Setting PLUG_IN_INFO */
GimpPlugInInfo PLUG_IN_INFO =
{
    NULL,    /* init_proc */
    NULL,    /* quit_proc */
    query,   /* query_proc */
    run      /* run_proc */
};

MAIN()

