예제 #1
0
        public static Vector <int>[,] ApplyDct(BitmapSource bitmap, DctParameters dctParameters)
        {
            var map           = bitmap.GetMap();
            var mapInYCbCr    = ToYCbCr(map);
            var decimatedMap  = Decimate(mapInYCbCr, dctParameters.DecimationType);
            var blocks        = SplitToBlocks(decimatedMap, 8);
            var dct           = GetDctMatrix();
            var transposedDct = dct.Transpose();
            var appliedDct    = ApplyDct(blocks, dct, transposedDct);
            var quantizedDct  = QuantizeMatrices(appliedDct, dctParameters);

            return(Flatten(quantizedDct));
        }
예제 #2
0
        public static BitmapSource DecodeDct(DctParameters dctParameters)
        {
            var encoded             = Jpeg.Load(dctParameters);
            var encodedMatrices     = SplitToBlocksInt(encoded, 8);
            var dequantizedMatrices = DequantizeMatrices(encodedMatrices, dctParameters);
            var dct           = GetDctMatrix();
            var transposedDct = dct.Transpose();
            var inversedDct   = ApplyDct(dequantizedMatrices, transposedDct, dct);
            var flattened     = FlattenByte(inversedDct);
            var undecimated   = Undecimate(flattened, dctParameters.DecimationType);
            var inRgb         = ToRgbBytes(undecimated);

            return(BitmapSource.Create(undecimated.GetLength(0), undecimated.GetLength(1), 96.0, 96.0, PixelFormats.Bgr32,
                                       null, inRgb, inRgb.Length / undecimated.GetLength(0)));
        }
예제 #3
0
        private static Vector <Matrix>[,] DequantizeMatrices(Vector <Matrix>[,] encodedMatrices, DctParameters dctParameters)
        {
            switch (dctParameters.QuantizationType)
            {
            case QuantizationType.LargestN:
                return(encodedMatrices);

            case QuantizationType.DefaultJpegMatrix:
                return(DequantizeByMatrices(encodedMatrices, DefaultMatrixY, DefaultMatrixC));

            case QuantizationType.QuantizationMatrix:
                var matrixY = GetQuantizationMatrix(dctParameters.GeneratorsY);
                var matrixC = GetQuantizationMatrix(dctParameters.GeneratorsC);
                return(DequantizeByMatrices(encodedMatrices, matrixY, matrixC));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
예제 #4
0
        private static Vector <Matrix>[,] QuantizeMatrices(Vector <Matrix>[,] appliedDct, DctParameters dctParameters)
        {
            switch (dctParameters.QuantizationType)
            {
            case QuantizationType.LargestN:
                if (!dctParameters.Ny.HasValue || !dctParameters.Nc.HasValue)
                {
                    throw new Exception("Invalid program state");
                }
                return(QuantizeLargest(appliedDct, dctParameters.Ny.Value, dctParameters.Nc.Value));

            case QuantizationType.QuantizationMatrix:
                var matrixY = GetQuantizationMatrix(dctParameters.GeneratorsY);
                var matrixC = GetQuantizationMatrix(dctParameters.GeneratorsC);
                return(QuantizeByMatrices(appliedDct, matrixY, matrixC));

            case QuantizationType.DefaultJpegMatrix:
                return(QuantizeByMatrices(appliedDct, DefaultMatrixY, DefaultMatrixC));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }