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