private unsafe void TransferDataFormat8bppIndexed(BitmapData imgData, PixData pixData)
        {
            var height = imgData.Height;
            var width  = imgData.Width;

            for (int y = 0; y < height; y++)
            {
                byte *imgLine = (byte *)imgData.Scan0 + (y * imgData.Stride);
                uint *pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine);

                for (int x = 0; x < width; x++)
                {
                    byte pixelVal = *(imgLine + x);
                    PixData.SetDataByte(pixLine, x, pixelVal);
                }
            }
        }
Example #2
0
        private int OCRNumber(Mat skillValue, string name = "")
        {
            if (!string.IsNullOrEmpty(name))
            {
                //skillValue.SaveImage($"{name}.png");
            }
            Pix pix;

            if (skillValue.Type() == MatType.CV_8UC3)
            {
                // 3 bytes
                pix      = Pix.Create(skillValue.Width, skillValue.Height, 32);
                pix.XRes = 72;
                pix.YRes = 72;
                PixData pixData = null;
                try
                {
                    pixData = pix.GetData();
                    Mat <Vec3b> mat3 = new Mat <Vec3b>(skillValue);
                    TransferData(mat3, pixData);
                }
                catch (Exception)
                {
                    pix.Dispose();
                    //throw;
                    return(0);
                }
            }
            else
            {
                skillValue.SaveImage("temp.png");
                pix = Pix.LoadFromFile("temp.png");
            }

            using (Page resultPage = _tessEngine.Process(pix))
            {
                string data = resultPage.GetText();

                if (int.TryParse(data, out int result))
                {
                    return(result);
                }
            }

            return(0);
        }
Example #3
0
        private unsafe PixColor GetPixel(Pix pix, int x, int y)
        {
            var  pixDepth = pix.Depth;
            var  pixData  = pix.GetData();
            var  pixLine  = (uint *)pixData.Data + pixData.WordsPerLine * y;
            uint pixValue;

            if (pixDepth == 1)
            {
                pixValue = PixData.GetDataBit(pixLine, x);
            }
            else if (pixDepth == 4)
            {
                pixValue = PixData.GetDataQBit(pixLine, x);
            }
            else if (pixDepth == 8)
            {
                pixValue = PixData.GetDataByte(pixLine, x);
            }
            else if (pixDepth == 32)
            {
                pixValue = PixData.GetDataFourByte(pixLine, x);
            }
            else
            {
                throw new ArgumentException(String.Format("Bit depth of {0} is not supported.", pix.Depth), "pix");
            }

            if (pix.Colormap != null)
            {
                return(pix.Colormap[(int)pixValue]);
            }
            else
            {
                if (pixDepth == 32)
                {
                    return(PixColor.FromRgba(pixValue));
                }
                else
                {
                    byte grayscale = (byte)(pixValue * 255 / ((1 << 16) - 1));
                    return(new PixColor(grayscale, grayscale, grayscale));
                }
            }
        }
        private static unsafe void TransferData16(PixData pixData, BitmapData imgData)
        {
            var imgFormat = imgData.PixelFormat;
            var height    = imgData.Height;
            var width     = imgData.Width;

            for (int y = 0; y < height; y++)
            {
                uint *  pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine);
                ushort *imgLine = (ushort *)imgData.Scan0 + (y * imgData.Stride);

                for (int x = 0; x < width; x++)
                {
                    ushort pixVal = (ushort)PixData.GetDataTwoByte(pixLine, x);

                    imgLine[x] = pixVal;
                }
            }
        }
Example #5
0
        private unsafe void TransferData1(PixData pixData, BitmapData imgData)
        {
            var imgFormat = imgData.PixelFormat;
            var height    = imgData.Height;
            var width     = imgData.Width / 8;

            for (int y = 0; y < height; y++)
            {
                uint *pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine);
                byte *imgLine = (byte *)imgData.Scan0 + (y * imgData.Stride);

                for (int x = 0; x < width; x++)
                {
                    byte pixVal = (byte)PixData.GetDataByte(pixLine, x);

                    imgLine[x] = pixVal;
                }
            }
        }
Example #6
0
        private static unsafe void TransferDataFormat32bppRgb(BitmapData imgData, PixData pixData)
        {
            int pixelFormat = (int)imgData.PixelFormat;
            int height      = imgData.Height;
            int width       = imgData.Width;

            for (int index1 = 0; index1 < height; ++index1)
            {
                byte *numPtr1 = (byte *)((IntPtr)(void *)imgData.Scan0 + index1 * imgData.Stride);
                uint *data    = (uint *)((IntPtr)(void *)pixData.Data + (index1 * pixData.WordsPerLine) * 4);
                for (int index2 = 0; index2 < width; ++index2)
                {
                    byte *numPtr2 = numPtr1 + (index2 << 2);
                    byte  blue    = *numPtr2;
                    byte  green   = numPtr2[1];
                    byte  red     = numPtr2[2];
                    PixData.SetDataFourByte(data, index2, BitmapHelper.EncodeAsRGBA(red, green, blue, byte.MaxValue));
                }
            }
        }
