loaders: JPG: Fix bussy loop on corrupted file.
[gfxprim.git] / libs / filters / GP_ApplyTables.gen.c.t
blob931d7606c318dad0810e58f06c1318f87e54e352
1 @ include source.t
2 /*
3  * Generic Point filer
4  *
5  * Copyright (C) 2009-2014 Cyril Hrubis <metan@ucw.cz>
6  */
8 #include <errno.h>
10 #include "core/GP_Pixmap.h"
11 #include "core/GP_GetPutPixel.h"
12 #include "core/GP_Debug.h"
14 #include "filters/GP_ApplyTables.h"
16 @ for pt in pixeltypes:
17 @     if not pt.is_unknown() and not pt.is_palette():
18 static int apply_tables_{{ pt.name }}(const GP_Pixmap *const src,
19                                       GP_Coord x_src, GP_Coord y_src,
20                                       GP_Size w_src, GP_Size h_src,
21                                       GP_Pixmap *dst,
22                                       GP_Coord x_dst, GP_Coord y_dst,
23                                       const GP_FilterTables *const tables,
24                                       GP_ProgressCallback *callback)
26         GP_DEBUG(1, "Point filter %ux%u", w_src, h_src);
28         unsigned int x, y;
30 @         for c in pt.chanslist:
31         GP_Pixel {{ c.name }};
32 @         end
34         for (y = 0; y < h_src; y++) {
35                 for (x = 0; x < w_src; x++) {
36                         unsigned int src_x = x_src + x;
37                         unsigned int src_y = y_src + y;
38                         unsigned int dst_x = x_dst + x;
39                         unsigned int dst_y = y_dst + y;
41                         GP_Pixel pix = GP_GetPixel_Raw_{{ pt.pixelsize.suffix }}(src, src_x, src_y);
43 @         for c in pt.chanslist:
44                         {{ c.name }} = GP_Pixel_GET_{{ c[0] }}_{{ pt.name }}(pix);
45                         {{ c.name }} = tables->table[{{ c.idx }}][{{ c.name }}];
46 @         end
48                         pix = GP_Pixel_CREATE_{{ pt.name }}({{ arr_to_params(pt.chan_names) }});
49                         GP_PutPixel_Raw_{{ pt.pixelsize.suffix }}(dst, dst_x, dst_y, pix);
50                 }
52                 if (GP_ProgressCallbackReport(callback, y, h_src, w_src)) {
53                         errno = ECANCELED;
54                         return 1;
55                 }
56         }
58         GP_ProgressCallbackDone(callback);
60         return 0;
63 @ end
65 int GP_FilterTablesApply(const GP_Pixmap *const src,
66                          GP_Coord x_src, GP_Coord y_src,
67                          GP_Size w_src, GP_Size h_src,
68                          GP_Pixmap *dst,
69                          GP_Coord x_dst, GP_Coord y_dst,
70                          const GP_FilterTables *const tables,
71                          GP_ProgressCallback *callback)
73         GP_ASSERT(src->pixel_type == dst->pixel_type);
74         //TODO: Assert size
76         switch (src->pixel_type) {
77 @ for pt in pixeltypes:
78 @     if not pt.is_unknown() and not pt.is_palette():
79         case GP_PIXEL_{{ pt.name }}:
80                 return apply_tables_{{ pt.name }}(src, x_src, y_src,
81                                                   w_src, h_src, dst,
82                                                   x_dst, y_dst,
83                                                   tables, callback);
84         break;
85 @ end
86         default:
87                 errno = EINVAL;
88                 return -1;
89         }