--
--	rastergraphics: raster graphics primitives
--	Copyright (C) 2007  David Wood <dcw@kent.ac.uk>
--
--	This library is free software; you can redistribute it and/or
--	modify it under the terms of the GNU Lesser General Public
--	License as published by the Free Software Foundation, either
--	version 2 of the License, or (at your option) any later version.
--
--	This library 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
--	Lesser General Public License for more details.
--
--	You should have received a copy of the GNU Lesser General Public
--	License along with this library.  If not, see
--	<http://www.gnu.org/licenses/>.
--

--** @module rastergraphics

--* Dither
--
--  @param n Order
--  @param i, j Coordinates
--  @return Dither

INT FUNCTION dither (VAL INT n, i, j)

  INT r:
  VALOF
    INITIAL INT p IS i >< j:
    INITIAL INT q IS i:
    SEQ
      r := 0
      SEQ k = 0 FOR n
        SEQ
          r := (r << 1) \/ (p /\ 1)
          p := p >> 1
          r := (r << 1) \/ (q /\ 1)
          q := q >> 1
    RESULT r
:

--* Reverse Dither
--
--  @param n Order
--  @param k Count
--  @returns i, j Coordinates

INT, INT FUNCTION undither (VAL INT n, k)

  INT p, q:
  VALOF
    INITIAL INT r IS k:
    SEQ
      p, q := 0, 0
      SEQ j = 0 FOR n
        SEQ
          p := (p << 1) \/ (r /\ 1)
          r := r >> 1
          q := (q << 1) \/ (r /\ 1)
          r := r >> 1
    RESULT p, p >< q
:
