3 * Nearest Neighbour resampling
5 * Copyright (C) 2009-2014 Cyril Hrubis <metan@ucw.cz>
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];
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;
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);
46 if (GP_ProgressCallbackReport(callback, y, dst->h, dst->w)) {
52 GP_ProgressCallbackDone(callback);
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);
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");
82 return resize_nn(src, dst, callback);