// sample based on code from:
// http://www.gimp.org/docs/plug-in/plug-in.html

#include <libgimp/gimp.h>

#include <stdio.h>
#include <string.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_install_procedure(
	"stat",                           /* name */
	"some stats for the image",       /* blurb */
	"some helptext here",             /* help */
	"Reinhard Hemmerling",            /* author */
	"(c)2007",                        /* copyright */
	"16. Mai 2007",                   /* date */
	"<Image>/BTU/stat",               /* menu_label */
	"GRAY",                           /* 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;
	guchar *src;
	char used[256];
	
	guchar max, min;
	guint avg, count, colors;
	char msg[80];

	/*
	 * 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;


	/* 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;
	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 */
	max = src[0];
	min = src[0];
	avg = 0;
	count = 0;
	idx = 0;
	memset(used, 0, sizeof(used));
	for (y = 0; y < h; y++)
	{
		for (x = 0; x < w; x++)
		{
		    used[src[idx]] |= 1;
		    if (min > src[idx])
			min = src[idx];
		    if (max < src[idx])
			max = src[idx];
		    avg += src[idx];
		    count++;
		    idx++;
		}
	}
	
	/* count used number of intensities */
	colors = 0;
	for (x = 0; x < 256; x++)
	    colors += used[x] ? 1 : 0;
	
	snprintf(msg, sizeof(msg), "min: %d   max: %d   avg: %d   colors: %d", min, max, avg / count, colors);
	gimp_message(msg);

	/* release the memory that was allocated with g_malloc */
	g_free(src);
}


/* Setting PLUG_IN_INFO */
GimpPlugInInfo PLUG_IN_INFO =
{
    NULL,    /* init_proc */
    NULL,    /* quit_proc */
    query,   /* query_proc */
    run      /* run_proc */
};

MAIN()

