Exemplo n.º 1
0
 /// <summary>
 /// returns colors from the given filename within the test directory
 /// </summary>
 public static Color[] GetColors(string filename)
 {
     using (var tex = new TextureArray2D(IO.LoadImage(Directory + filename)))
     {
         return(tex.GetPixelColors(LayerMipmapSlice.Mip0));
     }
 }
Exemplo n.º 2
0
        private void CompareAfterExport(string inputImage, string outputImage, string outputExtension, GliFormat format, Color.Channel channels = Color.Channel.Rgb, float tolerance = 0.01f)
        {
            var model = new Models(1);

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

            model.Export.Quality = 100;

            model.Export.Export(origTex, new ExportDescription(outputImage, outputExtension, model.Export)
            {
                FileFormat = format
            });
            var expTex = new TextureArray2D(IO.LoadImage(outputImage + "." + outputExtension));

            for (int curLayer = 0; curLayer < origTex.NumLayers; ++curLayer)
            {
                for (int curMipmap = 0; curMipmap < origTex.NumMipmaps; ++curMipmap)
                {
                    var origColors = origTex.GetPixelColors(curLayer, curMipmap);
                    var expColor   = expTex.GetPixelColors(curLayer, curMipmap);

                    TestData.CompareColors(origColors, expColor, channels, tolerance);
                }
            }
        }
Exemplo n.º 3
0
        public static void CompareWithSmall(DllImageData image, Color.Channel channels)
        {
            var tex    = new TextureArray2D(image);
            var colors = tex.GetPixelColors(LayerMipmapSlice.Mip0);

            CompareWithSmall(colors, channels);
        }
Exemplo n.º 4
0
        public void FastGaussTest()
        {
            // filter kernel:
            // 1 2 1
            // 2 4 2
            // 1 2 1

            var s   = new FastGaussShader();
            var img = IO.LoadImageTexture(TestData.Directory + "small.pfm");
            var dst = new TextureArray2D(LayerMipmapCount.One, img.Size, Format.R32G32B32A32_Float, true);

            s.Run(img, dst, 0, false, new UploadBuffer(256));

            var src = img.GetPixelColors(LayerMipmapSlice.Mip0);
            var res = dst.GetPixelColors(LayerMipmapSlice.Mip0);

            Assert.AreEqual(3 * 3, res.Length);

            // expected values calculated by hand
            float midR = (src[0].Red + src[1].Red * 2.0f + src[2].Red
                          + src[3].Red * 2.0f + src[4].Red * 4.0f + src[5].Red * 2.0f
                          + src[6].Red + src[7].Red * 2.0f + src[8].Red) / 16.0f;
            float midG = (src[0].Green + src[1].Green * 2.0f + src[2].Green
                          + src[3].Green * 2.0f + src[4].Green * 4.0f + src[5].Green * 2.0f
                          + src[6].Green + src[7].Green * 2.0f + src[8].Green) / 16.0f;
            var midColor = new Color(midR, midG, 1.0f);

            float firstR = (src[0].Red * 4.0f + src[1].Red * 2.0f
                            + src[3].Red * 2.0f + src[4].Red) / 9.0f;
            var firstColor = new Color(firstR);

            Assert.IsTrue(res[4].Equals(midColor, Color.Channel.R | Color.Channel.G));
            Assert.IsTrue(res[0].Equals(firstColor, Color.Channel.R));
        }
Exemplo n.º 5
0
        public static void CompareWithSmall(Image image, Color.Channel channels)
        {
            var tex    = new TextureArray2D(image);
            var colors = tex.GetPixelColors(0, 0);

            CompareWithSmall(colors, channels);
        }
Exemplo n.º 6
0
        public void SSIMMultiscaleLuminance()
        {
            var s       = new MultiscaleSSIMShader();
            var toRed   = new ImageFramework.Model.Shader.TransformShader("return value.r;", "float4", "float");
            var fromRed = new ImageFramework.Model.Shader.TransformShader("return float4(value, value, value, 1.0);", "float", "float4");
            var tex     = IO.LoadImageTexture(TestData.Directory + "checkers.dds");
            var redTex  = new TextureArray2D(tex.LayerMipmap, tex.Size, Format.R32_Float, true);
            var dstTex  = new TextureArray2D(tex.LayerMipmap, tex.Size, Format.R32G32B32A32_Float, true);
            var upload  = new UploadBuffer(256);

            Assert.AreEqual(tex.NumMipmaps, 3);
            var expected = tex.GetPixelColors(LayerMipmapSlice.Mip2)[0];

            // copy checkers red channel only
            foreach (var lm in tex.LayerMipmap.Range)
            {
                toRed.Run(tex, redTex, lm, upload);
            }

            // this should copy lowest resolution mipmap to first mipmap
            s.RunCopy(redTex, LayerMipmapSlice.Mip0, upload);

            // copy back
            foreach (var lm in tex.LayerMipmap.Range)
            {
                fromRed.Run(redTex, dstTex, lm, upload);
            }

            var actual = dstTex.GetPixelColors(LayerMipmapSlice.Mip0);

            foreach (var color in actual)
            {
                Assert.IsTrue(color.Equals(expected, Color.Channel.R, 0.0011f));
            }
        }
        public void Cropping()
        {
            var tex = new TextureArray2D(IO.LoadImage(TestData.Directory + "checkers.dds"));

            TestData.TestCheckersLevel0(tex.GetPixelColors(LayerMipmapSlice.Mip0));
            TestData.TestCheckersLevel1(tex.GetPixelColors(LayerMipmapSlice.Mip1));
            TestData.TestCheckersLevel2(tex.GetPixelColors(LayerMipmapSlice.Mip2));

            var newTex = shader.Convert(tex, Format.R8G8B8A8_UNorm_SRgb, new LayerMipmapRange(-1, 0), 1.0f, true, new Size3(1, 1, 0), new Size3(2, 2), Size3.Zero, models.Scaling);

            Assert.AreEqual(1, newTex.NumMipmaps);
            Assert.AreEqual(2, newTex.Size.Width);
            Assert.AreEqual(2, newTex.Size.Height);

            // should be the same as first mipmap level
            TestData.TestCheckersLevel1(newTex.GetPixelColors(LayerMipmapSlice.Mip0));
        }
        public void ExtractMipmap()
        {
            var tex = new TextureArray2D(IO.LoadImage(TestData.Directory + "checkers.dds"));

            Assert.AreEqual(3, tex.NumMipmaps);
            TestData.TestCheckersLevel0(tex.GetPixelColors(LayerMipmapSlice.Mip0));
            TestData.TestCheckersLevel1(tex.GetPixelColors(LayerMipmapSlice.Mip1));
            TestData.TestCheckersLevel2(tex.GetPixelColors(LayerMipmapSlice.Mip2));

            var newTex = shader.Convert(tex, Format.R8G8B8A8_UNorm_SRgb, models.Scaling, 1);

            Assert.AreEqual(1, newTex.NumMipmaps);
            Assert.AreEqual(2, newTex.Size.Width);
            Assert.AreEqual(2, newTex.Size.Height);

            TestData.TestCheckersLevel1(newTex.GetPixelColors(LayerMipmapSlice.Mip0));
        }
Exemplo n.º 9
0
        public void TestMipmaps()
        {
            // load checkers texture
            var tex = new TextureArray2D(IO.LoadImage(TestData.Directory + "checkers.dds"));

            Assert.AreEqual(3, tex.NumMipmaps);
            Assert.AreEqual(4, tex.Size.Width);
            Assert.AreEqual(4, tex.Size.Height);
            TestData.TestCheckersLevel0(tex.GetPixelColors(0, 0));
            TestData.TestCheckersLevel1(tex.GetPixelColors(0, 1));
            TestData.TestCheckersLevel2(tex.GetPixelColors(0, 2));

            // remove mipmaps
            tex = tex.CloneWithoutMipmapsT();
            Assert.AreEqual(1, tex.NumMipmaps);
            Assert.AreEqual(4, tex.Size.Width);
            Assert.AreEqual(4, tex.Size.Height);
            TestData.TestCheckersLevel0(tex.GetPixelColors(0, 0));

            // generate mipmaps again
            tex = tex.GenerateMipmapLevelsT(3);
            Assert.AreEqual(3, tex.NumMipmaps);
            Assert.AreEqual(4, tex.Size.Width);
            Assert.AreEqual(4, tex.Size.Height);
            TestData.TestCheckersLevel0(tex.GetPixelColors(0, 0));
            TestData.TestCheckersLevel1(tex.GetPixelColors(0, 1));
            TestData.TestCheckersLevel2(tex.GetPixelColors(0, 2));
        }
Exemplo n.º 10
0
        public void MitchelXYScale()
        {
            var shader   = new MitchellNetravaliScaleShader(new QuadShader(), new UploadBuffer(256));
            var checkers = new TextureArray2D(IO.LoadImage(TestData.Directory + "sphere.png"));

            var res = shader.Run(checkers, new Size3(20, 40));

            var reference = new TextureArray2D(IO.LoadImage(TestData.Directory + "sphere_scaled.png"));

            // compare with gimp reference
            TestData.CompareColors(reference.GetPixelColors(0, 0), res.GetPixelColors(0, 0));
        }
