Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "traster.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
#include "img.h"
Toshihiro Shimizu 890ddd
#include "file.h"
Toshihiro Shimizu 890ddd
#include "toonz.h"
Toshihiro Shimizu 890ddd
#include "raster.h"
Toshihiro Shimizu 890ddd
#include "tmsg.h"
Toshihiro Shimizu 890ddd
#include "copP.h"*/
Toshihiro Shimizu 890ddd
Campbell Barton 107701
#if defined(MACOSX) || defined(LINUX)
Toshihiro Shimizu 890ddd
#define TRUE 1
Toshihiro Shimizu 890ddd
#define FALSE 0
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
UCHAR First_preseed_table[256] =
Toshihiro Shimizu 890ddd
	{8, 0, 1, 0, 2, 2, 1, 0, 3, 3, 3, 3, 2, 2, 3, 3,
Toshihiro Shimizu 890ddd
	 4, 4, 1, 0, 2, 2, 1, 0, 4, 4, 3, 3, 2, 2, 3, 3,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 2, 2, 1, 5, 5, 5, 5, 5, 2, 2, 5, 5,
Toshihiro Shimizu 890ddd
	 4, 4, 1, 5, 2, 2, 1, 5, 4, 4, 5, 5, 2, 2, 5, 5,
Toshihiro Shimizu 890ddd
	 6, 6, 1, 6, 2, 2, 1, 6, 6, 6, 6, 6, 2, 2, 6, 6,
Toshihiro Shimizu 890ddd
	 4, 4, 1, 0, 2, 2, 1, 0, 4, 4, 8, 8, 2, 2, 8, 8,
Toshihiro Shimizu 890ddd
	 6, 6, 1, 6, 2, 2, 1, 6, 6, 6, 6, 6, 2, 2, 6, 6,
Toshihiro Shimizu 890ddd
	 4, 4, 1, 0, 2, 2, 1, 0, 4, 4, 8, 8, 2, 2, 8, 8,
Toshihiro Shimizu 890ddd
	 7, 7, 1, 7, 2, 2, 1, 7, 7, 7, 7, 7, 2, 2, 7, 7,
Toshihiro Shimizu 890ddd
	 4, 4, 1, 0, 2, 2, 1, 0, 4, 4, 3, 3, 2, 2, 3, 3,
Toshihiro Shimizu 890ddd
	 7, 7, 1, 7, 2, 2, 1, 7, 7, 7, 7, 7, 2, 2, 7, 7,
Toshihiro Shimizu 890ddd
	 4, 4, 1, 5, 2, 2, 1, 5, 4, 4, 5, 5, 2, 2, 5, 5,
Toshihiro Shimizu 890ddd
	 7, 7, 1, 7, 2, 2, 1, 7, 7, 7, 7, 7, 2, 2, 7, 7,
Toshihiro Shimizu 890ddd
	 4, 4, 1, 0, 2, 2, 1, 0, 4, 4, 8, 8, 2, 2, 8, 8,
Toshihiro Shimizu 890ddd
	 7, 7, 1, 7, 2, 2, 1, 7, 7, 7, 7, 7, 2, 2, 7, 7,
Toshihiro Shimizu 890ddd
	 4, 4, 1, 0, 2, 2, 1, 0, 4, 4, 8, 8, 2, 2, 8, 8};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static UCHAR Next_point_table[2048] =
