Exemplo n.º 1
0
        public static UInt16[,] ConvertIntPtrFlatTo2DArrayUInt16(IntPtr ptr, Int32 rows, Int32 cols, bool dispose)
        {
            UInt16[,] toReturn = new UInt16[rows, cols];
            IntPtr tempPtr = ptr;

            try
            {
                Int16[] currentRow = new Int16[cols];
                int     arrayIndex = 0;
                for (int i = 0; i < rows; ++i)
                {
                    // Copy from the pointer to the currentRow array.
                    Marshal.Copy(tempPtr, currentRow, 0, cols);
                    // Copy from the currentRow array to the array to return.
                    Buffer.BlockCopy(currentRow, 0, toReturn, arrayIndex, cols * sizeof(Int16));
                    arrayIndex += cols * sizeof(Int16);
                    tempPtr     = AdvanceIntPtr(tempPtr, cols * sizeof(Int16));
                }
            }
            finally
            {
                if (dispose)
                {
                    VisionDllCommon.imaqDispose(ptr);
                }
            }
            return(toReturn);
        }
Exemplo n.º 2
0
 public byte[] ReadFrame(VisionImage image, UInt32 frame, bool readData)
 {
     ThrowIfDisposed();
     if (image == null)
     {
         throw new ArgumentNullException("image");
     }
     image.ThrowIfDisposed();
     if (readData)
     {
         UInt32 dataSize = MaximumDataSize;
         byte[] tempData = new byte[dataSize];
         Utilities.ThrowError(VisionDllCommon.imaqReadAVIFrame(image._image, _session, frame, tempData, ref dataSize));
         byte[] data = tempData;
         if (dataSize != MaximumDataSize)
         {
             data = new byte[dataSize];
             Array.Copy(tempData, data, dataSize);
         }
         return(data);
     }
     else
     {
         Utilities.ThrowError(VisionDllCommon.imaqReadAVIFrame(image._image, _session, frame, IntPtr.Zero, IntPtr.Zero));
         return(null);
     }
 }
Exemplo n.º 3
0
        public static double[,] ConvertIntPtrIndirectTo2DArrayDouble(IntPtr ptr, int rows, int cols, bool dispose)
        {
            double[,] toReturn = new double[rows, cols];
            IntPtr tempPtr = ptr;

            try
            {
                double[] currentRow = new double[cols];
                int      arrayIndex = 0;
                for (int i = 0; i < rows; ++i)
                {
                    IntPtr rowPtr = tempPtr;
                    // Copy from the pointer to the currentRow array.
                    Marshal.Copy(rowPtr, currentRow, 0, cols);
                    // Copy from the currentRow array to the array to return.
                    Buffer.BlockCopy(currentRow, 0, toReturn, arrayIndex, cols * sizeof(double));
                    arrayIndex += cols * sizeof(double);
                    tempPtr     = AdvanceIntPtr(tempPtr, System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)));
                }
            }
            finally
            {
                if (dispose)
                {
                    VisionDllCommon.imaqDispose(ptr);
                }
            }
            return(toReturn);
        }
Exemplo n.º 4
0
        // imaqDraw*OnImage uses this weird format for passing colors.
        // Most things are grayscale, RGB is 0xAABBGGRR, and HSL is 0xAABBGGRR as well,
        // meaning you have to convert it to an RGB value first.
        public static float ConvertPixelValueToFloat(PixelValue value, ImageType type)
        {
            switch (type)
            {
            case ImageType.U8:
            case ImageType.I16:
            case ImageType.U16:
            case ImageType.Single:
                return(value.Grayscale);

            case ImageType.Rgb32:
                Rgb32Value rgbValue = value.Rgb32;
                return(rgbValue.Blue << 16 | rgbValue.Green << 8 | rgbValue.Red);

            case ImageType.Hsl32:
                Hsl32Value hslValue = value.Hsl32;
                // We need to convert this to a Rgb32 first.
                CVI_Color2 hslColor = new CVI_Color2();
                hslColor.Hsl = hslValue;
                CVI_Color2 result       = VisionDllCommon.imaqChangeColorSpace2(ref hslColor, ColorMode.Hsl, ColorMode.Rgb, 0.0, IntPtr.Zero);
                Rgb32Value rgbToConvert = result.Rgb;
                return(rgbToConvert.Blue << 16 | rgbToConvert.Green << 8 | rgbToConvert.Red);

            default:
                // Don't DebugAssert here since the user could get here by passing an invalid image type.
                //Debug.Fail("Unexpected image type " + type + " passed to ConvertPixelValueToFloat!");
                return(0.0F);
            }
        }