Exemplo n.º 11
0
        public void MitchelUpscale()
        {
            var models   = new Models(1);
            var shader   = new MitchellNetravaliScaleShader(new QuadShader(), new UploadBuffer(256));
            var checkers = new TextureArray2D(IO.LoadImage(TestData.Directory + "sphere.png"));

            var res = shader.Run(checkers, new Size3(62, 31), models.Scaling);

            var reference = new TextureArray2D(IO.LoadImage(TestData.Directory + "sphere_up.png"));

            // compare with gimp reference
            TestData.CompareColors(reference.GetPixelColors(LayerMipmapSlice.Mip0), res.GetPixelColors(LayerMipmapSlice.Mip0));
        }
Exemplo n.º 12
0
        public void ExportCroppedWithMipmaps()
        {
            var model = new Models(1);

            model.AddImageFromFile(TestData.Directory + "checkers.dds");
            model.Export.Mipmap      = -1;
            model.Export.UseCropping = true;
            model.Export.CropStartX  = 1;
            model.Export.CropEndX    = 2;
            model.Export.CropStartY  = 1;
            model.Export.CropEndY    = 2;
            model.Apply();

            model.ExportPipelineImage(ExportDir + "cropped", "dds", GliFormat.RGBA8_SRGB);
            var newTex = new TextureArray2D(IO.LoadImage(ExportDir + "cropped.dds"));

            Assert.AreEqual(2, newTex.Size.Width);
            Assert.AreEqual(2, newTex.Size.Height);
            Assert.AreEqual(2, newTex.NumMipmaps);

            TestData.TestCheckersLevel1(newTex.GetPixelColors(0, 0));
            TestData.TestCheckersLevel2(newTex.GetPixelColors(0, 1));
        }
Exemplo n.º 13
0
        public void ExportCroppedWithMipmaps()
        {
            var model = new Models(1);

            model.AddImageFromFile(TestData.Directory + "checkers.dds");
            model.Apply();

            model.Export.Export(new ExportDescription(model.Pipelines[0].Image, ExportDir + "cropped", "dds")
            {
                FileFormat  = GliFormat.RGBA8_SRGB,
                Mipmap      = -1,
                UseCropping = true,
                CropStart   = new Size3(1, 1, 0).ToCoords(model.Images.Size),
                CropEnd     = new Size3(2, 2, 0).ToCoords(model.Images.Size)
            });
            var newTex = new TextureArray2D(IO.LoadImage(ExportDir + "cropped.dds"));

            Assert.AreEqual(2, newTex.Size.Width);
            Assert.AreEqual(2, newTex.Size.Height);
            Assert.AreEqual(2, newTex.NumMipmaps);

            TestData.TestCheckersLevel1(newTex.GetPixelColors(LayerMipmapSlice.Mip0));
            TestData.TestCheckersLevel2(newTex.GetPixelColors(LayerMipmapSlice.Mip1));
        }
Exemplo n.º 14
0
        public void DrawCheckers()
        {
            var img = new TextureArray2D(LayerMipmapCount.One, new Size3(4, 4), Format.R32G32B32A32_Float, false);

            // create checkers texture
            var d2d = new Direct2D(img);

            using (var c = d2d.Begin())
            {
                c.Clear(Colors.White);
                c.FillRectangle(Float2.Zero, new Float2(2.0f), Colors.Black);
                c.FillRectangle(new Float2(2.0f), new Float2(4.0f), Colors.Black);
            }

            var colors = img.GetPixelColors(LayerMipmapSlice.Mip0);

            TestData.TestCheckersLevel0(colors);
        }
Exemplo n.º 15
0
        public void ExportCroppedImage()
        {
            var model = new Models(1);

            model.AddImageFromFile(TestData.Directory + "checkers.dds");
            model.Export.Mipmap      = 0;
            model.Export.UseCropping = true;
            model.Export.CropStartX  = 1;
            model.Export.CropStartY  = 1;
            model.Export.CropEndX    = 2;
            model.Export.CropEndY    = 2;
            model.Apply();

            model.ExportPipelineImage(ExportDir + "cropped", "dds", GliFormat.RGBA8_SRGB);
            var newTex = new TextureArray2D(IO.LoadImage(ExportDir + "cropped.dds"));

            TestData.TestCheckersLevel1(newTex.GetPixelColors(0, 0));
        }
