
#define _CRT_SECURE_NO_WARNINGS	  /* disable compiler warnings for standard C functions like strcpy() */

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winuser.h>
#include <math.h>

#include "globals.h"

#define	MIN_WEIGHT	0.5		/* minimum weight of any sensel to be included in region */

		/* This function groups sensels together into regions.  Each region is assumed to be a container (e.g. plate, cup). */

void SegmentRegions()

{
double		  Image[GRID_ROWS*GRID_COLS];
int			  r,c,i;
int			  SenselIndices[GRID_ROWS*GRID_COLS],TotalSensels;

		/* create single array holding image of weights (grams) at the current time index */
for (r=0; r<GRID_ROWS; r++)
  for (c=0; c<GRID_COLS; c++)
	Image[r*GRID_COLS+c]=Data[(GRID_ROWS*GRID_COLS*TimeIndex)+(r*GRID_COLS+c)];

		/* initialize all segment labels to zero (background) */
for (r=0; r<GRID_ROWS; r++)
  for (c=0; c<GRID_COLS; c++)
	Regions[r*GRID_COLS+c]=0;
		/* use paint-fill method to find 4-connected regions of weights > 0.1 g */
TotalRegions=0;
for (r=0; r<GRID_ROWS; r++)
  for (c=0; c<GRID_COLS; c++)
	{
	if (Regions[r*GRID_COLS+c] != 0)
	  continue;	  /* already belongs to region */
	if (Image[r*GRID_COLS+c] >= MIN_WEIGHT)
	  {
	  RegionGrow(Image,Regions,GRID_ROWS,GRID_COLS,r,c,0,TotalRegions+1,SenselIndices,&TotalSensels);
	  TotalRegions++;
	  RegionWeight[TotalRegions]=0.0;
	  for (i=0; i<TotalSensels; i++)
		RegionWeight[TotalRegions]+=Image[SenselIndices[i]];
	  }
	}

}





        /*
        ** Given an image, a starting point, and a label, this routine
        ** paint-fills (4-connected) the area with the given new label
        ** according to the given criteria (pixles with weight > 0.1 g
        ** are allowed to join).
        */

#define MAX_QUEUE 100 /* max perimeter size (pixels) of border wavefront */

void RegionGrow(double *image,			/* image data */
                unsigned char *labels,  /* segmentation labels */
                int ROWS,int COLS,      /* size of image */
                int r,int c,            /* pixel to paint from */
                int paint_over_label,   /* image label to paint over */
                int new_label,          /* image label for painting */
                int *indices,           /* output:  indices of pixels painted */
                int *count)             /* output:  count of pixels painted */
{
int     r2,c2;
int     queue[MAX_QUEUE],qh,qt;

*count=0;
if (labels[r*COLS+c] != paint_over_label)
  return;
labels[r*COLS+c]=new_label;
if (indices != NULL)
  indices[0]=r*COLS+c;
queue[0]=r*COLS+c;
qh=1;   /* queue head */
qt=0;   /* queue tail */
(*count)=1;
while (qt != qh)
  {
  for (r2=-1; r2<=1; r2++)
    for (c2=-1; c2<=1; c2++)
      {
      if (r2 == 0  &&  c2 == 0)
        continue;
	  if (r2 != 0  &&  c2 != 0)
		continue;	/* 4-connected for now */
      if ((queue[qt]/COLS+r2) < 0  ||  (queue[qt]/COLS+r2) >= ROWS  ||
          (queue[qt]%COLS+c2) < 0  ||  (queue[qt]%COLS+c2) >= COLS)
        continue;
      if (labels[(queue[qt]/COLS+r2)*COLS+queue[qt]%COLS+c2]!=paint_over_label)
        continue;
                /* test criteria to join region */
      if (image[(queue[qt]/COLS+r2)*COLS+queue[qt]%COLS+c2] < MIN_WEIGHT)
        continue;
      labels[(queue[qt]/COLS+r2)*COLS+queue[qt]%COLS+c2]=new_label;
      if (indices != NULL)
        indices[*count]=(queue[qt]/COLS+r2)*COLS+queue[qt]%COLS+c2;
      (*count)++;
      queue[qh]=(queue[qt]/COLS+r2)*COLS+queue[qt]%COLS+c2;
      qh=(qh+1)%MAX_QUEUE;
      if (qh == qt)
        {
        printf("Max queue size exceeded\n");
        exit(0);
        }
      }
  qt=(qt+1)%MAX_QUEUE;
  }
}