Exemplo n.º 5
0
        //==========================================================================================
        /// <summary>
        /// Returns an array of the compression filters on this system available to be used to create Audio Video Interleave (AVI) files.
        /// </summary>
        /// <returns>
        /// On success, this method returns a collection of names of compression filters that are available to compress AVI files on this system.
        /// </returns>

        public static Collection <string> GetCompressionFilters()
        {
            int    numFilters;
            IntPtr filtersPtr = VisionDllCommon.imaqGetFilterNames(out numFilters);

            Utilities.ThrowError(filtersPtr);
            return(Utilities.ConvertIntPtrToStringCollection(filtersPtr, numFilters, true));
        }
Exemplo n.º 6
0
        public static void ThrowError()
        {
            ErrorCode lastError = VisionDllCommon.imaqGetLastError();

            if (lastError != ErrorCode.Success)
            {
                throw new VisionException(lastError);
            }
        }
Exemplo n.º 7
0
        //==========================================================================================
        /// <summary>
        /// Initializes a new instance of the <see cref="NationalInstruments.Vision.AviInputSession" crefType="Unqualified"/> class with the specified filename.
        /// </summary>
        /// <param name="fileName">
        /// The name of the AVI file.
        /// </param>

        public AviInputSession(string fileName)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            _session = VisionDllCommon.imaqOpenAVI(fileName);
            Utilities.ThrowError(_session);
        }
Exemplo n.º 8
0
        public static T ConvertIntPtrToStructure <T>(IntPtr ptr, bool dispose)
        {
            T toReturn = (T)Marshal.PtrToStructure(ptr, typeof(T));

            if (dispose)
            {
                VisionDllCommon.imaqDispose(ptr);
            }
            return(toReturn);
        }
Exemplo n.º 9
0
        public static E ConvertIntPtrToStructure <E, I, E2>(IntPtr ptr, E2 auxiliary, bool dispose) where I : IHasExternalVersionOut <E, E2>
        {
            E toReturn = ((I)Marshal.PtrToStructure(ptr, typeof(I))).ConvertToExternal(auxiliary);

            if (dispose)
            {
                VisionDllCommon.imaqDispose(ptr);
            }
            return(toReturn);
        }
Exemplo n.º 10
0
 public AviOutputSession(string fileName, UInt32 framesPerSecond, string compressionFilter, Int32 quality, bool hasData, UInt32 maxDataSize)
 {
     if (fileName == null)
     {
         throw new ArgumentNullException("fileName");
     }
     // If compressionFilter is null, that's OK, since imaqCreateAVI() will accept it as NULL.
     _session = VisionDllCommon.imaqCreateAVI(fileName, compressionFilter, quality, framesPerSecond, hasData ? maxDataSize: 0);
     Utilities.ThrowError(_session);
 }
Exemplo n.º 11
0
 private AviInfo GetAviInfo()
 {
     ThrowIfDisposed();
     if (_info == null)
     {
         CVI_AVIInfo _cviInfo = new CVI_AVIInfo();
         Utilities.ThrowError(VisionDllCommon.imaqGetAVIInfo(_session, out _cviInfo));
         _info = _cviInfo.ConvertToExternal();
     }
     return(_info);
 }
Exemplo n.º 12
0
        public static Array1D ConvertCollectionToArray1D <E, I>(Collection <E> collection) where I : IHasExternalVersionIn <E>, new()
        {
            Array1D toReturn;

            VisionDllCommon.Priv_InitArray1D(out toReturn);
            IntPtr ptr = ConvertCollectionToIntPtr <E, I>(collection);

            toReturn.Count = (uint)collection.Count;
            toReturn.Ptr   = ptr;
            return(toReturn);
        }
