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