Exemplo n.º 1
0
        private void TryExportAllFormats(string inputImage, string outputImage, string outputExtension, FormatFilter filter = FormatFilter.All)
        {
            var model = new Models(1);

            model.AddImageFromFile(inputImage);
            model.Apply();
            var tex = (TextureArray2D)model.Pipelines[0].Image;

            var    eFmt      = ExportDescription.GetExportFormat(outputExtension);
            string errors    = "";
            int    numErrors = 0;

            foreach (var format in eFmt.Formats)
            {
                if (format.IsCompressed())
                {
                    if ((FormatFilter.Compressed & filter) == 0)
                    {
                        continue;
                    }
                }
                else if ((FormatFilter.Uncompressed & filter) == 0)
                {
                    continue;
                }

                try
                {
                    var desc = new ExportDescription(tex, outputImage, outputExtension);
                    desc.FileFormat = format;
                    model.Export.Export(desc);
                }
                catch (Exception e)
                {
                    errors += $"{format}: {e.Message}\n";
                    ++numErrors;
                }
            }

            if (errors.Length > 0)
            {
                throw new Exception($"export failed for {numErrors}/{eFmt.Formats.Count} formats:\n" + errors);
            }
        }
Exemplo n.º 2
0
        private void TryExportAllFormatsAndCompareGray(string outputExtension, bool onlySrgb = false)
        {
            var model = new Models(2);

            model.AddImageFromFile(TestData.Directory + "gray.png");
            model.Apply();
            var tex = (TextureArray2D)model.Pipelines[0].Image;

            var eFmt = ExportDescription.GetExportFormat(outputExtension);

            string errors    = "";
            int    numErrors = 0;

            var lastTexel = tex.Size.Product - 1;

            Color[] colors = null;
            var     i      = 0;

            foreach (var format in eFmt.Formats)
            {
                if (onlySrgb && format.GetDataType() != PixelDataType.Srgb)
                {
                    continue;
                }
                try
                {
                    int numTries = 0;
                    while (true)
                    {
                        try
                        {
                            var integerPrecision = IsIntegerPrecisionFormat(format);
                            var desc             = new ExportDescription(tex, ExportDir + "gray" + ++i, outputExtension);
                            desc.FileFormat = format;
                            desc.Quality    = 100;
                            if (integerPrecision)
                            {
                                desc.Multiplier = 100.0f;
                            }

                            model.Export.Export(desc);
                            Thread.Sleep(1);

                            // load and compare gray tone
                            using (var newTex = new TextureArray2D(IO.LoadImage($"{ExportDir}gray{i}.{outputExtension}")))
                            {
                                Assert.AreEqual(8, newTex.Size.Width);
                                Assert.AreEqual(4, newTex.Size.Height);
                                colors = newTex.GetPixelColors(LayerMipmapSlice.Mip0);
                                // compare last texel
                                var grayColor = colors[lastTexel];

                                float tolerance = 0.01f;
                                if (format.IsLessThan8Bit())
                                {
                                    tolerance = 0.1f;
                                }

                                // some formats don't write to red
                                // ReSharper disable once CompareOfFloatsByEqualityOperator
                                //if(grayColor.Red != 0.0f) Assert.AreEqual(TestData.Gray, grayColor.Red, tolerance);
                                if (integerPrecision)
                                {
                                    Assert.AreEqual(TestData.Gray * 100.0f, grayColor.Red, 1.0f);
                                }
                                else
                                {
                                    Assert.AreEqual(TestData.Gray, grayColor.Red, tolerance);
                                }
                                //else Assert.AreEqual(TestData.Gray, grayColor.Green, tolerance);
                                break;
                            }
                        }
                        catch (Exception)
                        {
                            ++numTries;
                            if (numTries > 3)
                            {
                                throw;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    errors += $"{format.ToString()}: {e.Message}\n";
                    ++numErrors;
                }
            }

            if (errors.Length > 0)
            {
                throw new Exception($"gray comparision failed for {numErrors}/{eFmt.Formats.Count} formats:\n" + errors);
            }
        }
Exemplo n.º 3
0
        public void Dds3DDirectXCompability()
        {
            var model = new Models(1);

            model.AddImageFromFile(TestData.Directory + "checkers3d.dds");
            model.Apply();
            var origTex    = (Texture3D)model.Pipelines[0].Image;
            var origColors = origTex.GetPixelColors(LayerMipmapSlice.Mip0);

            Color[] newColors = null;

            // colors multiplied by 100 for integer precision formats
            model.Pipelines[0].Color.Formula = "I0 * 100";
            model.Apply();
            var integerTex    = (Texture3D)model.Pipelines[0].Image;
            var origColors100 = integerTex.GetPixelColors(LayerMipmapSlice.Mip0);

            var    eFmt      = ExportDescription.GetExportFormat("dds");
            string errors    = "";
            int    numErrors = 0;
            int    nFormats  = 0;

            foreach (var format in eFmt.Formats)
            {
                if (format.IsExcludedFrom3DExport())
                {
                    continue;
                }
                nFormats++;
                try
                {
                    // export to dds
                    var isIntegerPrecision = IsIntegerPrecisionFormat(format);
                    var desc = new ExportDescription(origTex, ExportDir + "tmp", "dds");
                    desc.FileFormat = format;
                    desc.Quality    = 100;
                    if (isIntegerPrecision)
                    {
                        desc.Multiplier = 100.0f;
                    }
                    model.Export.Export(desc);

                    // load with directx dds loader
                    DDSTextureLoader.CreateDDSTextureFromFile(Device.Get().Handle, Device.Get().ContextHandle, ExportDir + "tmp.dds",
                                                              out var resource, out var resourceView, 0, out var alphaMode);

                    // convert to obtain color data
                    using (var newTex = model.SharedModel.Convert.ConvertFromRaw3D(resourceView, origTex.Size, Format.R32G32B32A32_Float, isIntegerPrecision))
                    {
                        resourceView.Dispose();
                        resource.Dispose();

                        newColors = newTex.GetPixelColors(LayerMipmapSlice.Mip0);
                        // only compare with red channel since some formats only store red
                        if (isIntegerPrecision)
                        {
                            TestData.CompareColors(origColors100, newColors, Color.Channel.R, 1.0f);
                        }
                        else
                        {
                            TestData.CompareColors(origColors, newColors, Color.Channel.R, 0.1f);
                        }
                    }
                }
                catch (Exception e)
                {
                    errors += $"{format}: {e.Message}\n";
                    ++numErrors;
                }
            }

            if (errors.Length > 0)
            {
                throw new Exception($"directX compability failed for {numErrors}/{nFormats} formats:\n" + errors);
            }
        }
Exemplo n.º 4
0
        public ExportViewModel(ModelsEx models, string extension, GliFormat preferredFormat, string filename, bool is3D, DefaultStatistics stats)
        {
            this.models                = models;
            this.extension             = extension;
            this.filename              = filename;
            this.is3D                  = is3D;
            this.usedFormat            = ExportDescription.GetExportFormat(extension);
            models.Display.IsExporting = true;

            // init layers
            for (var i = 0; i < models.Images.NumLayers; ++i)
            {
                AvailableLayers.Add(new ListItemViewModel <int>
                {
                    Cargo = i,
                    Name  = "Layer " + i
                });
            }

            models.ExportConfig.Layer = models.Display.ActiveLayer;
            selectedLayer             = AvailableLayers[models.Display.ActiveLayer];
            Debug.Assert(selectedLayer.Cargo == models.Display.ActiveLayer);

            // init mipmaps
            for (var i = 0; i < models.Images.NumMipmaps; ++i)
            {
                AvailableMipmaps.Add(new ListItemViewModel <int>
                {
                    Cargo = i,
                    Name  = "Mipmap " + i
                });
            }

            models.ExportConfig.Mipmap = models.Display.ActiveMipmap;
            selectedMipmap             = AvailableMipmaps[models.Display.ActiveMipmap];
            Debug.Assert(selectedMipmap.Cargo == models.Display.ActiveMipmap);

            // all layer option for ktx and dds
            if (models.Images.NumLayers > 1 && (extension == "ktx" || extension == "dds"))
            {
                AvailableLayers.Add(new ListItemViewModel <int>
                {
                    Cargo = -1,
                    Name  = "All Layer"
                });
                selectedLayer             = AvailableLayers.Last();
                models.ExportConfig.Layer = -1;
            }

            // all mipmaps option for ktx and dds
            if (models.Images.NumMipmaps > 1 && (extension == "ktx" || extension == "dds"))
            {
                AvailableMipmaps.Add(new ListItemViewModel <int>
                {
                    Cargo = -1,
                    Name  = "All Mipmaps"
                });
                selectedMipmap             = AvailableMipmaps.Last();
                models.ExportConfig.Mipmap = -1;
            }

            // init available pixel data types
            var usedPixelTypes = new SortedSet <PixelDataType>();

            foreach (var format in usedFormat.Formats)
            {
                // exclude some formats for 3d export
                if (is3D && format.IsExcludedFrom3DExport())
                {
                    continue;
                }

                allFormats.Add(new ListItemViewModel <GliFormat>
                {
                    Cargo   = format,
                    Name    = format.ToString(),
                    ToolTip = format.GetDescription()
                });
                formatRatings.Add(stats.GetFormatRating(format, preferredFormat));
                usedPixelTypes.Add(format.GetDataType());
            }

            if (usedPixelTypes.Count > 1)
            {
                AvailableDataTypes.Add(new ListItemViewModel <PixelDataType>
                {
                    Cargo = PixelDataType.Undefined,
                    Name  = "All"
                });
            }
            var preferredPixelType = preferredFormat.GetDataType();

            foreach (var usedPixelType in usedPixelTypes)
            {
                AvailableDataTypes.Add(new ListItemViewModel <PixelDataType>
                {
                    Cargo   = usedPixelType,
                    Name    = usedPixelType.ToString(),
                    ToolTip = usedPixelType.GetDescription()
                });
                if (usedPixelType == preferredPixelType)
                {
                    SelectedDataType = AvailableDataTypes.Last();
                }
            }

            if (SelectedDataType == null)
            {
                SelectedDataType = AvailableDataTypes[0];
            }

            // assert that those were were set by SelectedDataType
            Debug.Assert(AvailableFormats != null);
            Debug.Assert(SelectedFormat != null);

            // enable quality
            if (extension == "jpg")
            {
                hasQualityValue       = true;
                nonSrgbExportWarnings = true;
            }
            else if (extension == "bmp")
            {
                nonSrgbExportWarnings = true;
            }
            else
            {
                SetKtxDdsQuality();
            }

            models.ExportConfig.PropertyChanged += ExportConfigOnPropertyChanged;
            models.Settings.PropertyChanged     += SettingsOnPropertyChanged;

            if (models.ExportConfig.CropEnd == Float3.Zero)
            {
                // assume cropping was not set
                SetMaxCropping();
            }
        }