Exemplo n.º 13
0
        public static Array1D ConvertCollectionToArray1D <T>(Collection <T> collection) where T : new()
        {
            Array1D toReturn;

            VisionDllCommon.Priv_InitArray1D(out toReturn);
            IntPtr ptr = ConvertCollectionToIntPtr <T>(collection);

            toReturn.Count = (uint)collection.Count;
            toReturn.Ptr   = ptr;
            return(toReturn);
        }
Exemplo n.º 14
0
 private void Dispose(bool disposing)
 {
     lock (_disposeLock)
     {
         // _session is safe to use since it's unmanaged.
         if (_session != IntPtr.Zero)
         {
             VisionDllCommon.imaqCloseAVI(_session);
             _session = IntPtr.Zero;
         }
     }
 }
Exemplo n.º 15
0
        //==========================================================================================
        /// <summary>
        /// Changes the type of an image with the specified source, destination, type, and lookup table.
        /// </summary>
        /// <param name="source">
        /// The source image.
        /// </param>
        /// <param name="destination">
        /// The destination image. Set this parameter equal to source or <see langword="null"/> to perform the change directly on the source image.
        /// </param>
        /// <param name="type">
        /// The new type for the image. <paramref name="type"/> is the new type for the <paramref name="destination"/> image if it is being used, otherwise it is the new type for <paramref name="source"/>.
        /// </param>
        /// <param name="lookupTable">
        /// An optional lookup table. If you do not want  to use a lookup table, this parameter may be <see keyword="" keywordType="mstudio"/>.
        /// </param>

        public static void Cast(VisionImage source, VisionImage destination, ImageType type, Collection <float> lookupTable)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            source.ThrowIfDisposed();
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }
            destination.ThrowIfDisposed();
            int tableSize = 256;

            if (source.Type == ImageType.I16 || source.Type == ImageType.U16)
            {
                tableSize = 65536;
            }
            // If the table is the correct size, just do the conversion; otherwise, pad it first.
            float[] cviTable;
            if (lookupTable == null)
            {
                cviTable = null;
            }
            else
            {
                if (lookupTable.Count >= tableSize)
                {
                    cviTable = Utilities.ConvertCollectionToArray <float>(lookupTable);
                }
                else
                {
                    cviTable = new float[tableSize];
                    for (int i = 0; i < lookupTable.Count; ++i)
                    {
                        cviTable[i] = lookupTable[i];
                    }
                    for (int i = lookupTable.Count; i < tableSize; ++i)
                    {
                        if (source.Type == ImageType.I16)
                        {
                            cviTable[i] = (i <= 32768) ? (float)i : (float)(i - 65536);
                        }
                        else
                        {
                            cviTable[i] = i;
                        }
                    }
                }
            }
            Utilities.ThrowError(VisionDllCommon.imaqCast(destination._image, source._image, type, cviTable, 0));
        }
Exemplo n.º 16
0
        //==========================================================================================
        /// <summary>
        /// Changes the type of an image with the specified source, destination, type, and shifts.
        /// </summary>
        /// <param name="source">
        /// The source image.
        /// </param>
        /// <param name="destination">
        /// The destination image. Set this parameter equal to source or <see langword="null"/> to perform the change directly on the source image.
        /// </param>
        /// <param name="type">
        /// The new type for the image. <paramref name="type"/> is the new type for the <paramref name="destination"/> image if it is being used, otherwise it is the new type for <paramref name="source"/>.
        /// </param>
        /// <param name="numberOfShifts">
        /// The shift value for converting 16-bit images to 8-bit images. The method executes this conversion by shifting the 16-bit pixel values to the right by the specified number of shift operations, up to a maximum of 8 shift operations, and then truncating to get an 8-bit value. Enter a value of –1 to ignore the bit depth and shift 0. Enter a value of 0 to use the bit depth to cast the image.
        /// </param>

        public static void Cast(VisionImage source, VisionImage destination, ImageType type, Int32 numberOfShifts)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            source.ThrowIfDisposed();
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }
            destination.ThrowIfDisposed();
            Utilities.ThrowError(VisionDllCommon.imaqCast(destination._image, source._image, type, null, numberOfShifts));
        }
