Beispiel #1
0
        // 引数で渡されたビットマップ画像を 2 値化します
        public static Bitmap Apply(Bitmap source, byte threshold = 128)
        {
            // ビットマップ画像から全てのピクセルを抜き出す
            PixelManipulator s = PixelManipulator.LoadBitmap(source);
            PixelManipulator d = s.Clone();

            // しきい値の設定
            byte[] thr_array = new byte[256];
            int    i;

            for (i = 0; i < threshold; i++)
            {
                thr_array[i] = (byte)0;
            }
            for (; i < 256; i++)
            {
                thr_array[i] = (byte)255;
            }

            // 全てのピクセルを巡回する
            s.EachPixel((x, y) => {
                // 2 値化する
                // グレースケール化されてることが前提なのでrgbは同じ値と仮定
                byte color = thr_array[s.R(x, y)];
                d.SetPixel(x, y, color, color, color);
            });

            // 新しいビットマップ画像を作成して、ピクセルをセットする
            return(d.CreateBitmap());
        }
Beispiel #2
0
        // 引数で渡されたビットマップ画像にメディアンフィルタを適用します
        public static Bitmap Apply(Bitmap source, int size = 3)
        {
            // ビットマップ画像から全てのピクセルを抜き出す
            PixelManipulator s = PixelManipulator.LoadBitmap(source);
            PixelManipulator d = s.Clone();

            // 範囲チェック
            if (size < 3)
            {
                size = 3;
            }
            if (size > 9)
            {
                size = 9;
            }
            size--;
            size /= 2;

            // 全てのピクセルを巡回する
            s.EachPixel((x, y) => {
                byte r = _Median(s.RangeR(x, y, size));
                byte g = _Median(s.RangeG(x, y, size));
                byte b = _Median(s.RangeB(x, y, size));
                d.SetPixel(x, y, r, g, b);
            });

            // 新しいビットマップ画像を作成して、ピクセルをセットする
            return(d.CreateBitmap());
        }
Beispiel #3
0
        /*********************************
        *
        * bitmapからぱずぷれ形式のtxtを生成
        *
        * 引数
        *	mozaic	:	変換対象のbitmap
        *	size	:	マスのサイズ
        *
        * *******************************/
        public static void makeBoardFromDot(Bitmap mozaic, int size = 6)
        {
            // ビットマップ画像から全てのピクセルを抜き出す
            PixelManipulator s = PixelManipulator.LoadBitmap(mozaic);

            // 範囲チェック
            if (size < 1)
            {
                size = 1;
            }
            if (size > 32)
            {
                size = 32;
            }
            int w = size * 2 + 1;

            Tapa.MAX_BOARD_ROW = s.height / w;                          // 問題の行数
            Tapa.MAX_BOARD_COL = s.width / w;                           // 問題の列数
            Tapa.BOX_SUM       = Tapa.MAX_BOARD_ROW * Tapa.MAX_BOARD_COL;

            Tapa.resetBoard();

            // 全てのピクセルを巡回する
            Box.during_make_inputbord = true;
            s.EachPixel((x, y) => {
                // 確認の終わったモザイクマスは飛ばす
                if (x % w != 0 || y % w != 0)
                {
                    return;
                }
                // 2値画像なので、rのみのチェックでok
                Tapa.box[y / w][x / w].Color = (s.R(x, y) == 0) ? Box.BLACK : Box.NOCOLOR;
            });
            Box.during_make_inputbord = false;
        }
Beispiel #4
0
 // 範囲を塗りつぶす処理
 private static void _Fill(PixelManipulator d, int dx, int dy, int size, byte r, byte g, byte b)
 {
     for (int y = -size; y <= size; y++) {
         for (int x = -size; x <= size; x++) {
             if (y == -size || y == size || x == -size || x == size) {
                 d.SetPixel(dx + x, dy + y, 0, 255, 0);
             }
             else { d.SetPixel(dx + x, dy + y, r, g, b); }
         }
     }
 }
Beispiel #5
0
 // PixelManipulator をコピーする
 public PixelManipulator Clone(bool copyBytes = false)
 {
     PixelManipulator result = new PixelManipulator();
     result.width = width;
     result.height = height;
     result.stride = stride;
     result.pixelSize = pixelSize;
     result.bytes = new byte[bytes.Length];
     if (copyBytes) {
         Array.Copy(bytes, result.bytes, bytes.Length);
     }
     return result;
 }
Beispiel #6
0
        // Bitmap オブジェクトから PixelManipulator を作成する
        public static PixelManipulator LoadBitmap(Bitmap bitmap)
        {
            if (bitmap == null) {
                return null;
            }

            PixelManipulator result = new PixelManipulator();
            result.width = bitmap.Width;
            result.height = bitmap.Height;
            result.stride = _GetScanLineSize(bitmap, result.pixelFormat);
            result.pixelSize = Image.GetPixelFormatSize(result.pixelFormat) / 8;
            result.bytes = _GetPixels(bitmap, result.pixelFormat);
            return result;
        }