Example #7
0
        private unsafe void TransferDataFormat24bppRgb(BitmapData imgData, PixData pixData)
        {
            var imgFormat = imgData.PixelFormat;
            var height    = imgData.Height;
            var width     = imgData.Width;

            for (int y = 0; y < height; y++)
            {
                byte *imgLine = (byte *)imgData.Scan0 + (y * imgData.Stride);
                uint *pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine);

                for (int x = 0; x < width; x++)
                {
                    byte *pixelPtr = imgLine + x * 3;
                    byte  blue     = pixelPtr[0];
                    byte  green    = pixelPtr[1];
                    byte  red      = pixelPtr[2];
                    PixData.SetDataFourByte(pixLine, x, BitmapHelper.EncodeAsRGBA(red, green, blue, 255));
                }
            }
        }
Example #8
0
        private unsafe void TransferDataFormat32bppArgb(BitmapData imgData, PixData pixData)
        {
            var imgFormat = imgData.PixelFormat;
            var height    = imgData.Height;
            var width     = imgData.Width;

            for (int y = 0; y < height; y++)
            {
                byte *imgLine = (byte *)imgData.Scan0 + (y * imgData.Stride);
                uint *pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine);

                for (int x = 0; x < width; x++)
                {
                    byte *pixelPtr = imgLine + (x << 2);
                    byte  blue     = *pixelPtr;
                    byte  green    = *(pixelPtr + 1);
                    byte  red      = *(pixelPtr + 2);
                    byte  alpha    = *(pixelPtr + 3);
                    PixData.SetDataFourByte(pixLine, x, BitmapHelper.EncodeAsRGBA(red, green, blue, alpha));
                }
            }
        }
        private static unsafe void TransferData32(PixData pixData, BitmapData imgData, int alphaMask)
        {
            var imgFormat = imgData.PixelFormat;
            var height    = imgData.Height;
            var width     = imgData.Width;

            for (int y = 0; y < height; y++)
            {
                byte *imgLine = (byte *)imgData.Scan0 + (y * imgData.Stride);
                uint *pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine);

                for (int x = 0; x < width; x++)
                {
                    var pixVal = PixColor.FromRgba(pixLine[x]);

                    byte *pixelPtr = imgLine + (x << 2);
                    pixelPtr[0] = pixVal.Blue;
                    pixelPtr[1] = pixVal.Green;
                    pixelPtr[2] = pixVal.Red;
                    pixelPtr[3] = (byte)(alphaMask | pixVal.Alpha); // Allow user to include alpha or not
                }
            }
        }
Example #10
0
        public unsafe static OcrNode OCR(
            TesseractEngine engine,
            Mat input,
            int resolution)
        {
            using var pix = Pix.Create(input.Width, input.Height, 8);

            pix.XRes = resolution;
            pix.YRes = resolution;

            var pixData      = pix.GetData();
            var data         = (uint *)pixData.Data;
            var wordsPerLine = pixData.WordsPerLine;

            input.ForEachAsByte((value, position) =>
            {
                var y     = position[0];
                var x     = position[1];
                var color = *value;

                PixData.SetDataByte(data + y * wordsPerLine, x, color);
            });


            using var page = engine.Process(pix);

            var ocrPage = page.GetTsvText(1).
                          Split("\n", StringSplitOptions.RemoveEmptyEntries).
                          Select(line => line.Split("\t")).
                          Select(columns => new OcrNode
            {
                Level      = int.Parse(columns[0]),
                PageNum    = int.Parse(columns[1]),
                BlockNum   = int.Parse(columns[2]),
                ParNum     = int.Parse(columns[3]),
                LineNum    = int.Parse(columns[4]),
                WordNum    = int.Parse(columns[5]),
                Left       = int.Parse(columns[6]),
                Top        = int.Parse(columns[7]),
                Width      = int.Parse(columns[8]),
                Height     = int.Parse(columns[9]),
                Confidence = int.Parse(columns[10]),
                Text       = columns.Length > 11 ? columns[11] : null
            }).
                          Aggregate((prev, next) =>
            {
                if (prev == null)
                {
                    if (next.Level != 1)
                    {
                        throw new InvalidOperationException("nodes");
                    }
                }
                else
                {
                    if ((next.Level <= 0) || (next.Level > prev.Level + 1))
                    {
                        throw new InvalidOperationException("nodes");
                    }

                    while (next.Level != prev.Level + 1)
                    {
                        prev = prev.Parent;
                    }

                    next.Parent = prev;

                    if (prev.Children == null)
                    {
                        prev.Children = new List <OcrNode>();
                    }

                    next.Parent.Children.Add(next);
                }

                return(next);
            });

            while (ocrPage.Parent != null)
            {
                ocrPage = ocrPage.Parent;
            }

            ocrPage.Text = page.GetText();

            return(ocrPage);
        }