7#include <ascii-chat/util/image.h>
8#include <ascii-chat/common.h>
9#include <ascii-chat/video/image.h>
13 if (!out_pixel_count) {
14 return SET_ERRNO(ERROR_INVALID_PARAM,
"out_pixel_count is NULL");
18 if (width == 0 || height == 0) {
19 return SET_ERRNO(ERROR_INVALID_PARAM,
"Image dimensions must be non-zero: %zu x %zu", width, height);
23 if (height > SIZE_MAX / width) {
24 return SET_ERRNO(ERROR_INVALID_PARAM,
"Image dimensions too large (would overflow): %zu x %zu", width, height);
27 *out_pixel_count = width * height;
33 return SET_ERRNO(ERROR_INVALID_PARAM,
"out_size is NULL");
36 if (pixel_count == 0) {
37 return SET_ERRNO(ERROR_INVALID_PARAM,
"Pixel count must be non-zero");
40 if (bytes_per_pixel == 0) {
41 return SET_ERRNO(ERROR_INVALID_PARAM,
"bytes_per_pixel must be non-zero");
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,
50 *out_size = pixel_count * bytes_per_pixel;
56 return SET_ERRNO(ERROR_INVALID_PARAM,
"out_size is NULL");
62 if (err != ASCIICHAT_OK) {
73 return SET_ERRNO(ERROR_INVALID_PARAM,
"out_size is NULL");
79 if (err != ASCIICHAT_OK) {
84 size_t pixel_buffer_size;
86 if (err != ASCIICHAT_OK) {
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,
96 *out_size = struct_size + pixel_buffer_size;
102 if (width == 0 || height == 0) {
103 return SET_ERRNO(ERROR_INVALID_PARAM,
"Image dimensions must be non-zero: %zu x %zu", width, height);
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,
109 IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT);
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,
118 IMAGE_MAX_PIXELS_SIZE);
asciichat_error_t image_validate_buffer_size(size_t requested_size)
asciichat_error_t image_validate_dimensions(size_t width, size_t height)
asciichat_error_t image_calc_pixel_buffer_size(size_t pixel_count, size_t bytes_per_pixel, size_t *out_size)
asciichat_error_t image_calc_pixel_count(size_t width, size_t height, size_t *out_pixel_count)
asciichat_error_t image_calc_rgb_size(size_t width, size_t height, size_t *out_size)
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)