BitMiracle.LibJpeg.Classic.Internal.jpeg_downsampler.h2v1_downsample C# (CSharp) Method

h2v1_downsample() private method

Downsample pixel values of a single component. This version handles the common case of 2:1 horizontal and 1:1 vertical, without smoothing. A note about the "bias" calculations: when rounding fractional values to integer, we do not want to always round 0.5 up to the next integer. If we did that, we'd introduce a noticeable bias towards larger values. Instead, this code is arranged so that 0.5 will be rounded up or down at alternate pixel locations (a simple ordered dither pattern).
private h2v1_downsample ( int componentIndex, byte input_data, int startInputRow, byte output_data, int startOutRow ) : void
componentIndex int
input_data byte
startInputRow int
output_data byte
startOutRow int
return void
        private void h2v1_downsample(int componentIndex, byte[][] input_data, int startInputRow, byte[][] output_data, int startOutRow)
        {
            /* Expand input data enough to let all the output samples be generated
             * by the standard loop.  Special-casing padded output would be more
             * efficient.
             */
            jpeg_component_info compptr = m_cinfo.Component_info[componentIndex];
            int output_cols = compptr.Width_in_blocks * compptr.DCT_h_scaled_size;
            expand_right_edge(input_data, startInputRow, m_cinfo.m_max_v_samp_factor, m_cinfo.m_image_width, output_cols * 2);

            for (int outrow = 0; outrow < m_cinfo.m_max_v_samp_factor; outrow++)
            {
                /* bias = 0,1,0,1,... for successive samples */
                int bias = 0;
                int inputColumn = 0;
                for (int outcol = 0; outcol < output_cols; outcol++)
                {
                    output_data[startOutRow + outrow][outcol] = (byte)(
                        ((int)input_data[startInputRow + outrow][inputColumn] +
                        (int)input_data[startInputRow + outrow][inputColumn + 1] + bias) >> 1);

                    bias ^= 1;      /* 0=>1, 1=>0 */
                    inputColumn += 2;
                }
            }
        }