Пример #1
0
        protected internal static void Fidct(float[,] bloque, float[,] bloqueDct, int tamX, int tamY)
        {
            var bloqueS = new ArraySlice<float>(bloque);

            // Sacamos el IDCT de cada fila del bloque
            for (int y = 0; y < tamY; y++)
            {
                Ifct8(bloqueS.GetSlice(y), coefYUS.GetSlice(y), tCosXU);
            }

            Common.Transpose(coefYU, coefUY, tamX, tamY);

            // Ahora sacamos el DCT por columna de los resultados anteriores
            for (int u = 0; u < tamX; u++)
            {
                Ifct8(coefUYS.GetSlice(u), coefUVS.GetSlice(u), tCosYV);
            }

            for (int v = 0; v < tamY; v++)
            {
                for (int u = 0; u < tamX; u++)
                {
                    bloqueDct[v, u] = (float)Math.Round(coefUV[u, v]);
                }
            }
        }
Пример #2
0
        public void ReverseWithNegativeStep()
        {
            var a = new ArraySlice <char>("Stanley Yelnats".ToCharArray());

            // is it reversed?
            Assert.AreEqual("stanleY yelnatS", string.Join("", a.GetSlice("::-1")));
        }
Пример #3
0
        public virtual void GenerateClass(ApiClass api, CodeWriter s)
        {
            GenerateUsings(s);
            s.AppendLine($"namespace {NameSpace}");
            s.Block(() =>
            {
                var names          = new ArraySlice <string>(api.ClassName.Split('.'));
                var static_classes = names.GetSlice(new Slice(0, names.Length - 1));
                var class_name     = names.Last();
                int levels         = names.Length - 1;
                if (levels > 0)
                {
                    foreach (var name in static_classes)
                    {
                        s.Out($"public static partial class {EscapeName(name)} {{");
                        s.Indent();
                    }
                }
                GenerateDocString(api, s);
                s.Out($"public partial class {EscapeName(class_name)} : {EscapeName(api.BaseClass)}");
                s.Block(() =>
                {
                    s.Out($"// auto-generated class");
                    s.Break();
                    s.Out($"public {EscapeName(class_name)}(PyObject pyobj) : base(pyobj) {{ }}");
                    s.Break();
                    s.Out($"public {EscapeName(class_name)}({EscapeName(api.BaseClass)} other) : base(other.PyObject as PyObject) {{ }}");
                    s.Break();

                    // additional constructors
                    foreach (var func in api.Constructors)
                    {
                        try
                        {
                            if (func.ManualOverride || func.Ignore)
                            {
                                continue;
                            }
                            func.Sanitize();
                            func.IsConstructor = true;
                            func.ClassName     = string.Join(".", static_classes);
                            func.Name          = class_name;
                            var arguments      = GenerateArguments(func);
                            //var passed_args = GeneratePassedArgs(func);
                            s.Out($"public {EscapeName(class_name)}({arguments})");
                            s.Block(() =>
                            {
                                GenerateFunctionBody(func, s);
                            });
                        }
                        catch (Exception e)
                        {
                            s.Out("// Error generating constructor");
                            s.Out("// Message: " + e.Message);
                            s.Out("/*");
                            s.Out(e.StackTrace);
                            s.Out("----------------------------");
                            s.Out("Declaration JSON:");
                            s.Out(JObject.FromObject(func).ToString(Formatting.Indented));
                            s.Out("*/");
                        }
                    }
                    // functions
                    s.Break();
                    foreach (var decl in api.Declarations)
                    {
                        try
                        {
                            if (decl.ManualOverride || decl.Ignore)
                            {
                                continue;
                            }
                            GenerateApiFunction(decl, s);
                        }
                        catch (Exception e)
                        {
                            s.Out("// Error generating delaration: " + decl.Name);
                            s.Out("// Message: " + e.Message);
                            s.Out("/*");
                            s.Out(e.StackTrace);
                            s.Out("----------------------------");
                            s.Out("Declaration JSON:");
                            s.Out(JObject.FromObject(decl).ToString(Formatting.Indented));
                            s.Out("*/");
                        }
                    }
                });
                if (levels > 0)
                {
                    foreach (var name in static_classes)
                    {
                        s.Outdent();
                        s.Out("}");
                    }
                }
                //if (decl.CommentOut)
                //    s.Out("*/");
                s.Break();
            });
        }
Пример #4
0
        public void ImageProcessingExample()
        {
            var width           = 4; // px
            var height          = 4; // px
            var bytes_per_pixel = 3; // r, g, b

            // note that Slice and Dice uses the row major coordinate system like numpy
            // so the coordinate order is y, x, color in this example
            var shape = new Shape(height, width, bytes_per_pixel);

            // create the raw pixel data using Shape.Size to calculate how long the array
            // must be to hold a 3D volume of h x w x byte_per_pixel
            var raw_bytes = new byte[shape.Size];
            var image     = new ArraySlice <byte>(raw_bytes, shape);

            // we can now access single pixel values by coordinates

            // get the three bytes for the pixel at the upper right corner
            // as ArraySlice<T> without copying any data!
            var pixel = image.GetSlice("0, 3");

            Console.WriteLine("pixel: " + pixel);  // prints: pixel: [0, 0, 0]

            // set the pixel to white
            image.SetValues(new int[] { 0, 3 }, new byte[] { 255, 255, 255 });

            // get the 2 by 2 pixel patch at the center and color it green without copying any data:
            var patch = image.GetSlice("1:3, 1:3, 1"); // this gets only the green channel

            // note how we can now use relative coordinates in the patch
            // since this is a slice of the green channel we can directly set the green byte at location (y,x)
            for (int y = 0; y < 2; y++)
            {
                for (int x = 0; x < 2; x++)
                {
                    patch[y, x] = 255;
                }
            }

            // print the bitmap out row by row like this (black = '#', white = ' ', green= 'o')
            // obviously by using slicing this is a no-brainer
            for (int y = 0; y < height; y++)
            {
                Console.Write("|");
                // Slice.Index gives us a slice-definition for a row at index y.
                // You could also use image.GetSlice($"{y}")
                var row = image.GetSlice(Slice.Index(y));
                for (int x = 0; x < width; x++)
                {
                    var red   = row[x, 0];
                    var green = row[x, 1];
                    if (red == 0 && green == 0)
                    {
                        // must be black
                        Console.Write("#");
                        continue;
                    }
                    if (red == 255 && green == 255)
                    {
                        // must be white
                        Console.Write(" ");
                        continue;
                    }
                    if (green == 255)
                    {
                        Console.Write("o");
                    }
                }
                // at the end of the row, break the line
                Console.WriteLine("|");
            }
        }