public void Initialize(ushort restartInterval, ushort[] subsamplingFactors)
        {
            _restartInterval = restartInterval;

            // Make sure everything is initialized
            if (_components.Length == 0)
            {
                throw new InvalidDataException();
            }
            foreach (ComponentInfo component in _components)
            {
                if (!component.IsInitialized)
                {
                    throw new InvalidDataException();
                }
            }

            var frameComponents = new JpegFrameComponentSpecificationParameters[_components.Length];
            var scanComponents  = new JpegScanComponentSpecificationParameters[_components.Length];

            // Special case for YCbCr.
            if (subsamplingFactors.Length == 2)
            {
                if (frameComponents.Length != 3)
                {
                    // YCbCr image must have 3 components.
                    throw new InvalidDataException();
                }
                if (subsamplingFactors[0] != 1 && subsamplingFactors[0] != 2 && subsamplingFactors[0] != 4)
                {
                    throw new InvalidDataException("Subsampling factor other than 1,2,4 is not supported.");
                }
                if (subsamplingFactors[1] != 1 && subsamplingFactors[1] != 2 && subsamplingFactors[1] != 4)
                {
                    throw new InvalidDataException("Subsampling factor other than 1,2,4 is not supported.");
                }
                ushort maxFactor = Math.Max(subsamplingFactors[0], subsamplingFactors[1]);
                frameComponents[0] = new JpegFrameComponentSpecificationParameters(0, (byte)maxFactor, (byte)maxFactor, 0);
                frameComponents[1] = new JpegFrameComponentSpecificationParameters(1, (byte)(maxFactor / subsamplingFactors[0]), (byte)(maxFactor / subsamplingFactors[1]), 1);
                frameComponents[2] = new JpegFrameComponentSpecificationParameters(2, (byte)(maxFactor / subsamplingFactors[0]), (byte)(maxFactor / subsamplingFactors[1]), 2);
            }
            else
            {
                for (int i = 0; i < frameComponents.Length; i++)
                {
                    frameComponents[i] = new JpegFrameComponentSpecificationParameters((byte)i, 1, 1, (byte)i);
                }
            }

            for (int i = 0; i < scanComponents.Length; i++)
            {
                scanComponents[i] = new JpegScanComponentSpecificationParameters((byte)i, (byte)i, (byte)i);
            }

            _frameHeader = new JpegFrameHeader(8, 0, 0, (byte)frameComponents.Length, frameComponents);
            _scanHeader  = new JpegScanHeader((byte)scanComponents.Length, scanComponents, 0, 0, 0, 0);
        }
Пример #2
0
        protected int InitDecodeComponents(JpegFrameHeader frameHeader, JpegScanHeader scanHeader, Span <JpegArithmeticDecodingComponent> components)
        {
            Debug.Assert(!(frameHeader.Components is null));
            Debug.Assert(!(scanHeader.Components is null));

            // Compute maximum sampling factor
            int maxHorizontalSampling = 1;
            int maxVerticalSampling   = 1;

            foreach (JpegFrameComponentSpecificationParameters currentFrameComponent in frameHeader.Components !)
            {
                maxHorizontalSampling = Math.Max(maxHorizontalSampling, currentFrameComponent.HorizontalSamplingFactor);
                maxVerticalSampling   = Math.Max(maxVerticalSampling, currentFrameComponent.VerticalSamplingFactor);
            }

            // Resolve each component
            if (components.Length < scanHeader.NumberOfComponents)
            {
                throw new InvalidOperationException();
            }
            for (int i = 0; i < scanHeader.NumberOfComponents; i++)
            {
                JpegScanComponentSpecificationParameters scanComponenet = scanHeader.Components ![i];