/* Copyright 1990 by Christopher A. Wichura.
   See file GIFMachine.doc for full description of rights.
*/

#include "GIFMachine.h"

extern struct GIFdescriptor gdesc;
EXTERNBITPLANE;

extern char *AbortMsg;

extern int NoBorderLineThresh;

#define BorderCheck(a, b) ((BitPlane[b][a].rgb_Red   != BorderCol.rgb_Red) || \
			   (BitPlane[b][a].rgb_Green != BorderCol.rgb_Green) || \
			   (BitPlane[b][a].rgb_Blue  != BorderCol.rgb_Blue))

void StripBorder(void)
{
	register UWORD x;
	register UWORD y;
	register int thresh;
	register BOOL breakout;
	LONG LeftEdge, TopEdge;
	LONG Width, Height;
	UWORD OrigWidth, OrigHeight;
	int WidthThresh, HeightThresh;
	int Corner;

	struct RGB BorderCol;

	PutStr("...Removing border.\n");

	OrigWidth = gdesc.gd_Width;
	OrigHeight = gdesc.gd_Height;

	for (Corner = 0; Corner < 4; Corner++) {
		x = (Corner & 1) ? (gdesc.gd_Width - 1) : 0;
		y = (Corner & 2) ? (gdesc.gd_Height - 1) : 0;

		BorderCol = BitPlane[y][x];

		WidthThresh  = NoBorderLineThresh * gdesc.gd_Width  / 100;
		HeightThresh = NoBorderLineThresh * gdesc.gd_Height / 100;

		for (breakout = y = 0; (y < gdesc.gd_Height) && !breakout; y++) {
			for (thresh = x = 0; x < gdesc.gd_Width; x++)
				if (BorderCheck(x, y))
					if (++thresh > WidthThresh) {
						breakout = TRUE;
						break;
					}

			if (SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
				PutStr(AbortMsg);
				MyExit(ABORTEXITVAL);
			}
		}

		TopEdge = y - 1;

		for (breakout = 0, y = gdesc.gd_Height - 1; (y > 0) && !breakout; y--) {
			for (thresh = x = 0; x < gdesc.gd_Width; x++)
				if (BorderCheck(x, y))
					if (++thresh > WidthThresh) {
						breakout = TRUE;
						break;
					}

			if (SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
				PutStr(AbortMsg);
				MyExit(ABORTEXITVAL);
			}
		}

		Height = y - TopEdge + 2;

		for (breakout = x = 0; (x < gdesc.gd_Width) && !breakout; x++) {
			for (thresh = y = 0; y < gdesc.gd_Height; y++)
				if (BorderCheck(x, y))
					if (++thresh > HeightThresh) {
						breakout = TRUE;
						break;
					}

			if (SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
				PutStr(AbortMsg);
				MyExit(ABORTEXITVAL);
			}
		}

		LeftEdge = x - 1;

		for (breakout = 0, x = gdesc.gd_Width - 1; (x > 0) && !breakout; x--) {
			for (thresh = y = 0; y < gdesc.gd_Height; y++)
				if (BorderCheck(x, y))
					if (++thresh > HeightThresh) {
						breakout = TRUE;
						break;
					}

			if (SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
				PutStr(AbortMsg);
				MyExit(ABORTEXITVAL);
			}
		}

		Width = x - LeftEdge + 2;

		if ((Width != gdesc.gd_Width) || (Height != gdesc.gd_Height)) {
			if (Width < 5 || Height < 5) {
				PutStr("......Too much of picture would be removed.  Not modified.\n");
				return;
			}

			for (y = 0; y < Height; y++) {
				for (x = 0; x < Width; x++)
					BitPlane[y][x] = BitPlane[TopEdge + y][LeftEdge + x];

				BitPlane[y][x].rgb_Red   =
				BitPlane[y][x].rgb_Green =
				BitPlane[y][x].rgb_Blue  = 0;

				if (SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
					PutStr(AbortMsg);
					MyExit(ABORTEXITVAL);
				}
			}

			gdesc.gd_Width = Width;
			gdesc.gd_Height = Height;
		}
	}

	if ((gdesc.gd_Width != OrigWidth) || (gdesc.gd_Height != OrigHeight)) {
		if (gdesc.gd_Width & 1)
			gdesc.gd_Width++;

		MyPrintf("......New width = %ld, New height = %ld\n",
			gdesc.gd_Width, gdesc.gd_Height);
	}
}
