Esempio n. 1
0
        public void TestMethod1()
        {
            Byte nExValue;

            for (double dValue = -256; dValue < 256;)
            {
                if (dValue > 255.0)
                {
                    nExValue = 255;
                }
                else if (dValue < 0.0)
                {
                    nExValue = 0;
                }
                else
                {
                    nExValue = (byte)dValue;
                }
                byte nAns = ComFunc.DoubleToByte(dValue);
                Assert.AreEqual(nExValue, nAns);
                dValue += 0.1;
            }
        }
    public override bool GoImgProc(CancellationToken _token)
    {
        bool bRst = true;

        short[,] nMask =
        {
            { 1,  1, 1 },
            { 1, -8, 1 },
            { 1,  1, 1 }
        };

        int nWidthSize  = base.m_bitmap.Width;
        int nHeightSize = base.m_bitmap.Height;
        int nMasksize   = nMask.GetLength(0);

        base.m_bitmapAfter = new Bitmap(base.m_bitmap);

        BitmapData bitmapData = base.m_bitmapAfter.LockBits(new Rectangle(0, 0, nWidthSize, nHeightSize), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

        int nIdxWidth;
        int nIdxHeight;

        unsafe
        {
            for (nIdxHeight = 0; nIdxHeight < nHeightSize; nIdxHeight++)
            {
                if (_token.IsCancellationRequested)
                {
                    bRst = false;
                    break;
                }

                for (nIdxWidth = 0; nIdxWidth < nWidthSize; nIdxWidth++)
                {
                    if (_token.IsCancellationRequested)
                    {
                        bRst = false;
                        break;
                    }

                    byte *pPixel = (byte *)bitmapData.Scan0 + nIdxHeight * bitmapData.Stride + nIdxWidth * 4;

                    long   lCalB   = 0;
                    long   lCalG   = 0;
                    long   lCalR   = 0;
                    double dCalAve = 0.0;
                    int    nIdxWidthMask;
                    int    nIdxHightMask;


                    for (nIdxHightMask = 0; nIdxHightMask < nMasksize; nIdxHightMask++)
                    {
                        for (nIdxWidthMask = 0; nIdxWidthMask < nMasksize; nIdxWidthMask++)
                        {
                            if (nIdxWidth + nIdxWidthMask > 0 &&
                                nIdxWidth + nIdxWidthMask < nWidthSize &&
                                nIdxHeight + nIdxHightMask > 0 &&
                                nIdxHeight + nIdxHightMask < nHeightSize)
                            {
                                byte *pPixel2 = (byte *)bitmapData.Scan0 + (nIdxHeight + nIdxHightMask) * bitmapData.Stride + (nIdxWidth + nIdxWidthMask) * 4;

                                lCalB = pPixel2[(int)ComInfo.Pixel.B] * nMask[nIdxWidthMask, nIdxHightMask];
                                lCalG = pPixel2[(int)ComInfo.Pixel.G] * nMask[nIdxWidthMask, nIdxHightMask];
                                lCalR = pPixel2[(int)ComInfo.Pixel.R] * nMask[nIdxWidthMask, nIdxHightMask];

                                double dcalGray = (lCalB + lCalG + lCalR) / 3;
                                dCalAve = (dCalAve + dcalGray) / 2;
                            }
                        }
                    }
                    byte nGrayScale = ComFunc.DoubleToByte(dCalAve);

                    pPixel[(int)ComInfo.Pixel.B] = nGrayScale;
                    pPixel[(int)ComInfo.Pixel.G] = nGrayScale;
                    pPixel[(int)ComInfo.Pixel.R] = nGrayScale;
                }
            }
            base.m_bitmapAfter.UnlockBits(bitmapData);
        }

        return(bRst);
    }
Esempio n. 3
0
    public override bool GoImgProc(CancellationToken _token)
    {
        bool bRst = true;

        short[,] nMask =
        {
            { 1,  1, 1 },
            { 1, -8, 1 },
            { 1,  1, 1 }
        };
        int nWidthSize  = base.m_bitmap.PixelWidth;
        int nHeightSize = base.m_bitmap.PixelHeight;
        int nMasksize   = nMask.GetLength(0);

        base.m_wBitmap = new WriteableBitmap(base.m_bitmap);
        base.m_wBitmap.Lock();

        int nIdxWidth;
        int nIdxHeight;

        unsafe
        {
            for (nIdxHeight = 0; nIdxHeight < nHeightSize; nIdxHeight++)
            {
                if (_token.IsCancellationRequested)
                {
                    bRst = false;
                    break;
                }

                for (nIdxWidth = 0; nIdxWidth < nWidthSize; nIdxWidth++)
                {
                    if (_token.IsCancellationRequested)
                    {
                        bRst = false;
                        break;
                    }

                    byte *pPixel = (byte *)base.m_wBitmap.BackBuffer + nIdxHeight * base.m_wBitmap.BackBufferStride + nIdxWidth * 4;

                    long   lCalB   = 0;
                    long   lCalG   = 0;
                    long   lCalR   = 0;
                    double dCalAve = 0.0;
                    int    nIdxWidthMask;
                    int    nIdxHightMask;


                    for (nIdxHightMask = 0; nIdxHightMask < nMasksize; nIdxHightMask++)
                    {
                        for (nIdxWidthMask = 0; nIdxWidthMask < nMasksize; nIdxWidthMask++)
                        {
                            if (nIdxWidth + nIdxWidthMask > 0 &&
                                nIdxWidth + nIdxWidthMask < nWidthSize &&
                                nIdxHeight + nIdxHightMask > 0 &&
                                nIdxHeight + nIdxHightMask < nHeightSize)
                            {
                                byte *pPixel2 = (byte *)m_wBitmap.BackBuffer + (nIdxHeight + nIdxHightMask) * m_wBitmap.BackBufferStride + (nIdxWidth + nIdxWidthMask) * 4;

                                lCalB = pPixel2[(int)ComInfo.Pixel.B] * nMask[nIdxWidthMask, nIdxHightMask];
                                lCalG = pPixel2[(int)ComInfo.Pixel.G] * nMask[nIdxWidthMask, nIdxHightMask];
                                lCalR = pPixel2[(int)ComInfo.Pixel.R] * nMask[nIdxWidthMask, nIdxHightMask];

                                double dcalGray = (lCalB + lCalG + lCalR) / 3;
                                dCalAve = (dCalAve + dcalGray) / 2;
                            }
                        }
                    }
                    byte nGrayScale = ComFunc.DoubleToByte(dCalAve);

                    pPixel[(int)ComInfo.Pixel.B] = nGrayScale;
                    pPixel[(int)ComInfo.Pixel.G] = nGrayScale;
                    pPixel[(int)ComInfo.Pixel.R] = nGrayScale;
                }
            }
            base.m_wBitmap.AddDirtyRect(new Int32Rect(0, 0, nWidthSize, nHeightSize));
            base.m_wBitmap.Unlock();
            base.m_wBitmap.Freeze();
        }

        return(bRst);
    }
    /// <summary>
    /// グレースケール2次微分の実行
    /// </summary>
    /// <param name="_token">キャンセルトークン</param>
    /// <returns>実行結果 成功/失敗</returns>
    public override bool GoImgProc(CancellationToken _token)
    {
        bool bRst = true;

        base.m_nStatus = (int)ComInfo.ImageProcStatus.Implemented;

        short[,] nMask =
        {
            { 1,  1, 1 },
            { 1, -8, 1 },
            { 1,  1, 1 }
        };

        int nIdxWidth;
        int nIdxHeight;
        int nMasksize = nMask.GetLength(0);

        unsafe
        {
            using (var buffer = base.m_softwareBitmap.LockBuffer(BitmapBufferAccessMode.ReadWrite))
                using (var reference = buffer.CreateReference())
                {
                    if (reference is IMemoryBufferByteAccess)
                    {
                        byte *pData;
                        uint  nCapacity;
                        ((IMemoryBufferByteAccess)reference).GetBuffer(out pData, out nCapacity);

                        var desc = buffer.GetPlaneDescription(0);

                        for (nIdxHeight = 0; nIdxHeight < desc.Height; nIdxHeight++)
                        {
                            if (_token.IsCancellationRequested)
                            {
                                bRst           = false;
                                base.m_nStatus = (int)ComInfo.ImageProcStatus.NotImplemented;
                                break;
                            }

                            for (nIdxWidth = 0; nIdxWidth < desc.Width; nIdxWidth++)
                            {
                                if (_token.IsCancellationRequested)
                                {
                                    bRst           = false;
                                    base.m_nStatus = (int)ComInfo.ImageProcStatus.NotImplemented;
                                    break;
                                }

                                var nPixel = desc.StartIndex + desc.Stride * nIdxHeight + 4 * nIdxWidth;

                                long   lCalB   = 0;
                                long   lCalG   = 0;
                                long   lCalR   = 0;
                                double dCalAve = 0.0;
                                int    nIdxWidthMask;
                                int    nIdxHightMask;

                                for (nIdxHightMask = 0; nIdxHightMask < nMasksize; nIdxHightMask++)
                                {
                                    for (nIdxWidthMask = 0; nIdxWidthMask < nMasksize; nIdxWidthMask++)
                                    {
                                        if (nIdxWidth + nIdxWidthMask > 0 &&
                                            nIdxWidth + nIdxWidthMask < desc.Width &&
                                            nIdxHeight + nIdxHightMask > 0 &&
                                            nIdxHeight + nIdxHightMask < desc.Height)
                                        {
                                            var nPixel2 = desc.StartIndex + desc.Stride * (nIdxHeight + nIdxHightMask) + 4 * (nIdxWidth + nIdxWidthMask);

                                            lCalB = pData[nPixel2 + (int)ComInfo.Pixel.B] * nMask[nIdxWidthMask, nIdxHightMask];
                                            lCalG = pData[nPixel2 + (int)ComInfo.Pixel.G] * nMask[nIdxWidthMask, nIdxHightMask];
                                            lCalR = pData[nPixel2 + (int)ComInfo.Pixel.R] * nMask[nIdxWidthMask, nIdxHightMask];

                                            double dcalGray = (lCalB + lCalG + lCalR) / 3;
                                            dCalAve = (dCalAve + dcalGray) / 2;
                                        }
                                    }
                                }
                                byte nGrayScale = ComFunc.DoubleToByte(dCalAve);

                                pData[nPixel + (int)ComInfo.Pixel.B] = nGrayScale;
                                pData[nPixel + (int)ComInfo.Pixel.G] = nGrayScale;
                                pData[nPixel + (int)ComInfo.Pixel.R] = nGrayScale;
                            }
                        }
                    }
                }
        }

        return(bRst);
    }