Example #1
0
        private void OpenFile(StorageFile file)
        {
            //Add a loading screen
            if (rawImage != null)
            {
                EmptyImage();
            }
            Load.Show();
            ImageSelected = true;
            isBindingEnabled = false;

            Task.Run(async () =>
            {
                try
                {
                    var watchTotal = Stopwatch.StartNew();
                    using (Stream stream = (await file.OpenReadAsync()).AsStreamForRead())
                    {
                        var watchdecode = Stopwatch.StartNew();
                        RawDecoder decoder = RawParser.GetDecoder(stream, file);
                        try
                        {
                            thumbnail = decoder.DecodeThumb();
                            Task.Run(() =>
                            {
                                var result = thumbnail?.GetBitmap();
                                DisplayImage(result, true);
                            });
                        }
                        //since thumbnail are optionnal, we ignore all errors           
                        catch (Exception ex) { }

                        decoder.DecodeRaw();
                        decoder.DecodeMetadata();
                        rawImage = decoder.rawImage;

                        watchdecode.Stop();
                        Debug.WriteLine("Decoding done in " + watchdecode.ElapsedMilliseconds + " ms");

                        var watchLook = Stopwatch.StartNew();
                        rawImage.ApplyTableLookUp();
                        watchLook.Stop();
                        Debug.WriteLine("Lookup done in " + watchLook.ElapsedMilliseconds + " ms");

                        var watchScale = Stopwatch.StartNew();
                        ImageHelper.ScaleValues(rawImage);
                        watchScale.Stop();
                        Debug.WriteLine("Scale done in " + watchScale.ElapsedMilliseconds + " ms");
                    }

                    rawImage.metadata.SetFileMetatdata(file);

                    if (rawImage.isCFA)
                    {
                        var watchDemos = Stopwatch.StartNew();
                        //get the algo from the settings
                        DemosaicAlgorithm algo;
                        try
                        {
                            algo = SettingStorage.DemosAlgo;
                        }
                        catch (Exception)
                        {
                            algo = DemosaicAlgorithm.FastAdams;
                        }
                        Demosaic.Demos(rawImage, algo);

                        watchDemos.Stop();
                        Debug.WriteLine("Demos done in " + watchDemos.ElapsedMilliseconds + " ms");
                    }

                    if (rawImage.convertionM != null)
                    {
                        var watchConvert = Stopwatch.StartNew();
                        rawImage.ConvertRGB();
                        watchConvert.Stop();
                        Debug.WriteLine("ConvertRGB done in " + watchConvert.ElapsedMilliseconds + " ms");
                    }

                    var watchPreview = Stopwatch.StartNew();
                    ImageHelper.CreatePreview(SettingStorage.PreviewFactor, ImageDisplay.ViewportHeight, ImageDisplay.ViewportWidth, rawImage);
                    watchPreview.Stop();
                    Debug.WriteLine("Preview done in " + watchPreview.ElapsedMilliseconds + " ms");

                    watchTotal.Stop();
                    rawImage.metadata.ParsingTime = watchTotal.ElapsedMilliseconds;
                    GC.Collect();
                    //check if enough memory
                    if (MemoryManager.AppMemoryUsageLimit - MemoryManager.AppMemoryUsage < ((ulong)rawImage.fullSize.green.Length * 6) || MemoryManager.AppMemoryUsageLevel == AppMemoryUsageLevel.High)
                    {
                        TextDisplay.DisplayWarning("The image is bigger than what your device support, this application may fail when saving. Only " + ((MemoryManager.AppMemoryUsageLimit - MemoryManager.AppMemoryUsage) / (1024 * 1024)) + "Mb left of memory for this app to use");
                    }
#if !DEBUG
                    //send an event with file extension, camera model and make
                    logger.Log("SuccessOpening " + rawImage?.metadata?.FileExtension.ToLower() + " " + rawImage?.metadata?.Make + " " + rawImage?.metadata?.Model);
#endif
                    DefaultValue.Rotation = rawImage.metadata.OriginalRotation;
                    DefaultValue.ReverseGamma = rawImage.IsGammaCorrected;
                    DefaultValue.RMul = rawImage?.metadata.WbCoeffs?.Red ?? 1;
                    DefaultValue.GMul = rawImage?.metadata.WbCoeffs?.Green ?? 1;
                    DefaultValue.BMul = rawImage?.metadata.WbCoeffs?.Blue ?? 1;
                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        var exif = rawImage.ParseExif();
                        //Group the data
                        var groups = from x in exif group x by x.Group into grp orderby grp.Key select grp;
                        //Set the grouped data to CollectionViewSource
                        ExifSource.Source = groups;
                        ResetControls();
                        UpdatePreview(true);
                        ControlVisibilty.Value = true;
                        SetImageSizeText();
                    });
                    if (rawImage.errors.Count > 0)
                    {
                        var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
                        TextDisplay.DisplayWarning(loader.GetString("ErrorOnLoadWarning"));
#if !DEBUG
                        //send an event with file extension and camera model and make if any                   
                        logger.Log("ErrorOnOpen " + file?.FileType.ToLower() + " " + rawImage?.metadata?.Make + " " + rawImage?.metadata?.Model + "" + rawImage.errors.Count);
#endif
                    }
                    isBindingEnabled = true;
                    thumbnail = null;
                }
                catch (Exception ex)
                {
#if !DEBUG
                    //send an event with file extension and camera model and make if any                   
                    logger.Log("FailOpening " + file?.FileType.ToLower() + " " + rawImage?.metadata?.Make + " " + rawImage?.metadata?.Model);
#endif

                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        EmptyImage();
                    });
                    ImageSelected = false;
                    var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
                    TextDisplay.DisplayError(loader.GetString("ExceptionText"));
                }

                CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    // Blur(false);
                    Load.Hide();
                });
                ImageSelected = false;
            });
        }