BitMiracle.LibJpeg.Classic.Internal.my_1pass_cquantizer.make_odither_array C# (CSharp) Method

make_odither_array() private static method

Create an ordered-dither array for a component having ncolors distinct output values.
private static make_odither_array ( int ncolors ) : int[][]
ncolors int
return int[][]
        private static int[][] make_odither_array(int ncolors)
        {
            int[][] odither = new int[ODITHER_SIZE][];
            for (int i = 0; i < ODITHER_SIZE; i++)
                odither[i] = new int[ODITHER_SIZE];

            /* The inter-value distance for this color is MAXJSAMPLE/(ncolors-1).
             * Hence the dither value for the matrix cell with fill order f
             * (f=0..N-1) should be (N-1-2*f)/(2*N) * MAXJSAMPLE/(ncolors-1).
             * On 16-bit-int machine, be careful to avoid overflow.
             */
            int den = 2 * ODITHER_CELLS * (ncolors - 1);
            for (int j = 0; j < ODITHER_SIZE; j++)
            {
                for (int k = 0; k < ODITHER_SIZE; k++)
                {
                    int num = ((int)(ODITHER_CELLS - 1 - 2 * ((int)base_dither_matrix[j][k]))) * JpegConstants.MAXJSAMPLE;

                    /* Ensure round towards zero despite C's lack of consistency
                     * about rounding negative values in integer division...
                     */
                    odither[j][k] = num < 0 ? -((-num) / den) : num / den;
                }
            }

            return odither;
        }
    }