Exemplo n.º 17
0
        //==========================================================================================
        /// <summary>
        /// Copies the source image to the destination image, including the border size and calibration information.
        /// </summary>
        /// <param name="source">
        /// The source image to copy.
        /// </param>
        /// <param name="destination">
        /// The resulting image.
        /// </param>
        /// <remarks>
        /// Use this method with all image types.
        /// <para>
        /// To copy an area of one image to an area of another image, use the Extract2 method with xSubsample and ySubsample set to 1.
        /// </para>
        ///     <para>
        /// The <paramref name="source"/> and <paramref name="destination"/> images must be the same type of image. The method copies the complete definition of the source image and its pixel data to the destination image. The method also modifies the border of the destination image so that it becomes the same size as the border of the source image.
        /// </para>
        /// </remarks>

        public static void Copy(VisionImage source, VisionImage destination)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            source.ThrowIfDisposed();
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }
            destination.ThrowIfDisposed();
            Utilities.ThrowError(VisionDllCommon.imaqDuplicate(destination._image, source._image));
        }
Exemplo n.º 18
0
        public static T[] ConvertIntPtrTo1DStructureArray <T>(IntPtr ptr, UInt32 count, bool dispose)
        {
            T[]    toReturn = new T[count];
            IntPtr tempPtr  = ptr;

            for (uint i = 0; i < count; ++i)
            {
                toReturn[i] = (T)Marshal.PtrToStructure(tempPtr, typeof(T));
                tempPtr     = AdvanceIntPtr(tempPtr, Marshal.SizeOf(typeof(T)));
            }
            if (dispose)
            {
                VisionDllCommon.imaqDispose(ptr);
            }
            return(toReturn);
        }
Exemplo n.º 19
0
 public static float[] ConvertIntPtrTo1DArraySingle(IntPtr ptr, Int32 size, bool dispose)
 {
     float[] toReturn = new float[size];
     try
     {
         Marshal.Copy(ptr, toReturn, 0, size);
         return(toReturn);
     }
     finally
     {
         if (dispose)
         {
             VisionDllCommon.imaqDispose(ptr);
         }
     }
 }
Exemplo n.º 20
0
 public static Int32[] ConvertIntPtrTo1DArrayInt32(IntPtr ptr, Int32 size, bool dispose)
 {
     Int32[] toReturn = new Int32[size];
     try
     {
         Marshal.Copy(ptr, toReturn, 0, size);
     }
     finally
     {
         if (dispose)
         {
             VisionDllCommon.imaqDispose(ptr);
         }
     }
     return(toReturn);
 }
Exemplo n.º 21
0
        //==========================================================================================
        /// <summary>
        /// Writes a frame to the currently open Audio Video Interleave (AVI) file with the specified image and data.
        /// </summary>
        /// <param name="image">
        /// The image to write to the frame of the AVI.
        /// </param>
        /// <param name="data">
        /// The data to attach to this frame, if any.
        /// </param>

        public void WriteFrame(VisionImage image, byte[] data)
        {
            ThrowIfDisposed();
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }
            image.ThrowIfDisposed();
            if (data == null)
            {
                Utilities.ThrowError(VisionDllCommon.imaqWriteAVIFrame(image._image, _session, IntPtr.Zero, 0));
            }
            else
            {
                Utilities.ThrowError(VisionDllCommon.imaqWriteAVIFrame(image._image, _session, data, (UInt32)data.Length));
            }
        }
Exemplo n.º 22
0
        public static T[,] ConvertIntPtrTo2DStructureArray <T>(IntPtr ptr, Int32 rows, Int32 cols, bool dispose)
        {
            T[,] toReturn = new T[rows, cols];
            IntPtr tempPtr = ptr;

            for (int i = 0; i < rows; ++i)
            {
                for (int j = 0; j < cols; ++j)
                {
                    toReturn[i, j] = (T)Marshal.PtrToStructure(tempPtr, typeof(T));
                    tempPtr        = AdvanceIntPtr(tempPtr, Marshal.SizeOf(typeof(T)));
                }
            }
            if (dispose)
            {
                VisionDllCommon.imaqDispose(ptr);
            }
            return(toReturn);
        }