Toshihiro Shimizu 890ddd
	{8, 8, 8, 8, 8, 8, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0,
Toshihiro Shimizu 890ddd
	 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
Toshihiro Shimizu 890ddd
	 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 2, 2, 2, 2, 2,
Toshihiro Shimizu 890ddd
	 2, 2, 1, 2, 2, 2, 2, 2, 2, 0, 1, 2, 2, 2, 2, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 1, 1, 1, 1, 1, 3, 0, 1, 1, 1, 1, 1, 1,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 2, 2, 2, 2, 2, 3, 0, 0, 2, 2, 2, 2, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 2, 2, 2, 2, 2, 3, 0, 1, 2, 2, 2, 2, 2,
Toshihiro Shimizu 890ddd
	 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 4, 0, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 4, 4, 1, 4, 1, 4, 4, 4, 4, 0, 1, 4, 1, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 4, 4, 4, 4, 2, 4, 4, 4, 4, 0, 0, 4, 2, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 4, 4, 1, 4, 2, 4, 4, 4, 4, 0, 1, 4, 2, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 4, 3, 4, 4, 4, 3, 0, 0, 4, 0, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 4, 1, 4, 4, 4, 3, 0, 1, 4, 1, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 4, 2, 4, 4, 4, 3, 0, 0, 4, 2, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 4, 2, 4, 4, 4, 3, 0, 1, 4, 2, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 5, 0, 0, 0, 0,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 1, 1, 1, 1, 5, 0, 1, 5, 1, 1, 1, 1,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 2, 2, 2, 2, 5, 0, 0, 5, 2, 2, 2, 2,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 2, 2, 2, 2, 5, 0, 1, 5, 2, 2, 2, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 3, 3, 3, 3, 3, 0, 0, 5, 0, 0, 0, 0,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 1, 1, 1, 1, 3, 0, 1, 5, 1, 1, 1, 1,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 2, 2, 2, 2, 3, 0, 0, 5, 2, 2, 2, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 2, 2, 2, 2, 3, 0, 1, 5, 2, 2, 2, 2,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 5, 4, 4, 4, 5, 0, 0, 5, 0, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 1, 4, 4, 4, 5, 0, 1, 5, 1, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 2, 4, 4, 4, 5, 0, 0, 5, 2, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 2, 4, 4, 4, 5, 0, 1, 5, 2, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 3, 4, 4, 4, 3, 0, 0, 5, 0, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 1, 4, 4, 4, 3, 0, 1, 5, 1, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 2, 4, 4, 4, 3, 0, 0, 5, 2, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 2, 4, 4, 4, 3, 0, 1, 5, 2, 4, 4, 4,
Toshihiro Shimizu 890ddd
	 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 0, 6, 0, 6, 0, 0,
Toshihiro Shimizu 890ddd
	 6, 6, 1, 6, 1, 6, 1, 1, 6, 0, 1, 6, 1, 6, 1, 1,
Toshihiro Shimizu 890ddd
	 6, 6, 6, 6, 2, 6, 2, 2, 6, 0, 0, 6, 2, 6, 2, 2,
Toshihiro Shimizu 890ddd
	 6, 6, 1, 6, 2, 6, 2, 2, 6, 0, 1, 6, 2, 6, 2, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 6, 3, 6, 3, 3, 3, 0, 0, 6, 0, 6, 0, 0,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 6, 1, 6, 1, 1, 3, 0, 1, 6, 1, 6, 1, 1,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 6, 2, 6, 2, 2, 3, 0, 0, 6, 2, 6, 2, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 6, 2, 6, 2, 2, 3, 0, 1, 6, 2, 6, 2, 2,
Toshihiro Shimizu 890ddd
	 6, 6, 6, 6, 6, 6, 4, 4, 6, 0, 0, 6, 0, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 6, 6, 1, 6, 1, 6, 4, 4, 6, 0, 1, 6, 1, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 6, 6, 6, 6, 2, 6, 4, 4, 6, 0, 0, 6, 2, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 6, 6, 1, 6, 2, 6, 4, 4, 6, 0, 1, 6, 2, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 6, 3, 6, 4, 4, 3, 0, 0, 6, 0, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 6, 1, 6, 4, 4, 3, 0, 1, 6, 1, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 6, 2, 6, 4, 4, 3, 0, 0, 6, 2, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 6, 2, 6, 4, 4, 3, 0, 1, 6, 2, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 5, 6, 5, 5, 5, 0, 0, 5, 0, 6, 0, 0,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 1, 6, 1, 1, 5, 0, 1, 5, 1, 6, 1, 1,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 2, 6, 2, 2, 5, 0, 0, 5, 2, 6, 2, 2,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 2, 6, 2, 2, 5, 0, 1, 5, 2, 6, 2, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 3, 6, 3, 3, 3, 0, 0, 5, 0, 6, 0, 0,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 1, 6, 1, 1, 3, 0, 1, 5, 1, 6, 1, 1,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 2, 6, 2, 2, 3, 0, 0, 5, 2, 6, 2, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 2, 6, 2, 2, 3, 0, 1, 5, 2, 6, 2, 2,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 5, 6, 4, 4, 5, 0, 0, 5, 0, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 1, 6, 4, 4, 5, 0, 1, 5, 1, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 2, 6, 4, 4, 5, 0, 0, 5, 2, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 2, 6, 4, 4, 5, 0, 1, 5, 2, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 3, 6, 4, 4, 3, 0, 0, 5, 0, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 1, 6, 4, 4, 3, 0, 1, 5, 1, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 2, 6, 4, 4, 3, 0, 0, 5, 2, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 2, 6, 4, 4, 3, 0, 1, 5, 2, 6, 4, 4,
Toshihiro Shimizu 890ddd
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 7, 0, 7, 7, 0,
Toshihiro Shimizu 890ddd
	 7, 7, 1, 7, 1, 7, 7, 1, 7, 0, 1, 7, 1, 7, 7, 1,
Toshihiro Shimizu 890ddd
	 7, 7, 7, 7, 2, 7, 7, 2, 7, 0, 0, 7, 2, 7, 7, 2,
Toshihiro Shimizu 890ddd
	 7, 7, 1, 7, 2, 7, 7, 2, 7, 0, 1, 7, 2, 7, 7, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 7, 3, 7, 7, 3, 3, 0, 0, 7, 0, 7, 7, 0,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 7, 1, 7, 7, 1, 3, 0, 1, 7, 1, 7, 7, 1,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 7, 2, 7, 7, 2, 3, 0, 0, 7, 2, 7, 7, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 7, 2, 7, 7, 2, 3, 0, 1, 7, 2, 7, 7, 2,
Toshihiro Shimizu 890ddd
	 7, 7, 7, 7, 7, 7, 7, 4, 7, 0, 0, 7, 0, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 7, 7, 1, 7, 1, 7, 7, 4, 7, 0, 1, 7, 1, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 7, 7, 7, 7, 2, 7, 7, 4, 7, 0, 0, 7, 2, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 7, 7, 1, 7, 2, 7, 7, 4, 7, 0, 1, 7, 2, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 7, 3, 7, 7, 4, 3, 0, 0, 7, 0, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 7, 1, 7, 7, 4, 3, 0, 1, 7, 1, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 7, 2, 7, 7, 4, 3, 0, 0, 7, 2, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 7, 2, 7, 7, 4, 3, 0, 1, 7, 2, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 5, 7, 7, 5, 5, 0, 0, 5, 0, 7, 7, 0,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 1, 7, 7, 1, 5, 0, 1, 5, 1, 7, 7, 1,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 2, 7, 7, 2, 5, 0, 0, 5, 2, 7, 7, 2,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 2, 7, 7, 2, 5, 0, 1, 5, 2, 7, 7, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 3, 7, 7, 3, 3, 0, 0, 5, 0, 7, 7, 0,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 1, 7, 7, 1, 3, 0, 1, 5, 1, 7, 7, 1,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 2, 7, 7, 2, 3, 0, 0, 5, 2, 7, 7, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 2, 7, 7, 2, 3, 0, 1, 5, 2, 7, 7, 2,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 5, 7, 7, 4, 5, 0, 0, 5, 0, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 1, 7, 7, 4, 5, 0, 1, 5, 1, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 2, 7, 7, 4, 5, 0, 0, 5, 2, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 2, 7, 7, 4, 5, 0, 1, 5, 2, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 3, 7, 7, 4, 3, 0, 0, 5, 0, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 1, 7, 7, 4, 3, 0, 1, 5, 1, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 2, 7, 7, 4, 3, 0, 0, 5, 2, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 2, 7, 7, 4, 3, 0, 1, 5, 2, 7, 7, 4,
Toshihiro Shimizu 890ddd
	 6, 6, 6, 6, 6, 6, 7, 6, 6, 0, 0, 6, 0, 6, 7, 0,
Toshihiro Shimizu 890ddd
	 6, 6, 1, 6, 1, 6, 7, 1, 6, 0, 1, 6, 1, 6, 7, 1,
Toshihiro Shimizu 890ddd
	 6, 6, 6, 6, 2, 6, 7, 2, 6, 0, 0, 6, 2, 6, 7, 2,
Toshihiro Shimizu 890ddd
	 6, 6, 1, 6, 2, 6, 7, 2, 6, 0, 1, 6, 2, 6, 7, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 6, 3, 6, 7, 3, 3, 0, 0, 6, 0, 6, 7, 0,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 6, 1, 6, 7, 1, 3, 0, 1, 6, 1, 6, 7, 1,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 6, 2, 6, 7, 2, 3, 0, 0, 6, 2, 6, 7, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 6, 2, 6, 7, 2, 3, 0, 1, 6, 2, 6, 7, 2,
Toshihiro Shimizu 890ddd
	 6, 6, 6, 6, 6, 6, 7, 4, 6, 0, 0, 6, 0, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 6, 6, 1, 6, 1, 6, 7, 4, 6, 0, 1, 6, 1, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 6, 6, 6, 6, 2, 6, 7, 4, 6, 0, 0, 6, 2, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 6, 6, 1, 6, 2, 6, 7, 4, 6, 0, 1, 6, 2, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 6, 3, 6, 7, 4, 3, 0, 0, 6, 0, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 6, 1, 6, 7, 4, 3, 0, 1, 6, 1, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 6, 2, 6, 7, 4, 3, 0, 0, 6, 2, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 6, 2, 6, 7, 4, 3, 0, 1, 6, 2, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 5, 6, 7, 5, 5, 0, 0, 5, 0, 6, 7, 0,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 1, 6, 7, 1, 5, 0, 1, 5, 1, 6, 7, 1,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 2, 6, 7, 2, 5, 0, 0, 5, 2, 6, 7, 2,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 2, 6, 7, 2, 5, 0, 1, 5, 2, 6, 7, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 3, 6, 7, 3, 3, 0, 0, 5, 0, 6, 7, 0,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 1, 6, 7, 1, 3, 0, 1, 5, 1, 6, 7, 1,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 2, 6, 7, 2, 3, 0, 0, 5, 2, 6, 7, 2,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 2, 6, 7, 2, 3, 0, 1, 5, 2, 6, 7, 2,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 5, 6, 7, 4, 5, 0, 0, 5, 0, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 1, 6, 7, 4, 5, 0, 1, 5, 1, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 5, 5, 2, 6, 7, 4, 5, 0, 0, 5, 2, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 5, 5, 1, 5, 2, 6, 7, 4, 5, 0, 1, 5, 2, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 3, 6, 7, 4, 3, 0, 0, 5, 0, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 1, 6, 7, 4, 3, 0, 1, 5, 1, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 3, 5, 2, 6, 7, 4, 3, 0, 0, 5, 2, 6, 7, 4,
Toshihiro Shimizu 890ddd
	 3, 3, 1, 5, 2, 6, 7, 4, 3, 0, 1, 5, 2, 6, 7, 4};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int Lx;
