Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
guardare DAFARE
Toshihiro Shimizu 890ddd
write_lut_image ??? quando ?
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "autoadjust.h"
Toshihiro Shimizu 890ddd
#include "cleanupcommon.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define TO_ABS(x)       \
Toshihiro Shimizu 890ddd
	{                   \
Toshihiro Shimizu 890ddd
		if ((x) < 0)    \
Toshihiro Shimizu 890ddd
			(x) = -(x); \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef struct big {
Toshihiro Shimizu 890ddd
	UINT lo, hi;
Toshihiro Shimizu 890ddd
} BIG;
Toshihiro Shimizu 890ddd
#define CLEAR_BIG(B) ((B).lo = 0, (B).hi = 0, (B))
Toshihiro Shimizu 890ddd
#define ADD_BIG(B, X) ((B).lo += (UINT)(X), \
Toshihiro Shimizu 890ddd
					   (B).hi += (B).lo >> 30, (B).lo &= 0x3fffffff, (B))
Toshihiro Shimizu 890ddd
#define ADD_BIG_BIG(B1, B2) ((B1).lo += (B2).lo, (B1).hi += (B2).hi, \
Toshihiro Shimizu 890ddd
							 (B1).hi += (B1).lo >> 30, (B1).lo &= 0x3fffffff, (B1))
