ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
util/image.c
Go to the documentation of this file.
1
7#include "image.h"
8#include "common.h"
9#include "video/image.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,
47 bytes_per_pixel);
48 }
49
50 *out_size = pixel_count * bytes_per_pixel;
51 return ASCIICHAT_OK;
52}
53
54asciichat_error_t image_calc_rgb_size(size_t width, size_t height, size_t *out_size) {
55 if (!out_size) {
56 return SET_ERRNO(ERROR_INVALID_PARAM, "out_size is NULL");
57 }
58
59 // Calculate pixel count first
60 size_t pixel_count;
61 asciichat_error_t err = image_calc_pixel_count(width, height, &pixel_count);
62 if (err != ASCIICHAT_OK) {
63 return err;
64 }
65
66 // Calculate size with 3 bytes per pixel (RGB)
67 return image_calc_pixel_buffer_size(pixel_count, 3, out_size);
68}
69
70asciichat_error_t image_calc_total_allocation(size_t width, size_t height, size_t struct_size, size_t bytes_per_pixel,
71 size_t *out_size) {
72 if (!out_size) {
73 return SET_ERRNO(ERROR_INVALID_PARAM, "out_size is NULL");
74 }
75
76 // Calculate pixel count first with overflow checking
77 size_t pixel_count;
78 asciichat_error_t err = image_calc_pixel_count(width, height, &pixel_count);
79 if (err != ASCIICHAT_OK) {
80 return err;
81 }
82
83 // Calculate pixel buffer size
84 size_t pixel_buffer_size;
85 err = image_calc_pixel_buffer_size(pixel_count, bytes_per_pixel, &pixel_buffer_size);
86 if (err != ASCIICHAT_OK) {
87 return err;
88 }
89
90 // Check overflow when adding struct size
91 if (pixel_buffer_size > SIZE_MAX - struct_size) {
92 return SET_ERRNO(ERROR_INVALID_PARAM, "Total allocation size would overflow: struct=%zu + pixels=%zu", struct_size,
93 pixel_buffer_size);
94 }
95
96 *out_size = struct_size + pixel_buffer_size;
97 return ASCIICHAT_OK;
98}
99
100asciichat_error_t image_validate_dimensions(size_t width, size_t height) {
101 // Check for zero dimensions
102 if (width == 0 || height == 0) {
103 return SET_ERRNO(ERROR_INVALID_PARAM, "Image dimensions must be non-zero: %zu x %zu", width, height);
104 }
105
106 // Check against maximum allowed dimensions
107 if (width > IMAGE_MAX_WIDTH || height > IMAGE_MAX_HEIGHT) {
108 return SET_ERRNO(ERROR_INVALID_PARAM, "Image dimensions exceed maximum: %zu x %zu (max %u x %u)", width, height,
110 }
111
112 return ASCIICHAT_OK;
113}
114
116 if (requested_size > IMAGE_MAX_PIXELS_SIZE) {
117 return SET_ERRNO(ERROR_INVALID_PARAM, "Image buffer size exceeds maximum: %zu > %zu bytes", requested_size,
119 }
120
121 return ASCIICHAT_OK;
122}
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:46
@ ASCIICHAT_OK
Definition error_codes.h:48
@ ERROR_INVALID_PARAM
#define IMAGE_MAX_WIDTH
Maximum image width (4K resolution)
#define IMAGE_MAX_PIXELS_SIZE
Maximum pixel data size in bytes.
#define IMAGE_MAX_HEIGHT
Maximum image height (4K resolution)
Application limits and constraints.
asciichat_error_t image_validate_buffer_size(size_t requested_size)
Validate buffer size against maximum allocation limit.
Definition util/image.c:115
asciichat_error_t image_validate_dimensions(size_t width, size_t height)
Validate image dimensions (non-zero, within limits)
Definition util/image.c:100
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:54
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:70
Image Data Structures and Operations.