#include "movrecgion.h"
#include <stdio.h>
#include "math.h"
#include "string.h"

int myassert(int x,int min,int max)
{
	if(x>=min&&x<=max)
		return	1;
	else return 0;
}

////////////poslink 记载坐标 队列形式
////////////regionlink 记载矩形所在
POSLINK *myposinsert(int pos[2],
							POSLINK *head)
{
	POSLINK *p;
	POSLINK *q1=head,*q2=NULL;

	while(q1!=NULL)
	{
		q2=q1;
		q1=q1->next;
	}
	
	p=(POSLINK *)malloc(sizeof(POSLINK));
	if(p==NULL)
	{
		printf("Memory error!");
		return NULL;
	}
	else
	{
		p->pos[0]=pos[0];
		p->pos[1]=pos[1];
		p->next=NULL;		
		if(q2==NULL)	//link is null
			head=p;
		else			//element inserted in the mid
			q2->next=p;
		return head;	
	}
}

void myposdelete(POSLINK *head)
{
	POSLINK *p=head,*q;
	while(p)
	{
		q=p->next;
		free(p);
		p=q;
	}
}

REGIONLINK *myregioninsert(int up,int down,int left,int right,
		REGIONLINK *head)
{
	REGIONLINK *p;
	REGIONLINK *q1=head,*q2=NULL;

	while(q1!=NULL)
	{
		q2=q1;
		q1=q1->next;
	}
	
	p=(REGIONLINK *)malloc(sizeof(REGIONLINK));
	if(p==NULL)
	{
		printf("Memory error!");
		return NULL;
	}
	else
	{
		p->up=up;
		p->down=down;
		p->left=left;
		p->right=right;
		p->next=NULL;
	
		if(q2==NULL)	//link is null
			head=p;
		else		
			q2->next=p;
		return head;	
	}
}

REGIONLINK *myregiondelete(REGIONLINK *head)
{
	REGIONLINK *p=head,*q;
	while(p)
	{
		q=p->next;
		free(p);
		p=q;
	}
	return NULL;
}

REGIONLINK *myregionfree(REGIONLINK *p,
								REGIONLINK *head)
{
	if(p==NULL||head==NULL)
		return NULL;
	if(p==head)
	{
		head=p->next;
		free(p);
		return head;
	}
	else
	{
		REGIONLINK *q1=head,*q2=NULL;
		while(q1!=NULL&&q1!=p)
		{
			q2=q1;
			q1=q1->next;
		}
		if(q1!=NULL)
			q2->next=q1->next;
		free(p);
		return head;
	}
}

int findup(POSLINK *head)
{
	POSLINK *p=head;
	int i_max=head->pos[0];

	while(p)
	{
		if(p->pos[0]>i_max)
			i_max=p->pos[0];
		else
			;
		p=p->next;
	}
	return i_max;
}

int finddown(POSLINK *head)
{
	POSLINK *p=head;
	int i_min=head->pos[0];

	while(p)
	{
		if(p->pos[0]<i_min)
			i_min=p->pos[0];
		else
			;
		p=p->next;
	}
	return i_min;
}

int findleft(POSLINK *head)
{
	POSLINK *p=head;
	int i_min=head->pos[1];

	while(p)
	{
		if(p->pos[1]<i_min)
			i_min=p->pos[1];
		else
			;
		p=p->next;
	}
	return i_min;
}

int findright(POSLINK *head)
{
	POSLINK *p=head;
	int i_max=head->pos[1];

	while(p)
	{
		if(p->pos[1]>i_max)
			i_max=p->pos[1];
		else
			;
		p=p->next;
	}
	return i_max;
}

int myposlinknum(REGIONLINK *head)
{
	REGIONLINK *p=head;
	int num=0;
	while(p)
	{
		num++;
		p=p->next;
	}
	return num;
}

int mylonglinknum(POSLINK *head)
{
	POSLINK *p=head;
	int num=0;
	while(p)
	{
		num++;
		p=p->next;
	}
	return num;
}

//draw rectangles on the RGB image
void draw_rectangle(double *sci_data,int start_w,int start_h,int Width,int Height,
					int DibWidth,int DibHeight,int color[3])
{
	int i,k;
	int pos;

	for(i=start_w;i<start_w+Width;i++)
	{
	   if(myassert(start_h,0,DibHeight-1)&&myassert(i,0,DibWidth-1))
	   {
			pos=i*DibHeight+start_h;
			for(k=0;k<3;k++)
				*(sci_data + k*DibWidth*DibHeight + pos) = color[k];
	   }
	   if(myassert(start_h+Height,0,DibHeight-1)&&myassert(i,0,DibWidth-1))
	   {
			pos=i*DibHeight+(start_h+Height);
			for(k=0;k<3;k++)
				*(sci_data + k*DibWidth*DibHeight + pos) = color[k];
	   }
	}
	
	for(i=start_h;i<start_h+Height;i++)
	{
		if(myassert(i,0,DibHeight-1)&&myassert(start_w,0,DibWidth-1))
		{
			pos=start_w*DibHeight+i;
			for(k=0;k<3;k++)
				*(sci_data + k*DibWidth*DibHeight + pos) = color[k];
		}
	
		if(myassert(i,0,DibHeight-1)&&myassert(start_w+Width,0,DibWidth-1))
		{
			pos=(start_w+Width)*Height + i;
			for(k=0;k<3;k++)
				*(sci_data + k*DibWidth*DibHeight + pos) = color[k];
		}
	}

}
