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