Toshihiro Shimizu 890ddd
int Displace_vector[8];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define E_PIX(pix) \
Toshihiro Shimizu 890ddd
	(*(pix + 1))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define W_PIX(pix) \
Toshihiro Shimizu 890ddd
	(*(pix - 1))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define N_PIX(pix) \
Toshihiro Shimizu 890ddd
	(*(pix + Lx))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define S_PIX(pix) \
Toshihiro Shimizu 890ddd
	(*(pix - Lx))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define SW_PIX(pix) \
Toshihiro Shimizu 890ddd
	(*(pix - Lx - 1))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define NW_PIX(pix) \
Toshihiro Shimizu 890ddd
	(*(pix + Lx - 1))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define NE_PIX(pix) \
Toshihiro Shimizu 890ddd
	(*(pix + Lx + 1))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define SE_PIX(pix) \
Toshihiro Shimizu 890ddd
	(*(pix - Lx + 1))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define NEIGHBOURS_CODE(pix)     \
Toshihiro Shimizu 890ddd
	(((SW_PIX(pix) != 0) << 0) | \
Toshihiro Shimizu 890ddd
	 ((S_PIX(pix) != 0) << 1) |  \
Toshihiro Shimizu 890ddd
	 ((SE_PIX(pix) != 0) << 2) | \
Toshihiro Shimizu 890ddd
	 ((W_PIX(pix) != 0) << 3) |  \
Toshihiro Shimizu 890ddd
	 ((E_PIX(pix) != 0) << 4) |  \
Toshihiro Shimizu 890ddd
	 ((NW_PIX(pix) != 0) << 5) | \
Toshihiro Shimizu 890ddd
	 ((N_PIX(pix) != 0) << 6) |  \
Toshihiro Shimizu 890ddd
	 ((NE_PIX(pix) != 0) << 7))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define FIRST_PRESEED(code) \