Beispiel #7
0
        // PixelManipulator をコピーする
        public PixelManipulator Clone(bool copyBytes = false)
        {
            PixelManipulator result = new PixelManipulator();

            result.width     = width;
            result.height    = height;
            result.stride    = stride;
            result.pixelSize = pixelSize;
            result.bytes     = new byte[bytes.Length];
            if (copyBytes)
            {
                Array.Copy(bytes, result.bytes, bytes.Length);
            }
            return(result);
        }
Beispiel #8
0
        // Bitmap オブジェクトから PixelManipulator を作成する
        public static PixelManipulator LoadBitmap(Bitmap bitmap)
        {
            if (bitmap == null)
            {
                return(null);
            }

            PixelManipulator result = new PixelManipulator();

            result.width     = bitmap.Width;
            result.height    = bitmap.Height;
            result.stride    = _GetScanLineSize(bitmap, result.pixelFormat);
            result.pixelSize = Image.GetPixelFormatSize(result.pixelFormat) / 8;
            result.bytes     = _GetPixels(bitmap, result.pixelFormat);
            return(result);
        }
Beispiel #9
0
 // 範囲を塗りつぶす処理
 private static void _Fill(PixelManipulator d, int dx, int dy, int size, byte r, byte g, byte b)
 {
     for (int y = -size; y <= size; y++)
     {
         for (int x = -size; x <= size; x++)
         {
             if (y == -size || y == size || x == -size || x == size)
             {
                 d.SetPixel(dx + x, dy + y, 0, 255, 0);
             }
             else
             {
                 d.SetPixel(dx + x, dy + y, r, g, b);
             }
         }
     }
 }
Beispiel #10
0
        // 引数で渡されたビットマップ画像にモザイクフィルタを適用します
        public static Bitmap Apply(Bitmap source, float rate = 50f, int size = 6)
        {
            // ビットマップ画像から全てのピクセルを抜き出す
            PixelManipulator s = PixelManipulator.LoadBitmap(source);
            PixelManipulator d = s.Clone();

            rate /= 1000;

            // 範囲チェック
            if (size < 1)
            {
                size = 1;
            }
            if (size > 32)
            {
                size = 32;
            }
            int w = size * 2 + 1;

            Tapa.MAX_BOARD_ROW = s.height / w;                          // 問題の行数
            Tapa.MAX_BOARD_COL = s.width / w;                           // 問題の列数
            Tapa.BOX_SUM       = Tapa.MAX_BOARD_ROW * Tapa.MAX_BOARD_COL;

            // 全てのピクセルを巡回する
            s.EachPixel((x, y) => {
                // 塗り終わったところを飛ばす
                if (x % w != 0 || y % w != 0)
                {
                    return;
                }

                // モザイクに色を塗る
                byte r = _SMA(s.RangeR(x, y, size), rate);
                byte g = _SMA(s.RangeG(x, y, size), rate);
                byte b = _SMA(s.RangeB(x, y, size), rate);
                _Fill(d, x, y, size, r, g, b);
                // MozaicFilter._Fill(d, x, y, size, s.R(x,y), s.G(x,y), s.B(x,y));
            });

            // 新しいビットマップ画像を作成して、ピクセルをセットする
            return(d.CreateBitmap());
        }
Beispiel #11
0
        // 引数で渡されたビットマップ画像をグレースケール化します
        public static Bitmap Apply(Bitmap source)
        {
            // ビットマップ画像から全てのピクセルを抜き出す
            PixelManipulator s = PixelManipulator.LoadBitmap(source);
            PixelManipulator d = s.Clone();

            // 全てのピクセルを巡回する
            s.EachPixel((x, y) => {
                // グレースケールにする
                byte r     = s.R(x, y);
                byte g     = s.G(x, y);
                byte b     = s.B(x, y);
                byte color = _GrayScale(r, g, b);

                d.SetPixel(x, y, color, color, color);
            });

            // 新しいビットマップ画像を作成して、ピクセルをセットする
            return(d.CreateBitmap());
        }
Beispiel #12
0
        // 引数で渡されたビットマップ画像にラプラシアンフィルタを適用します
        public static Bitmap Apply(Bitmap source)
        {
            // ビットマップ画像から全てのピクセルを抜き出す
            PixelManipulator s = PixelManipulator.LoadBitmap(source);
            PixelManipulator d = s.Clone();

            // フィルタを作成する
            float[] filter = _CreateLaplacianFilter();

            // 全てのピクセルを巡回する
            s.EachPixel((x, y) => {
                byte r = _Laplacian(filter, s.RangeR(x, y, 1));
                byte g = _Laplacian(filter, s.RangeG(x, y, 1));
                byte b = _Laplacian(filter, s.RangeB(x, y, 1));
                d.SetPixel(x, y, r, g, b);
            });

            // 新しいビットマップ画像を作成して、ピクセルをセットする
            return(d.CreateBitmap());
        }