Пример #1
0
        public static JpegQuantizationTable ScaleByQuality(JpegQuantizationTable quantizationTable, int quality)
        {
            if (quantizationTable.IsEmpty)
            {
                throw new ArgumentException("Quantization table is not initialized.", nameof(quantizationTable));
            }
            if ((uint)quality > 100)
            {
                throw new ArgumentOutOfRangeException(nameof(quality));
            }

            int scale = quality < 50 ? 5000 / quality : 200 - (quality * 2);

            ReadOnlySpan <ushort> source = quantizationTable.Elements;

            ushort[] elements = new ushort[64];
            for (int i = 0; i < elements.Length; i++)
            {
                int x = source[i];
                x           = ((x * scale) + 50) / 100;
                elements[i] = (ushort)JpegMathHelper.Clamp(x, 1, 255);
            }

            return(new JpegQuantizationTable(quantizationTable.ElementPrecision, quantizationTable.Identifier, elements));
        }
Пример #2
0
        public bool TryEstimateQuality(out float quality)
        {
            if (_quantizationTables is null)
            {
                quality = 0;
                return(false);
            }

            // Luminance
            JpegQuantizationTable quantizationTable = GetQuantizationTable(0);

            if (quantizationTable.IsEmpty)
            {
                quality = 0;
                return(false);
            }
            quality = EstimateQuality(quantizationTable, JpegStandardQuantizationTable.GetLuminanceTable(0, 0), out _);

            // Chrominance
            quantizationTable = GetQuantizationTable(1);
            if (!quantizationTable.IsEmpty)
            {
                float quality2 = EstimateQuality(quantizationTable, JpegStandardQuantizationTable.GetChrominanceTable(0, 0), out _);
                quality = Math.Min(quality, quality2);
            }

            quality = JpegMathHelper.Clamp(quality, 0f, 100f);
            return(true);
        }