BitMiracle.LibJpeg.Classic.Internal.my_2pass_cquantizer.init_error_limit C# (CSharp) Method

init_error_limit() private method

Initialize the error-limiting transfer function (lookup table). The raw F-S error computation can potentially compute error values of up to +- MAXJSAMPLE. But we want the maximum correction applied to a pixel to be much less, otherwise obviously wrong pixels will be created. (Typical effects include weird fringes at color-area boundaries, isolated bright pixels in a dark area, etc.) The standard advice for avoiding this problem is to ensure that the "corners" of the color cube are allocated as output colors; then repeated errors in the same direction cannot cause cascading error buildup. However, that only prevents the error from getting completely out of hand; Aaron Giles reports that error limiting improves the results even with corner colors allocated. A simple clamping of the error values to about +- MAXJSAMPLE/8 works pretty well, but the smoother transfer function used below is even better. Thanks to Aaron Giles for this idea.
private init_error_limit ( ) : void
return void
        private void init_error_limit()
        {
            m_error_limiter = new int [JpegConstants.MAXJSAMPLE * 2 + 1];
            int tableOffset = JpegConstants.MAXJSAMPLE;

            const int STEPSIZE = ((JpegConstants.MAXJSAMPLE + 1) / 16);

            /* Map errors 1:1 up to +- MAXJSAMPLE/16 */
            int output = 0;
            int input = 0;
            for (; input < STEPSIZE; input++, output++)
            {
                m_error_limiter[tableOffset + input] = output; 
                m_error_limiter[tableOffset - input] = -output;
            }

            /* Map errors 1:2 up to +- 3*MAXJSAMPLE/16 */
            for (; input < STEPSIZE*3; input++)
            {
                m_error_limiter[tableOffset + input] = output; 
                m_error_limiter[tableOffset - input] = -output;
                output += (input & 1) != 0 ? 1 : 0;
            }

            /* Clamp the rest to final output value (which is (MAXJSAMPLE+1)/8) */
            for (; input <= JpegConstants.MAXJSAMPLE; input++)
            {
                m_error_limiter[tableOffset + input] = output; 
                m_error_limiter[tableOffset - input] = -output;
            }
        }