@@ -97,7 +97,7 @@ private GilbertCurve(int width, int height, int[] pixels, Color[] palette, int[]
9797 DITHER_MAX = ( byte ) ( weight < .015 ? ( weight > .0025 ) ? 25 : 16 : 9 ) ;
9898 var edge = hasAlpha ? 1 : Math . Exp ( weight ) + .25 ;
9999 var deviation = ! hasAlpha && weight > .002 ? .25 : 1 ;
100- ditherMax = ( hasAlpha || DITHER_MAX > 9 ) ? ( byte ) BitmapUtilities . Sqr ( Math . Sqrt ( DITHER_MAX ) + edge * deviation ) : ( byte ) ( DITHER_MAX * 2 ) ;
100+ ditherMax = ( hasAlpha || DITHER_MAX > 9 ) ? ( byte ) BitmapUtilities . Sqr ( Math . Sqrt ( DITHER_MAX ) + edge * deviation ) : ( byte ) ( DITHER_MAX * 1.5 ) ;
101101 int density = palette . Length > 16 ? 3200 : 1500 ;
102102 if ( palette . Length / weight > 5000 && ( weight > .045 || ( weight > .01 && palette . Length < 64 ) ) )
103103 ditherMax = ( byte ) BitmapUtilities . Sqr ( 5 + edge ) ;
@@ -118,28 +118,29 @@ private int DitherPixel(int x, int y, Color c2, float beta)
118118 int b_pix = c2 . B ;
119119 int a_pix = c2 . A ;
120120
121+ var qPixelIndex = qPixels [ bidx ] ;
121122 var strength = 1 / 3f ;
122123 int acceptedDiff = Math . Max ( 2 , palette . Length - margin ) ;
123124 if ( palette . Length <= 4 && saliencies [ bidx ] > .2f && saliencies [ bidx ] < .25f )
124- c2 = BlueNoise . Diffuse ( pixel , palette [ qPixels [ bidx ] ] , beta * 2 / saliencies [ bidx ] , strength , x , y ) ;
125+ c2 = BlueNoise . Diffuse ( pixel , palette [ qPixelIndex ] , beta * 2 / saliencies [ bidx ] , strength , x , y ) ;
125126 else if ( palette . Length <= 4 || CIELABConvertor . Y_Diff ( pixel , c2 ) < ( 2 * acceptedDiff ) ) {
126- if ( palette . Length <= 128 || BlueNoise . TELL_BLUE_NOISE [ bidx & 4095 ] > 0 )
127- c2 = BlueNoise . Diffuse ( pixel , palette [ qPixels [ bidx ] ] , beta * .5f / saliencies [ bidx ] , strength , x , y ) ;
127+ if ( palette . Length <= 128 || BlueNoise . TELL_BLUE_NOISE [ bidx & 4095 ] > 0 )
128+ c2 = BlueNoise . Diffuse ( pixel , palette [ qPixelIndex ] , beta * .5f / saliencies [ bidx ] , strength , x , y ) ;
128129 if ( CIELABConvertor . U_Diff ( pixel , c2 ) > ( margin * acceptedDiff ) )
129- c2 = BlueNoise . Diffuse ( pixel , palette [ qPixels [ bidx ] ] , beta / saliencies [ bidx ] , strength , x , y ) ;
130+ c2 = BlueNoise . Diffuse ( pixel , palette [ qPixelIndex ] , beta / saliencies [ bidx ] , strength , x , y ) ;
130131 }
131132
132133 if ( palette . Length < 3 || margin > 6 ) {
133134 var delta = ( weight > .0015 && weight < .0025 ) ? beta : Math . PI ;
134- if ( palette . Length > 4 && ( CIELABConvertor . Y_Diff ( pixel , c2 ) > ( delta * acceptedDiff ) || CIELABConvertor . U_Diff ( pixel , c2 ) < ( margin * acceptedDiff ) ) ) {
135+ if ( palette . Length > 4 && ( CIELABConvertor . Y_Diff ( pixel , c2 ) > ( delta * acceptedDiff ) || CIELABConvertor . U_Diff ( pixel , c2 ) < ( margin * acceptedDiff ) ) ) {
135136 var kappa = saliencies [ bidx ] < .4f ? beta * .4f * saliencies [ bidx ] : beta * .4f / saliencies [ bidx ] ;
136137 var c1 = saliencies [ bidx ] < .4f ? pixel : Color . FromArgb ( a_pix , r_pix , g_pix , b_pix ) ;
137- c2 = BlueNoise . Diffuse ( c1 , palette [ qPixels [ bidx ] ] , kappa , strength , x , y ) ;
138+ c2 = BlueNoise . Diffuse ( c1 , palette [ qPixelIndex ] , kappa , strength , x , y ) ;
138139 }
139140 }
140141 else if ( palette . Length > 4 && ( CIELABConvertor . Y_Diff ( pixel , c2 ) > ( beta * acceptedDiff ) || CIELABConvertor . U_Diff ( pixel , c2 ) < acceptedDiff ) ) {
141142 if ( beta < .3f && ( palette . Length <= 32 || saliencies [ bidx ] < beta ) )
142- c2 = BlueNoise . Diffuse ( c2 , palette [ qPixels [ bidx ] ] , beta * .4f * saliencies [ bidx ] , strength , x , y ) ;
143+ c2 = BlueNoise . Diffuse ( c2 , palette [ qPixelIndex ] , beta * .4f * saliencies [ bidx ] , strength , x , y ) ;
143144 else
144145 c2 = Color . FromArgb ( a_pix , r_pix , g_pix , b_pix ) ;
145146 }
@@ -225,11 +226,11 @@ private void DiffusePixel(int x, int y)
225226 var errLength = denoise ? error . Length - 1 : 0 ;
226227 for ( int j = 0 ; j < errLength ; ++ j )
227228 {
228- if ( sortedByYDiff && saliencies != null )
229- unaccepted = true ;
230-
231229 if ( Math . Abs ( error [ j ] ) >= ditherMax )
232230 {
231+ if ( sortedByYDiff && saliencies != null )
232+ unaccepted = true ;
233+
233234 if ( diffuse )
234235 error [ j ] = ( float ) Math . Tanh ( error [ j ] / maxErr * 20 ) * ( ditherMax - 1 ) ;
235236 else if ( illusion )
0 commit comments