Toshihiro Shimizu 890ddd
#define BIG_TO_DOUBLE(B) ((double)(B).hi * (double)0x40000000 + (double)(B).lo)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int Black = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int Ref_cum[256];
Toshihiro Shimizu 890ddd
static int Ref_edgelen;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void autoadj_set_black_value(int black)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Black = black;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static void smooth_func256(float func[256], int rad)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, j, k;
Toshihiro Shimizu 890ddd
	float smooth[256];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < 256; i++) {
Toshihiro Shimizu 890ddd
		k = i - rad;
Toshihiro Shimizu 890ddd
		notLessThan(0, k);
Toshihiro Shimizu 890ddd
		smooth[i] = func[k] / 2;
Toshihiro Shimizu 890ddd
		k = i + rad;
Toshihiro Shimizu 890ddd
		notMoreThan(255, k);
Toshihiro Shimizu 890ddd
		smooth[i] += func[k] / 2;
Toshihiro Shimizu 890ddd
		for (j = i - rad + 1; j <= i + rad - 1; j++) {
Toshihiro Shimizu 890ddd
			k = j;
Toshihiro Shimizu 890ddd
			notLessThan(0, k);
Toshihiro Shimizu 890ddd
			notMoreThan(255, k);
Toshihiro Shimizu 890ddd
			smooth[i] += func[k];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		smooth[i] /= 2 * rad;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (i = 0; i < 256; i++)
Toshihiro Shimizu 890ddd
		func[i] = smooth[i];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static void build_cum(int histo[256], int cum[256])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	cum[0] = histo[0];
Toshihiro Shimizu 890ddd
	for (i = 1; i < 256; i++)
Toshihiro Shimizu 890ddd
		cum[i] = cum[i - 1] + histo[i];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int Window_x0, Window_y0, Window_x1, Window_y1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void set_autoadjust_window(int x0, int y0, int x1, int y1)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Window_x0 = x0;
Toshihiro Shimizu 890ddd
	Window_y0 = y0;
Toshihiro Shimizu 890ddd
	Window_x1 = x1;
Toshihiro Shimizu 890ddd
	Window_y1 = y1;
Toshihiro Shimizu 890ddd
	if (Window_x0 > Window_x1)
Toshihiro Shimizu 890ddd
		tswap(Window_x0, Window_x1);
Toshihiro Shimizu 890ddd
	if (Window_y0 > Window_y1)
Toshihiro Shimizu 890ddd
		tswap(Window_y0, Window_y1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static void get_virtual_buffer(const TRasterImageP &image,
Toshihiro Shimizu 890ddd
							   int *p_lx, int *p_ly, int *p_wrap,
Toshihiro Shimizu 890ddd
							   UCHAR **p_buffer)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int x0, y0, x1, y1;
Toshihiro Shimizu 890ddd
	int x_margin, y_margin;
Toshihiro Shimizu 890ddd
	int lx, ly, wrap;
Toshihiro Shimizu 890ddd
	UCHAR *buffer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterGR8P ras8(image->getRaster());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(ras8);
Toshihiro Shimizu 890ddd
	double xdpi, ydpi;
Toshihiro Shimizu 890ddd
	image->getDpi(xdpi, ydpi);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* BORDO DI MEZZO CENTIMETRO */
Toshihiro Shimizu 890ddd
	x_margin = troundp(mmToPixel(5.0, xdpi));
Toshihiro Shimizu 890ddd
	y_margin = troundp(mmToPixel(5.0, ydpi));
Toshihiro Shimizu 890ddd
	x0 = Window_x0 + x_margin;
Toshihiro Shimizu 890ddd
	y0 = Window_y0 + y_margin;
Toshihiro Shimizu 890ddd
	x1 = Window_x1 - x_margin;
Toshihiro Shimizu 890ddd
	y1 = Window_y1 - y_margin;
Toshihiro Shimizu 890ddd
	notLessThan(x0 + 9, x1);
Toshihiro Shimizu 890ddd
	notLessThan(y0 + 9, y1);
Toshihiro Shimizu 890ddd
	notLessThan(0, x0);
Toshihiro Shimizu 890ddd
	notMoreThan(ras8->getLx() - 1, x0);
Toshihiro Shimizu 890ddd
	notLessThan(0, y0);
Toshihiro Shimizu 890ddd
	notMoreThan(ras8->getLy() - 1, y0);
Toshihiro Shimizu 890ddd
	notLessThan(0, x1);
Toshihiro Shimizu 890ddd
	notMoreThan(ras8->getLx() - 1, x1);
Toshihiro Shimizu 890ddd
	notLessThan(0, y1);
Toshihiro Shimizu 890ddd
	notMoreThan(ras8->getLy() - 1, y1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	lx = x1 - x0 + 1;
Toshihiro Shimizu 890ddd
	ly = y1 - y0 + 1;
Toshihiro Shimizu 890ddd
	wrap = ras8->getWrap();
Toshihiro Shimizu 890ddd
	buffer = (UCHAR *)ras8->getRawData() + x0 + y0 * wrap;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	*p_lx = lx;
Toshihiro Shimizu 890ddd
	*p_ly = ly;
Toshihiro Shimizu 890ddd
	*p_wrap = wrap;
Toshihiro Shimizu 890ddd
	*p_buffer = buffer;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void black_eq_algo(const TRasterImageP &image)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int lx, ly, wrap;
Toshihiro Shimizu 890ddd
	int x, y, grey /*, width*/;
Toshihiro Shimizu 890ddd
	int d_histo[256][256];
Toshihiro Shimizu 890ddd
	int d, dd, m;
Toshihiro Shimizu 890ddd
	UCHAR *buffer, *pix, *north, *south;
Toshihiro Shimizu 890ddd
	UCHAR prev, darkest;
Toshihiro Shimizu 890ddd
	long n;
Toshihiro Shimizu 890ddd
	float mean_d[256];
Toshihiro Shimizu 890ddd
	BIG s;
Toshihiro Shimizu 890ddd
	int max_d_grey;
Toshihiro Shimizu 890ddd
	float max_d;
Toshihiro Shimizu 890ddd
	float fac;
Toshihiro Shimizu 890ddd
	int val;
Toshihiro Shimizu 890ddd
	UCHAR lut[256];
Toshihiro Shimizu 890ddd
	image->getRaster()->lock();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	get_virtual_buffer(image, &lx, &ly, &wrap, &buffer);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (grey = 0; grey < 256; grey++) {
Toshihiro Shimizu 890ddd
		for (d = 0; d < 256; d++)
Toshihiro Shimizu 890ddd
			d_histo[grey][d] = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (y = 1; y < ly - 1; y++) {
Toshihiro Shimizu 890ddd
		pix = buffer + y * wrap + 1;
Toshihiro Shimizu 890ddd
		north = pix + wrap;
Toshihiro Shimizu 890ddd
		south = pix - wrap;
Toshihiro Shimizu 890ddd
		for (x = 1; x < lx - 1; x++, pix++, north++, south++) {
Toshihiro Shimizu 890ddd
			m = 2 * ((int)north[0] + (int)pix[-1] + 2 * (int)pix[0] + (int)pix[1] + (int)south[0]) + (int)north[-1] + (int)north[1] + (int)south[-1] + (int)south[1];
Toshihiro Shimizu 890ddd
			m = (m + 8) >> 4;
Toshihiro Shimizu 890ddd
			d = (int)north[-1] + (int)north[0] * 2 + (int)north[1] - (int)south[-1] - (int)south[0] * 2 - (int)south[1];
Toshihiro Shimizu 890ddd
			TO_ABS(d)
Toshihiro Shimizu 890ddd
			dd = (int)north[-1] + (int)pix[-1] * 2 + (int)south[-1] - (int)north[1] - (int)pix[1] * 2 - (int)south[1];
Toshihiro Shimizu 890ddd
			TO_ABS(dd)
Toshihiro Shimizu 890ddd
			if (dd > d)
Toshihiro Shimizu 890ddd
				d = dd;
Toshihiro Shimizu 890ddd
			dd = (int)pix[-1] + (int)north[-1] * 2 + (int)north[0] - (int)pix[1] - (int)south[1] * 2 - (int)south[0];
Toshihiro Shimizu 890ddd
			TO_ABS(dd)
Toshihiro Shimizu 890ddd
			if (dd > d)
Toshihiro Shimizu 890ddd
				d = dd;
Toshihiro Shimizu 890ddd
			dd = (int)pix[1] + (int)north[1] * 2 + (int)north[0] - (int)pix[-1] - (int)south[-1] * 2 - (int)south[0];
Toshihiro Shimizu 890ddd
			TO_ABS(dd)
Toshihiro Shimizu 890ddd
			if (dd > d)
Toshihiro Shimizu 890ddd
				d = dd;
Toshihiro Shimizu 890ddd
			d = (d + 2) >> 2;
Toshihiro Shimizu 890ddd
			d_histo[m][d]++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (grey = 0; grey < 256; grey++) {
Toshihiro Shimizu 890ddd
		n = 0;
Toshihiro Shimizu 890ddd
		CLEAR_BIG(s);
Toshihiro Shimizu 890ddd
		for (d = 0; d < 256; d++) {
Toshihiro Shimizu 890ddd
			ADD_BIG(s, d_histo[grey][d] * d);
Toshihiro Shimizu 890ddd
			n += d_histo[grey][d];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (n)
Toshihiro Shimizu 890ddd
			mean_d[grey] = (float)(BIG_TO_DOUBLE(s) / n);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			mean_d[grey] = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	smooth_func256(mean_d, 5);
Toshihiro Shimizu 890ddd
	max_d_grey = 0;
Toshihiro Shimizu 890ddd
	max_d = 0.0;
Toshihiro Shimizu 890ddd
	for (grey = 0; grey < 256; grey++) {
Toshihiro Shimizu 890ddd
		if (max_d < mean_d[grey]) {
Toshihiro Shimizu 890ddd
			max_d = mean_d[grey];
Toshihiro Shimizu 890ddd
			max_d_grey = grey;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	n = 0;
Toshihiro Shimizu 890ddd
	CLEAR_BIG(s);
Toshihiro Shimizu 890ddd
	for (y = 0; y < ly; y++) {
Toshihiro Shimizu 890ddd
		pix = buffer + y * wrap;
Toshihiro Shimizu 890ddd
		prev = 255;
Toshihiro Shimizu 890ddd
		darkest = 255;
Toshihiro Shimizu 890ddd
		for (x = 0; x < lx; x++, pix++) {
Toshihiro Shimizu 890ddd
			if (*pix < max_d_grey) {
Toshihiro Shimizu 890ddd
				if (prev < max_d_grey)
Toshihiro Shimizu 890ddd
					notMoreThan(*pix, darkest);
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					darkest = *pix;
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				if (prev < max_d_grey) {
Toshihiro Shimizu 890ddd
					ADD_BIG(s, darkest);
Toshihiro Shimizu 890ddd
					n++;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			prev = *pix;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	darkest = troundp(BIG_TO_DOUBLE(s) / n);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
for (grey = 0; grey < darkest; grey++)
Toshihiro Shimizu 890ddd
  lut[grey] = 0;
Toshihiro Shimizu 890ddd
fac = (float)255 / (float)(255 - darkest);
Toshihiro Shimizu 890ddd
for (grey = darkest; grey < 256; grey++)
Toshihiro Shimizu 890ddd
  lut[grey] = 255 - troundp ((255 - grey) * fac);
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
	notLessThan(0, Black);
Toshihiro Shimizu 890ddd
	;
Toshihiro Shimizu 890ddd
	notMoreThan(255, Black);
Toshihiro Shimizu 890ddd
	fac = (float)(255 - Black) / (float)(255 - darkest);
Toshihiro Shimizu 890ddd
	for (grey = 0; grey < 256; grey++) {
Toshihiro Shimizu 890ddd
		val = 255 - troundp((255 - grey) * fac);
Toshihiro Shimizu 890ddd
		notLessThan(0, val);
Toshihiro Shimizu 890ddd
		notMoreThan(255, val);
Toshihiro Shimizu 890ddd
		lut[grey] = val;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	apply_lut(image, lut);
Toshihiro Shimizu 890ddd
	image->getRaster()->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define MAX_WIDTH 100
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void build_lw(const TRasterImageP &image, float lw[256])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int lx, ly, wrap;
Toshihiro Shimizu 890ddd
	int x, y, grey, width;
Toshihiro Shimizu 890ddd
	int d_histo[256][256];
Toshihiro Shimizu 890ddd
	int d, dd;
Toshihiro Shimizu 890ddd
	int lw_histo[256][MAX_WIDTH + 1];
Toshihiro Shimizu 890ddd
	int x_start[256];
Toshihiro Shimizu 890ddd
	static int *y_start[256];
Toshihiro Shimizu 890ddd
	static int y_start_alloc = 0;
Toshihiro Shimizu 890ddd
	UCHAR *buffer, *pix, *north, *south;
Toshihiro Shimizu 890ddd
	UCHAR cur_grey, x_prev_grey, x_next_grey, y_prev_grey, y_next_grey;
Toshihiro Shimizu 890ddd
	int x_grad, y_grad;
Toshihiro Shimizu 890ddd
	long n;
Toshihiro Shimizu 890ddd
	float mean_d[256];
Toshihiro Shimizu 890ddd
	BIG s;
Toshihiro Shimizu 890ddd
	int max_d_grey;
Toshihiro Shimizu 890ddd
	float max_d;
Toshihiro Shimizu 890ddd
	long max_d_n;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	get_virtual_buffer(image, &lx, &ly, &wrap, &buffer);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (y_start_alloc < lx) {
Toshihiro Shimizu 890ddd
		for (grey = 0; grey < 256; grey++) {
Toshihiro Shimizu 890ddd
			delete[] y_start[grey];
Toshihiro Shimizu 890ddd
			y_start[grey] = new int[lx];
Toshihiro Shimizu 890ddd
			//TREALLOC (y_start[grey], lx);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		y_start_alloc = lx;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (grey = 0; grey < 256; grey++) {
Toshihiro Shimizu 890ddd
		for (d = 0; d < 256; d++)
Toshihiro Shimizu 890ddd
			d_histo[grey][d] = 0;
Toshihiro Shimizu 890ddd
		for (x = 0; x < lx; x++)
Toshihiro Shimizu 890ddd
			y_start[grey][x] = -MAX_WIDTH - 1;
Toshihiro Shimizu 890ddd
		for (width = 0; width <= MAX_WIDTH; width++)
Toshihiro Shimizu 890ddd
			lw_histo[grey][width] = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (y = 1; y < ly - 1; y++) {
Toshihiro Shimizu 890ddd
		for (grey = 0; grey < 256; grey++)
Toshihiro Shimizu 890ddd
			x_start[grey] = -MAX_WIDTH - 1;
Toshihiro Shimizu 890ddd
		pix = buffer + y * wrap + 1;
Toshihiro Shimizu 890ddd
		north = pix + wrap;
Toshihiro Shimizu 890ddd
		south = pix - wrap;
Toshihiro Shimizu 890ddd
		for (x = 1; x < lx - 1; x++, pix++, north++, south++) {
Toshihiro Shimizu 890ddd
			d = (int)north[-1] + (int)north[0] * 2 + (int)north[1] - (int)south[-1] - (int)south[0] * 2 - (int)south[1];
Toshihiro Shimizu 890ddd
			TO_ABS(d)
Toshihiro Shimizu 890ddd
			dd = (int)north[-1] + (int)pix[-1] * 2 + (int)south[-1] - (int)north[1] - (int)pix[1] * 2 - (int)south[1];
Toshihiro Shimizu 890ddd
			TO_ABS(dd)
Toshihiro Shimizu 890ddd
			if (dd > d)
Toshihiro Shimizu 890ddd
				d = dd;
Toshihiro Shimizu 890ddd
			dd = (int)pix[-1] + (int)north[-1] * 2 + (int)north[0] - (int)pix[1] - (int)south[1] * 2 - (int)south[0];
Toshihiro Shimizu 890ddd
			TO_ABS(dd)
Toshihiro Shimizu 890ddd
			if (dd > d)
Toshihiro Shimizu 890ddd
				d = dd;
Toshihiro Shimizu 890ddd
			dd = (int)pix[1] + (int)north[1] * 2 + (int)north[0] - (int)pix[-1] - (int)south[-1] * 2 - (int)south[0];
Toshihiro Shimizu 890ddd
			TO_ABS(dd)
Toshihiro Shimizu 890ddd
			if (dd > d)
Toshihiro Shimizu 890ddd
				d = dd;
Toshihiro Shimizu 890ddd
			d >>= 3;
Toshihiro Shimizu 890ddd
			d_histo[*pix][d]++;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			cur_grey = pix[0];
Toshihiro Shimizu 890ddd
			x_prev_grey = pix[-1];
Toshihiro Shimizu 890ddd
			x_next_grey = pix[1];
Toshihiro Shimizu 890ddd
			y_prev_grey = south[0];
Toshihiro Shimizu 890ddd
			y_next_grey = north[0];
Toshihiro Shimizu 890ddd
			x_grad = (int)x_next_grey - (int)x_prev_grey;
Toshihiro Shimizu 890ddd
			y_grad = (int)y_next_grey - (int)y_prev_grey;
Toshihiro Shimizu 890ddd
			if (cur_grey < x_prev_grey) {
Toshihiro Shimizu 890ddd
				if (x_grad < 0 && x_grad < y_grad && x_grad < -y_grad)
Toshihiro Shimizu 890ddd
					for (grey = cur_grey; grey < x_prev_grey; grey++)
Toshihiro Shimizu 890ddd
						x_start[grey] = x;
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					for (grey = cur_grey; grey < x_prev_grey; grey++)
Toshihiro Shimizu 890ddd
						x_start[grey] = -MAX_WIDTH - 1;
Toshihiro Shimizu 890ddd
			} else if (cur_grey > x_prev_grey) {
Toshihiro Shimizu 890ddd
				if (x_grad > 0 && x_grad > y_grad && x_grad > -y_grad)
Toshihiro Shimizu 890ddd
					for (grey = x_prev_grey; grey < cur_grey; grey++) {
Toshihiro Shimizu 890ddd
						width = x - x_start[grey];
Toshihiro Shimizu 890ddd
						if (width <= MAX_WIDTH)
Toshihiro Shimizu 890ddd
							lw_histo[grey][width]++;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (cur_grey < y_prev_grey) {
Toshihiro Shimizu 890ddd
				if (y_grad < 0 && y_grad < x_grad && y_grad < -x_grad)
Toshihiro Shimizu 890ddd
					for (grey = cur_grey; grey < y_prev_grey; grey++)
Toshihiro Shimizu 890ddd
						y_start[grey][x] = y;
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					for (grey = cur_grey; grey < y_prev_grey; grey++)
Toshihiro Shimizu 890ddd
						y_start[grey][x] = -MAX_WIDTH - 1;
Toshihiro Shimizu 890ddd
			} else if (cur_grey > y_prev_grey) {
Toshihiro Shimizu 890ddd
				if (y_grad > 0 && y_grad > x_grad && y_grad > -x_grad)
Toshihiro Shimizu 890ddd
					for (grey = y_prev_grey; grey < cur_grey; grey++) {
Toshihiro Shimizu 890ddd
						width = y - y_start[grey][x];
Toshihiro Shimizu 890ddd
						if (width <= MAX_WIDTH)
Toshihiro Shimizu 890ddd
							lw_histo[grey][width]++;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (grey = 0; grey < 256; grey++) {
Toshihiro Shimizu 890ddd
		n = 0;
Toshihiro Shimizu 890ddd
		CLEAR_BIG(s);
Toshihiro Shimizu 890ddd
		for (width = 0; width <= MAX_WIDTH; width++) {
Toshihiro Shimizu 890ddd
			ADD_BIG(s, lw_histo[grey][width] * width);
Toshihiro Shimizu 890ddd
			n += lw_histo[grey][width];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (n)
Toshihiro Shimizu 890ddd
			lw[grey] = (float)(BIG_TO_DOUBLE(s) / n);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			lw[grey] = 0.0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	max_d_grey = 0;
Toshihiro Shimizu 890ddd
	max_d = 0.0;
Toshihiro Shimizu 890ddd
	for (grey = 0; grey < 256; grey++) {
Toshihiro Shimizu 890ddd
		n = 0;
Toshihiro Shimizu 890ddd
		CLEAR_BIG(s);
Toshihiro Shimizu 890ddd
		for (d = 0; d < 256; d++) {
Toshihiro Shimizu 890ddd
			ADD_BIG(s, d_histo[grey][d] * d);
Toshihiro Shimizu 890ddd
			n += d_histo[grey][d];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (n)
Toshihiro Shimizu 890ddd
			mean_d[grey] = (float)(BIG_TO_DOUBLE(s) / n);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			mean_d[grey] = 0.0;
Toshihiro Shimizu 890ddd
		if (max_d < mean_d[grey]) {
Toshihiro Shimizu 890ddd
			max_d = mean_d[grey];
Toshihiro Shimizu 890ddd
			max_d_grey = grey;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	max_d_n = 0;
Toshihiro Shimizu 890ddd
	for (width = 0; width <= MAX_WIDTH; width++)
Toshihiro Shimizu 890ddd
		max_d_n += lw_histo[max_d_grey][width];
Toshihiro Shimizu 890ddd
	for (grey = max_d_grey - 1; grey >= 0; grey--) {
Toshihiro Shimizu 890ddd
		if (!lw[grey])
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		n = 0;
Toshihiro Shimizu 890ddd
		for (width = 0; width <= MAX_WIDTH; width++)
Toshihiro Shimizu 890ddd
			n += lw_histo[grey][width];
Toshihiro Shimizu 890ddd
		if (n < max_d_n / 10)
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (; grey >= 0; grey--)
Toshihiro Shimizu 890ddd
		lw[grey] = 0.0;
Toshihiro Shimizu 890ddd
	for (grey = max_d_grey + 1; grey < 256; grey++) {
Toshihiro Shimizu 890ddd
		if (!lw[grey])
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		if (255 - grey < (255 - max_d_grey) / 2)
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		if (mean_d[grey] < max_d / 2.0)
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (; grey < 256; grey++)
Toshihiro Shimizu 890ddd
		lw[grey] = 0.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void build_lw_lut(float ref_lw[256], float lw[256], UCHAR lut[256])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/* crea una lut tale che l'immagine con il profilo di linewidths lw
Toshihiro Shimizu 890ddd
   venga mappata in un'immagine con il profilo di riferimento ref_lw.
Toshihiro Shimizu 890ddd
   Le lw sono non decrescenti e delimitate da 0.0 li' dove non sono valide
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
	float bot_ref_lw, top_ref_lw, bot_lw, top_lw;
Toshihiro Shimizu 890ddd
	int bot_ref_gr, top_ref_gr, bot_gr, top_gr;
Toshihiro Shimizu 890ddd
	float min_lw, max_lw;
Toshihiro Shimizu 890ddd
	int min_ref_gr, max_ref_gr, min_gr, max_gr;
Toshihiro Shimizu 890ddd
	float fac;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; !ref_lw[i]; i++) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	bot_ref_lw = ref_lw[i];
Toshihiro Shimizu 890ddd
	bot_ref_gr = i;
Toshihiro Shimizu 890ddd
	for (i = 255; !ref_lw[i]; i--) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	top_ref_lw = ref_lw[i];
Toshihiro Shimizu 890ddd
	top_ref_gr = i;
Toshihiro Shimizu 890ddd
	for (i = 0; !lw[i]; i++) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	bot_lw = lw[i];
Toshihiro Shimizu 890ddd
	bot_gr = i;
Toshihiro Shimizu 890ddd
	for (i = 255; !lw[i]; i--) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	top_lw = lw[i];
Toshihiro Shimizu 890ddd
	top_gr = i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	min_lw = tmax(bot_ref_lw, bot_lw);
Toshihiro Shimizu 890ddd
	max_lw = tmin(top_ref_lw, top_lw);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (min_lw >= max_lw) {
Toshihiro Shimizu 890ddd
		for (i = 0; i < 256; i++)
Toshihiro Shimizu 890ddd
			lut[i] = i;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = bot_ref_gr; ref_lw[i] < min_lw; i++) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	min_ref_gr = i;
Toshihiro Shimizu 890ddd
	for (i = top_ref_gr; ref_lw[i] > max_lw; i--) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	max_ref_gr = i;
Toshihiro Shimizu 890ddd
	for (i = bot_gr; lw[i] < min_lw; i++) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	min_gr = i;
Toshihiro Shimizu 890ddd
	for (i = top_gr; lw[i] > max_lw; i--) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	max_gr = i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	j = min_ref_gr;
Toshihiro Shimizu 890ddd
	for (i = min_gr; i <= max_gr; i++) {
Toshihiro Shimizu 890ddd
		while (ref_lw[j] < lw[i] && j < max_ref_gr)
Toshihiro Shimizu 890ddd
			j++;
Toshihiro Shimizu 890ddd
		lut[i] = j;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	fac = (float)min_ref_gr / (float)min_gr;
Toshihiro Shimizu 890ddd
	for (i = 0; i < min_gr; i++)
Toshihiro Shimizu 890ddd
		lut[i] = troundp(i * fac);
Toshihiro Shimizu 890ddd
	fac = (float)(255 - max_ref_gr) / (float)(255 - max_gr);
Toshihiro Shimizu 890ddd
	for (i = 255; i > max_gr; i--)
Toshihiro Shimizu 890ddd
		lut[i] = 255 - troundp((255 - i) * fac);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/***
Toshihiro Shimizu 890ddd
printf ("-----\n\lut:\n\n");
Toshihiro Shimizu 890ddd
for (i = 255; i >= 0; i--)
Toshihiro Shimizu 890ddd
  printf ("%4d :%4u\n", i, lut[i]);
Toshihiro Shimizu 890ddd
printf ("\n");
Toshihiro Shimizu 890ddd
***/
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void build_gr_cum(const TRasterImageP &image, int cum[256])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int lx, ly, wrap, true_lx, true_ly;
Toshihiro Shimizu 890ddd
	int i, x, y;
Toshihiro Shimizu 890ddd
	UCHAR *pix, *buffer;
Toshihiro Shimizu 890ddd
	int histo[256], raster_is_savebox;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	get_virtual_buffer(image, &lx, &ly, &wrap, &buffer);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < 256; i++)
Toshihiro Shimizu 890ddd
		histo[i] = 0;
Toshihiro Shimizu 890ddd
	for (y = 0; y < ly; y++) {
Toshihiro Shimizu 890ddd
		pix = buffer + y * wrap;
Toshihiro Shimizu 890ddd
		for (x = 0; x < lx; x++)
Toshihiro Shimizu 890ddd
			histo[*pix++]++;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	raster_is_savebox = 1;
Toshihiro Shimizu 890ddd
	TRect saveBox = image->getSavebox();
Toshihiro Shimizu 890ddd
	if ((saveBox.getLx() > 0 && saveBox.getLx() < image->getRaster()->getLx()) ||
Toshihiro Shimizu 890ddd
		(saveBox.getLy() > 0 && saveBox.getLy() < image->getRaster()->getLy()))
Toshihiro Shimizu 890ddd
		raster_is_savebox = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (raster_is_savebox) {
Toshihiro Shimizu 890ddd
		true_lx = saveBox.getLx() ? saveBox.getLx() : image->getRaster()->getLx();
Toshihiro Shimizu 890ddd
		true_ly = saveBox.getLy() ? saveBox.getLy() : image->getRaster()->getLy();
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		true_lx = image->getRaster()->getLx();
Toshihiro Shimizu 890ddd
		true_ly = image->getRaster()->getLy();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	histo[255] += true_lx * true_ly - lx * ly;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	build_cum(histo, cum);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void build_gr_lut(int ref_cum[256], int cum[256], UCHAR lut[256])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/* crea una lut tale che l'immagine con la distribuzione cumulativa cum
Toshihiro Shimizu 890ddd
   venga mappata in un'immagine con la cumulativa di riferimento ref_cum
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
	int i, j, n;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	j = 0;
Toshihiro Shimizu 890ddd
	for (i = 0; i < 256; i++) {
Toshihiro Shimizu 890ddd
		n = cum[i];
Toshihiro Shimizu 890ddd
		while (ref_cum[j] < n)
Toshihiro Shimizu 890ddd
			j++;
Toshihiro Shimizu 890ddd
		lut[i] = j;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
struct edge_config {
Toshihiro Shimizu 890ddd
	char *str;
Toshihiro Shimizu 890ddd
	int val;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static struct edge_config Edge_base[] =
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		{"     "
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "     ",
Toshihiro Shimizu 890ddd
		 0},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X    "
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "     ",
Toshihiro Shimizu 890ddd
		 0},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"  X  "
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "     ",
Toshihiro Shimizu 890ddd
		 0},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "     ",
Toshihiro Shimizu 890ddd
		 5},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X   X"
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "     ",
Toshihiro Shimizu 890ddd
		 20},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X    "
Toshihiro Shimizu 890ddd
		 "  X X"
Toshihiro Shimizu 890ddd
		 "     ",
Toshihiro Shimizu 890ddd
		 24},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X    "
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "    X",
Toshihiro Shimizu 890ddd
		 28},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"  X  "
Toshihiro Shimizu 890ddd
		 "  X X"
Toshihiro Shimizu 890ddd
		 "     ",
Toshihiro Shimizu 890ddd
		 7},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"  X  "
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "  X  ",
Toshihiro Shimizu 890ddd
		 20},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "     ",
Toshihiro Shimizu 890ddd
		 10},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "  X X"
Toshihiro Shimizu 890ddd
		 "     ",
Toshihiro Shimizu 890ddd
		 12},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "    X",
Toshihiro Shimizu 890ddd
		 26},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "  X  ",
Toshihiro Shimizu 890ddd
		 22},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "X    ",
Toshihiro Shimizu 890ddd
		 22},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "X X  "
Toshihiro Shimizu 890ddd
		 "     ",
Toshihiro Shimizu 890ddd
		 5},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X   X"
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "    X",
Toshihiro Shimizu 890ddd
		 34},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X   X"
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "  X  ",
Toshihiro Shimizu 890ddd
		 34},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X    "
Toshihiro Shimizu 890ddd
		 "  X X"
Toshihiro Shimizu 890ddd
		 "  X  ",
Toshihiro Shimizu 890ddd
		 24},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"  X  "
Toshihiro Shimizu 890ddd
		 "  X X"
Toshihiro Shimizu 890ddd
		 "  X  ",
Toshihiro Shimizu 890ddd
		 10},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "  X X"
Toshihiro Shimizu 890ddd
		 "     ",
Toshihiro Shimizu 890ddd
		 12},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "    X",
Toshihiro Shimizu 890ddd
		 24},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "  X  ",
Toshihiro Shimizu 890ddd
		 24},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "  X X"
Toshihiro Shimizu 890ddd
		 "    X",
Toshihiro Shimizu 890ddd
		 14},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "  X X"
Toshihiro Shimizu 890ddd
		 "  X  ",
Toshihiro Shimizu 890ddd
		 12},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "  X X"
Toshihiro Shimizu 890ddd
		 "X    ",
Toshihiro Shimizu 890ddd
		 22},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "     ",
Toshihiro Shimizu 890ddd
		 10},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "  X X",
Toshihiro Shimizu 890ddd
		 24},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "X   X",
Toshihiro Shimizu 890ddd
		 32},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "X X  "
Toshihiro Shimizu 890ddd
		 "    X",
Toshihiro Shimizu 890ddd
		 24},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "X X  ",
Toshihiro Shimizu 890ddd
		 20},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X   X"
Toshihiro Shimizu 890ddd
		 "  X  "
Toshihiro Shimizu 890ddd
		 "X   X",
Toshihiro Shimizu 890ddd
		 40},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"  X  "
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "  X  ",
Toshihiro Shimizu 890ddd
		 0},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "     ",
Toshihiro Shimizu 890ddd
		 10},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "X X  "
Toshihiro Shimizu 890ddd
		 "    X",
Toshihiro Shimizu 890ddd
		 22},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "    X",
Toshihiro Shimizu 890ddd
		 12},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X   X"
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "    X",
Toshihiro Shimizu 890ddd
		 22},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"  X X"
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "    X",
Toshihiro Shimizu 890ddd
		 12},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "  X X"
Toshihiro Shimizu 890ddd
		 "    X",
Toshihiro Shimizu 890ddd
		 14},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X  "
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "  X  ",
Toshihiro Shimizu 890ddd
		 0},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X   X"
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "  X  ",
Toshihiro Shimizu 890ddd
		 10},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X   X"
Toshihiro Shimizu 890ddd
		 "X X  "
Toshihiro Shimizu 890ddd
		 "  X X",
Toshihiro Shimizu 890ddd
		 20},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X   X"
Toshihiro Shimizu 890ddd
		 "X X  "
Toshihiro Shimizu 890ddd
		 "X   X",
Toshihiro Shimizu 890ddd
		 30},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "    X",
Toshihiro Shimizu 890ddd
		 12},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "  X  ",
Toshihiro Shimizu 890ddd
		 0},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "X X  "
Toshihiro Shimizu 890ddd
		 "  X X",
Toshihiro Shimizu 890ddd
		 10},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"  X X"
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "X X  ",
Toshihiro Shimizu 890ddd
		 0},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "X X  "
Toshihiro Shimizu 890ddd
		 "X   X",
Toshihiro Shimizu 890ddd
		 20},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X   X"
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "X   X",
Toshihiro Shimizu 890ddd
		 20},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "  X X",
Toshihiro Shimizu 890ddd
		 0},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "X   X",
Toshihiro Shimizu 890ddd
		 10},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{"X X X"
Toshihiro Shimizu 890ddd
		 "X X X"
Toshihiro Shimizu 890ddd
		 "X X X",
Toshihiro Shimizu 890ddd
		 0},
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{0, 0},
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int Edge_value[256];
Toshihiro Shimizu 890ddd
static int Edge_init_done = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int edge_int(char x[8]);
Toshihiro Shimizu 890ddd
static void edge_rotate(char x[8]);
Toshihiro Shimizu 890ddd
static void edge_mirror(char x[8]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static void edge_init(void)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int b;
Toshihiro Shimizu 890ddd
	char *str;
Toshihiro Shimizu 890ddd
	int val;
Toshihiro Shimizu 890ddd
	char x[8];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (b = 0; Edge_base[b].str; b++) {
Toshihiro Shimizu 890ddd
		str = Edge_base[b].str;
Toshihiro Shimizu 890ddd
		val = Edge_base[b].val;
Toshihiro Shimizu 890ddd
		x[0] = str[0];
Toshihiro Shimizu 890ddd
		x[1] = str[2];
Toshihiro Shimizu 890ddd
		x[2] = str[4];
Toshihiro Shimizu 890ddd
		x[3] = str[5];
Toshihiro Shimizu 890ddd
		x[4] = str[9];
Toshihiro Shimizu 890ddd
		x[5] = str[10];
Toshihiro Shimizu 890ddd
		x[6] = str[12];
Toshihiro Shimizu 890ddd
		x[7] = str[14];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		Edge_value[edge_int(x)] = val;
Toshihiro Shimizu 890ddd
		edge_rotate(x);
Toshihiro Shimizu 890ddd
		Edge_value[edge_int(x)] = val;
Toshihiro Shimizu 890ddd
		edge_rotate(x);
Toshihiro Shimizu 890ddd
		Edge_value[edge_int(x)] = val;
Toshihiro Shimizu 890ddd
		edge_rotate(x);
Toshihiro Shimizu 890ddd
		Edge_value[edge_int(x)] = val;
Toshihiro Shimizu 890ddd
		edge_rotate(x);
Toshihiro Shimizu 890ddd
		edge_mirror(x);
Toshihiro Shimizu 890ddd
		Edge_value[edge_int(x)] = val;
Toshihiro Shimizu 890ddd
		edge_rotate(x);
Toshihiro Shimizu 890ddd
		Edge_value[edge_int(x)] = val;
Toshihiro Shimizu 890ddd
		edge_rotate(x);
Toshihiro Shimizu 890ddd
		Edge_value[edge_int(x)] = val;
Toshihiro Shimizu 890ddd
		edge_rotate(x);
Toshihiro Shimizu 890ddd
		Edge_value[edge_int(x)] = val;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	Edge_init_done = 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int edge_int(char x[8])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (x[0] != ' ') << 7 |
Toshihiro Shimizu 890ddd
		   (x[1] != ' ') << 6 |
Toshihiro Shimizu 890ddd
		   (x[2] != ' ') << 5 |
Toshihiro Shimizu 890ddd
		   (x[3] != ' ') << 4 |
Toshihiro Shimizu 890ddd
		   (x[4] != ' ') << 3 |
Toshihiro Shimizu 890ddd
		   (x[5] != ' ') << 2 |
Toshihiro Shimizu 890ddd
		   (x[6] != ' ') << 1 |
Toshihiro Shimizu 890ddd
		   (x[7] != ' ');
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static void edge_rotate(char x[8])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	char tmp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	tmp = x[0];
Toshihiro Shimizu 890ddd
	x[0] = x[2];
Toshihiro Shimizu 890ddd
	x[2] = x[7];
Toshihiro Shimizu 890ddd
	x[7] = x[5];
Toshihiro Shimizu 890ddd
	x[5] = tmp;
Toshihiro Shimizu 890ddd
	tmp = x[1];
Toshihiro Shimizu 890ddd
	x[1] = x[4];
Toshihiro Shimizu 890ddd
	x[4] = x[6];
Toshihiro Shimizu 890ddd
	x[6] = x[3];
Toshihiro Shimizu 890ddd
	x[3] = tmp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static void edge_mirror(char x[8])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	char tmp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	tmp = x[0];
Toshihiro Shimizu 890ddd
	x[0] = x[2];
Toshihiro Shimizu 890ddd
	x[2] = tmp;
Toshihiro Shimizu 890ddd
	tmp = x[3];
Toshihiro Shimizu 890ddd
	x[3] = x[4];
Toshihiro Shimizu 890ddd
	x[4] = tmp;
Toshihiro Shimizu 890ddd
	tmp = x[5];
Toshihiro Shimizu 890ddd
	x[5] = x[7];
Toshihiro Shimizu 890ddd
	x[7] = tmp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void histo_l_algo(const TRasterImageP &image, int reference)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int lx, ly, wrap;
Toshihiro Shimizu 890ddd
	int x, y, grey, ref_grey, m, d, dd;
Toshihiro Shimizu 890ddd
	UCHAR *buffer, *pix, *north, *south;
Toshihiro Shimizu 890ddd
	int g_histo[256];
Toshihiro Shimizu 890ddd
	int d_histo[256][256];
Toshihiro Shimizu 890ddd
	int cum[256];
Toshihiro Shimizu 890ddd
	long n;
Toshihiro Shimizu 890ddd
	BIG s;
Toshihiro Shimizu 890ddd
	float mean_d[256];
Toshihiro Shimizu 890ddd
	int max_d_grey;
Toshihiro Shimizu 890ddd
	float max_d;
Toshihiro Shimizu 890ddd
	int conf;
Toshihiro Shimizu 890ddd
	int edgelen;
Toshihiro Shimizu 890ddd
	float fac;
Toshihiro Shimizu 890ddd
	UCHAR lut[256];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!Edge_init_done)
Toshihiro Shimizu 890ddd
		edge_init();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	get_virtual_buffer(image, &lx, &ly, &wrap, &buffer);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (grey = 0; grey < 256; grey++) {
Toshihiro Shimizu 890ddd
		g_histo[grey] = 0;
Toshihiro Shimizu 890ddd
		for (d = 0; d < 256; d++)
Toshihiro Shimizu 890ddd
			d_histo[grey][d] = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* istogramma dei grigi e istogramma delle derivate */
Toshihiro Shimizu 890ddd
	for (y = 1; y < ly - 1; y++) {
Toshihiro Shimizu 890ddd
		pix = buffer + y * wrap + 1;
Toshihiro Shimizu 890ddd
		north = pix + wrap;
Toshihiro Shimizu 890ddd
		south = pix - wrap;
Toshihiro Shimizu 890ddd
		for (x = 1; x < lx - 1; x++, pix++, north++, south++) {
Toshihiro Shimizu 890ddd
			m = 2 * ((int)north[0] + (int)pix[-1] + 2 * (int)pix[0] + (int)pix[1] + (int)south[0]) + (int)north[-1] + (int)north[1] + (int)south[-1] + (int)south[1];
Toshihiro Shimizu 890ddd
			m = (m + 8) >> 4;
Toshihiro Shimizu 890ddd
			d = (int)north[-1] + (int)north[0] * 2 + (int)north[1] - (int)south[-1] - (int)south[0] * 2 - (int)south[1];
Toshihiro Shimizu 890ddd
			TO_ABS(d)
Toshihiro Shimizu 890ddd
			dd = (int)north[-1] + (int)pix[-1] * 2 + (int)south[-1] - (int)north[1] - (int)pix[1] * 2 - (int)south[1];
Toshihiro Shimizu 890ddd
			TO_ABS(dd)
Toshihiro Shimizu 890ddd
			if (dd > d)
Toshihiro Shimizu 890ddd
				d = dd;
Toshihiro Shimizu 890ddd
			dd = (int)pix[-1] + (int)north[-1] * 2 + (int)north[0] - (int)pix[1] - (int)south[1] * 2 - (int)south[0];
Toshihiro Shimizu 890ddd
			TO_ABS(dd)
Toshihiro Shimizu 890ddd
			if (dd > d)
Toshihiro Shimizu 890ddd
				d = dd;
Toshihiro Shimizu 890ddd
			dd = (int)pix[1] + (int)north[1] * 2 + (int)north[0] - (int)pix[-1] - (int)south[-1] * 2 - (int)south[0];
Toshihiro Shimizu 890ddd
			TO_ABS(dd)
Toshihiro Shimizu 890ddd
			if (dd > d)
Toshihiro Shimizu 890ddd
				d = dd;
Toshihiro Shimizu 890ddd
			d = (d + 2) >> 2;
Toshihiro Shimizu 890ddd
			d_histo[m][d]++;
Toshihiro Shimizu 890ddd
			g_histo[*pix]++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	build_cum(g_histo, cum);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* costruzione pendenze medie */
Toshihiro Shimizu 890ddd
	for (grey = 0; grey < 256; grey++) {
Toshihiro Shimizu 890ddd
		n = 0;
Toshihiro Shimizu 890ddd
		CLEAR_BIG(s);
Toshihiro Shimizu 890ddd
		for (d = 0; d < 256; d++) {
Toshihiro Shimizu 890ddd
			ADD_BIG(s, d_histo[grey][d] * d);
Toshihiro Shimizu 890ddd
			n += d_histo[grey][d];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (n)
Toshihiro Shimizu 890ddd
			mean_d[grey] = (float)(BIG_TO_DOUBLE(s) / n);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			mean_d[grey] = 0.0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	smooth_func256(mean_d, 5);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* determinazione grigio di massima pendenza */
Toshihiro Shimizu 890ddd
	max_d_grey = 0;
Toshihiro Shimizu 890ddd
	max_d = 0.0;
Toshihiro Shimizu 890ddd
	for (grey = 0; grey < 256; grey++) {
Toshihiro Shimizu 890ddd
		if (max_d < mean_d[grey]) {
Toshihiro Shimizu 890ddd
			max_d = mean_d[grey];
Toshihiro Shimizu 890ddd
			max_d_grey = grey;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* stima della lunghezza dei bordi */
Toshihiro Shimizu 890ddd
	edgelen = 0;
Toshihiro Shimizu 890ddd
	for (y = 1; y < ly - 1; y++) {
Toshihiro Shimizu 890ddd
		pix = buffer + y * wrap + 1;
Toshihiro Shimizu 890ddd
		north = pix + wrap;
Toshihiro Shimizu 890ddd
		south = pix - wrap;
Toshihiro Shimizu 890ddd
		conf = -1;
Toshihiro Shimizu 890ddd
		for (x = 1; x < lx - 1; x++, pix++, north++, south++) {
Toshihiro Shimizu 890ddd
			if (pix[0] <= max_d_grey) {
Toshihiro Shimizu 890ddd
				if (conf >= 0) {
Toshihiro Shimizu 890ddd
					conf = ((conf << 1) & ((1 << 7) | (1 << 6) | (1 << 2) | (1 << 1))) |
Toshihiro Shimizu 890ddd
						   ((north[1] <= max_d_grey) << 5) |
Toshihiro Shimizu 890ddd
						   ((1) << 4) |
Toshihiro Shimizu 890ddd
						   ((pix[1] <= max_d_grey) << 3) |
Toshihiro Shimizu 890ddd
						   ((south[1] <= max_d_grey));
Toshihiro Shimizu 890ddd
				} else {
Toshihiro Shimizu 890ddd
					conf = ((north[-1] <= max_d_grey) << 7) |
Toshihiro Shimizu 890ddd
						   ((north[0] <= max_d_grey) << 6) |
Toshihiro Shimizu 890ddd
						   ((north[1] <= max_d_grey) << 5) |
Toshihiro Shimizu 890ddd
						   ((pix[-1] <= max_d_grey) << 4) |
Toshihiro Shimizu 890ddd
						   ((pix[1] <= max_d_grey) << 3) |
Toshihiro Shimizu 890ddd
						   ((south[-1] <= max_d_grey) << 2) |
Toshihiro Shimizu 890ddd
						   ((south[0] <= max_d_grey) << 1) |
Toshihiro Shimizu 890ddd
						   ((south[1] <= max_d_grey));
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				edgelen += Edge_value[conf];
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				conf = -1;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (reference) {
Toshihiro Shimizu 890ddd
		for (grey = 0; grey < 256; grey++)
Toshihiro Shimizu 890ddd
			Ref_cum[grey] = cum[grey];
Toshihiro Shimizu 890ddd
		Ref_edgelen = edgelen;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* normalizza la cumulativa per il numero di linee */
Toshihiro Shimizu 890ddd
	fac = (float)Ref_edgelen / edgelen;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/***
Toshihiro Shimizu 890ddd
printf ("fac: %f\n", fac);
Toshihiro Shimizu 890ddd
printf ("cum prima:\n");
Toshihiro Shimizu 890ddd
for (grey = 0; grey < 256; grey++)
Toshihiro Shimizu 890ddd
  printf ("%9d\n", cum[grey]);
Toshihiro Shimizu 890ddd
***/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (grey = 0; grey < 255; grey++) {
Toshihiro Shimizu 890ddd
		cum[grey] = (int)(cum[grey] * fac);
Toshihiro Shimizu 890ddd
		notMoreThan(cum[255], cum[grey]);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/***
Toshihiro Shimizu 890ddd
printf ("cum dopo:\n");
Toshihiro Shimizu 890ddd
for (grey = 0; grey < 256; grey++)
Toshihiro Shimizu 890ddd
  printf ("%9d\n", cum[grey]);
Toshihiro Shimizu 890ddd
printf ("Ref_cum:\n");
Toshihiro Shimizu 890ddd
for (grey = 0; grey < 256; grey++)
Toshihiro Shimizu 890ddd
  printf ("%9d\n", Ref_cum[grey]);
Toshihiro Shimizu 890ddd
***/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* equalizza l'istogramma */
Toshihiro Shimizu 890ddd
	ref_grey = 0;
Toshihiro Shimizu 890ddd
	for (grey = 0; grey < 255; grey++) {
Toshihiro Shimizu 890ddd
		while (Ref_cum[ref_grey] < cum[grey])
Toshihiro Shimizu 890ddd
			ref_grey++;
Toshihiro Shimizu 890ddd
		lut[grey] = ref_grey;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	lut[255] = 255;
Toshihiro Shimizu 890ddd
	/* DAFARE
Toshihiro Shimizu 890ddd
if (Wl_flag)
Toshihiro Shimizu 890ddd
  write_lut_image (lut);
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
	apply_lut(image, lut);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define MAX_N_CHAINS 100
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define MAX_WIDTH 100
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define MIN_COUNT 100
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define MAX_HGREY (244 >> 1)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
#ifdef DAFARE
Toshihiro Shimizu 890ddd
static int build_th_histo(const TRasterImageP &image, int histo[256 >> 1][MAX_WIDTH + 1])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int lx, ly, wrap, x, y, hgrey, width;
Toshihiro Shimizu 890ddd
	UCHAR *buffer, *cur, *x_prev, *y_prev, *x_next, *y_next;
Toshihiro Shimizu 890ddd
	UCHAR cur_hgrey, x_prev_hgrey, y_prev_hgrey, x_next_hgrey, y_next_hgrey;
Toshihiro Shimizu 890ddd
	int x_grad, y_grad;
Toshihiro Shimizu 890ddd
	int hgrey_histo[256 >> 1];
Toshihiro Shimizu 890ddd
	int x_start[256 >> 1];
Toshihiro Shimizu 890ddd
	static int *y_start[256 >> 1];
Toshihiro Shimizu 890ddd
	static int y_start_alloc = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	get_virtual_buffer(image, &lx, &ly, &wrap, &buffer);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (y_start_alloc < lx) {
Toshihiro Shimizu 890ddd
		for (hgrey = 0; hgrey<256>> 1; hgrey++) {
Toshihiro Shimizu 890ddd
			delete[] y_start[hgrey];
Toshihiro Shimizu 890ddd
			y_start[hgrey] = new int[lx];
Toshihiro Shimizu 890ddd
			//    TREALLOC (y_start[hgrey], lx);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		y_start_alloc = lx;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (hgrey = 0; hgrey<256>> 1; hgrey++) {
Toshihiro Shimizu 890ddd
		for (x = 0; x < lx; x++)
Toshihiro Shimizu 890ddd
			y_start[hgrey][x] = -MAX_WIDTH - 1;
Toshihiro Shimizu 890ddd
		for (width = 0; width <= MAX_WIDTH; width++)
Toshihiro Shimizu 890ddd
			histo[hgrey][width] = 0;
Toshihiro Shimizu 890ddd
		hgrey_histo[hgrey] = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (y = 1; y < ly - 1; y++) {
Toshihiro Shimizu 890ddd
		for (hgrey = 0; hgrey<256>> 1; hgrey++)
Toshihiro Shimizu 890ddd
			x_start[hgrey] = -MAX_WIDTH - 1;
Toshihiro Shimizu 890ddd
		cur = buffer + y * wrap + 1;
Toshihiro Shimizu 890ddd
		x_prev = cur - 1;
Toshihiro Shimizu 890ddd
		y_prev = cur - wrap;
Toshihiro Shimizu 890ddd
		x_next = cur + 1;
Toshihiro Shimizu 890ddd
		y_next = cur + wrap;
Toshihiro Shimizu 890ddd
		for (x = 1; x < lx - 1; x++, cur++, x_prev++, y_prev++, x_next++, y_next++) {
Toshihiro Shimizu 890ddd
			cur_hgrey = *cur >> 1;
Toshihiro Shimizu 890ddd
			x_prev_hgrey = *x_prev >> 1;
Toshihiro Shimizu 890ddd
			x_next_hgrey = *x_next >> 1;
Toshihiro Shimizu 890ddd
			y_prev_hgrey = *y_prev >> 1;
Toshihiro Shimizu 890ddd
			y_next_hgrey = *y_next >> 1;
Toshihiro Shimizu 890ddd
			hgrey_histo[cur_hgrey]++;
Toshihiro Shimizu 890ddd
			x_grad = (int)x_next_hgrey - (int)x_prev_hgrey;
Toshihiro Shimizu 890ddd
			y_grad = (int)y_next_hgrey - (int)y_prev_hgrey;
Toshihiro Shimizu 890ddd
			if (cur_hgrey < x_prev_hgrey) {
Toshihiro Shimizu 890ddd
				if (x_grad < 0 && x_grad < y_grad && x_grad < -y_grad)
Toshihiro Shimizu 890ddd
					for (hgrey = cur_hgrey; hgrey < x_prev_hgrey; hgrey++)
Toshihiro Shimizu 890ddd
						x_start[hgrey] = x;
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					for (hgrey = cur_hgrey; hgrey < x_prev_hgrey; hgrey++)
Toshihiro Shimizu 890ddd
						x_start[hgrey] = -MAX_WIDTH - 1;
Toshihiro Shimizu 890ddd
			} else if (cur_hgrey > x_prev_hgrey) {
Toshihiro Shimizu 890ddd
				if (x_grad > 0 && x_grad > y_grad && x_grad > -y_grad)
Toshihiro Shimizu 890ddd
					for (hgrey = x_prev_hgrey; hgrey < cur_hgrey; hgrey++) {
Toshihiro Shimizu 890ddd
						width = x - x_start[hgrey];
Toshihiro Shimizu 890ddd
						if (width <= MAX_WIDTH)
Toshihiro Shimizu 890ddd
							histo[hgrey][width]++;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (cur_hgrey < y_prev_hgrey) {
Toshihiro Shimizu 890ddd
				if (y_grad < 0 && y_grad < x_grad && y_grad < -x_grad)
Toshihiro Shimizu 890ddd
					for (hgrey = cur_hgrey; hgrey < y_prev_hgrey; hgrey++)
Toshihiro Shimizu 890ddd
						y_start[hgrey][x] = y;
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					for (hgrey = cur_hgrey; hgrey < y_prev_hgrey; hgrey++)
Toshihiro Shimizu 890ddd
						y_start[hgrey][x] = -MAX_WIDTH - 1;
Toshihiro Shimizu 890ddd
			} else if (cur_hgrey > y_prev_hgrey) {
Toshihiro Shimizu 890ddd
				if (y_grad > 0 && y_grad > x_grad && y_grad > -x_grad)
Toshihiro Shimizu 890ddd
					for (hgrey = y_prev_hgrey; hgrey < cur_hgrey; hgrey++) {
Toshihiro Shimizu 890ddd
						width = y - y_start[hgrey][x];
Toshihiro Shimizu 890ddd
						if (width <= MAX_WIDTH)
Toshihiro Shimizu 890ddd
							histo[hgrey][width]++;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
#ifdef DAFARE
Toshihiro Shimizu 890ddd
//SEMBRA NON ESSERE UTILIZZATO!
Toshihiro Shimizu 890ddd
int eval_image_th(const TRasterImageP &image, int *threshold, float *linewidth)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#define MIN_COUNT 100
Toshihiro Shimizu 890ddd
#define MAX_HGREY (244 >> 1)
Toshihiro Shimizu 890ddd
	int histo[256 >> 1][MAX_WIDTH + 1];
Toshihiro Shimizu 890ddd
	char is_max[256 >> 1][MAX_WIDTH + 1];
Toshihiro Shimizu 890ddd
	int chain_map[256 >> 1][MAX_WIDTH + 1];
Toshihiro Shimizu 890ddd
	int chain_n[MAX_N_CHAINS];
Toshihiro Shimizu 890ddd
	int n_chains;
Toshihiro Shimizu 890ddd
	int peak[256 >> 1];
Toshihiro Shimizu 890ddd
	float mean_width[256 >> 1];
Toshihiro Shimizu 890ddd
	int min_hgrey, max_hgrey, min_chain_hgrey, max_chain_hgrey;
Toshihiro Shimizu 890ddd
	int hgrey, width, chain, c, last;
Toshihiro Shimizu 890ddd
	long n, nx;
Toshihiro Shimizu 890ddd
	float delta, min_delta;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterGR8P ras8(image->getRaster());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!ras8) {
Toshihiro Shimizu 890ddd
		TERROR("image is not RAS_GR8");
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (!(*threshold != 0 && *linewidth == 0 ||
Toshihiro Shimizu 890ddd
		  *threshold == 0 && *linewidth != 0))
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	build_th_histo(image, histo);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (*threshold != 0) {
Toshihiro Shimizu 890ddd
		min_hgrey = *threshold >> 1;
Toshihiro Shimizu 890ddd
		max_hgrey = *threshold >> 1;
Toshihiro Shimizu 890ddd
		notLessThan(0, min_hgrey);
Toshihiro Shimizu 890ddd
		notMoreThan<int>(MAX_HGREY, max_hgrey);</int>
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		for (hgrey = 0; hgrey<256>> 1; hgrey++)
Toshihiro Shimizu 890ddd
			for (width = 1; width < MAX_WIDTH; width++)
Toshihiro Shimizu 890ddd
				if (histo[hgrey][width] >= MIN_COUNT)
Toshihiro Shimizu 890ddd
					goto endfor;
Toshihiro Shimizu 890ddd
	endfor:
Toshihiro Shimizu 890ddd
		if (hgrey == 256 >> 1)
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		min_hgrey = hgrey;
Toshihiro Shimizu 890ddd
		max_hgrey = MAX_HGREY;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (min_hgrey > max_hgrey)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* costruisci la matrice dai massimi */
Toshihiro Shimizu 890ddd
	for (hgrey = min_hgrey; hgrey <= max_hgrey; hgrey++) {
Toshihiro Shimizu 890ddd
		is_max[hgrey][0] = 0;
Toshihiro Shimizu 890ddd
		is_max[hgrey][MAX_WIDTH] = 0;
Toshihiro Shimizu 890ddd
		for (width = 1; width < MAX_WIDTH; width++) {
Toshihiro Shimizu 890ddd
			is_max[hgrey][width] = histo[hgrey][width] > histo[hgrey][width - 1] &&
Toshihiro Shimizu 890ddd
								   histo[hgrey][width] > histo[hgrey][width + 1] &&
Toshihiro Shimizu 890ddd
								   histo[hgrey][width] >= MIN_COUNT;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	/* costruisci la matrice delle dorsali */
Toshihiro Shimizu 890ddd
	n_chains = 0;
Toshihiro Shimizu 890ddd
	for (width = 0; width <= MAX_WIDTH; width++) {
Toshihiro Shimizu 890ddd
		if (is_max[min_hgrey][width] && n_chains < MAX_N_CHAINS) {
Toshihiro Shimizu 890ddd
			chain_map[min_hgrey][width] = n_chains++;
Toshihiro Shimizu 890ddd
			chain_n[chain_map[min_hgrey][width]] = histo[min_hgrey][width];
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			chain_map[min_hgrey][width] = -1;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (hgrey = min_hgrey + 1; hgrey <= max_hgrey; hgrey++) {
Toshihiro Shimizu 890ddd
		for (width = 0; width <= MAX_WIDTH; width++) {
Toshihiro Shimizu 890ddd
			if (is_max[hgrey][width]) {
Toshihiro Shimizu 890ddd
				if (chain_map[hgrey - 1][width] >= 0) {
Toshihiro Shimizu 890ddd
					chain_map[hgrey][width] = chain_map[hgrey - 1][width];
Toshihiro Shimizu 890ddd
					chain_n[chain_map[hgrey][width]] += histo[hgrey][width];
Toshihiro Shimizu 890ddd
				} else if (chain_map[hgrey - 1][width - 1] >= 0 &&
Toshihiro Shimizu 890ddd
						   chain_map[hgrey - 1][width - 1] != chain_map[hgrey][width - 2]) {
Toshihiro Shimizu 890ddd
					chain_map[hgrey][width] = chain_map[hgrey - 1][width - 1];
Toshihiro Shimizu 890ddd
					chain_n[chain_map[hgrey][width]] += histo[hgrey][width];
Toshihiro Shimizu 890ddd
				} else if (chain_map[hgrey - 1][width + 1] >= 0) {
Toshihiro Shimizu 890ddd
					chain_map[hgrey][width] = chain_map[hgrey - 1][width + 1];
Toshihiro Shimizu 890ddd
					chain_n[chain_map[hgrey][width]] += histo[hgrey][width];
Toshihiro Shimizu 890ddd
				} else if (n_chains < MAX_N_CHAINS) {
Toshihiro Shimizu 890ddd
					chain_map[hgrey][width] = n_chains++;
Toshihiro Shimizu 890ddd
					chain_n[chain_map[hgrey][width]] = histo[hgrey][width];
Toshihiro Shimizu 890ddd
				} else {
Toshihiro Shimizu 890ddd
					chain_map[hgrey][width] = -1;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				chain_map[hgrey][width] = -1;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (!n_chains)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* scegli la dorsale principale */
Toshihiro Shimizu 890ddd
	chain = 0;
Toshihiro Shimizu 890ddd
	for (c = 1; c < n_chains; c++)
Toshihiro Shimizu 890ddd
		if (chain_n[c] > chain_n[chain])
Toshihiro Shimizu 890ddd
			chain = c;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* costruisci il vettore dei massimi */
Toshihiro Shimizu 890ddd
	for (hgrey = min_hgrey; hgrey <= max_hgrey; hgrey++)
Toshihiro Shimizu 890ddd
		for (width = 1; width < MAX_WIDTH; width++)
Toshihiro Shimizu 890ddd
			if (chain_map[hgrey][width] == chain)
Toshihiro Shimizu 890ddd
				goto chain_start_found;
Toshihiro Shimizu 890ddd
chain_start_found:
Toshihiro Shimizu 890ddd
	min_chain_hgrey = hgrey;
Toshihiro Shimizu 890ddd
	peak[min_chain_hgrey] = width;
Toshihiro Shimizu 890ddd
	for (hgrey = min_chain_hgrey + 1; hgrey <= max_hgrey; hgrey++) {
Toshihiro Shimizu 890ddd
		if (chain_map[hgrey][width] == chain) {
Toshihiro Shimizu 890ddd
		} else if (chain_map[hgrey][width + 1] == chain)
Toshihiro Shimizu 890ddd
			width++;
Toshihiro Shimizu 890ddd
		else if (chain_map[hgrey][width - 1] == chain)
Toshihiro Shimizu 890ddd
			width--;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		peak[hgrey] = width;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	max_chain_hgrey = hgrey - 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* costruisci il vettore delle medie */
Toshihiro Shimizu 890ddd
	for (hgrey = min_chain_hgrey; hgrey <= max_chain_hgrey; hgrey++) {
Toshihiro Shimizu 890ddd
		width = peak[hgrey];
Toshihiro Shimizu 890ddd
		n = histo[hgrey][width];
Toshihiro Shimizu 890ddd
		nx = n * width;
Toshihiro Shimizu 890ddd
		last = n;
Toshihiro Shimizu 890ddd
		for (width = peak[hgrey] - 1; width > 0; width--) {
Toshihiro Shimizu 890ddd
			if (histo[hgrey][width] > last)
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			n += histo[hgrey][width];
Toshihiro Shimizu 890ddd
			nx += histo[hgrey][width] * width;
Toshihiro Shimizu 890ddd
			last = histo[hgrey][width];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		last = histo[hgrey][peak[hgrey]];
Toshihiro Shimizu 890ddd
		for (width = peak[hgrey] + 1; width <= MAX_WIDTH; width++) {
Toshihiro Shimizu 890ddd
			if (width > peak[hgrey] * 5)
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			if (histo[hgrey][width] > last)
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			n += histo[hgrey][width];
Toshihiro Shimizu 890ddd
			nx += histo[hgrey][width] * width;
Toshihiro Shimizu 890ddd
			last = histo[hgrey][width];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		mean_width[hgrey] = (float)nx / (float)n;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (*threshold != 0) {
Toshihiro Shimizu 890ddd
		*linewidth = mean_width[*threshold >> 1];
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		min_delta = 123456789.0;
Toshihiro Shimizu 890ddd
		for (hgrey = min_chain_hgrey; hgrey <= max_chain_hgrey; hgrey++) {
Toshihiro Shimizu 890ddd
			delta = mean_width[hgrey] - *linewidth;
Toshihiro Shimizu 890ddd
			if (delta < 0)
Toshihiro Shimizu 890ddd
				delta = -delta;
Toshihiro Shimizu 890ddd
			if (delta < min_delta) {
Toshihiro Shimizu 890ddd
				min_delta = delta;
Toshihiro Shimizu 890ddd
				*threshold = hgrey << 1;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
#ifdef DAFARE
Toshihiro Shimizu 890ddd
//capire che cosa fa !?? chi la usa ?
Toshihiro Shimizu 890ddd
//sembra prendere una gr8, modificare il buffer e cambiare il tipo in BW
Toshihiro Shimizu 890ddd
void thresh_image(const TRasterImageP &image, int threshold, int oversample_factor)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR *buffer, *cur, *xnext, *ynext, *xynext;
Toshihiro Shimizu 890ddd
	UCHAR *bigline, *out;
Toshihiro Shimizu 890ddd
	UCHAR tmp;
Toshihiro Shimizu 890ddd
	int lx, ly, wrap, outwrap_bytes, outwrap_pix;
Toshihiro Shimizu 890ddd
	int x, y, bit;
Toshihiro Shimizu 890ddd
	UINT thresh, thresh_2, thresh_4;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterGR8P ras8(image->getRaster());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(ras8);
Toshihiro Shimizu 890ddd
	thresh = threshold;
Toshihiro Shimizu 890ddd
	thresh_2 = thresh * 2;
Toshihiro Shimizu 890ddd
	thresh_4 = thresh * 4;
Toshihiro Shimizu 890ddd
	buffer = ras8->getRawData();
Toshihiro Shimizu 890ddd
	lx = ras8->getLx();
Toshihiro Shimizu 890ddd
	ly = ras8->getLy();
Toshihiro Shimizu 890ddd
	wrap = ras8->getWrap();
Toshihiro Shimizu 890ddd
	outwrap_pix = lx * oversample_factor;
Toshihiro Shimizu 890ddd
	outwrap_bytes = (outwrap_pix + 7) / 8;
Toshihiro Shimizu 890ddd
	TMALLOC(bigline, outwrap_bytes * oversample_factor)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (oversample_factor) {
Toshihiro Shimizu 890ddd
		CASE 1 : for (y = 0; y < ly; y++)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			cur = buffer + y * wrap;
Toshihiro Shimizu 890ddd
			out = y == 0 ? bigline : buffer + y * outwrap_bytes;
Toshihiro Shimizu 890ddd
			tmp = 0;
Toshihiro Shimizu 890ddd
			bit = 7;
Toshihiro Shimizu 890ddd
			for (x = 0; x < lx; x++, cur++) {
Toshihiro Shimizu 890ddd
				if (*cur <= thresh)
Toshihiro Shimizu 890ddd
					tmp |= 1 << bit;
Toshihiro Shimizu 890ddd
				bit--;
Toshihiro Shimizu 890ddd
				if (bit < 0) {
Toshihiro Shimizu 890ddd
					*out++ = tmp;
Toshihiro Shimizu 890ddd
					tmp = 0;
Toshihiro Shimizu 890ddd
					bit = 7;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (bit != 7)
Toshihiro Shimizu 890ddd
				*out++ = tmp;
Toshihiro Shimizu 890ddd
			if (y == 0)
Toshihiro Shimizu 890ddd
				memcpy(buffer, bigline, outwrap_bytes);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		CASE 2 : for (y = 0; y < ly; y++)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			cur = buffer + y * wrap;
Toshihiro Shimizu 890ddd
			xnext = cur + 1;
Toshihiro Shimizu 890ddd
			out = y == 0 ? bigline : buffer + y * 2 * outwrap_bytes;
Toshihiro Shimizu 890ddd
			tmp = 0;
Toshihiro Shimizu 890ddd
			bit = 7;
Toshihiro Shimizu 890ddd
			for (x = 0; x < lx; x++, cur++, xnext += x < lx - 1) {
Toshihiro Shimizu 890ddd
				if (*cur <= thresh)
Toshihiro Shimizu 890ddd
					tmp |= 1 << bit;
Toshihiro Shimizu 890ddd
				bit--;
Toshihiro Shimizu 890ddd
				if (*cur + *xnext <= thresh_2)
Toshihiro Shimizu 890ddd
					tmp |= 1 << bit;
Toshihiro Shimizu 890ddd
				bit--;
Toshihiro Shimizu 890ddd
				if (bit < 0) {
Toshihiro Shimizu 890ddd
					*out++ = tmp;
Toshihiro Shimizu 890ddd
					tmp = 0;
Toshihiro Shimizu 890ddd
					bit = 7;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (bit != 7)
Toshihiro Shimizu 890ddd
				*out++ = tmp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			cur = buffer + y * wrap;
Toshihiro Shimizu 890ddd
			xnext = cur + 1;
Toshihiro Shimizu 890ddd
			ynext = y == ly - 1 ? cur : cur + wrap;
Toshihiro Shimizu 890ddd
			xynext = ynext + 1;
Toshihiro Shimizu 890ddd
			out = y == 0 ? bigline + outwrap_bytes : buffer + (y * 2 + 1) * outwrap_bytes;
Toshihiro Shimizu 890ddd
			tmp = 0;
Toshihiro Shimizu 890ddd
			bit = 7;
Toshihiro Shimizu 890ddd
			for (x = 0; x < lx; x++, cur++, xnext += x < lx - 1, ynext++, xynext += x < lx - 1) {
Toshihiro Shimizu 890ddd
				if (*cur + *ynext <= thresh_2)
Toshihiro Shimizu 890ddd
					tmp |= 1 << bit;
Toshihiro Shimizu 890ddd
				bit--;
Toshihiro Shimizu 890ddd
				if (*cur + *xnext + *ynext + *xynext <= thresh_4)
Toshihiro Shimizu 890ddd
					tmp |= 1 << bit;
Toshihiro Shimizu 890ddd
				bit--;
Toshihiro Shimizu 890ddd
				if (bit < 0) {
Toshihiro Shimizu 890ddd
					*out++ = tmp;
Toshihiro Shimizu 890ddd
					tmp = 0;
Toshihiro Shimizu 890ddd
					bit = 7;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (bit != 7)
Toshihiro Shimizu 890ddd
				*out++ = tmp;
Toshihiro Shimizu 890ddd
			if (y == 0)
Toshihiro Shimizu 890ddd
				memcpy(buffer, bigline, outwrap_bytes * 2);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		assert(0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	image->ras.type = RAS_WB;
Toshihiro Shimizu 890ddd
	image->ras.lx = lx * oversample_factor;
Toshihiro Shimizu 890ddd
	image->ras.ly = ly * oversample_factor;
Toshihiro Shimizu 890ddd
	image->ras.wrap = outwrap_pix;
Toshihiro Shimizu 890ddd
	image->x_dpi *= oversample_factor;
Toshihiro Shimizu 890ddd
	image->y_dpi *= oversample_factor;
Toshihiro Shimizu 890ddd
	image->sb_x0 *= oversample_factor;
Toshihiro Shimizu 890ddd
	image->sb_y0 *= oversample_factor;
Toshihiro Shimizu 890ddd
	image->sb_or_img_lx *= oversample_factor;
Toshihiro Shimizu 890ddd
	image->sb_or_img_ly *= oversample_factor;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFREE(bigline)
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void apply_lut(const TRasterImageP &image, UCHAR lut[256])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int x, y, lx, ly, wrap;
Toshihiro Shimizu 890ddd
	UCHAR *buffer, *pix;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterGR8P ras8(image->getRaster());
Toshihiro Shimizu 890ddd
	assert(ras8);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	lx = ras8->getLx();
Toshihiro Shimizu 890ddd
	ly = ras8->getLy();
Toshihiro Shimizu 890ddd
	wrap = ras8->getWrap();
Toshihiro Shimizu 890ddd
	ras8->lock();
Toshihiro Shimizu 890ddd
	buffer = (UCHAR *)ras8->getRawData();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (y = 0; y < ly; y++) {
Toshihiro Shimizu 890ddd
		pix = buffer + y * wrap;
Toshihiro Shimizu 890ddd
		for (x = 0; x < lx; x++, pix++)
Toshihiro Shimizu 890ddd
			*pix = lut[*pix];
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	ras8->unlock();
Toshihiro Shimizu 890ddd
}