// TODO: make chunks variable
        public static void ProgressiveQuantization(Triplet <short[]> input, ProgressiveQuantizationFactors progQuants, bool UseReduceExtrapolate, out List <Triplet <short[]> > output)
        {
            output = new List <Triplet <short[]> >();
            DwtTile DRS = new DwtTile(input.X, input.Y, input.Z);

            foreach (var quantArray in progQuants.ProgQuants)
            {
                DwtBands yBD  = DwtBands.GetFromLinearizationResult(DRS.Y_DwtQ, UseReduceExtrapolate);
                DwtBands cbBD = DwtBands.GetFromLinearizationResult(DRS.Cb_DwtQ, UseReduceExtrapolate);
                DwtBands crBD = DwtBands.GetFromLinearizationResult(DRS.Cr_DwtQ, UseReduceExtrapolate);

                RFX_PROGRESSIVE_CODEC_QUANT quant = Utility.ConvertProgQuant(quantArray);

                ProgressiveQuantization_Component(yBD, TileComponents.Y, quant);
                ProgressiveQuantization_Component(cbBD, TileComponents.Cb, quant);
                ProgressiveQuantization_Component(crBD, TileComponents.Cr, quant);

                DwtBands yDTS  = DwtBands.GetFromLinearizationResult(DRS.Y_DwtQ, UseReduceExtrapolate);
                DwtBands cbDTS = DwtBands.GetFromLinearizationResult(DRS.Cb_DwtQ, UseReduceExtrapolate);
                DwtBands crDTS = DwtBands.GetFromLinearizationResult(DRS.Cr_DwtQ, UseReduceExtrapolate);

                DTS_Component(yDTS, TileComponents.Y, quant);
                DTS_Component(cbDTS, TileComponents.Cb, quant);
                DTS_Component(crDTS, TileComponents.Cr, quant);
                DwtTile dwtDts = new DwtTile(yDTS.GetLinearizationData(), cbDTS.GetLinearizationData(), crDTS.GetLinearizationData());
                DRS.Sub(dwtDts);

                var triplet = new Triplet <short[]>(yBD.GetLinearizationData(), cbBD.GetLinearizationData(), crBD.GetLinearizationData());
                output.Add(triplet);
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Encode()
        {
            try
            {
                using (var bodyStream = new StreamReader(Request.Body))
                {
                    var bodyText = await bodyStream.ReadToEndAsync();

                    dynamic obj = JsonConvert.DeserializeObject(bodyText);

                    // retrieve the quant
                    var quantArray = JsonHelper.RetrieveQuantsArray(obj.Params.QuantizationFactorsArray);
                    _codecAction.Parameters[Constants.PARAM_NAME_QUANT_FACTORS_ARRAY] = quantArray;

                    // retrive the progressive quants
                    var progQuantList = new List <QuantizationFactorsArray>();
                    foreach (var layer in obj.Params.ProgQuantizationArray)
                    {
                        var layerQuants = JsonHelper.RetrieveQuantsArray(layer);
                        progQuantList.Add(layerQuants);
                    }
                    var progQuantarray = new ProgressiveQuantizationFactors
                    {
                        ProgQuants = progQuantList
                    };
                    _codecAction.Parameters[Constants.PARAM_NAME_PROGRESSIVE_QUANT_LIST] = progQuantarray;

                    _codecAction.Parameters[Constants.PARAM_NAME_ENTROPY_ALGORITHM]      = JsonHelper.CastTo <EntropyAlgorithm>(obj.Params.EntropyAlgorithm);
                    _codecAction.Parameters[Constants.PARAM_NAME_USE_REDUCE_EXTRAPOLATE] = JsonHelper.CastTo <UseReduceExtrapolate>(obj.Params.UseReduceExtrapolate);

                    // TODO: error handle
                    var   preFramePath = this.HttpContext.Session.Get <string>(PreviousFrameImage);
                    Frame preFrame     = Utility.GetPreviousFrame(preFramePath, _codecAction.Parameters);

                    var encodeImagePath = this.HttpContext.Session.Get <string>(EncodedImage);
                    var tile            = Tile.FromFile(encodeImagePath);

                    ICodecAction diffing = _codecAction.SubActions.SingleOrDefault(c => c.Name.Equals(Constants.PENCODE_NAME_SUBBANDDIFFING));
                    diffing.Parameters[Constants.PARAM_NAME_PREVIOUS_FRAME] = preFrame;

                    _codecAction.DoAction(new[] { tile });
                }
                return(Json(ReturnResult <string> .Success("Success")));
            }
            catch (Exception ex)
            {
                return(Json(ReturnResult <string> .Fail(ex.Message)));
            }
        }
        public ActionResult IndexWithInputs()
        {
            dynamic obj = GetJsonObject(Request.InputStream);

            foreach (var input in obj)
            {
                // TODO: refine this
                if (input != null && input.Inputs != null)
                {
                    int layer = JsonHelper.CastTo <int>(input.Layer);
                    if (layer == 0)
                    {
                        // TODO: refine this
                        // retrieve the quant
                        var quantArray = JsonHelper.RetrieveQuantsArray(input.Params.QuantizationFactorsArray);
                        // retrive the progressive quants
                        var progQuantList = new List <QuantizationFactorsArray>();
                        foreach (var layerQuant in input.Params.ProgQuantizationArray)
                        {
                            var layerQuants = JsonHelper.RetrieveQuantsArray(layerQuant);
                            progQuantList.Add(layerQuants);
                        }
                        var progQuantarray = new ProgressiveQuantizationFactors
                        {
                            ProgQuants = progQuantList
                        };

                        EntropyAlgorithm     algorithm            = JsonHelper.CastTo <EntropyAlgorithm>(input.Params.EntropyAlgorithm);
                        UseDifferenceTile    useDifferenceTile    = JsonHelper.CastTo <UseDifferenceTile>(input.Params.UseDifferenceTile);
                        UseReduceExtrapolate useReduceExtrapolate = JsonHelper.CastTo <UseReduceExtrapolate>(input.Params.UseReduceExtrapolate);

                        _viewModel = new RFXPDecodeViewModel(0);
                        ((RFXPDecodeViewModel)_viewModel).ProvideParam(quantArray, progQuantarray, algorithm, useDifferenceTile, useReduceExtrapolate);
                        ((RFXPDecodeViewModel)_viewModel).ProvidePanelInputs(layer, input.Inputs[0], input.Inputs[1], input.Inputs[2]);

                        Session[ModelKey]        = _viewModel;
                        Session[isPreFrameValid] = true;
                    }
                    Decode(input);
                    // Updates Decode Status
                    UpdateDecodeStatus(layer);
                }
            }
            Session[IsValid] = true;
            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
        public ActionResult Encode()
        {
            dynamic obj = GetJsonObject(Request.InputStream);

            // retrieve the quant
            var quantArray = JsonHelper.RetrieveQuantsArray(obj.Params.QuantizationFactorsArray);

            _codecAction.Parameters[Constants.PARAM_NAME_QUANT_FACTORS_ARRAY] = quantArray;

            // retrive the progressive quants
            var progQuantList = new List <QuantizationFactorsArray>();

            foreach (var layer in obj.Params.ProgQuantizationArray)
            {
                var layerQuants = JsonHelper.RetrieveQuantsArray(layer);
                progQuantList.Add(layerQuants);
            }
            var progQuantarray = new ProgressiveQuantizationFactors
            {
                ProgQuants = progQuantList
            };

            _codecAction.Parameters[Constants.PARAM_NAME_PROGRESSIVE_QUANT_LIST] = progQuantarray;

            _codecAction.Parameters[Constants.PARAM_NAME_ENTROPY_ALGORITHM]      = JsonHelper.CastTo <EntropyAlgorithm>(obj.Params.EntropyAlgorithm);
            _codecAction.Parameters[Constants.PARAM_NAME_USE_REDUCE_EXTRAPOLATE] = JsonHelper.CastTo <UseReduceExtrapolate>(obj.Params.UseReduceExtrapolate);

            // TODO: error handle
            var   preFramePath = (string)Session[PreviousFrameImage];
            Frame preFrame     = Utility.GetPreviousFrame(preFramePath, _codecAction.Parameters);

            var encodeImagePath = (string)Session[EncodedImage];
            var tile            = Tile.FromFile(encodeImagePath);

            ICodecAction diffing = _codecAction.SubActions.SingleOrDefault(c => c.Name.Equals(Constants.PENCODE_NAME_SUBBANDDIFFING));

            diffing.Parameters[Constants.PARAM_NAME_PREVIOUS_FRAME] = preFrame;

            _codecAction.DoAction(new[] { tile });

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
        // The output format is
        // firstPass [X, Y, Z]
        // ProgressivePass1 [encodedX, encodedY, encodedZ]
        // ProgressivePass1 [rawX, rawY, rawZ]
        // ...
        public static void RLGR_SRLEncode(List <Triplet <short[]> > input, ProgressiveQuantizationFactors proQuants, EntropyAlgorithm mode, bool UseReduceExtrapolate, out List <Triplet <byte[]> > output)
        {
            output = new List <Triplet <byte[]> >();
            byte[] x, y, z;

            // fisrt pass
            Triplet <short[]> firstPass = input[0];

            ComputeLL3Deltas(firstPass, UseReduceExtrapolate);
            RFXEncode.RFXEncoderWrapper.RLGREncode(firstPass.X, mode, out x);
            RFXEncode.RFXEncoderWrapper.RLGREncode(firstPass.Y, mode, out y);
            RFXEncode.RFXEncoderWrapper.RLGREncode(firstPass.Z, mode, out z);
            output.Add(new Triplet <byte[]>(x, y, z));

            // fake encodingContext used for SRL encode
            var encodingContext = new RfxProgressiveCodecContext(new [] { RdpegfxTileUtils.GetCodecQuant(ImageQuality_Values.Midium) }, 0, 0, 0, UseReduceExtrapolate);

            encodingContext.DAS = new DwtTile(firstPass.X, firstPass.Y, firstPass.Z);

            var progQuantList = new List <RFX_PROGRESSIVE_CODEC_QUANT>();

            foreach (var quant in proQuants.ProgQuants)
            {
                progQuantList.Add(Utility.ConvertProgQuant(quant));
            }

            // progressive pass
            for (int i = 1; i < input.Count; i++)
            {
                Triplet <short[]> progressivePass = input[i];
                encodingContext.ProgQ         = new DwtTile(progressivePass.X, progressivePass.Y, progressivePass.Z);
                encodingContext.prevProgQuant = progQuantList[i - 1];
                EncodedTile encodedTile = SRLEncode(encodingContext, progQuantList[i]);
                output.Add(new Triplet <byte[]>(encodedTile.YEncodedData, encodedTile.CbEncodedData, encodedTile.CrEncodedData));
                output.Add(new Triplet <byte[]>(encodedTile.YRawData, encodedTile.CbRawData, encodedTile.CrRawData));
                encodingContext.DAS.Add(new DwtTile(progressivePass.X, progressivePass.Y, progressivePass.Z));
                encodingContext.prevProgQuant = progQuantList[i];
            }
        }
        public override ActionResult Recompute()
        {
            // TODO: Add parameters obtain into a function
            dynamic obj = GetJsonObject(Request.InputStream);

            string name = JsonHelper.CastTo <string>(obj.Action);

            // gets the action with the same name as the argument
            var action = _codecAction.SubActions.SingleOrDefault(c => c.Name.Equals(name));

            // if action not found
            if (action == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
            }

            // retrieve the quant
            var quantArray = JsonHelper.RetrieveQuantsArray(obj.Params.QuantizationFactorsArray);

            _codecAction.Parameters[Constants.PARAM_NAME_QUANT_FACTORS_ARRAY] = quantArray;

            // retrive the progressive quants
            var progQuantList = new List <QuantizationFactorsArray>();

            foreach (var layer in obj.Params.ProgQuantizationArray)
            {
                var layerQuants = JsonHelper.RetrieveQuantsArray(layer);
                progQuantList.Add(layerQuants);
            }
            var progQuantarray = new ProgressiveQuantizationFactors
            {
                ProgQuants = progQuantList
            };

            _codecAction.Parameters[Constants.PARAM_NAME_PROGRESSIVE_QUANT_LIST] = progQuantarray;

            _codecAction.Parameters[Constants.PARAM_NAME_ENTROPY_ALGORITHM]      = JsonHelper.CastTo <EntropyAlgorithm>(obj.Params.EntropyAlgorithm);
            _codecAction.Parameters[Constants.PARAM_NAME_USE_REDUCE_EXTRAPOLATE] = JsonHelper.CastTo <UseReduceExtrapolate>(obj.Params.UseReduceExtrapolate);

            // TODO: error handle
            var   preFramePath = (string)Session[PreviousFrameImage];
            Frame preFrame     = Utility.GetPreviousFrame(preFramePath, _codecAction.Parameters);

            ICodecAction diffing = _codecAction.SubActions.SingleOrDefault(c => c.Name.Equals(Constants.PENCODE_NAME_SUBBANDDIFFING));

            diffing.Parameters[Constants.PARAM_NAME_PREVIOUS_FRAME] = preFrame;

            // retrive tiles from Inputs
            var tileList = new List <Tile>();

            foreach (var tileJson in obj.Inputs)
            {
                Triplet <string> triplet = JsonHelper.RetrieveTriplet(tileJson);

                string dataFormat = obj.Params.UseDataFormat;
                Tile   tile       = null;
                if (dataFormat.Equals(Constants.DataFormat.HEX))
                {
                    tile = Tile.FromStrings(triplet, new HexTileSerializer());
                }
                else
                {
                    tile = Tile.FromStrings(triplet, new IntegerTileSerializer());
                }
                if (dataFormat.Equals(Constants.DataFormat.FixedPoint_11_5))
                {
                    tile.RightShift(5);
                }
                if (dataFormat.Equals(Constants.DataFormat.FixedPoint_12_4))
                {
                    tile.RightShift(4);
                }

                tileList.Add(tile);
            }

            var result = action.DoAction(tileList.ToArray());

            // recompute the following steps and update
            bool following = false;

            Tile[] input = result;
            foreach (var act in _codecAction.SubActions)
            {
                if (following)
                {
                    result = act.DoAction(input);
                    input  = result;
                }
                else
                {
                    if (act.Name.Equals(name))
                    {
                        following = true;
                    }
                }
            }

            // TODO: recompute the following steps and update

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
        private ICodecAction GetDecoderWithParameters(dynamic obj)
        {
            ICodecAction _rfxPDecode = new RFXPDecode();

            int layer = JsonHelper.CastTo <int>(obj.Layer);

            // retrieve the quant
            var quantArray = JsonHelper.RetrieveQuantsArray(obj.Params.QuantizationFactorsArray);

            _rfxPDecode.Parameters[Constants.PARAM_NAME_QUANT_FACTORS_ARRAY] = quantArray;

            // retrive the progressive quants
            var progQuantList = new List <QuantizationFactorsArray>();

            foreach (var layerQuant in obj.Params.ProgQuantizationArray)
            {
                var layerQuants = JsonHelper.RetrieveQuantsArray(layerQuant);
                progQuantList.Add(layerQuants);
            }
            var progQuantarray = new ProgressiveQuantizationFactors
            {
                ProgQuants = progQuantList
            };

            _rfxPDecode.Parameters[Constants.PARAM_NAME_PROGRESSIVE_QUANT_LIST] = progQuantarray;

            _rfxPDecode.Parameters[Constants.PARAM_NAME_ENTROPY_ALGORITHM]      = JsonHelper.CastTo <EntropyAlgorithm>(obj.Params.EntropyAlgorithm);
            _rfxPDecode.Parameters[Constants.PARAM_NAME_USE_DIFFERENCE_TILE]    = JsonHelper.CastTo <UseDifferenceTile>(obj.Params.UseDifferenceTile);
            _rfxPDecode.Parameters[Constants.PARAM_NAME_USE_REDUCE_EXTRAPOLATE] = JsonHelper.CastTo <UseReduceExtrapolate>(obj.Params.UseReduceExtrapolate);

            if (layer == 0)
            {
                // Session used to store DAS and previous codec
                Tile DAS = Tile.FromArrays <short>(new Triplet <short[]>(
                                                       new short[Tile.TileSize * Tile.TileSize],
                                                       new short[Tile.TileSize * Tile.TileSize],
                                                       new short[Tile.TileSize * Tile.TileSize])
                                                   );
                Dictionary <int, ICodecAction> CodecActionDic = new Dictionary <int, ICodecAction>();
                Session[ConstantDAS]            = DAS;
                Session[ConstantCodecActionDic] = CodecActionDic;
                Session[ConstantDASDic]         = new Dictionary <int, Tile>();

                // TODO: error handle
                var   preFramePath = (string)Session[PreviousFrameImage];
                Frame preFrame     = Utility.GetPreviousFrame(preFramePath, _rfxPDecode.Parameters);
                Session[DecodePreviousFrame] = preFrame;
                Session[PreviousFrameDic]    = new Dictionary <int, Frame>();
            }

            // TODO: deal with null
            // Add current codecAction in the session
            Dictionary <int, ICodecAction> SessionCodecActionDic = (Dictionary <int, ICodecAction>)Session[ConstantCodecActionDic];

            SessionCodecActionDic[layer] = _rfxPDecode;

            var          EncodeType    = layer == 0 ? CodecToolSet.Core.EncodedTileType.EncodedType.FirstPass : CodecToolSet.Core.EncodedTileType.EncodedType.UpgradePass;
            ICodecAction rlgrSRLDecode = _rfxPDecode.SubActions.SingleOrDefault(c => c.Name.Equals(Constants.PDECODE_NAME_RLGRSRLDECODE));

            rlgrSRLDecode.Parameters[Constants.PARAM_NAME_ENCODED_TILE_TYPE] = new CodecToolSet.Core.EncodedTileType {
                Type = EncodeType
            };

            QuantizationFactorsArray progQuant          = progQuantarray.ProgQuants[layer];
            ICodecAction             progDeQuantization = _rfxPDecode.SubActions.SingleOrDefault(c => c.Name.Equals(Constants.PDECODE_NAME_PROGRESSIVEDEQUANTIZATION));

            progDeQuantization.Parameters[Constants.PARAM_NAME_PROGRESSIVE_QUANTS] = progQuant;

            ICodecAction subbandDiffing = _rfxPDecode.SubActions.SingleOrDefault(c => c.Name.Equals(Constants.PDECODE_NAME_SUBBANDDIFFING));
            Frame        preframe       = (Frame)Session[DecodePreviousFrame];

            subbandDiffing.Parameters[Constants.PARAM_NAME_PREVIOUS_FRAME]    = preframe;
            subbandDiffing.Parameters[Constants.PARAM_NAME_ENCODED_TILE_TYPE] = new CodecToolSet.Core.EncodedTileType {
                Type = EncodeType
            };

            ICodecAction DeQuantization = _rfxPDecode.SubActions.SingleOrDefault(c => c.Name.Equals(Constants.PDECODE_NAME_DEQUANTIZATION));

            // deal with some intermediate parameters
            if (layer >= 1)
            {
                rlgrSRLDecode.Parameters[Constants.PARAM_NAME_DAS] = new Frame {
                    Tile = (Tile)Session[ConstantDAS]
                };
                rlgrSRLDecode.Parameters[Constants.PARAM_NAME_PREVIOUS_PROGRESSIVE_QUANTS] = progQuantarray.ProgQuants[layer - 1];
                rlgrSRLDecode.Parameters[Constants.PARAM_NAME_PROGRESSIVE_QUANTS]          = progQuantarray.ProgQuants[layer];
            }

            return(_rfxPDecode);
        }
        public async Task <IActionResult> IndexWithInputs()
        {
            try
            {
                using (var bodyStream = new StreamReader(Request.Body))
                {
                    var bodyText = await bodyStream.ReadToEndAsync();

                    dynamic obj = JsonConvert.DeserializeObject(bodyText);

                    foreach (var input in obj)
                    {
                        // TODO: refine this
                        if (input != null && input.Inputs != null)
                        {
                            int layer = JsonHelper.CastTo <int>(input.Layer);
                            if (layer == 0)
                            {
                                // TODO: refine this
                                // retrieve the quant
                                var quantArray = JsonHelper.RetrieveQuantsArray(input.Params.QuantizationFactorsArray);
                                // retrive the progressive quants
                                var progQuantList = new List <QuantizationFactorsArray>();
                                foreach (var layerQuant in input.Params.ProgQuantizationArray)
                                {
                                    var layerQuants = JsonHelper.RetrieveQuantsArray(layerQuant);
                                    progQuantList.Add(layerQuants);
                                }
                                var progQuantarray = new ProgressiveQuantizationFactors
                                {
                                    ProgQuants = progQuantList
                                };

                                EntropyAlgorithm     algorithm            = JsonHelper.CastTo <EntropyAlgorithm>(input.Params.EntropyAlgorithm);
                                UseDifferenceTile    useDifferenceTile    = JsonHelper.CastTo <UseDifferenceTile>(input.Params.UseDifferenceTile);
                                UseReduceExtrapolate useReduceExtrapolate = JsonHelper.CastTo <UseReduceExtrapolate>(input.Params.UseReduceExtrapolate);

                                _viewModel = new RFXPDecodeViewModel(0);
                                ((RFXPDecodeViewModel)_viewModel).ProvideParam(quantArray, progQuantarray, algorithm, useDifferenceTile, useReduceExtrapolate);

                                JArray jsonInputs = JArray.Parse(input["Inputs"].ToString());
                                ((RFXPDecodeViewModel)_viewModel).ProvidePanelInputs(layer, jsonInputs[0].TrimEnter(), jsonInputs[1].TrimEnter(), jsonInputs[2].TrimEnter());

                                this.HttpContext.Session.SetObject(ModelKey, _viewModel);
                                this.HttpContext.Session.SetObject(isPreFrameValid, true);
                            }

                            Decode(input);
                            // Updates Decode Status
                            await UpdateDecodeStatus(layer);
                        }
                    }
                }

                this.HttpContext.Session.SetObject(IsValid, true);
                return(Json(ReturnResult <string> .Success("Success")));
            }
            catch (Exception ex)
            {
                return(Json(ReturnResult <string> .Fail(ex.Message)));
            }
        }