Exemplo n.º 16
0
        public void DrawText()
        {
            var img = new TextureArray2D(LayerMipmapCount.One, new Size3(16, 16), Format.R8G8B8A8_UNorm_SRgb, false);
            var d2d = new Direct2D(img);

            using (var c = d2d.Begin())
            {
                c.Clear(Colors.White);
                c.Text(Float2.Zero, new Float2(16.0f, 16.0f), 16.0f, Colors.Black, "a!");
            }

            // compare with reference
            var orig       = IO.LoadImageTexture(TestData.Directory + "a.png");
            var origColors = orig.GetPixelColors(LayerMipmapSlice.Mip0);
            var newColors  = img.GetPixelColors(LayerMipmapSlice.Mip0);

            TestData.CompareColors(origColors, newColors);
        }
Exemplo n.º 17
0
        public void LoadKtx2()
        {
            var tex = new TextureArray2D(IO.LoadImage(TestData.KtxDirectory + "rgb-mipmap-reference-u.ktx2"));

            Assert.AreEqual(tex.NumMipmaps, 7);
            Assert.AreEqual(tex.Size.Width, 64);
            Assert.AreEqual(tex.Size.Height, 64);

            // test colors
            Assert.IsTrue(tex.GetPixelColors(LayerMipmapSlice.Mip0)[0].Equals(new Color(1.0f, 0.0f, 0.0f), Color.Channel.Rgb));
            Assert.IsTrue(tex.GetPixelColors(LayerMipmapSlice.Mip1)[0].Equals(new Color(1.0f, 0.175f, 0.0f), Color.Channel.Rgb));
            Assert.IsTrue(tex.GetPixelColors(LayerMipmapSlice.Mip2)[0].Equals(new Color(1.0f, 1.0f, 0.0f), Color.Channel.Rgb));
            Assert.IsTrue(tex.GetPixelColors(LayerMipmapSlice.Mip3)[0].Equals(new Color(0.0f, 1.0f, 0.0f), Color.Channel.Rgb));
            Assert.IsTrue(tex.GetPixelColors(LayerMipmapSlice.Mip4)[0].Equals(new Color(0.0f, 0.0f, 1.0f), Color.Channel.Rgb));
            Assert.IsTrue(tex.GetPixelColors(LayerMipmapSlice.Mip5)[0].Equals(new Color(0.0f, 1.0f, 1.0f), Color.Channel.Rgb));
            Assert.IsTrue(tex.GetPixelColors(LayerMipmapSlice.Mip6)[0].Equals(new Color(1.0f, 0.0f, 1.0f), Color.Channel.Rgb));
        }
Exemplo n.º 18
0
        public void SmallImage()
        {
            var tex       = new TextureArray2D(IO.LoadImage(TestData.Directory + "small.pfm"));
            var refColors = tex.GetPixelColors(0, 0);

            // recreate colors by picking them with the shader
            var shader = new PixelValueShader(new SharedModel());
            var colors = new Color[refColors.Length];

            for (int y = 0; y < tex.Size.Height; ++y)
            {
                for (int x = 0; x < tex.Size.Width; ++x)
                {
                    colors[y * tex.Size.Width + x] = shader.Run(tex, new Size3(x, y, 0), 0, 0, 0);
                }
            }

            TestData.CompareColors(refColors, colors, Color.Channel.Rgb);
        }
        public void RunSmall()
        {
            var s   = new TransformShader("return float4(value.rgb * 2.0, value.a)", "float4", "float4");
            var img = IO.LoadImageTexture(TestData.Directory + "small.pfm");

            var dst = new TextureArray2D(img.LayerMipmap, img.Size, Format.R32G32B32A32_Float, true);

            s.Run(img, dst, LayerMipmapSlice.Mip0, new UploadBuffer(256));

            var expected = img.GetPixelColors(LayerMipmapSlice.Mip0);

            for (var index = 0; index < expected.Length; index++)
            {
                expected[index].Red   *= 2.0f;
                expected[index].Green *= 2.0f;
                expected[index].Blue  *= 2.0f;
            }

            var actual = dst.GetPixelColors(LayerMipmapSlice.Mip0);

            TestData.CompareColors(expected, actual, Color.Channel.Rgba);
        }
Exemplo n.º 20
0
        private void CompareAfterExport(string inputImage, string outputImage, string outputExtension, GliFormat format, Color.Channel channels = Color.Channel.Rgb, float tolerance = 0.01f)
        {
            var model = new Models(1);

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

            model.Export.Export(new ExportDescription(origTex, outputImage, outputExtension)
            {
                FileFormat = format,
                Quality    = 100
            });
            var expTex = new TextureArray2D(IO.LoadImage(outputImage + "." + outputExtension));

            foreach (var lm in origTex.LayerMipmap.Range)
            {
                var origColors = origTex.GetPixelColors(lm);
                var expColor   = expTex.GetPixelColors(lm);

                TestData.CompareColors(origColors, expColor, channels, tolerance);
            }
        }
Exemplo n.º 21
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);
            }
        }