Rename GP_Context -> GP_Pixmap
[gfxprim.git] / libs / gfx / GP_HLineAA.gen.c.t
blob49c40ef77ebd8015572887f4b917fe57bf36dae8
1 /*****************************************************************************
2  * This file is part of gfxprim library.                                     *
3  *                                                                           *
4  * Gfxprim is free software; you can redistribute it and/or                  *
5  * modify it under the terms of the GNU Lesser General Public                *
6  * License as published by the Free Software Foundation; either              *
7  * version 2.1 of the License, or (at your option) any later version.        *
8  *                                                                           *
9  * Gfxprim is distributed in the hope that it will be useful,                *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
12  * Lesser General Public License for more details.                           *
13  *                                                                           *
14  * You should have received a copy of the GNU Lesser General Public          *
15  * License along with gfxprim; if not, write to the Free Software            *
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor,                        *
17  * Boston, MA  02110-1301  USA                                               *
18  *                                                                           *
19  * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz>                       *
20  *                                                                           *
21  *****************************************************************************/
23 /* Anti Aliased Horizontal Line */
25 #include "core/GP_Pixmap.h"
26 #include "core/GP_MixPixels.h"
27 #include "core/GP_FixedPoint.h"
28 #include "core/GP_GammaCorrection.h"
30 #define FP_TO_PERC(a) (GP_FP_ROUND_TO_INT((a) * 255))
33  * Computes pixel in buffer that we should start drawing in.
34  *
35  * This is different at the start and the end of the HLine. The difference is
36  * in where do integer coordinates belong (gfxprim puts integer coordinates in
37  * the middle of pixels this is where the +1/2 comes from.
38  */
39 #define TO_X_S(x) GP_FP_FLOOR_TO_INT((x) + GP_FP_1_2)
40 #define TO_X_E(x) GP_FP_CEIL_TO_INT((x) - GP_FP_1_2)
42 void GP_HLineAA_Raw(GP_Pixmap *pixmap, GP_Coord x0, GP_Coord x1,
43                     GP_Coord y, GP_Pixel pixel)
45         /* Nothing to draw */
46         if (x0 == x1)
47                 return;
49         if (x1 < x0)
50                 GP_SWAP(x1, x0);
52         GP_Coord int_x0 = TO_X_S(x0);
53         GP_Coord int_x1 = TO_X_E(x1);
54         GP_Coord int_y  = GP_FP_FLOOR_TO_INT(y);
56 //      printf("%f %f %f -> %i %i %i\n", GP_FP_TO_FLOAT(x0), GP_FP_TO_FLOAT(x1), GP_FP_TO_FLOAT(y), int_x0, int_x1, int_y);
58         /* Draw the four starting and ending pixels */
59         unsigned int perc;
60         unsigned int w;
62         w = GP_FP_RFRAC(x0 + GP_FP_1_2);
64         perc = FP_TO_PERC(GP_FP_MUL(GP_FP_RFRAC(y), w));
65         GP_MixPixel_Raw_Clipped(pixmap, int_x0, int_y, pixel, perc);
67         perc = FP_TO_PERC(GP_FP_MUL(GP_FP_FRAC(y), w));
68         GP_MixPixel_Raw_Clipped(pixmap, int_x0, int_y+1, pixel, perc);
70         if (int_x0 != int_x1) {
71                 w = GP_FP_FRAC(x1 + GP_FP_1_2);
73                 perc = FP_TO_PERC(GP_FP_MUL(GP_FP_RFRAC(y), w));
74                 GP_MixPixel_Raw_Clipped(pixmap, int_x1, int_y, pixel, perc);
76                 perc = FP_TO_PERC(GP_FP_MUL(GP_FP_FRAC(y), w));
77                 GP_MixPixel_Raw_Clipped(pixmap, int_x1, int_y+1, pixel, perc);
78         }
80         GP_Coord x;
82         /* Now fill the inner part of the HLine */
83         uint8_t up = FP_TO_PERC(GP_FP_RFRAC(y));
84         uint8_t lp = FP_TO_PERC(GP_FP_FRAC(y));
86         for (x = int_x0 + 1; x < int_x1; x++) {
87                 GP_MixPixel_Raw_Clipped(pixmap, x, int_y, pixel, up);
88                 GP_MixPixel_Raw_Clipped(pixmap, x, int_y+1, pixel, lp);
89         }