Rename GP_Context -> GP_Pixmap
[gfxprim.git] / libs / filters / GP_ResizeNN.gen.c.t
blob385c7577620ef07c69a36a8450167bb679535583
1 @ include source.t
2 /*
3  * Nearest Neighbour resampling
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"
13 #include "core/GP_Debug.h"
15 #include "GP_ResizeNN.h"
17 @ for pt in pixeltypes:
18 @     if not pt.is_unknown():
19 static int resize_nn{{ pt.name }}(const GP_Pixmap *src, GP_Pixmap *dst,
20                                   GP_ProgressCallback *callback)
22         uint32_t xmap[dst->w];
23         uint32_t ymap[dst->h];
24         uint32_t i;
25         GP_Coord x, y;
27         GP_DEBUG(1, "Scaling image %ux%u -> %ux%u %2.2f %2.2f",
28                     src->w, src->h, dst->w, dst->h,
29                     1.00 * dst->w / src->w, 1.00 * dst->h / src->h);
31         /* Pre-compute mapping for interpolation */
32         for (i = 0; i < dst->w; i++)
33                 xmap[i] = ((((i * (src->w - 1))<<8)) / (dst->w - 1) + (1<<7))>>8;
35         for (i = 0; i < dst->h; i++)
36                 ymap[i] = ((((i * (src->h - 1))<<8) + (dst->h - 1)/2) / (dst->h - 1) + (1<<7))>>8;
38         /* Interpolate */
39         for (y = 0; y < (GP_Coord)dst->h; y++) {
40                 for (x = 0; x < (GP_Coord)dst->w; x++) {
41                         GP_Pixel pix = GP_GetPixel_Raw_{{ pt.pixelsize.suffix }}(src, xmap[x], ymap[y]);
43                         GP_PutPixel_Raw_{{ pt.pixelsize.suffix }}(dst, x, y, pix);
44                 }
46                 if (GP_ProgressCallbackReport(callback, y, dst->h, dst->w)) {
47                         errno = ECANCELED;
48                         return 1;
49                 }
50         }
52         GP_ProgressCallbackDone(callback);
53         return 0;
56 @ end
58 static int resize_nn(const GP_Pixmap *src, GP_Pixmap *dst,
59                      GP_ProgressCallback *callback)
61         switch (src->pixel_type) {
62 @ for pt in pixeltypes:
63 @     if not pt.is_unknown():
64         case GP_PIXEL_{{ pt.name }}:
65                 return resize_nn{{ pt.name }}(src, dst, callback);
66         break;
67 @ end
68         default:
69                 return -1;
70         }
73 int GP_FilterResizeNN(const GP_Pixmap *src, GP_Pixmap *dst,
74                            GP_ProgressCallback *callback)
76         if (src->pixel_type != dst->pixel_type) {
77                 GP_WARN("The src and dst pixel types must match");
78                 errno = EINVAL;
79                 return 1;
80         }
82         return resize_nn(src, dst, callback);