ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
aspect_ratio.c
Go to the documentation of this file.
1
6
7#include <ascii-chat/common.h>
9
10#define CHAR_ASPECT 2.0f // terminal cell height ÷ width
11
12// Constants for aspect ratio calculations
13enum {
14 MIN_DIMENSION = 1 // minimum width/height to prevent zero dimensions
15};
16
17// Helper functions for aspect ratio calculations
18static inline ssize_t calc_width_from_height(ssize_t height, ssize_t img_w, ssize_t img_h) {
19 if (img_h == 0) {
20 return MIN_DIMENSION;
21 }
22
23 float width = (float)height * (float)img_w / (float)img_h * CHAR_ASPECT;
24 int result = ROUND(width);
25 return (result > 0) ? result : MIN_DIMENSION;
26}
27
28static inline ssize_t calc_height_from_width(ssize_t width, ssize_t img_w, ssize_t img_h) {
29 if (img_w == 0) {
30 return MIN_DIMENSION;
31 }
32
33 float height = ((float)width / CHAR_ASPECT) * (float)img_h / (float)img_w;
34 int result = ROUND(height);
35 return (result > 0) ? result : MIN_DIMENSION;
36}
37
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, ssize_t *out_height) {
39 if (!out_width || !out_height) {
40 log_error("out_width or out_height is NULL");
41 return;
42 }
43
44 // Calculate both possible dimensions
45 ssize_t width_from_height = calc_width_from_height(max_h, img_w, img_h);
46 ssize_t height_from_width = calc_height_from_width(max_w, img_w, img_h);
47
48 // Choose the option that fits within both constraints
49 if (width_from_height <= max_w) {
50 // Height-constrained: use full height, calculated width
51 *out_width = width_from_height;
52 *out_height = max_h;
53 } else {
54 // Width-constrained: use full width, calculated height
55 *out_width = max_w;
56 *out_height = height_from_width;
57 }
58
59 // Final safety check
60 if (*out_width <= 0) {
61 *out_width = MIN_DIMENSION;
62 }
63 if (*out_height <= 0) {
64 *out_height = MIN_DIMENSION;
65 }
66}
67
68void 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) {
69 // Input validation
70 if (!out_width || !out_height) {
71 return; // or log error
72 }
73
74 if (img_w <= 0 || img_h <= 0) {
75 // Handle degenerate image dimensions
76 *out_width = MIN_DIMENSION;
77 *out_height = MIN_DIMENSION;
78 return;
79 }
80
81 if (stretch) {
82 // If stretching is enabled, just use the full terminal dimensions
83 *out_width = width;
84 *out_height = height;
85 } else {
86 // Calculate aspect-correct dimensions to fit within the given width/height
87 calculate_fit_dimensions(img_w, img_h, width, height, out_width, out_height);
88 }
89}
90
91// Simple aspect ratio calculation without terminal character correction
92// Used for image resizing where we want pixel-perfect aspect ratio
93void 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) {
94 // Input validation
95 if (!out_width || !out_height) {
96 return;
97 }
98
99 if (img_w <= 0 || img_h <= 0 || target_w <= 0 || target_h <= 0) {
100 // Handle invalid dimensions
101 *out_width = MIN_DIMENSION;
102 *out_height = MIN_DIMENSION;
103 return;
104 }
105
106 // Calculate aspect ratio
107 float img_aspect = (float)img_w / (float)img_h;
108
109 // We want the image to fill at least one dimension of the target box
110 // while maintaining aspect ratio and fitting within the box
111
112 // Calculate dimensions if we scale to fill width
113 ssize_t width_if_fill_width = target_w;
114 ssize_t height_if_fill_width = (ssize_t)((float)target_w / img_aspect);
115
116 // Calculate dimensions if we scale to fill height
117 ssize_t width_if_fill_height = (ssize_t)((float)target_h * img_aspect);
118 ssize_t height_if_fill_height = target_h;
119
120 // Choose the scaling that maximizes usage of the target box
121 // We want to fill at least one dimension completely
122 if (height_if_fill_width <= target_h) {
123 // Filling width fits within height - use it
124 *out_width = width_if_fill_width;
125 *out_height = height_if_fill_width;
126 } else {
127 // Filling width would exceed height, so fill height instead
128 *out_width = width_if_fill_height;
129 *out_height = height_if_fill_height;
130 }
131
132 // Ensure minimum dimensions
133 if (*out_width <= 0) {
134 *out_width = MIN_DIMENSION;
135 }
136 if (*out_height <= 0) {
137 *out_height = MIN_DIMENSION;
138 }
139}
140
141// Calculate the best dimensions to fit an image in a terminal area while preserving aspect ratio
142// Returns the dimensions in characters/pixels (1:1 for our use case with stretch=false)
143void calculate_fit_dimensions_pixel(int img_width, int img_height, int max_width, int max_height, int *out_width, int *out_height) {
144 if (!out_width || !out_height || img_width <= 0 || img_height <= 0) {
145 if (out_width)
146 *out_width = max_width;
147 if (out_height)
148 *out_height = max_height;
149 return;
150 }
151
152 float src_aspect = (float)img_width / (float)img_height;
153
154 // Try filling width
155 int width_if_fill_w = max_width;
156 int height_if_fill_w = (int)(((float)max_width / src_aspect) + 0.5f);
157
158 // Try filling height
159 int width_if_fill_h = (int)(((float)max_height * src_aspect) + 0.5f);
160 int height_if_fill_h = max_height;
161
162 // log_debug("calculate_fit_dimensions: img %dx%d (aspect %.3f), max %dx%d", img_width, img_height, src_aspect,
163 // max_width, max_height);
164 // log_debug(" Fill width: %dx%d, Fill height: %dx%d", width_if_fill_w, height_if_fill_w, width_if_fill_h,
165 // height_if_fill_h);
166
167 // Choose the option that fits
168 if (height_if_fill_w <= max_height) {
169 // Filling width fits
170 // log_debug(" Choosing fill width: %dx%d", width_if_fill_w, height_if_fill_w);
171 *out_width = width_if_fill_w;
172 *out_height = height_if_fill_w;
173 } else {
174 // Fill height instead
175 // log_debug(" Choosing fill height: %dx%d", width_if_fill_h, height_if_fill_h);
176 *out_width = width_if_fill_h;
177 *out_height = height_if_fill_h;
178 }
179
180 // Clamp to bounds
181 if (*out_width > max_width)
182 *out_width = max_width;
183 if (*out_height > max_height)
184 *out_height = max_height;
185 if (*out_width < 1)
186 *out_width = 1;
187 if (*out_height < 1)
188 *out_height = 1;
189
190 // log_debug(" Final output: %dx%d", *out_width, *out_height);
191}
@ MIN_DIMENSION
#define CHAR_ASPECT
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
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.
Definition math.h:57
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.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
🔢 Mathematical Utility Functions