Skip to content

Commit 9e8f267

Browse files
committed
0.20190917: Filter: add: quant
1 parent 25afb0e commit 9e8f267

File tree

8 files changed

+130
-31
lines changed

8 files changed

+130
-31
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CPP = g++
55
CFLAGS = -DUNIX -O2 -Wall -s
66
LIBS = -lfreeimage
77
VER = 0
8-
VERB = 20190824
8+
VERB = 20190917
99
PLIBF = lib$(PNAME).so.$(VER)
1010
PLIBFI = lib$(PNAME)freeimage.so.$(VER)
1111
PLIB = $(PLIBF) $(PLIBFI)

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ Main directions:
1313
3) HRIS - image scaling up.
1414
![Half Reverse Interpolation Scale](https://github.com/zvezdochiot/imthreshold/blob/master/images/hris_small.jpg)
1515

16-
2016 zvezdochiot.
17-
16+
2016-2019 zvezdochiot.
17+
1818
2006-2016 monday2000.
19-
19+
2020
Website: https://sourceforge.net/projects/imthreshold/
21-
21+
2222
GIT: https://github.com/ImageProcessing-ElectronicPublications/imthreshold
23-
23+
2424
BookScanLib.ru Website: http://djvu-soft.narod.ru/
25-
25+
2626
Email: zvezdochiot@users.sourceforge.net
27-
27+

doc/imthreshold/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ This program is free software;
4545
You can redistribute it and/or
4646
modify it under the terms of the Zlib License.
4747

48-
Copyright (C) 2016 zvezdochiot.
48+
Copyright (C) 2016-2019 zvezdochiot.

doc/imthreshold/CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
0.20190917
2+
3+
Filter: add: quant
4+
15
0.20190824
26

37
LibImthreshold: extern C

doc/imthreshold/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.20190824
1+
0.20190917

src/filter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void ImthresholdFilterUsage()
4747
printf(" 'none' (default)\n");
4848
printf(" 'peron'\n");
4949
printf(" 'posterize'\n");
50+
printf(" 'quant'\n");
5051
printf(" 'retinex'\n");
5152
printf(" 'rs'\n");
5253
printf(" 'selgauss'\n");
@@ -489,6 +490,20 @@ int main(int argc, char *argv[])
489490
dst_dib = FreeImage_Allocate(width, height, 24);
490491
ImthresholdSetData(dst_dib, p_im);
491492
IMTfree(p_im, height);
493+
} else if (strcmp(namefilter, "quant") == 0) {
494+
printf("Filter= %s\n", namefilter);
495+
double imsh = 0;
496+
IMTpixel** p_im = IMTalloc(height, width);
497+
498+
if (posterdiv < 1) {posterdiv = 1;}
499+
printf("Quant= %d\n", posterdiv);
500+
ImthresholdGetData(dib, p_im);
501+
FreeImage_Unload(dib);
502+
imsh = IMTFilterQuant(p_im, height, width, posterdiv);
503+
printf("Delta= %f\n", imsh);
504+
dst_dib = FreeImage_Allocate(width, height, 24);
505+
ImthresholdSetData(dst_dib, p_im);
506+
IMTfree(p_im, height);
492507
} else if (strcmp(namefilter, "retinex") == 0) {
493508
printf("Filter= %s\n", namefilter);
494509
int radiusint;

src/imthreshold.c

Lines changed: 99 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ double RADGRD = 57.295779513082320876798154814105;
1313
////////////////////////////////////////////////////////////////////////////////
1414

1515
BYTE ByteClamp(int c)
16-
{
17-
BYTE buff[3] = {(BYTE)c, 255, 0};
18-
return buff[ (c < 0) + ((unsigned)c > 255) ];
16+
{
17+
BYTE buff[3] = {0, (BYTE)c, 255};
18+
return buff[ (int)(c > 0) + (int)(c > 255) ];
1919
}
2020

2121
////////////////////////////////////////////////////////////////////////////////
2222

2323
WORD Byte3Clamp(int c)
24-
{
25-
WORD buff[3] = {(WORD)c, 765, 0};
26-
return buff[ (c < 0) + ((unsigned)c > 765) ];
24+
{
25+
WORD buff[3] = {0, (WORD)c, 765};
26+
return buff[ (int)(c > 0) + (int)(c > 765) ];
2727
}
2828

2929
////////////////////////////////////////////////////////////////////////////////
3030

3131
unsigned IndexClamp(int i, unsigned threshold)
32-
{
33-
unsigned buff[3] = {(unsigned)i, threshold, 0};
34-
return buff[ (i < 0) + ((unsigned)i > threshold) ];
32+
{
33+
unsigned buff[3] = {0, (unsigned)i, threshold};
34+
return buff[ (int)(i > 0) + (int)(i > threshold) ];
3535
}
3636

3737
////////////////////////////////////////////////////////////////////////////////
@@ -722,7 +722,7 @@ unsigned IMTFilterDMag2 (BYTE** p_im, unsigned height, unsigned width, unsigned
722722
unsigned height2, width2, y, threshold;
723723
height2 = height * 2;
724724
width2 = width * 2;
725-
725+
726726
BYTE** d_im = BWalloc(height2, width2);
727727

728728
threshold = 0;
@@ -733,9 +733,9 @@ unsigned IMTFilterDMag2 (BYTE** p_im, unsigned height, unsigned width, unsigned
733733
}
734734
threshold /= Ksize;
735735
threshold /= 2;
736-
736+
737737
BWfree(d_im, height2);
738-
738+
739739
return threshold;
740740
}
741741

@@ -2094,7 +2094,7 @@ void IMTFilterSeparateBGFGL (IMTpixel** p_im, BYTE** m_im, IMTpixel** fg_im, IMT
20942094
bgdist = IMTdist(pim, bgim);
20952095
m_im[y][x] = (BYTE)((fgdist < bgdist) ? 0 : 255);
20962096
}
2097-
}
2097+
}
20982098

20992099
IMTfree(fgt_im, heightbg);
21002100
}
@@ -2574,7 +2574,7 @@ void IMTFilterMathDivide (IMTpixel** p_im, IMTpixel** m_im, unsigned height, uns
25742574
imm = m_im[y][x].c[d];
25752575
imm++;
25762576
im /= imm;
2577-
im *= 256;
2577+
im *= 256;
25782578
im += delta;
25792579
im -= 0.5;
25802580
p_im[y][x].c[d] = ByteClamp((int)im);
@@ -3651,6 +3651,85 @@ double IMTFilterPosterize (IMTpixel** p_im, unsigned height, unsigned width, uns
36513651

36523652
////////////////////////////////////////////////////////////////////////////////
36533653

3654+
double IMTFilterQuant (IMTpixel** p_im, unsigned height, unsigned width, unsigned quant)
3655+
{
3656+
unsigned y, x, d, k, l, n;
3657+
unsigned im, imt;
3658+
int imd;
3659+
unsigned histogram[256];
3660+
unsigned histthres[256];
3661+
unsigned histquant[256];
3662+
double histstep, histsum, histsumk, imds = 0.0;
3663+
3664+
quant = (quant > 0) ? quant : 1;
3665+
n = height * width;
3666+
n = (n > 0) ? n : 1;
3667+
histstep = (double)n;
3668+
histstep /= quant;
3669+
for (d = 0; d < 3; d++)
3670+
{
3671+
for (k = 0; k < 256; k++)
3672+
{
3673+
histogram[k] = 0;
3674+
histthres[k] = 0;
3675+
histquant[k] = 0;
3676+
}
3677+
for (y = 0; y < height; y++)
3678+
{
3679+
for (x = 0; x < width; x++)
3680+
{
3681+
im = p_im[y][x].c[d];
3682+
histogram[im]++;
3683+
}
3684+
}
3685+
histsum = 0.0;
3686+
for (k = 0; k < 256; k++)
3687+
{
3688+
histsum += histogram[k];
3689+
l = (histsum > 0.0) ? (int)((histsum - 1.0)/ histstep) : 0;
3690+
histthres[k] = l;
3691+
}
3692+
for (l = 0; l < quant; l++)
3693+
{
3694+
histsum = 0;
3695+
histsumk = 0;
3696+
for (k = 0; k < 256; k++)
3697+
{
3698+
if (histthres[k] == l)
3699+
{
3700+
histsum += histogram[k];
3701+
histsumk += (histogram[k] * k);
3702+
}
3703+
}
3704+
histsumk = (histsum > 0) ? (histsumk / histsum) : histsumk;
3705+
for (k = 0; k < 256; k++)
3706+
{
3707+
if (histthres[k] == l)
3708+
{
3709+
histquant[k] = histsumk;
3710+
}
3711+
}
3712+
}
3713+
for (y = 0; y < height; y++)
3714+
{
3715+
for (x = 0; x < width; x++)
3716+
{
3717+
im = p_im[y][x].c[d];
3718+
imt = histquant[im];
3719+
imd = (im < imt) ? (imt - im) : (im - imt);
3720+
p_im[y][x].c[d] = ByteClamp(imt);
3721+
imds += imd;
3722+
}
3723+
}
3724+
}
3725+
imds /= n;
3726+
imds /= 3;
3727+
3728+
return imds;
3729+
}
3730+
3731+
////////////////////////////////////////////////////////////////////////////////
3732+
36543733
void IMTFilterPMean (IMTpixel** p_im, IMTpixel** d_im, unsigned height, unsigned width, double radius, int fmode, bool fneared)
36553734
{
36563735
int y, x, i, j, rn, dy, dx;
@@ -4918,7 +4997,7 @@ double IMTFilterClusterBWC (IMTpixel** p_im, IMTpixel** d_im, unsigned height, u
49184997
if (gpart == 0) {gpart = 1;}
49194998
for (y = 0; y < height; y++)
49204999
{
4921-
5000+
49225001
if (y <= radius) {y1 = 0;} else {y1 = y - radius;}
49235002
y2 = y;
49245003
y2 += radius;
@@ -5521,7 +5600,7 @@ void IMTFilterSBicont (IMTpixel** p_im, IMTpixel** d_im, unsigned height, unsign
55215600
{
55225601
pdist[k] += sdist;
55235602
pdist[k] = sdist / pdist[k];
5524-
5603+
55255604
pdist[k] -= 0.5;
55265605
zdist = (pdist[k] < 0.0) ? -1.0: 1.0;
55275606
pdist[k] *= zdist;
@@ -6225,7 +6304,7 @@ int IMTFilterThresholdLayer (IMTpixel** p_im, WORD** t_im, BYTE** d_im, unsigned
62256304
imts /= height;
62266305
imts /= width;
62276306
threshold = (int)(imts + 0.5);
6228-
6307+
62296308
return threshold;
62306309
}
62316310

@@ -6704,7 +6783,7 @@ int IMTFilterTBiModValue (IMTpixel** p_im, unsigned height, unsigned width)
67046783
int IMTFilterTBiMod (IMTpixel** p_im, BYTE** d_im, unsigned height, unsigned width, int delta)
67056784
{
67066785
int threshold = 0;
6707-
6786+
67086787
threshold = IMTFilterTBiModValue (p_im, height, width);
67096788
threshold += delta;
67106789
threshold = IMTFilterThreshold (p_im, d_im, height, width, threshold);
@@ -8198,7 +8277,7 @@ int IMTFilterTMscaleLayer (IMTpixel** p_im, WORD** t_im, unsigned height, unsign
81988277
for (x = x0; x < x1; x++)
81998278
{
82008279
if (p_im[y][x].s < immin) {immin = p_im[y][x].s;}
8201-
if (p_im[y][x].s > immax) {immax = p_im[y][x].s;}
8280+
if (p_im[y][x].s > immax) {immax = p_im[y][x].s;}
82028281
}
82038282
}
82048283
immean = immax + immin;
@@ -8438,7 +8517,7 @@ int IMTFilterTQuadModValue (IMTpixel** p_im, unsigned height, unsigned width)
84388517
int IMTFilterTQuadMod (IMTpixel** p_im, BYTE** d_im, unsigned height, unsigned width, int delta)
84398518
{
84408519
int threshold = 0;
8441-
8520+
84428521
threshold = IMTFilterTQuadModValue (p_im, height, width);
84438522
threshold += delta;
84448523
threshold = IMTFilterThreshold (p_im, d_im, height, width, threshold);

src/imthreshold.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#define MAX(a,b) ((a) > (b) ? (a) : (b))
2929
#endif
3030
#ifndef TRIM
31-
#define TRIM(x,a,b) (FA_MIN(FA_MAX(x,a),b))
31+
#define TRIM(x,a,b) (MIN(FA_MAX((x),(a)),(b)))
3232
#endif
3333

3434
typedef uint8_t BYTE;
@@ -149,6 +149,7 @@ IMTpixel IMTmeanMaxIcM (IMTpixel**, IMTpixel, bool**, unsigned, unsigned, unsign
149149
int IMTFilterKMeans (IMTpixel**, unsigned, unsigned, unsigned, unsigned);
150150
void IMTFilterPeron (IMTpixel**, IMTpixel**, unsigned, unsigned, double, double);
151151
double IMTFilterPosterize (IMTpixel**, unsigned, unsigned, unsigned);
152+
double IMTFilterQuant (IMTpixel**, unsigned, unsigned, unsigned);
152153
void IMTFilterPMean (IMTpixel**, IMTpixel**, unsigned, unsigned, double, int, bool);
153154
int IMTFilterRetinex (IMTpixel**, IMTpixel**, unsigned, unsigned, int, double);
154155
double IMTFilterRS (IMTpixel**, unsigned, unsigned);

0 commit comments

Comments
 (0)