2 * Image Library -- Image scaling algorithms
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include "images/images.h"
16 #define IMAGE_SCALE_PREFIX(x) image_scale_1_##x
17 #define IMAGE_SCALE_PIXEL_SIZE 1
18 #include "images/scale-gen.h"
20 #define IMAGE_SCALE_PREFIX(x) image_scale_2_##x
21 #define IMAGE_SCALE_PIXEL_SIZE 2
22 #include "images/scale-gen.h"
24 #define IMAGE_SCALE_PREFIX(x) image_scale_3_##x
25 #define IMAGE_SCALE_PIXEL_SIZE 3
26 #include "images/scale-gen.h"
28 #define IMAGE_SCALE_PREFIX(x) image_scale_4_##x
29 #define IMAGE_SCALE_PIXEL_SIZE 4
30 #include "images/scale-gen.h"
33 image_scale(struct image_thread *it, struct image *dest, struct image *src)
35 if (src->cols < dest->cols || src->rows < dest->rows)
37 image_thread_err(it, IMAGE_ERR_INVALID_DIMENSIONS, "Upsampling not supported.");
40 if ((src->flags & IMAGE_PIXEL_FORMAT) != (dest->flags & IMAGE_PIXEL_FORMAT))
42 image_thread_err(it, IMAGE_ERR_INVALID_PIXEL_FORMAT, "Different pixel format not supported.");
45 switch (src->pixel_size)
49 image_scale_1_downsample(dest, src);
53 image_scale_2_downsample(dest, src);
57 image_scale_3_downsample(dest, src);
59 /* RGBA or aligned RGB */
61 image_scale_4_downsample(dest, src);
69 image_dimensions_fit_to_box(u32 *cols, u32 *rows, u32 max_cols, u32 max_rows, uns upsample)
71 ASSERT(*cols && *rows && *cols <= 0xffff && *rows <= 0xffff);
72 ASSERT(max_cols && max_rows && max_cols <= 0xffff && max_rows <= 0xffff);
73 if (*cols <= max_cols && *rows <= max_rows)
77 if (max_cols / *cols > max_rows / *rows)
79 *cols = *cols * max_rows / *rows;
80 *cols = MIN(*cols, max_cols);
85 *rows = *rows * max_cols / *cols;
86 *rows = MIN(*rows, max_rows);
90 else if (*cols <= max_cols)
92 else if (*rows <= max_rows || max_rows * *cols > max_cols * *rows)
95 *cols = *cols * max_rows / *rows;
96 *cols = MAX(*cols, 1);
100 *rows = *rows * max_cols / *cols;
101 *rows = MAX(*rows, 1);