Back to index

lightning-sunbird  0.9+nobinonly
renderedge.c
Go to the documentation of this file.
00001 /*
00002  * $Id: renderedge.c,v 1.3.4.1 2005/09/27 23:09:03 vladimir%pobox.com Exp $
00003  *
00004  * Copyright © 2004 Keith Packard
00005  *
00006  * Permission to use, copy, modify, distribute, and sell this software and its
00007  * documentation for any purpose is hereby granted without fee, provided that
00008  * the above copyright notice appear in all copies and that both that
00009  * copyright notice and this permission notice appear in supporting
00010  * documentation, and that the name of Keith Packard not be used in
00011  * advertising or publicity pertaining to distribution of the software without
00012  * specific, written prior permission.  Keith Packard makes no
00013  * representations about the suitability of this software for any purpose.  It
00014  * is provided "as is" without express or implied warranty.
00015  *
00016  * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00017  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
00018  * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
00019  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
00020  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
00021  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00022  * PERFORMANCE OF THIS SOFTWARE.
00023  */
00024 
00025 #include "pixman-xserver-compat.h"
00026 
00027 /*
00028  * Compute the smallest value no less than y which is on a
00029  * grid row
00030  */
00031 
00032 xFixed
00033 RenderSampleCeilY (xFixed y, int n)
00034 {
00035     xFixed   f = xFixedFrac(y);
00036     xFixed   i = xFixedFloor(y);
00037     
00038     f = ((f + Y_FRAC_FIRST(n)) / STEP_Y_SMALL(n)) * STEP_Y_SMALL(n) + Y_FRAC_FIRST(n);
00039     if (f > Y_FRAC_LAST(n))
00040     {
00041        f = Y_FRAC_FIRST(n);
00042        i += xFixed1;
00043     }
00044     return (i | f);
00045 }
00046 
00047 #define _div(a,b)    ((a) >= 0 ? (a) / (b) : -((-(a) + (b) - 1) / (b)))
00048 
00049 /*
00050  * Compute the largest value no greater than y which is on a
00051  * grid row
00052  */
00053 xFixed
00054 RenderSampleFloorY (xFixed y, int n)
00055 {
00056     xFixed   f = xFixedFrac(y);
00057     xFixed   i = xFixedFloor (y);
00058     
00059     f = _div(f - Y_FRAC_FIRST(n), STEP_Y_SMALL(n)) * STEP_Y_SMALL(n) + Y_FRAC_FIRST(n);
00060     if (f < Y_FRAC_FIRST(n))
00061     {
00062        f = Y_FRAC_LAST(n);
00063        i -= xFixed1;
00064     }
00065     return (i | f);
00066 }
00067 
00068 /*
00069  * Step an edge by any amount (including negative values)
00070  */
00071 void
00072 RenderEdgeStep (RenderEdge *e, int n)
00073 {
00074     xFixed_48_16     ne;
00075 
00076     e->x += n * e->stepx;
00077     
00078     ne = e->e + n * (xFixed_48_16) e->dx;
00079     
00080     if (n >= 0)
00081     {
00082        if (ne > 0)
00083        {
00084            int nx = (ne + e->dy - 1) / e->dy;
00085            e->e = ne - nx * (xFixed_48_16) e->dy;
00086            e->x += nx * e->signdx;
00087        }
00088     }
00089     else
00090     {
00091        if (ne <= -e->dy)
00092        {
00093            int nx = (-ne) / e->dy;
00094            e->e = ne + nx * (xFixed_48_16) e->dy;
00095            e->x -= nx * e->signdx;
00096        }
00097     }
00098 }
00099 
00100 /*
00101  * A private routine to initialize the multi-step
00102  * elements of an edge structure
00103  */
00104 static void
00105 _RenderEdgeMultiInit (RenderEdge *e, int n, xFixed *stepx_p, xFixed *dx_p)
00106 {
00107     xFixed    stepx;
00108     xFixed_48_16     ne;
00109     
00110     ne = n * (xFixed_48_16) e->dx;
00111     stepx = n * e->stepx;
00112     if (ne > 0)
00113     {
00114        int nx = ne / e->dy;
00115        ne -= nx * e->dy;
00116        stepx += nx * e->signdx;
00117     }
00118     *dx_p = ne;
00119     *stepx_p = stepx;
00120 }
00121 
00122 /*
00123  * Initialize one edge structure given the line endpoints and a
00124  * starting y value
00125  */
00126 void
00127 RenderEdgeInit (RenderEdge  *e,
00128               int           n,
00129               xFixed        y_start,
00130               xFixed        x_top,
00131               xFixed        y_top,
00132               xFixed        x_bot,
00133               xFixed        y_bot)
00134 {
00135     xFixed    dx, dy;
00136 
00137     e->x = x_top;
00138     e->e = 0;
00139     dx = x_bot - x_top;
00140     dy = y_bot - y_top;
00141     e->dy = dy;
00142     if (dy)
00143     {
00144        if (dx >= 0)
00145        {
00146            e->signdx = 1;
00147            e->stepx = dx / dy;
00148            e->dx = dx % dy;
00149            e->e = -dy;
00150        }
00151        else
00152        {
00153            e->signdx = -1;
00154            e->stepx = -(-dx / dy);
00155            e->dx = -dx % dy;
00156            e->e = 0;
00157        }
00158     
00159        _RenderEdgeMultiInit (e, STEP_Y_SMALL(n), &e->stepx_small, &e->dx_small);
00160        _RenderEdgeMultiInit (e, STEP_Y_BIG(n), &e->stepx_big, &e->dx_big);
00161     }
00162     RenderEdgeStep (e, y_start - y_top);
00163 }
00164 
00165 /*
00166  * Initialize one edge structure given a line, starting y value
00167  * and a pixel offset for the line
00168  */
00169 void
00170 RenderLineFixedEdgeInit (RenderEdge *e,
00171                       int       n,
00172                       xFixed           y,
00173                       const xLineFixed *line,
00174                       int       x_off,
00175                       int       y_off)
00176 {
00177     xFixed    x_off_fixed = IntToxFixed(x_off);
00178     xFixed    y_off_fixed = IntToxFixed(y_off);
00179     const xPointFixed *top, *bot;
00180 
00181     if (line->p1.y <= line->p2.y)
00182     {
00183        top = &line->p1;
00184        bot = &line->p2;
00185     }
00186     else
00187     {
00188        top = &line->p2;
00189        bot = &line->p1;
00190     }
00191     RenderEdgeInit (e, n, y,
00192                   top->x + x_off_fixed,
00193                   top->y + y_off_fixed,
00194                   bot->x + x_off_fixed,
00195                   bot->y + y_off_fixed);
00196 }
00197