FluxJpeg.Core.Decoder.JpegComponent.scaleByFactors C# (CSharp) Method

scaleByFactors() public method

Stretches components as needed to normalize the size of all components. For example, in a 2x1 (4:2:2) sequence, the Cr and Cb channels will be scaled vertically by a factor of 2.
public scaleByFactors ( BlockUpsamplingMode mode ) : void
mode BlockUpsamplingMode
return void
        public void scaleByFactors( BlockUpsamplingMode mode )
        {
            int factorUpVertical = factorUpV,
                factorUpHorizontal = factorUpH;

            if (factorUpVertical == 1 && factorUpHorizontal == 1) return;

            for (int i = 0; i < scanDecoded.Count; i++)
            {
                byte[,] src = scanDecoded[i];

                int oldV = src.GetLength(0),
                    oldH = src.GetLength(1),
                    newV = oldV * factorUpVertical,
                    newH = oldH * factorUpHorizontal;
                
                byte[,] dest = new byte[newV, newH];

                switch (mode)
                {
                    case BlockUpsamplingMode.BoxFilter:
                        #region Upsampling by repeating values
                        /* Perform scaling (Box filter) */
                        for (int u = 0; u < newH; u++)
                        {
                            int src_u = u / factorUpHorizontal;
                            for (int v = 0; v < newV; v++)
                            {
                                int src_v = v / factorUpVertical;
                                dest[v, u] = src[src_v, src_u];
                            }
                        }
                        #endregion
                        break;

                    case BlockUpsamplingMode.Interpolate:
                        #region Upsampling by interpolation

                        for (int u = 0; u < newH; u++)
                        {
                            for (int v = 0; v < newV; v++)
                            {
                                int val = 0;

                                for (int x = 0; x < factorUpHorizontal; x++)
                                {
                                    int src_u = (u + x) / factorUpHorizontal;
                                    if (src_u >= oldH) src_u = oldH - 1;

                                    for (int y = 0; y < factorUpVertical; y++)
                                    {
                                        int src_v = (v + y) / factorUpVertical;

                                        if (src_v >= oldV) src_v = oldV - 1;

                                        val += src[src_v, src_u];
                                    }
                                }

                                dest[v, u] = (byte)(val / (factorUpHorizontal * factorUpVertical));
                            }
                        }

                        #endregion
                        break;

                    default:
                        throw new ArgumentException("Upsampling mode not supported.");
                }

                scanDecoded[i] = dest;
            }

        }