public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Action <string, byte[]> action && parameter is string filter)
            {
                return(new TransientCommand(() =>
                {
                    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                    dlg.Filter = filter;

                    if (dlg.ShowDialog() == true)
                    {
                        bool failed = false;
                        try
                        {
                            var bytes = File.ReadAllBytes(dlg.FileName);

                            if (Path.GetExtension(dlg.FileName)?.ToLower() == ".png")
                            {
                                BitmapImage biImg = new BitmapImage();
                                using MemoryStream ms = new MemoryStream(bytes);
                                biImg.BeginInit();
                                biImg.StreamSource = ms;
                                biImg.EndInit();

                                if (biImg.PixelWidth != 512 || biImg.PixelHeight != 512)
                                {
                                    throw new InvalidOperationException($"Wrong LUT size {biImg.PixelWidth}x{biImg.PixelHeight}");
                                }
                            }
                            else
                            {
                                var data = CubeParser.Read(bytes);
                                if (data == null)
                                {
                                    throw new InvalidOperationException("Bad cube format");
                                }
                            }

                            action(dlg.FileName, bytes);
                        }
                        catch (Exception e)
                        {
                            Log.Warning(e, $"Failed to open image file");
                            failed = true;
                        }
                        if (failed)
                        {
                            action(dlg.FileName, null);
                        }
                    }
                }));
            }

            return(null);
        }
Пример #2
0
 public static IDirectXFilterStage Create(DirectXContext dx, byte[] buffer, SingleFrameType type, double amount)
 {
     if (type == SingleFrameType.Cube)
     {
         var cube = CubeParser.Read(buffer);
         if (cube != null)
         {
             var texture1 = cube.Is3D ? null : CreateTexture1D(dx, cube);
             var texture3 = cube.Is3D ? CreateTexture3D(dx, cube) : null;
             return(new DirectXFilterStageLUT(dx, texture1, texture3, amount, cube.Size, CreateConfig(cube.Is3D), cube.DomainMin, cube.DomainMax));
         }
     }
     else if (type == SingleFrameType.Png)
     {
         var texture = CreateTexture3D(dx, buffer);
         return(new DirectXFilterStageLUT(dx, null, texture, amount, 64, CreateConfig(true), new Vector3(0, 0, 0), new Vector3(1, 1, 1)));
     }
     return(null);
 }