Rename GP_Context -> GP_Pixmap
[gfxprim.git] / include / core / GP_MixPixels.gen.h.t
blob5d06e43af99090ce8010dab0ab6684cfe83dd62f
1 @ include header.t
2 /*
3  * Macros to mix two pixels accordingly to percentage.
4  *
5  * Copyright (C) 2011-2014 Cyril Hrubis <metan@ucw.cz>
6  */
8 #include "core/GP_Pixmap.h"
9 #include "core/GP_Pixel.h"
10 #include "core/GP_GetPutPixel.h"
11 #include "core/GP_GammaCorrection.h"
13 @ for pt in pixeltypes:
14 @     if not pt.is_unknown():
17  * Mixes two {{ pt.name }} pixels.
18  *
19  * The percentage is expected as 8 bit unsigned integer [0 .. 255]
20  */
21 #define GP_MIX_PIXELS_LINEAR_{{ pt.name }}(pix1, pix2, perc) ({ \
22 @         for c in pt.chanslist:
23         GP_Pixel {{ c[0] }}; \
25         {{ c[0] }}  = GP_Pixel_GET_{{ c[0] }}_{{ pt.name }}(pix1) * (perc); \
26         {{ c[0] }} += GP_Pixel_GET_{{ c[0] }}_{{ pt.name }}(pix2) * (255 - (perc)); \
27         {{ c[0] }} = ({{ c[0] }} + 128) / 255; \
29 @         end
31         GP_Pixel_CREATE_{{ pt.name }}({{ ', '.join(pt.chan_names) }}); \
35  * Mixes two {{ pt.name }} pixels.
36  *
37  * The percentage is expected as 8 bit unsigned integer [0 .. 255]
38  */
39 #define GP_MIX_PIXELS_GAMMA_{{ pt.name }}(pix1, pix2, perc) ({ \
40 @         for c in pt.chanslist:
41         GP_Pixel {{ c[0] }}; \
43         {{ c[0] }}  = GP_Gamma{{ c[2] }}ToLinear10(GP_Pixel_GET_{{ c[0] }}_{{ pt.name }}(pix1)) * (perc); \
44         {{ c[0] }} += GP_Gamma{{ c[2] }}ToLinear10(GP_Pixel_GET_{{ c[0] }}_{{ pt.name }}(pix2)) * (255 - (perc)); \
45         {{ c[0] }} = ({{ c[0] }} + 128) / 255; \
46         {{ c[0] }} = GP_Linear10ToGamma{{ c[2] }}({{ c[0] }}); \
48 @         end
50         GP_Pixel_CREATE_{{ pt.name }}({{ ", ".join(pt.chan_names) }}); \
53 #define GP_MIX_PIXELS_{{ pt.name }}(pix1, pix2, perc) \
54 @         if pt.is_rgb():
55         GP_MIX_PIXELS_LINEAR_{{ pt.name }}(pix1, pix2, perc)
56 @         else:
57         GP_MIX_PIXELS_LINEAR_{{ pt.name }}(pix1, pix2, perc)
58 @ end
60 static inline GP_Pixel GP_MixPixels(GP_Pixel pix1, GP_Pixel pix2,
61                                     uint8_t perc, GP_PixelType pixel_type)
63         switch (pixel_type) {
64 @ for pt in pixeltypes:
65 @     if not pt.is_unknown():
66         case GP_PIXEL_{{ pt.name }}:
67                 return GP_MIX_PIXELS_LINEAR_{{ pt.name }}(pix1, pix2, perc);
68 @ end
69         default:
70                 GP_ABORT("Unknown pixeltype");
71         }
75 @ for pt in pixeltypes:
76 @     if not pt.is_unknown():
77 static inline void GP_MixPixel_Raw_{{ pt.name }}(GP_Pixmap *pixmap,
78                         GP_Coord x, GP_Coord y, GP_Pixel pixel, uint8_t perc)
80         GP_Pixel pix = GP_GetPixel_Raw_{{ pt.pixelsize.suffix }}(pixmap, x, y);
81         pix = GP_MIX_PIXELS_{{ pt.name }}(pixel, pix, perc);
82         GP_PutPixel_Raw_{{ pt.pixelsize.suffix }}(pixmap, x, y, pix);
85 @ end
87 @ for pt in pixeltypes:
88 @     if not pt.is_unknown():
89 static inline void GP_MixPixel_Raw_Clipped_{{ pt.name }}(GP_Pixmap *pixmap,
90                         GP_Coord x, GP_Coord y, GP_Pixel pixel, uint8_t perc)
92         if (GP_PIXEL_IS_CLIPPED(pixmap, x, y))
93                 return;
95         GP_MixPixel_Raw_{{ pt.name }}(pixmap, x, y, pixel, perc);
98 @ end
100 static inline void GP_MixPixel_Raw(GP_Pixmap *pixmap, GP_Coord x, GP_Coord y,
101                                    GP_Pixel pixel, uint8_t perc)
103         switch (pixmap->pixel_type) {
104 @ for pt in pixeltypes:
105 @     if not pt.is_unknown():
106         case GP_PIXEL_{{ pt.name }}:
107                                 GP_MixPixel_Raw_{{ pt.name }}(pixmap, x, y, pixel, perc);
108         break;
109 @ end
110         default:
111                 GP_ABORT("Unknown pixeltype");
112         }
115 static inline void GP_MixPixel_Raw_Clipped(GP_Pixmap *pixmap,
116                                            GP_Coord x, GP_Coord y,
117                                            GP_Pixel pixel, uint8_t perc)
119         switch (pixmap->pixel_type) {
120 @ for pt in pixeltypes:
121 @     if not pt.is_unknown():
122         case GP_PIXEL_{{ pt.name }}:
123                 GP_MixPixel_Raw_Clipped_{{ pt.name }}(pixmap, x, y, pixel, perc);
124         break;
125 @ end
126         default:
127                 GP_ABORT("Unknown pixeltype");
128         }