@@ -13,25 +13,25 @@ double RADGRD = 57.295779513082320876798154814105;
1313////////////////////////////////////////////////////////////////////////////////
1414
1515BYTE 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
2323WORD 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
3131unsigned 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+
36543733void 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)
67046783int 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)
84388517int 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 );
0 commit comments