@@ -13,25 +13,25 @@ double RADGRD = 57.295779513082320876798154814105;
13
13
////////////////////////////////////////////////////////////////////////////////
14
14
15
15
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 ) ];
19
19
}
20
20
21
21
////////////////////////////////////////////////////////////////////////////////
22
22
23
23
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 ) ];
27
27
}
28
28
29
29
////////////////////////////////////////////////////////////////////////////////
30
30
31
31
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 ) ];
35
35
}
36
36
37
37
////////////////////////////////////////////////////////////////////////////////
@@ -722,7 +722,7 @@ unsigned IMTFilterDMag2 (BYTE** p_im, unsigned height, unsigned width, unsigned
722
722
unsigned height2 , width2 , y , threshold ;
723
723
height2 = height * 2 ;
724
724
width2 = width * 2 ;
725
-
725
+
726
726
BYTE * * d_im = BWalloc (height2 , width2 );
727
727
728
728
threshold = 0 ;
@@ -733,9 +733,9 @@ unsigned IMTFilterDMag2 (BYTE** p_im, unsigned height, unsigned width, unsigned
733
733
}
734
734
threshold /= Ksize ;
735
735
threshold /= 2 ;
736
-
736
+
737
737
BWfree (d_im , height2 );
738
-
738
+
739
739
return threshold ;
740
740
}
741
741
@@ -2094,7 +2094,7 @@ void IMTFilterSeparateBGFGL (IMTpixel** p_im, BYTE** m_im, IMTpixel** fg_im, IMT
2094
2094
bgdist = IMTdist (pim , bgim );
2095
2095
m_im [y ][x ] = (BYTE )((fgdist < bgdist ) ? 0 : 255 );
2096
2096
}
2097
- }
2097
+ }
2098
2098
2099
2099
IMTfree (fgt_im , heightbg );
2100
2100
}
@@ -2574,7 +2574,7 @@ void IMTFilterMathDivide (IMTpixel** p_im, IMTpixel** m_im, unsigned height, uns
2574
2574
imm = m_im [y ][x ].c [d ];
2575
2575
imm ++ ;
2576
2576
im /= imm ;
2577
- im *= 256 ;
2577
+ im *= 256 ;
2578
2578
im += delta ;
2579
2579
im -= 0.5 ;
2580
2580
p_im [y ][x ].c [d ] = ByteClamp ((int )im );
@@ -3651,6 +3651,85 @@ double IMTFilterPosterize (IMTpixel** p_im, unsigned height, unsigned width, uns
3651
3651
3652
3652
////////////////////////////////////////////////////////////////////////////////
3653
3653
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
+
3654
3733
void IMTFilterPMean (IMTpixel * * p_im , IMTpixel * * d_im , unsigned height , unsigned width , double radius , int fmode , bool fneared )
3655
3734
{
3656
3735
int y , x , i , j , rn , dy , dx ;
@@ -4918,7 +4997,7 @@ double IMTFilterClusterBWC (IMTpixel** p_im, IMTpixel** d_im, unsigned height, u
4918
4997
if (gpart == 0 ) {gpart = 1 ;}
4919
4998
for (y = 0 ; y < height ; y ++ )
4920
4999
{
4921
-
5000
+
4922
5001
if (y <= radius ) {y1 = 0 ;} else {y1 = y - radius ;}
4923
5002
y2 = y ;
4924
5003
y2 += radius ;
@@ -5521,7 +5600,7 @@ void IMTFilterSBicont (IMTpixel** p_im, IMTpixel** d_im, unsigned height, unsign
5521
5600
{
5522
5601
pdist [k ] += sdist ;
5523
5602
pdist [k ] = sdist / pdist [k ];
5524
-
5603
+
5525
5604
pdist [k ] -= 0.5 ;
5526
5605
zdist = (pdist [k ] < 0.0 ) ? -1.0 : 1.0 ;
5527
5606
pdist [k ] *= zdist ;
@@ -6225,7 +6304,7 @@ int IMTFilterThresholdLayer (IMTpixel** p_im, WORD** t_im, BYTE** d_im, unsigned
6225
6304
imts /= height ;
6226
6305
imts /= width ;
6227
6306
threshold = (int )(imts + 0.5 );
6228
-
6307
+
6229
6308
return threshold ;
6230
6309
}
6231
6310
@@ -6704,7 +6783,7 @@ int IMTFilterTBiModValue (IMTpixel** p_im, unsigned height, unsigned width)
6704
6783
int IMTFilterTBiMod (IMTpixel * * p_im , BYTE * * d_im , unsigned height , unsigned width , int delta )
6705
6784
{
6706
6785
int threshold = 0 ;
6707
-
6786
+
6708
6787
threshold = IMTFilterTBiModValue (p_im , height , width );
6709
6788
threshold += delta ;
6710
6789
threshold = IMTFilterThreshold (p_im , d_im , height , width , threshold );
@@ -8198,7 +8277,7 @@ int IMTFilterTMscaleLayer (IMTpixel** p_im, WORD** t_im, unsigned height, unsign
8198
8277
for (x = x0 ; x < x1 ; x ++ )
8199
8278
{
8200
8279
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 ;}
8202
8281
}
8203
8282
}
8204
8283
immean = immax + immin ;
@@ -8438,7 +8517,7 @@ int IMTFilterTQuadModValue (IMTpixel** p_im, unsigned height, unsigned width)
8438
8517
int IMTFilterTQuadMod (IMTpixel * * p_im , BYTE * * d_im , unsigned height , unsigned width , int delta )
8439
8518
{
8440
8519
int threshold = 0 ;
8441
-
8520
+
8442
8521
threshold = IMTFilterTQuadModValue (p_im , height , width );
8443
8522
threshold += delta ;
8444
8523
threshold = IMTFilterThreshold (p_im , d_im , height , width , threshold );
0 commit comments