ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
util/image.c
Go to the documentation of this file.
1
6
8#include <ascii-chat/common.h>
10#include <limits.h>
11
12asciichat_error_t image_calc_pixel_count(size_t width, size_t height, size_t *out_pixel_count) {
13 if (!out_pixel_count) {
14 return SET_ERRNO(ERROR_INVALID_PARAM, "out_pixel_count is NULL");
15 }
16
17 // Check for zero dimensions
18 if (width == 0 || height == 0) {
19 return SET_ERRNO(ERROR_INVALID_PARAM, "Image dimensions must be non-zero: %zu x %zu", width, height);
20 }
21
22 // Check overflow before multiplication
23 if (height > SIZE_MAX / width) {
24 return SET_ERRNO(ERROR_INVALID_PARAM, "Image dimensions too large (would overflow): %zu x %zu", width, height);
25 }
26
27 *out_pixel_count = width * height;
28 return ASCIICHAT_OK;
29}
30
31asciichat_error_t image_calc_pixel_buffer_size(size_t pixel_count, size_t bytes_per_pixel, size_t *out_size) {
32 if (!out_size) {
33 return SET_ERRNO(ERROR_INVALID_PARAM, "out_size is NULL");
34 }
35
36 if (pixel_count == 0) {
37 return SET_ERRNO(ERROR_INVALID_PARAM, "Pixel count must be non-zero");
38 }
39
40 if (bytes_per_pixel == 0) {
41 return SET_ERRNO(ERROR_INVALID_PARAM, "bytes_per_pixel must be non-zero");
42 }
43
44 // Check overflow before multiplication
45 if (pixel_count > SIZE_MAX / bytes_per_pixel) {
46 return SET_ERRNO(ERROR_INVALID_PARAM, "Pixel buffer too large (would overflow): %zu pixels * %zu bpp", pixel_count, bytes_per_pixel);
47 }
48
49 *out_size = pixel_count * bytes_per_pixel;
50 return ASCIICHAT_OK;
51}
52
53asciichat_error_t image_calc_rgb_size(size_t width, size_t height, size_t *out_size) {
54 if (!out_size) {
55 return SET_ERRNO(ERROR_INVALID_PARAM, "out_size is NULL");
56 }
57
58 // Calculate pixel count first
59 size_t pixel_count;
61 if (err != ASCIICHAT_OK) {
62 return err;
63 }
64
65 // Calculate size with 3 bytes per pixel (RGB)
66 return image_calc_pixel_buffer_size(pixel_count, 3, out_size);
67}
68
69asciichat_error_t image_calc_total_allocation(size_t width, size_t height, size_t struct_size, size_t bytes_per_pixel, size_t *out_size) {
70 if (!out_size) {
71 return SET_ERRNO(ERROR_INVALID_PARAM, "out_size is NULL");
72 }
73
74 // Calculate pixel count first with overflow checking
75 size_t pixel_count;
77 if (err != ASCIICHAT_OK) {
78 return err;
79 }
80
81 // Calculate pixel buffer size
82 size_t pixel_buffer_size;
83 err = image_calc_pixel_buffer_size(pixel_count, bytes_per_pixel, &pixel_buffer_size);
84 if (err != ASCIICHAT_OK) {
85 return err;
86 }
87
88 // Check overflow when adding struct size
89 if (pixel_buffer_size > SIZE_MAX - struct_size) {
90 return SET_ERRNO(ERROR_INVALID_PARAM, "Total allocation size would overflow: struct=%zu + pixels=%zu", struct_size, pixel_buffer_size);
91 }
92
93 *out_size = struct_size + pixel_buffer_size;
94 return ASCIICHAT_OK;
95}
96
98 // Check for zero dimensions
99 if (width == 0 || height == 0) {
100 return SET_ERRNO(ERROR_INVALID_PARAM, "Image dimensions must be non-zero: %zu x %zu", width, height);
101 }
102
103 // Check against maximum allowed dimensions
105 return SET_ERRNO(ERROR_INVALID_PARAM, "Image dimensions exceed maximum: %zu x %zu (max %u x %u)", width, height, IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT);
106 }
107
108 return ASCIICHAT_OK;
109}
110
112 if (requested_size > IMAGE_MAX_PIXELS_SIZE) {
113 return SET_ERRNO(ERROR_INVALID_PARAM, "Image buffer size exceeds maximum: %zu > %zu bytes", requested_size, IMAGE_MAX_PIXELS_SIZE);
114 }
115
116 return ASCIICHAT_OK;
117}
#define SET_ERRNO(code,...)
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define IMAGE_MAX_WIDTH
An owned RGB image with dimensions and row-major pixel storage.
#define IMAGE_MAX_PIXELS_SIZE
Maximum pixel data size in bytes.
#define IMAGE_MAX_HEIGHT
Maximum image height (4K resolution).
⚙️ Common definitions, error codes, macros, and types shared throughout the application
asciichat_error_t image_validate_buffer_size(size_t requested_size)
Validate buffer size against maximum allocation limit.
Definition util/image.c:111
asciichat_error_t image_validate_dimensions(size_t width, size_t height)
Validate image dimensions (non-zero, within limits).
Definition util/image.c:97
asciichat_error_t image_calc_pixel_buffer_size(size_t pixel_count, size_t bytes_per_pixel, size_t *out_size)
Calculate pixel buffer size with overflow checking.
Definition util/image.c:31
asciichat_error_t image_calc_pixel_count(size_t width, size_t height, size_t *out_pixel_count)
Calculate pixel count with overflow checking.
Definition util/image.c:12
asciichat_error_t image_calc_rgb_size(size_t width, size_t height, size_t *out_size)
Calculate total RGB buffer size from dimensions.
Definition util/image.c:53
asciichat_error_t image_calc_total_allocation(size_t width, size_t height, size_t struct_size, size_t bytes_per_pixel, size_t *out_size)
Calculate combined size of image struct + pixel buffer.
Definition util/image.c:69
🖼️ Safe overflow-checked buffer size calculations for images and video frames