Esempio n. 1
0
        public void TestNakedPointer()
        {
            NakedPointer nakedPointer = new NakedPointer(0);

            Assert.True(nakedPointer.IsNull);
            Assert.True(nakedPointer.ToCodePointer <int>().IsNull);
        }
Esempio n. 2
0
        /// <summary>
        /// Reads pixels data into single array of pixels with new stride equal to <paramref name="width"/>.
        /// </summary>
        /// <typeparam name="T">Type of the pixel.</typeparam>
        /// <param name="width">Bitmap width.</param>
        /// <param name="height">Bitmap height.</param>
        /// <param name="data">Pointer to start of bitmap data.</param>
        /// <param name="stride">Row stride in bytes.</param>
        /// <param name="channels">Number of channels in the image.</param>
        /// <returns>Array of image pixels.</returns>
        private static T[] ReadPixels <T>(int width, int height, NakedPointer data, int stride, int channels)
        {
            int pixelByteSize = System.Runtime.InteropServices.Marshal.SizeOf <T>();

            if (stride <= 0 || stride == pixelByteSize * channels * width)
            {
                return(new CodeArray <T>(data, width * height * channels).ToArray());
            }
            else
            {
                T[] result      = new T[width * height * channels];
                int rowElements = width * channels;

                for (int y = 0, j = 0; y < height; y++)
                {
                    CodeArray <T> array = new CodeArray <T>(data.AdjustPointer(stride * y), rowElements);

                    for (int x = 0; x < rowElements; x++, j++)
                    {
                        result[j] = array[x];
                    }
                }

                return(result);
            }
        }
Esempio n. 3
0
        public void TestNakedPointer()
        {
            NakedPointer nakedPointer = new NakedPointer(0);

            Assert.IsTrue(nakedPointer.IsNull);
            Assert.IsTrue(nakedPointer.ToCodePointer<int>().IsNull);
        }
Esempio n. 4
0
 /// <summary>
 /// Initializes bitmap for visualization.
 /// </summary>
 /// <param name="width">Bitmap width.</param>
 /// <param name="height">Bitmap height.</param>
 /// <param name="dataAddress">Address of the pixels data.</param>
 /// <param name="channels">Channels in the bitmap.</param>
 /// <param name="dataType">Type of the single pixel channel value.</param>
 /// <param name="stride">Bitmap data stride.</param>
 protected void Initialize(int width, int height, ulong dataAddress, ChannelType[] channels, BuiltinType dataType = BuiltinType.UInt8, int stride = 0)
 {
     Width         = width;
     Height        = height;
     this.stride   = stride;
     this.channels = channels;
     this.dataType = dataType;
     data          = dataAddress != 0 ? new NakedPointer(GetCodeType().Module.Process, dataAddress) : null;
 }
Esempio n. 5
0
        private static void TestReading <T>(Variable var, T expectedValue)
        {
            Assert.Equal(expectedValue, var.CastAs <T>());
            Assert.Equal(expectedValue, (T)Convert.ChangeType(var, typeof(T)));
            NakedPointer    pointer     = new NakedPointer(var.GetPointerAddress());
            CodePointer <T> codePointer = new CodePointer <T>(pointer);

            Assert.Equal(expectedValue, codePointer.Element);
            codePointer = new CodePointer <T>(pointer.GetPointerAddress());
            Assert.Equal(expectedValue, codePointer.Element);
            codePointer = new CodePointer <T>(var.GetPointer());
            Assert.Equal(expectedValue, codePointer.Element);
            CodeArray <T> codeArray = new CodeArray <T>(pointer, 1);

            Assert.Equal(expectedValue, codeArray[0]);
        }
Esempio n. 6
0
        /// <summary>
        /// Creates drawing that should be visualized based on cv::Mat data.
        /// </summary>
        /// <param name="graphics">Graphics object used to create drawings.</param>
        /// <param name="width">Bitmap width.</param>
        /// <param name="height">Bitmap height.</param>
        /// <param name="data">Pointer to where start of the data is.</param>
        /// <param name="stride">Row stride in bytes.</param>
        /// <param name="type">Matrix element type.</param>
        /// <returns>Drawing object that should be visualized.</returns>
        internal static IDrawing CreateDrawing(IGraphics graphics, int width, int height, NakedPointer data, int stride, MatType type)
        {
            ChannelType[] channels;

            if (type.Channels == 3)
            {
                channels = Channels.BGR;
            }
            else if (type.Channels == 1)
            {
                channels = Channels.Grayscale;
            }
            else
            {
                throw new NotImplementedException();
            }

            return(BitmapUserType.CreateDrawing(graphics, width, height, data, channels, type.BuiltinType, stride));
        }
Esempio n. 7
0
        /// <summary>
        /// Creates drawing that should be visualized.
        /// </summary>
        /// <param name="graphics">Graphics object used to create drawings.</param>
        /// <param name="width">Bitmap width.</param>
        /// <param name="height">Bitmap height.</param>
        /// <param name="data">Address of the pixels data.</param>
        /// <param name="channels">Channels in the bitmap.</param>
        /// <param name="dataType">Type of the single pixel channel value.</param>
        /// <param name="stride">Bitmap data stride.</param>
        /// <returns>Drawing object that should be visualized.</returns>
        public static IDrawing CreateDrawing(IGraphics graphics, int width, int height, NakedPointer data, ChannelType[] channels, BuiltinType dataType = BuiltinType.UInt8, int stride = 0)
        {
            switch (dataType)
            {
            case BuiltinType.Float32:
                return(graphics.CreateBitmap(width, height, channels, ReadPixels <float>(width, height, data, stride, channels.Length)));

            case BuiltinType.Float64:
                return(graphics.CreateBitmap(width, height, channels, ReadPixels <double>(width, height, data, stride, channels.Length)));

            case BuiltinType.Int8:
                return(graphics.CreateBitmap(width, height, channels, ReadPixels <sbyte>(width, height, data, stride, channels.Length)));

            case BuiltinType.Int16:
                return(graphics.CreateBitmap(width, height, channels, ReadPixels <short>(width, height, data, stride, channels.Length)));

            case BuiltinType.Int32:
                return(graphics.CreateBitmap(width, height, channels, ReadPixels <int>(width, height, data, stride, channels.Length)));

            case BuiltinType.NoType:
            case BuiltinType.Char8:
            case BuiltinType.Void:
            case BuiltinType.UInt8:
                return(graphics.CreateBitmap(width, height, channels, ReadPixels <byte>(width, height, data, stride, channels.Length)));

            case BuiltinType.UInt16:
                return(graphics.CreateBitmap(width, height, channels, ReadPixels <ushort>(width, height, data, stride, channels.Length)));

            default:
                throw new NotImplementedException($"Unknown image data type: {dataType}");
            }
        }