Exemplo n.º 1
0
        /// <summary>
        /// Retrieves a feature from the device.
        /// </summary>
        /// <param name="device"></param>
        /// <param name="code"></param>
        /// <param name="datalen"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static HIDFeatureResult GetHIDFeature(IntPtr device, int code, int datalen = 16)
        {
            HIDFeatureResult GetHIDFeatureRet = default;
            MemPtr           mm = new MemPtr();

            int i = code;

            try
            {
                mm.AllocZero(datalen);
                mm.ByteAt(0L) = (byte)i;
                if (UsbLibHelpers.HidD_GetFeature(device, mm.Handle, (int)mm.Length))
                {
                    GetHIDFeatureRet = new HIDFeatureResult(i, mm);
                }
                else
                {
                    GetHIDFeatureRet = null;
                }

                mm.Free();
            }
            catch
            {
                mm.Free();
                return(null);
            }

            return(GetHIDFeatureRet);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the raw byte data for a Hid feature code.
        /// </summary>
        /// <param name="featureCode">The Hid feature code to retrieve.</param>
        /// <param name="result">Receives the result of the operation.</param>
        /// <param name="expectedSize">The expected size, in bytes, of the result.</param>
        /// <returns>True if successful.</returns>
        /// <remarks></remarks>
        public bool HidGetFeature(byte featureCode, ref byte[] result, int expectedSize)
        {
            bool HidGetFeatureRet = default;
            var  hfile            = HidFeatures.OpenHid(this);

            if (hfile == IntPtr.Zero)
            {
                return(false);
            }
            var mm = new MemPtr();

            mm.Alloc(expectedSize + 1);
            mm.ByteAt(0L) = featureCode;
            if (!UsbLibHelpers.HidD_GetFeature(hfile, mm, expectedSize))
            {
                HidGetFeatureRet = false;
            }
            else
            {
                HidGetFeatureRet = true;
                result           = mm.ToByteArray(1L, expectedSize);
            }

            HidFeatures.CloseHid(hfile);
            mm.Free();
            return(HidGetFeatureRet);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns the short value of a Hid feature code.
        /// </summary>
        /// <param name="featureCode">The Hid feature code to retrieve.</param>
        /// <param name="result">Receives the result of the operation.</param>
        /// <returns>True if successful.</returns>
        /// <remarks></remarks>
        public bool HidGetFeature(byte featureCode, ref short result)
        {
            bool HidGetFeatureRet = default;
            var  hfile            = HidFeatures.OpenHid(this);

            if (hfile == IntPtr.Zero)
            {
                return(false);
            }
            var mm = new MemPtr();

            mm.Alloc(3L);
            mm.ByteAt(0L) = featureCode;
            if (!UsbLibHelpers.HidD_GetFeature(hfile, mm, 3))
            {
                HidGetFeatureRet = false;
            }
            else
            {
                HidGetFeatureRet = true;
                result           = mm.ShortAtAbsolute(1L);
            }

            mm.Free();
            HidFeatures.CloseHid(hfile);
            return(HidGetFeatureRet);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sets the long value of a Hid feature code.
        /// </summary>
        /// <param name="featureCode">The Hid feature code to set.</param>
        /// <param name="value">The value to set.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool HidSetFeature(byte featureCode, long value)
        {
            bool HidSetFeatureRet = default;
            var  hfile            = HidFeatures.OpenHid(this);

            if (hfile == IntPtr.Zero)
            {
                return(false);
            }
            var mm = new MemPtr();

            mm.Alloc(9L);
            mm.ByteAt(0L)         = featureCode;
            mm.LongAtAbsolute(1L) = value;
            if (!UsbLibHelpers.HidD_SetFeature(hfile, mm, 9))
            {
                HidSetFeatureRet = false;
            }
            else
            {
                HidSetFeatureRet = true;
            }

            HidFeatures.CloseHid(hfile);
            mm.Free();
            return(HidSetFeatureRet);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sets the raw byte value of a Hid feature code.
        /// </summary>
        /// <param name="featureCode">The Hid feature code to set.</param>
        /// <param name="value">The value to set.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool HidSetFeature(byte featureCode, byte[] value)
        {
            bool HidSetFeatureRet = default;
            var  hfile            = HidFeatures.OpenHid(this);

            if (hfile == IntPtr.Zero)
            {
                return(false);
            }
            var mm = new MemPtr();

            mm.Alloc(value.Length + 1);
            mm.FromByteArray(value, 1L);
            mm.ByteAt(0L) = featureCode;
            if (!UsbLibHelpers.HidD_SetFeature(hfile, mm, (int)mm.Length))
            {
                HidSetFeatureRet = false;
            }
            else
            {
                HidSetFeatureRet = true;
            }

            mm.Free();
            HidFeatures.CloseHid(hfile);
            return(HidSetFeatureRet);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gray out an icon.
        /// </summary>
        /// <param name="icn">The input icon.</param>
        /// <returns>The grayed out icon.</returns>
        /// <remarks></remarks>
        public static Image GrayIcon(Icon icn)
        {
            var n = new Bitmap(icn.Width, icn.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            var g = System.Drawing.Graphics.FromImage(n);

            g.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, n.Width, n.Height));
            g.DrawIcon(icn, 0, 0);
            g.Dispose();
            var bm = new System.Drawing.Imaging.BitmapData();
            var mm = new MemPtr(n.Width * n.Height * 4);

            bm.Stride      = n.Width * 4;
            bm.Scan0       = mm;
            bm.PixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
            bm.Width       = n.Width;
            bm.Height      = n.Height;
            bm             = n.LockBits(new Rectangle(0, 0, n.Width, n.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite | System.Drawing.Imaging.ImageLockMode.UserInputBuffer, System.Drawing.Imaging.PixelFormat.Format32bppArgb, bm);
            int i;
            int c;

            // Dim b() As Byte

            // ReDim b((bm.Stride * bm.Height) - 1)
            // MemCpy(b, bm.Scan0, bm.Stride * bm.Height)
            c = bm.Stride * bm.Height - 1;
            int stp = (int)(bm.Stride / (double)bm.Width);

            // For i = 3 To c Step stp
            // If b(i) > &H7F Then b(i) = &H7F
            // Next

            for (i = 3; stp >= 0 ? i <= c : i >= c; i += stp)
            {
                if (mm.ByteAt(i) > 0x7F)
                {
                    mm.ByteAt(i) = 0x7F;
                }
            }

            // MemCpy(bm.Scan0, b, bm.Stride * bm.Height)
            n.UnlockBits(bm);
            mm.Free();
            return(n);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Converts this raw icon source into a managed System.Drawing.Icon image.
        /// </summary>
        /// <returns>A new Icon object.</returns>
        /// <remarks></remarks>
        public Icon ToIcon()
        {
            Icon iconOut;

            if (IsPngFormat)
            {
                Bitmap bmp = (Bitmap)ToImage();

                var bmi    = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
                var lpicon = default(ICONINFO);

                int i;

                var bm = bmi.LockBits(new Rectangle(0, 0, bmi.Width, bmi.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);

                MemPtr mm = bm.Scan0;

                int z = (int)(Math.Max(bmp.Width, 32) * bmp.Height / 8d);

                for (i = 0; i < z; i++)
                {
                    mm.ByteAt(i) = 255;
                }

                bmi.UnlockBits(bm);

                lpicon.hbmColor = bmp.GetHbitmap();
                lpicon.hbmMask  = bmi.GetHbitmap();
                lpicon.fIcon    = 1;

                var hIcon = User32.CreateIconIndirect(ref lpicon);

                if (hIcon != IntPtr.Zero)
                {
                    iconOut = (Icon)Icon.FromHandle(hIcon).Clone();
                    User32.DestroyIcon(hIcon);
                }
                else
                {
                    iconOut = null;
                }

                NativeShell.DeleteObject(lpicon.hbmMask);
                NativeShell.DeleteObject(lpicon.hbmColor);
            }
            else
            {
                iconOut = _constructIcon();
            }

            return(iconOut);
        }
Exemplo n.º 8
0
        protected bool CheckRect(Rectangle rc, int cx, int cy, MemPtr mm)
        {
            int x;
            int y;
            int l;
            var b = rc.Bottom;

            for (y = rc.Top; y <= b; y++)
            {
                var r = rc.Right;
                for (x = rc.Left; x <= r; x++)
                {
                    l = (y * cx + x) * 4;
                    if (mm.ByteAt(l) < 250 || mm.ByteAt(l + 1) < 250 || mm.ByteAt(l + 2) < 250)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Mount the virtual drive, permanently.  The virtual drive will stay mounted beyond the lifetime of this instance.
        /// </summary>
        /// <param name="makePermanent"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool Attach(bool makePermanent)
        {
            if (_Handle == IntPtr.Zero || Attached)
            {
                return(false);
            }

            var mm = new MemPtr(8L);

            mm.ByteAt(0L) = 1;

            uint r = VirtDisk.AttachVirtualDisk(_Handle, IntPtr.Zero, makePermanent ? ATTACH_VIRTUAL_DISK_FLAG.ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME : ATTACH_VIRTUAL_DISK_FLAG.ATTACH_VIRTUAL_DISK_FLAG_NONE, 0U, mm.Handle, IntPtr.Zero);

            mm.Free();

            return(r == 0L);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Apply transparency mask bits to the output image (for converting to a bitmap)
        /// </summary>
        /// <param name="hBits">A pointer to the memory address of the bitmap bits.</param>
        /// <param name="hMask">A pointer to the memory address of the mask bits.</param>
        /// <param name="Width">The width of the image.</param>
        /// <param name="Height">The height of the image.</param>
        /// <remarks></remarks>
        private void _applyMask(MemPtr hBits, MemPtr hMask, int Width, int Height)
        {
            // Masks in icon images are bitstreams wherein a single bit represents a 1 or 0 transparency
            // for an entire pixel on the screen.  In order to convert an icon into a 32 bit images,
            // we need to access each individual bit, and apply the NOT of the value to the byte-length alpha mask
            // of the bitmap.

            int x;
            int y;
            int shift;
            int bit;
            int shift2;
            int mask;

            // in transparency masks for icons, the minimum stride is 32 pixels/bits, no matter the actual size of the image.
            int boundary = Math.Max(32, Width);

            // walk every pixel of the image.

            for (y = 0; y < Height; y++)
            {
                for (x = 0; x < Width; x++)
                {
                    // the first shift is our position in the bitmap output.
                    // 4 bytes is 32 bits ... then we add 3 to get directly
                    // to the alpha mask.
                    shift = 4 * (x + y * Width) + 3;

                    // we find the exact bit-wise position by modulus with 8 (the length of a byte, in bits)
                    bit = 7 - x % 8;

                    // the second shift is the position in the bitmask, byte-wise.  We subtract 1 from y before subtracting it from the
                    // height because the first scan line is 0.
                    shift2 = (int)((x + (Height - y - 1) * boundary) / 8d);

                    // we get a number that is either 1 or 0 from the mask by accessing the exact byte, and then
                    // accessing the exact bit by left-shifting its value into the 1 position.
                    mask = 1 & hMask.ByteAt(shift2) >> bit;

                    // we do a quick logical AND via multiplication with the inverse of the mask.
                    // we do this because alpha channel 0 is transparent, but transparent mask 1 is also transparent.
                    hBits.ByteAt(shift) *= (byte)(1 - mask);
                }
            }
        }
Exemplo n.º 11
0
        private void StartWatching(HidDeviceInfo d)
        {
            IntPtr h;

            int i = 0;

            if (_devThread is object)
            {
                StopWatching();
            }

            var s = new ObservableCollection <string>();

            _lastDevice = d;

            this.ViewingArea.ItemsSource = s;

            for (i = 0; i <= 255; i++)
            {
                s.Add("");
            }

            var th = new Thread(() =>
            {
                cts = new CancellationTokenSource();
                h   = HidFeatures.OpenHid(d);
                if ((long)h <= 0L)
                {
                    return;
                }
                var mm = new MemPtr(65L);
                try
                {
                    do
                    {
                        this.Dispatcher.Invoke(() =>
                        {
                            for (i = 0; i <= 255; i++)
                            {
                                mm.LongAtAbsolute(1L) = 0L;
                                mm.ByteAt(0L)         = (byte)i;
                                if (HidD_GetFeature(h, mm, 65))
                                {
                                    s[i] = "HID CODE " + i.ToString("X2") + " = " + mm.IntAtAbsolute(1L);
                                }
                            }
                        });

                        Thread.Sleep(1000);
                        if (cts is null || cts.IsCancellationRequested)
                        {
                            break;
                        }
                    }while (true);
                    mm.Free();
                    HidFeatures.CloseHid(h);
                    cts = null;
                    return;
                }
                catch (ThreadAbortException)
                {
                    mm.Free();
                    HidFeatures.CloseHid(h);
                    cts = null;
                    return;
                }
                catch (Exception)
                {
                    mm.Free();
                    HidFeatures.CloseHid(h);
                    cts = null;
                    return;
                }
            });

            th.IsBackground = true;
            th.SetApartmentState(ApartmentState.STA);
            _devThread = th;
            th.Start();
        }
Exemplo n.º 12
0
        /// <summary>
        /// Perform all the calculations necessary to determine the
        /// usable text area from the given template and stationary image.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        protected Rectangle[] Calculate()
        {
            Rectangle[] CalculateRet = default;
            Bitmap      bmp;

            // what we're going to do is map every possible rectangular region in the image.
            // we will accept a certain percentage of white space to add that rectangle to the image.

            double minX;
            double minY;
            int    x;
            int    y;
            int    cx;
            int    cy;
            int    l;
            int    c = 0;
            int    d = 0;

            var rcCol     = new List <Rectangle>();
            var lineParts = new LineSegments();

            var ceArgs = new CalculatedEventArgs(null, 0, DateTime.MinValue, DateTime.MinValue);

            ceArgs.StartTimer();

            // Dim sMult As Double = 1.0#
            // Dim maxImg As Integer = 2400

            bmp = _img;

            // If _img.Width > maxImg OrElse _img.Height > maxImg Then

            // If _img.Width > _img.Height Then
            // x = maxImg
            // y = _img.Height * (maxImg / _img.Width)
            // sMult = maxImg / _img.Width
            // Else
            // y = maxImg
            // x = _img.Width * (maxImg / _img.Height)
            // sMult = maxImg / _img.Height
            // End If

            // bmp = New Bitmap(x, y, PixelFormat.Format32bppArgb)
            // Dim g As Graphics = Graphics.FromImage(bmp)

            // g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
            // g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            // g.SmoothingMode = Drawing2D.SmoothingMode.None

            // g.DrawImage(_img, 0, 0, x, y)

            // g.Dispose()
            // _img = bmp
            // Else
            // bmp = _img
            // End If

            cx   = bmp.Width;
            cy   = bmp.Height;
            minX = _minUsableX * cx;
            minY = _minUsableY * cy;
            var    bm = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            MemPtr mm = bm.Scan0;

            ceArgs.TotalCalculations = cx * cy * 2;

            for (y = 0; y < cy; y++)
            {
                c = -1;
                d = -1;

                for (x = 0; x < cx; x++)
                {
                    l = (y * cx + x) * 4;
                    if (mm.ByteAt(l) >= 250 && mm.ByteAt(l + 1) >= 250 && mm.ByteAt(l + 2) >= 250)

                    {
                        if (c == -1)
                        {
                            c = x;
                            d = 0;
                        }
                        else
                        {
                            d += 1;
                        }
                    }
                    else if (d != -1)
                    {
                        if (d > minX)
                        {
                            lineParts.Add(new LineSegment(c, d, y));
                        }

                        c = -1;
                        d = -1;
                    }
                }

                if (d > minX)
                {
                    lineParts.Add(new LineSegment(c, d, y));
                }
            }

            lineParts.Sort();
            _rawData = lineParts;
            var          experiment = new List <LineSegments>();
            LineSegments temp;
            // Dim temp2 As LineSegments

            double minArea = minX * minY;

            minArea *= 0.5d;
            ceArgs.TotalCalculations += cx * cy;

            var loopTo2 = cx - 1;

            for (x = 0; (int)(cx * 0.0375d) >= 0 ? x <= loopTo2 : x >= loopTo2; x += (int)(cx * 0.0375d))
            {
                var loopTo3 = cy - 1;

                for (y = 0; (int)(cy * 0.0375d) >= 0 ? y <= loopTo3 : y >= loopTo3; y += (int)(cy * 0.0375d))
                {
                    l    = (y * cx + x) * 4;
                    temp = _rawData.FindSet(x, cx - x, y, false);
                    if (temp is null || temp.Area < minArea)
                    {
                        continue;
                    }
                    for (l = temp.Count - 2; l >= 1; l -= 1)
                    {
                        if (temp[l].Index - temp[l - 1].Index > temp.OriginContinuityThreshold)
                        {
                            temp.RemoveAt(l - 1);
                        }
                    }

                    ceArgs.TotalCalculations += temp.Count + _rawData.Count;
                    experiment.Add(temp);
                }
            }

            experiment.Sort(new SortByArea());
            experiment.Reverse();
            LineSegments[] pps; // = lineParts.FindAllContiguousRegions
            pps = experiment.ToArray();
            Clear();
            bmp.UnlockBits(bm);
            ceArgs.TotalCalculations += pps.Count() + _rawData.Count;
            _Regions.Clear();
            foreach (var px in pps)
            {
                var rc = px.Bounds;
                temp = _rawData.FindSet(rc.Left, rc.Right, rc.Top, true, false);
                if (temp is null)
                {
                    continue;
                }
                rc = temp.Bounds;
                if (rc.Width < minX)
                {
                    continue;
                }
                if (rc.Height < minY)
                {
                    continue;
                }
                rc.Inflate(-20, -20);
                if (_purgeOverlaps)
                {
refor:
                    ;
                    foreach (var rcChk in rcCol)
                    {
                        if (rcChk.IntersectsWith(rc))
                        {
                            if (rcChk.Width * rcChk.Height > rc.Width * rc.Height) // AndAlso (rcChk.Width > rc.Width) Then
                            {
                                rc = rcChk;
                            }
                            else if (TestSquareness(rcChk) < TestSquareness(rc)) // AndAlso (rc.Width >= rcChk.Width) Then
                            {
                                rcCol.Remove(rcChk);
                                goto refor;
                            }
                            else
                            {
                                rc = Rectangle.Empty;
                            }

                            break;
                        }
                    }
                }

                if (temp is object)
                {
                    Add(temp);
                }
                if (rc != Rectangle.Empty)
                {
                    // If CheckRect(rc, cx, cy, mm) = False Then Continue For
                    rcCol.Add(rc);
                    _Regions.Add(rc);
                }
            }

            ceArgs.StopTimer();
            CalculateRet   = rcCol.ToArray();
            ceArgs.Regions = _Regions;
            OnPropertyChanged("Regions");
            OnPropertyChanged("RawOutput");
            Calculated?.Invoke(this, ceArgs);
            return(CalculateRet);
        }