f78b12e570284aa8291f4ca1add24937fd107403vboxsync/*
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncCopyright 1987, 1998 The Open Group
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncPermission to use, copy, modify, distribute, and sell this software and its
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncdocumentation for any purpose is hereby granted without fee, provided that
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncthe above copyright notice appear in all copies and that both that
f78b12e570284aa8291f4ca1add24937fd107403vboxsynccopyright notice and this permission notice appear in supporting
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncdocumentation.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncThe above copyright notice and this permission notice shall be included
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncin all copies or substantial portions of the Software.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncOTHER DEALINGS IN THE SOFTWARE.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncExcept as contained in this notice, the name of The Open Group shall
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncnot be used in advertising or otherwise to promote the sale, use or
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncother dealings in this Software without prior written authorization
f78b12e570284aa8291f4ca1add24937fd107403vboxsyncfrom The Open Group.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync*/
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync#ifdef HAVE_DIX_CONFIG_H
f78b12e570284aa8291f4ca1add24937fd107403vboxsync#include <dix-config.h>
f78b12e570284aa8291f4ca1add24937fd107403vboxsync#endif
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync#ifndef SCANFILLINCLUDED
f78b12e570284aa8291f4ca1add24937fd107403vboxsync#define SCANFILLINCLUDED
f78b12e570284aa8291f4ca1add24937fd107403vboxsync/*
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * scanfill.h
f78b12e570284aa8291f4ca1add24937fd107403vboxsync *
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * Written by Brian Kelleher; Jan 1985
f78b12e570284aa8291f4ca1add24937fd107403vboxsync *
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * This file contains a few macros to help track
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * the edge of a filled object. The object is assumed
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * to be filled in scanline order, and thus the
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * algorithm used is an extension of Bresenham's line
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * drawing algorithm which assumes that y is always the
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * major axis.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * Since these pieces of code are the same for any filled shape,
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * it is more convenient to gather the library in one
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * place, but since these pieces of code are also in
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * the inner loops of output primitives, procedure call
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * overhead is out of the question.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * See the author for a derivation if needed.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync */
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync/*
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * In scan converting polygons, we want to choose those pixels
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * which are inside the polygon. Thus, we add .5 to the starting
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * x coordinate for both left and right edges. Now we choose the
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * first pixel which is inside the pgon for the left edge and the
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * first pixel which is outside the pgon for the right edge.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * Draw the left pixel, but not the right.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync *
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * How to add .5 to the starting x coordinate:
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * If the edge is moving to the right, then subtract dy from the
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * error term from the general form of the algorithm.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * If the edge is moving to the left, then add dy to the error term.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync *
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * The reason for the difference between edges moving to the left
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * and edges moving to the right is simple: If an edge is moving
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * to the right, then we want the algorithm to flip immediately.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * If it is moving to the left, then we don't want it to flip until
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * we traverse an entire pixel.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync */
f78b12e570284aa8291f4ca1add24937fd107403vboxsync#define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync int dx; /* local storage */ \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync\
f78b12e570284aa8291f4ca1add24937fd107403vboxsync /* \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * if the edge is horizontal, then it is ignored \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * and assumed not to be processed. Otherwise, do this stuff. \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync */ \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync if ((dy) != 0) { \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync xStart = (x1); \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync dx = (x2) - xStart; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync if (dx < 0) { \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync m = dx / (dy); \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync m1 = m - 1; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync incr1 = -2 * dx + 2 * (dy) * m1; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync incr2 = -2 * dx + 2 * (dy) * m; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync } else { \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync m = dx / (dy); \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync m1 = m + 1; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync incr1 = 2 * dx - 2 * (dy) * m1; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync incr2 = 2 * dx - 2 * (dy) * m; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync d = -2 * m * (dy) + 2 * dx; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync } \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync } \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync}
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync#define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync if (m1 > 0) { \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync if (d > 0) { \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync minval += m1; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync d += incr1; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync } \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync else { \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync minval += m; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync d += incr2; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync } \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync } else {\
f78b12e570284aa8291f4ca1add24937fd107403vboxsync if (d >= 0) { \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync minval += m1; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync d += incr1; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync } \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync else { \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync minval += m; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync d += incr2; \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync } \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync } \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync}
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync/*
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * This structure contains all of the information needed
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * to run the bresenham algorithm.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * The variables may be hardcoded into the declarations
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * instead of using this structure to make use of
f78b12e570284aa8291f4ca1add24937fd107403vboxsync * register declarations.
f78b12e570284aa8291f4ca1add24937fd107403vboxsync */
f78b12e570284aa8291f4ca1add24937fd107403vboxsynctypedef struct {
f78b12e570284aa8291f4ca1add24937fd107403vboxsync int minor; /* minor axis */
f78b12e570284aa8291f4ca1add24937fd107403vboxsync int d; /* decision variable */
f78b12e570284aa8291f4ca1add24937fd107403vboxsync int m, m1; /* slope and slope+1 */
f78b12e570284aa8291f4ca1add24937fd107403vboxsync int incr1, incr2; /* error increments */
f78b12e570284aa8291f4ca1add24937fd107403vboxsync} BRESINFO;
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync#define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync BRESINITPGON(dmaj, min1, min2, bres.minor, bres.d, \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync bres.m, bres.m1, bres.incr1, bres.incr2)
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync#define BRESINCRPGONSTRUCT(bres) \
f78b12e570284aa8291f4ca1add24937fd107403vboxsync BRESINCRPGON(bres.d, bres.minor, bres.m, bres.m1, bres.incr1, bres.incr2)
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync
f78b12e570284aa8291f4ca1add24937fd107403vboxsync#endif