Rename GP_Context -> GP_Pixmap
[gfxprim.git] / libs / filters / arithmetic_filter.t
blob4580e690ada25f5bb429cdb670fee2f7c5294770
1 @ def filter_arithmetic(name, filter_op, opts='', params=''):
2 #include "core/GP_Pixmap.h"
3 #include "core/GP_Pixel.h"
4 #include "core/GP_GetPutPixel.h"
5 #include "core/GP_Debug.h"
6 #include "filters/GP_Filter.h"
7 #include "filters/GP_Arithmetic.h"
9 @     for pt in pixeltypes:
10 @         if not pt.is_unknown():
11 static int GP_Filter{{ name }}_{{ pt.name }}(const GP_Pixmap *src_a, const GP_Pixmap *src_b,
12         GP_Pixmap *dst, {{ maybe_opts_r(opts) }}GP_ProgressCallback *callback)
14         uint32_t x, y, w, h;
16         w = GP_MIN(src_a->w, src_b->w);
17         h = GP_MIN(src_a->h, src_b->h);
19         for (y = 0; y < h; y++) {
20                 for (x = 0; x < w; x++) {
21                         GP_Pixel pix_a = GP_GetPixel_Raw_{{ pt.pixelsize.suffix }}(src_a, x, y);
22                         GP_Pixel pix_b = GP_GetPixel_Raw_{{ pt.pixelsize.suffix }}(src_b, x, y);
24 @             for c in pt.chanslist:
25                         int32_t {{ c.name }}_A = GP_Pixel_GET_{{ c.name }}_{{ pt.name }}(pix_a);
26                         int32_t {{ c.name }}_B = GP_Pixel_GET_{{ c.name }}_{{ pt.name }}(pix_b);
27 @             end
29 @             for c in pt.chanslist:
30                         int32_t {{ c.name }};
31                         {@ filter_op(c.name, c.size) @}
32 @             end
34                         GP_Pixel pix;
35                         pix = GP_Pixel_CREATE_{{ pt.name }}({{ arr_to_params(pt.chan_names) }});
37                         GP_PutPixel_Raw_{{ pt.pixelsize.suffix }}(dst, x, y, pix);
38                 }
40                 if (GP_ProgressCallbackReport(callback, y, h, w))
41                         return 1;
42         }
44         GP_ProgressCallbackDone(callback);
45         return 0;
48 @     end
50 int GP_Filter{{ name }}_Raw(const GP_Pixmap *src_a, const GP_Pixmap *src_b,
51         GP_Pixmap *dst{{ maybe_opts_l(opts) }}, GP_ProgressCallback *callback)
53         GP_DEBUG(1, "Running filter {{ name }}");
55         switch (src_a->pixel_type) {
56 @     for pt in pixeltypes:
57 @         if not pt.is_unknown():
58         case GP_PIXEL_{{ pt.name }}:
59                 return GP_Filter{{ name }}_{{ pt.name }}(src_a, src_b, dst{{ maybe_opts_l(params) }}, callback);
60 @     end
61         default:
62         break;
63         }
65         return 1;
68 int GP_Filter{{ name }}(const GP_Pixmap *src_a, const GP_Pixmap *src_b,
69                         GP_Pixmap *dst{{ maybe_opts_l(opts) }},
70                         GP_ProgressCallback *callback)
72         GP_Size w = GP_MIN(src_a->w, src_b->w);
73         GP_Size h = GP_MIN(src_a->h, src_b->h);
75         GP_ASSERT(src_a->pixel_type == dst->pixel_type,
76                   "The src and dst pixel types must match");
77         GP_ASSERT(w <= dst->w && h <= dst->h,
78                   "Destination is not big enough");
80         if (GP_Filter{{ name }}_Raw(src_a, src_b, dst{{ maybe_opts_l(params) }}, callback)) {
81                 GP_DEBUG(1, "Operation aborted");
82                 return 1;
83         }
85         return 0;
89 GP_Pixmap *GP_Filter{{ name }}Alloc(const GP_Pixmap *src_a, const GP_Pixmap *src_b,
90                                     {{ maybe_opts_r(opts) }}GP_ProgressCallback *callback)
92         GP_Pixmap *res;
94         GP_ASSERT(src_a->pixel_type == src_b->pixel_type,
95                   "Pixel types for sources must match.");
97         GP_Size w = GP_MIN(src_a->w, src_b->w);
98         GP_Size h = GP_MIN(src_a->h, src_b->h);
100         res = GP_PixmapAlloc(w, h, src_a->pixel_type);
102         if (res == NULL)
103                 return NULL;
105         if (GP_Filter{{ name }}_Raw(src_a, src_b, res{{ maybe_opts_l(params) }}, callback)) {
106                 GP_DEBUG(1, "Operation aborted");
108                 GP_PixmapFree(res);
110                 return NULL;
111         }
113         return res;
115 @ end