Toshihiro Shimizu 890ddd
	First_preseed_table[code]
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define NEXT_POINT(code, preseed) \
Toshihiro Shimizu 890ddd
	Next_point_table[(code << 3) | preseed]
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define UPDATE_POS(displ_code, x_pos, y_pos) \
Shinya Kitaoka d4642c
	switch (displ_code) { \
Shinya Kitaoka d4642c
	case 0: x_pos--; y_pos--; break; \
Shinya Kitaoka d4642c
	case 1:          y_pos--; break; \
Shinya Kitaoka d4642c
	case 2: x_pos++; y_pos--; break; \
Shinya Kitaoka d4642c
	case 3: x_pos--;          break; \
Shinya Kitaoka d4642c
	case 4: x_pos++;          break; \
Shinya Kitaoka d4642c
	case 5: x_pos--; y_pos++; break; \
Shinya Kitaoka d4642c
	case 6:          y_pos++; break; \
Shinya Kitaoka d4642c
	case 7: x_pos++; y_pos++; break; \
Shinya Kitaoka d4642c
	default: assert(FALSE); \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int fillByteRaster(const TRaster32P &rin, TRasterGR8P &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	bool ret = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(rin->getLx() == rout->getLx() - 2 && rin->getLy() == rout->getLy() - 2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	memset(rout->getRawData(), 0, rout->getLx() * rout->getLy());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < rin->getLy(); i++) {
Toshihiro Shimizu 890ddd
		TPixel32 *bufIn = (TPixel32 *)rin->getRawData() + i * rin->getWrap();
Toshihiro Shimizu 890ddd
		UCHAR *bufOut = (UCHAR *)rout->getRawData() + (i + 1) * rout->getWrap() + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int j = 0; j < rin->getLx(); j++, bufIn++, bufOut++)
Toshihiro Shimizu 890ddd
			if (bufIn->m != 0) {
Toshihiro Shimizu 890ddd
				ret = true;
Toshihiro Shimizu 890ddd
				*bufOut = 1;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static void initialize_displace_vector(void)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Displace_vector[0] = -Lx - 1;
Toshihiro Shimizu 890ddd
	Displace_vector[1] = -Lx;
Toshihiro Shimizu 890ddd
	Displace_vector[2] = -Lx + 1;
Toshihiro Shimizu 890ddd
	Displace_vector[3] = -1;
Toshihiro Shimizu 890ddd
	Displace_vector[4] = +1;
Toshihiro Shimizu 890ddd
	Displace_vector[5] = Lx - 1;
Toshihiro Shimizu 890ddd
	Displace_vector[6] = Lx;
Toshihiro Shimizu 890ddd
	Displace_vector[7] = Lx + 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int find_next_seed(const TRasterGR8P &r, int first_seed,
Toshihiro Shimizu 890ddd
						  UCHAR *&seed, int &x_pos, int &y_pos)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static int Curr_x = 0, Curr_y = 0;
Toshihiro Shimizu 890ddd
	UCHAR *pix;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (first_seed) {
Toshihiro Shimizu 890ddd
		Curr_x = 0;
Toshihiro Shimizu 890ddd
		Curr_y = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	seed = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (; Curr_y < r->getLy(); Curr_y++) {
Toshihiro Shimizu 890ddd
		pix = (UCHAR *)r->getRawData() + Curr_y * r->getWrap() + Curr_x;
Toshihiro Shimizu 890ddd
		for (; Curr_x < r->getLx(); Curr_x++, pix++)
Toshihiro Shimizu 890ddd
			if (*pix == 1 &&
Toshihiro Shimizu 890ddd
				(!E_PIX(pix) ||
Toshihiro Shimizu 890ddd
				 !W_PIX(pix) ||
Toshihiro Shimizu 890ddd
				 !N_PIX(pix) ||
Toshihiro Shimizu 890ddd
				 !S_PIX(pix))) {
Toshihiro Shimizu 890ddd
				seed = pix;
Toshihiro Shimizu 890ddd
				x_pos = Curr_x;
Toshihiro Shimizu 890ddd
				y_pos = Curr_y;
Toshihiro Shimizu 890ddd
				return true;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		Curr_x = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define COMPUTE_NUM_DEN(x_pos, y_pos, old_x_pos, old_y_pos) \
Toshihiro Shimizu 890ddd
	{                                                       \
Toshihiro Shimizu 890ddd
		aux = old_x_pos * y_pos - x_pos * old_y_pos;        \
Toshihiro Shimizu 890ddd
		den += aux;                                         \
Toshihiro Shimizu 890ddd
		x_num += (x_pos + old_x_pos) * aux;                 \
Toshihiro Shimizu 890ddd
		y_num += (y_pos + old_y_pos) * aux;                 \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void doComputeCentroid(const TRasterGR8P &r, TPoint &cp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int preseed, displ, prewalker, code;
Toshihiro Shimizu 890ddd
	UCHAR *seed, *walker;
Toshihiro Shimizu 890ddd
	int x_num = 0, y_num = 0, den = 0;
Toshihiro Shimizu 890ddd
	int x_pos, y_pos, old_x_pos, old_y_pos;
Toshihiro Shimizu 890ddd
	int aux, old_displ = -1;
Toshihiro Shimizu 890ddd
	int first_seed;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Lx = r->getLx();
Toshihiro Shimizu 890ddd
	Displace_vector[0] = -Lx - 1;
Toshihiro Shimizu 890ddd
	Displace_vector[1] = -Lx;
Toshihiro Shimizu 890ddd
	Displace_vector[2] = -Lx + 1;
Toshihiro Shimizu 890ddd
	Displace_vector[3] = -1;
Toshihiro Shimizu 890ddd
	Displace_vector[4] = +1;
Toshihiro Shimizu 890ddd
	Displace_vector[5] = Lx - 1;
Toshihiro Shimizu 890ddd
	Displace_vector[6] = Lx;
Toshihiro Shimizu 890ddd
	Displace_vector[7] = Lx + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	first_seed = TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (find_next_seed(r, first_seed, seed, x_pos, y_pos)) {
Toshihiro Shimizu 890ddd
		first_seed = FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		*seed |= 0x2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		code = NEIGHBOURS_CODE(seed);
Toshihiro Shimizu 890ddd
		preseed = FIRST_PRESEED(code);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (preseed == 8)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		displ = NEXT_POINT(code, preseed);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		walker = seed + Displace_vector[displ];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		old_x_pos = x_pos;
Toshihiro Shimizu 890ddd
		old_y_pos = y_pos;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		UPDATE_POS(displ, x_pos, y_pos)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		COMPUTE_NUM_DEN(x_pos, y_pos, old_x_pos, old_y_pos)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		prewalker = displ ^ 0x7;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		while ((walker != seed) || (preseed != prewalker)) {
Toshihiro Shimizu 890ddd
			*walker |= 0x2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			displ = NEXT_POINT(NEIGHBOURS_CODE(walker), prewalker);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			walker += Displace_vector[displ];
Toshihiro Shimizu 890ddd
			old_x_pos = x_pos;
Toshihiro Shimizu 890ddd
			old_y_pos = y_pos;
Toshihiro Shimizu 890ddd
			UPDATE_POS(displ, x_pos, y_pos)
Toshihiro Shimizu 890ddd
			COMPUTE_NUM_DEN(x_pos, y_pos, old_x_pos, old_y_pos)
Toshihiro Shimizu 890ddd
			prewalker = displ ^ 0x7;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (den)
Toshihiro Shimizu 890ddd
		cp = TPoint(tround(x_num / (3.0 * den)), tround(y_num / (3.0 * den)));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPoint computeCentroid(const TRaster32P &r)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPoint ret(1, 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterGR8P raux(r->getLx() + 2, r->getLy() + 2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (fillByteRaster(r, raux))
Toshihiro Shimizu 890ddd
		doComputeCentroid(raux, ret);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ret.x--;
Toshihiro Shimizu 890ddd
	ret.y--; /* per il bordo aggiunto */
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}