/* 
 * PROJECT: NyARToolkitCS(Extension)
 * --------------------------------------------------------------------------------
 * The NyARToolkitCS is C# edition ARToolKit class library.
 * Copyright (C)2008-2009 Ryo Iizuka
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For further information please contact.
 *	http://nyatla.jp/nyatoolkit/
 *	<airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
 * 
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace jp.nyatla.nyartoolkit.cs.core
{
    /**
     * Roberts法で勾配を計算します。
     * 出力画像のピクセルは、X,Y軸方向に-1され、下端、右端の画素は無効な値が入ります。
     * X=|-1, 0|  Y=|0,-1|
     *   | 0, 1|    |1, 0|
     * V=sqrt(X^2+Y+2)/2
     */
    public class NyARRasterFilter_Roberts : INyARRasterFilter
    {
        private IdoFilterImpl _do_filter_impl;
        public NyARRasterFilter_Roberts(int i_raster_type)
        {
            switch (i_raster_type)
            {
                case INyARBufferReader.BUFFERFORMAT_INT1D_GRAY_8:
                    this._do_filter_impl = new IdoFilterImpl_GRAY_8();
                    break;
                default:
                    throw new NyARException();
            }
        }
        public void doFilter(INyARRaster i_input, INyARRaster i_output)
        {
            this._do_filter_impl.doFilter(i_input.getBufferReader(), i_output.getBufferReader(), i_input.getSize());
        }

        interface IdoFilterImpl
        {
            void doFilter(INyARBufferReader i_input, INyARBufferReader i_output, NyARIntSize i_size);
        }
        class IdoFilterImpl_GRAY_8 : IdoFilterImpl
        {
            public void doFilter(INyARBufferReader i_input, INyARBufferReader i_output, NyARIntSize i_size)
            {
                Debug.Assert(i_input.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_GRAY_8));
                Debug.Assert(i_output.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_GRAY_8));
                int[] in_ptr = (int[])i_input.getBuffer();
                int[] out_ptr = (int[])i_output.getBuffer();
                int width = i_size.w;
                int height = i_size.h;
                for (int y = 0; y < height - 1; y++)
                {
                    int idx = y * width;
                    int p00 = in_ptr[idx];
                    int p10 = in_ptr[width + idx];
                    int p01, p11;
                    for (int x = 0; x < width - 1; x++)
                    {
                        p01 = in_ptr[idx + 1];
                        p11 = in_ptr[idx + width + 1];
                        int fx = p11 - p00;
                        int fy = p10 - p01;
                        out_ptr[idx] = (int)Math.Sqrt(fx * fx + fy * fy) >> 1;
                        p00 = p01;
                        p10 = p11;
                        idx++;
                    }
                }
                return;
            }
        }
    }

}
