10#define CHAR_ASPECT 2.0f
18static inline ssize_t calc_width_from_height(ssize_t height, ssize_t img_w, ssize_t img_h) {
23 float width = (float)height * (
float)img_w / (float)img_h *
CHAR_ASPECT;
24 int result =
ROUND(width);
28static inline ssize_t calc_height_from_width(ssize_t width, ssize_t img_w, ssize_t img_h) {
33 float height = ((float)width /
CHAR_ASPECT) * (float)img_h / (
float)img_w;
34 int result =
ROUND(height);
38static void calculate_fit_dimensions(ssize_t img_w, ssize_t img_h, ssize_t max_w, ssize_t max_h, ssize_t *out_width,
39 ssize_t *out_height) {
40 if (!out_width || !out_height) {
41 log_error(
"calculate_fit_dimensions: out_width or out_height is NULL");
46 ssize_t width_from_height = calc_width_from_height(max_h, img_w, img_h);
47 ssize_t height_from_width = calc_height_from_width(max_w, img_w, img_h);
50 if (width_from_height <= max_w) {
52 *out_width = width_from_height;
57 *out_height = height_from_width;
61 if (*out_width <= 0) {
64 if (*out_height <= 0) {
69void aspect_ratio(
const ssize_t img_w,
const ssize_t img_h,
const ssize_t width,
const ssize_t height,
70 const bool stretch, ssize_t *out_width, ssize_t *out_height) {
72 if (!out_width || !out_height) {
76 if (img_w <= 0 || img_h <= 0) {
89 calculate_fit_dimensions(img_w, img_h, width, height, out_width, out_height);
95void aspect_ratio2(
const ssize_t img_w,
const ssize_t img_h,
const ssize_t target_w,
const ssize_t target_h,
96 ssize_t *out_width, ssize_t *out_height) {
98 if (!out_width || !out_height) {
102 if (img_w <= 0 || img_h <= 0 || target_w <= 0 || target_h <= 0) {
110 float img_aspect = (float)img_w / (
float)img_h;
116 ssize_t width_if_fill_width = target_w;
117 ssize_t height_if_fill_width = (ssize_t)((
float)target_w / img_aspect);
120 ssize_t width_if_fill_height = (ssize_t)((
float)target_h * img_aspect);
121 ssize_t height_if_fill_height = target_h;
125 if (height_if_fill_width <= target_h) {
127 *out_width = width_if_fill_width;
128 *out_height = height_if_fill_width;
131 *out_width = width_if_fill_height;
132 *out_height = height_if_fill_height;
136 if (*out_width <= 0) {
139 if (*out_height <= 0) {
148 if (!out_width || !out_height || img_width <= 0 || img_height <= 0) {
150 *out_width = max_width;
152 *out_height = max_height;
156 float src_aspect = (float)img_width / (
float)img_height;
159 int width_if_fill_w = max_width;
160 int height_if_fill_w = (int)(((
float)max_width / src_aspect) + 0.5f);
163 int width_if_fill_h = (int)(((
float)max_height * src_aspect) + 0.5f);
164 int height_if_fill_h = max_height;
172 if (height_if_fill_w <= max_height) {
175 *out_width = width_if_fill_w;
176 *out_height = height_if_fill_w;
180 *out_width = width_if_fill_h;
181 *out_height = height_if_fill_h;
185 if (*out_width > max_width)
186 *out_width = max_width;
187 if (*out_height > max_height)
188 *out_height = max_height;
#define log_error(...)
Log an ERROR message.
void aspect_ratio2(const ssize_t img_w, const ssize_t img_h, const ssize_t target_w, const ssize_t target_h, ssize_t *out_width, ssize_t *out_height)
Simple aspect ratio calculation without terminal character correction.
void calculate_fit_dimensions_pixel(int img_width, int img_height, int max_width, int max_height, int *out_width, int *out_height)
Calculate fit dimensions for pixel-based images.
#define ROUND(xfloat)
Round floating-point value to nearest integer.
void aspect_ratio(const ssize_t img_w, const ssize_t img_h, const ssize_t width, const ssize_t height, const bool stretch, ssize_t *out_width, ssize_t *out_height)
Calculate aspect ratio with terminal character correction.
🔢 Mathematical Utility Functions