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"
14 #include "images/error.h"
18 #define IMAGE_SCALE_PREFIX(x) image_scale_1_##x
19 #define IMAGE_SCALE_PIXEL_SIZE 1
20 #include "images/scale-gen.h"
22 #define IMAGE_SCALE_PREFIX(x) image_scale_2_##x
23 #define IMAGE_SCALE_PIXEL_SIZE 2
24 #include "images/scale-gen.h"
26 #define IMAGE_SCALE_PREFIX(x) image_scale_3_##x
27 #define IMAGE_SCALE_PIXEL_SIZE 3
28 #include "images/scale-gen.h"
30 #define IMAGE_SCALE_PREFIX(x) image_scale_4_##x
31 #define IMAGE_SCALE_PIXEL_SIZE 4
32 #include "images/scale-gen.h"
35 image_scale(struct image_context *ctx, struct image *dest, struct image *src)
37 if (src->cols < dest->cols || src->rows < dest->rows)
39 IMAGE_ERROR(ctx, IMAGE_ERROR_INVALID_DIMENSIONS, "Upsampling not supported.");
42 if ((src->flags & IMAGE_PIXEL_FORMAT) != (dest->flags & IMAGE_PIXEL_FORMAT))
44 IMAGE_ERROR(ctx, IMAGE_ERROR_INVALID_PIXEL_FORMAT, "Different pixel format not supported.");
47 switch (src->pixel_size)
51 image_scale_1_downsample(dest, src);
55 image_scale_2_downsample(dest, src);
59 image_scale_3_downsample(dest, src);
61 /* RGBA or aligned RGB */
63 image_scale_4_downsample(dest, src);
71 image_dimensions_fit_to_box(u32 *cols, u32 *rows, u32 max_cols, u32 max_rows, uns upsample)
73 ASSERT(image_dimensions_valid(*cols, *rows));
74 ASSERT(image_dimensions_valid(max_cols, max_rows));
75 if (*cols <= max_cols && *rows <= max_rows)
79 if (max_cols * *rows > max_rows * *cols)
81 *cols = *cols * max_rows / *rows;
82 *cols = MIN(*cols, max_cols);
87 *rows = *rows * max_cols / *cols;
88 *rows = MIN(*rows, max_rows);
92 else if (*cols <= max_cols)
94 else if (*rows <= max_rows || max_rows * *cols > max_cols * *rows)
97 *cols = *cols * max_rows / *rows;
98 *cols = MAX(*cols, 1);
102 *rows = *rows * max_cols / *cols;
103 *rows = MAX(*rows, 1);