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