Skip to content

Commit

Permalink
Use clamp instead of a mix of min and max
Browse files Browse the repository at this point in the history
Signed-off-by: Abe Wieland <[email protected]>
  • Loading branch information
abewieland authored and artemsen committed Jan 4, 2025
1 parent d53f64a commit dfe2873
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/formats/pnm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

#include <limits.h>

// Divide a by b, rounding to the nearest integer; evaluates b twice
// Both assume positive arguments and evaluate b more than once
// Divide, rounding to nearest (up on ties)
#define div_near(a, b) (((a) + (b) / 2) / (b))
// Divide a by b, rounding up; evaluates b twice
// Divide, rounding up
#define div_ceil(a, b) (((a) + (b) - 1) / (b))

// PNM file types
Expand Down
8 changes: 5 additions & 3 deletions src/pixmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <sys/sysctl.h>
#endif

#define clamp(a, low, high) (min((high), max((a), (low))))

/** Scale filter parameters. */
struct scale_param {
const struct pixmap* src; ///< Source pixmap
Expand Down Expand Up @@ -285,11 +287,11 @@ static void scale_bicubic(struct scale_param* sp, size_t start, size_t step)
(state[pc][3][0] + state[pc][3][1] * diff_x + state[pc][3][2] * diff_x2 + state[pc][3][3] * diff_x3) * diff_y3;
// clang-format on
if (pc == 3) {
const uint8_t alpha = max(min(inter, 255), 0);
const uint8_t alpha = clamp(inter, 0, 255);
fg |= ARGB_SET_A(alpha);
} else {
const uint8_t alpha = ARGB_GET_A(fg);
const uint16_t mul = max(min(inter, 255 * alpha), 0);
const uint16_t mul = clamp(inter, 0, 255 * alpha);
const uint8_t color = alpha ? mul / alpha : 0;
fg |= (color << (pc * 8));
}
Expand Down Expand Up @@ -523,7 +525,7 @@ void pixmap_scale(enum pixmap_scale scaler, const struct pixmap* src,
#endif

// background threads
const size_t threads_num = min(16, max(cpus, 1)) - 1;
const size_t threads_num = clamp(cpus, 1, 16) - 1;
struct scale_task* tasks = NULL;

// scaling parameters
Expand Down

0 comments on commit dfe2873

Please sign in to comment.