Exemplo n.º 23
0
        public static Collection <E> ConvertIntPtrToCollection <E, I>(IntPtr ptr, uint count, bool dispose, Converter <I, E> converter)
        {
            Collection <E> toReturn = new Collection <E>();
            IntPtr         tempPtr  = ptr;

            try
            {
                for (uint i = 0; i < count; ++i)
                {
                    toReturn.Add(converter(ConvertIntPtrToStructure <I>(tempPtr, false)));
                    tempPtr = AdvanceIntPtr(tempPtr, Marshal.SizeOf(typeof(I)));
                }
            }
            finally
            {
                if (dispose)
                {
                    VisionDllCommon.imaqDispose(ptr);
                }
            }
            return(toReturn);
        }
Exemplo n.º 24
0
        /// <summary>
        /// This method converts an array of items (like a report) that we get from CVI
        /// to its corresponding external type, and disposes the array.
        /// </summary>
        /// <param name="ptr">The array that we get back from CVI.</param>
        /// <param name="count">The number of elements in the array.</param>
        /// <param name="dispose">Whether to dispose the pointer after done</param>
        /// <returns>The collection to return to the user.</returns>
        public static Collection <E> ConvertIntPtrToCollection <E, I>(IntPtr ptr, uint count, bool dispose) where I : IHasExternalVersionOut <E>
        {
            Collection <E> toReturn = new Collection <E>();
            IntPtr         tempPtr  = ptr;

            try
            {
                for (int i = 0; i < count; ++i)
                {
                    I curItem = (I)Marshal.PtrToStructure(tempPtr, typeof(I));
                    toReturn.Add(curItem.ConvertToExternal());
                    tempPtr = AdvanceIntPtr(tempPtr, Marshal.SizeOf(typeof(I)));
                }
            }
            finally
            {
                if (dispose)
                {
                    VisionDllCommon.imaqDispose(ptr);
                }
            }
            return(toReturn);
        }
Exemplo n.º 25
0
        public static Collection <string> ConvertIntPtrToStringCollection(IntPtr ptr, int count, bool dispose)
        {
            Collection <string> toReturn = new Collection <string>();
            IntPtr tempPtr = ptr;

            try
            {
                for (int i = 0; i < count; ++i)
                {
                    IntPtr currentPtr = Marshal.ReadIntPtr(tempPtr);
                    toReturn.Add(Marshal.PtrToStringAnsi(currentPtr));
                    tempPtr = AdvanceIntPtr(tempPtr, Marshal.SizeOf(typeof(IntPtr)));
                }
            }
            finally
            {
                if (dispose)
                {
                    VisionDllCommon.imaqDispose(ptr);
                }
            }
            return(toReturn);
        }
Exemplo n.º 26
0
        //==========================================================================================
        /// <summary>
        /// Returns information regarding the contents of an image file. You can retrieve information from the following image file formats only: AIPD, BMP, JPEG, JPEG2000, PNG, and TIFF.
        /// </summary>
        /// <param name="fileName">
        /// The name of the file from which the method gets information.
        /// </param>

        public static FileInformation GetFileInformation(string fileName)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            Int32 calibrationUnit;
            float calibrationX;
            float calibrationY;
            Int32 width;
            Int32 height;
            Int32 imageType;

            Utilities.ThrowError(VisionDllCommon.imaqGetFileInfo(fileName, out calibrationUnit, out calibrationX, out calibrationY, out width, out height, out imageType));
            FileInformation toReturn = new FileInformation();

            toReturn.CalibrationGrid.Unit  = (CalibrationUnit)calibrationUnit;
            toReturn.CalibrationGrid.XStep = calibrationX;
            toReturn.CalibrationGrid.YStep = calibrationY;
            toReturn.Height    = height;
            toReturn.Width     = width;
            toReturn.ImageType = (ImageType)imageType;
            return(toReturn);
        }
Exemplo n.º 27
0
        //==========================================================================================
        /// <summary>
        /// Initializes a new instance of the <see cref="NationalInstruments.Vision.VisionException" crefType="Unqualified"/> class.
        /// </summary>
        /// <param name="errorCode">
        /// The Vision error code that this VisionException represents.
        /// </param>

        public VisionException(ErrorCode errorCode)
        {
            VisionErrorCode = errorCode;
            VisionErrorText = VisionDllCommon.imaqGetErrorText((int)VisionErrorCode);
        }