commit 249a954276e6e2317a9a908946591403a46e1dc7
parent b2924cd50878cb9957b7820e4842478ddb566127
Author: bsandro <email@bsandro.tech>
Date: Fri, 2 Jan 2026 11:29:07 +0200
Removed assert() calls
Diffstat:
| M | cli/main.c | | | 76 | +++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
1 file changed, 45 insertions(+), 31 deletions(-)
diff --git a/cli/main.c b/cli/main.c
@@ -14,7 +14,6 @@
#include <libgen.h>
#include <string.h>
#include <strings.h>
-#include <assert.h>
#include "arg.h"
#ifdef __OpenBSD__
@@ -59,23 +58,21 @@ typedef struct {
void *raw_mem;
} AnimatedImage;
-void alloc_image(AnimatedImage *img, uint32_t frame_count) {
- assert(frame_count > 0);
+int alloc_image(AnimatedImage *img, uint32_t frame_count) {
+ if (frame_count==0) return 1;
uint8_t *mem = NULL;
DecodedFrame *frames = NULL;
const uint64_t rgba_size = img->width * img->height * NUM_CHANNELS;
const uint64_t img_size = frame_count * rgba_size * sizeof(uint8_t);
const uint64_t frames_size = frame_count * sizeof(DecodedFrame);
-
- assert(img_size == (size_t)img_size);
- assert(frames_size == (size_t)frames_size);
-
printf("img mem: %" PRIu64 "\nframes mem: %" PRIu64 "\n", img_size, frames_size);
mem = malloc(img_size);
frames = malloc(frames_size);
- assert(mem != NULL);
- assert(frames != NULL);
+ if (mem==NULL||frames==NULL) {
+ fputs("Memory allocation error", stderr);
+ return 1;
+ }
for (uint32_t i=0; i<frame_count; ++i) {
frames[i].rgba = mem+i*rgba_size;
@@ -85,6 +82,7 @@ void alloc_image(AnimatedImage *img, uint32_t frame_count) {
img->frame_count = frame_count;
img->frames = frames;
img->raw_mem = mem;
+ return 0;
}
void free_image(AnimatedImage *img) {
@@ -93,20 +91,24 @@ void free_image(AnimatedImage *img) {
}
int read_file(const char *fname, const uint8_t **data, size_t *size) {
- assert(data != NULL);
- assert(size != NULL);
-
+ if (data==NULL||size==NULL||fname==NULL) return 1;
*data = NULL;
*size = 0;
FILE *infile = fopen(fname, "rb");
- assert(infile != NULL);
+ if (infile==NULL) {
+ fputs("Error opening file", stderr);
+ return 1;
+ }
fseek(infile, 0, SEEK_END);
size_t fsize = ftell(infile);
printf("%s: %zu bytes\n", fname, fsize);
fseek(infile, 0, SEEK_SET);
uint8_t *fdata = malloc(fsize+1);
- assert(fdata != NULL);
+ if (fdata==NULL) {
+ fputs("Memory allocation error", stderr);
+ return 1;
+ }
fdata[fsize] = '\0';
int ok = (fread(fdata, fsize, 1, infile)==1);
fclose(infile);
@@ -166,7 +168,7 @@ int read_webp(const char *fname, AnimatedImage *anim) {
fprintf(stderr, "Error decoding frame %d\n", frame_index);
return 1;
}
- assert(frame_index<anim_info.frame_count);
+ if (frame_index>=anim_info.frame_count) break;
frame = &anim->frames[frame_index];
curr_rgba = frame->rgba;
frame->duration = ts - prev_ts;
@@ -181,19 +183,23 @@ int read_webp(const char *fname, AnimatedImage *anim) {
return 0;
}
-void write_webp(AnimatedImage *img, const char *fname, int cols) {
+int write_webp(AnimatedImage *img, const char *fname, int cols) {
int rows = (int)img->frame_count / cols;
- if ((int)img->frame_count % cols > 0) {
- ++rows;
- }
+ if ((int)img->frame_count % cols > 0) rows++;
FILE *fp = fopen(fname, "wb");
- assert(fp!=NULL);
+ if (fp==NULL) {
+ fputs("Error opening output file", stderr);
+ return 1;
+ }
uint8_t *out;
size_t frame_size = img->width * img->height * sizeof(uint32_t);
size_t line_size = img->width * sizeof(uint32_t);
size_t full_line = line_size * cols;
uint8_t *merged_orig = calloc(rows * cols, frame_size);
- assert(merged_orig!=NULL);
+ if (merged_orig==NULL) {
+ fputs("Memory allocation error", stderr);
+ return 1;
+ }
uint8_t *merged = merged_orig;
for (int row = 0; row < rows; ++row) {
for (int y = 0; y < img->height; ++y) {
@@ -208,26 +214,34 @@ void write_webp(AnimatedImage *img, const char *fname, int cols) {
}
int stride = full_line;
printf("stride: %d\n", stride);
- size_t encoded = WebPEncodeLosslessRGBA(merged_orig, img->width * cols, img->height * rows, stride, &out);
+ size_t encoded = WebPEncodeLosslessRGBA(merged_orig, img->width*cols, img->height*rows, stride, &out);
printf("size: %zu, encoded: %zu\n", img->width*img->height*sizeof(uint32_t), encoded);
- assert(encoded!=0);
+ if (encoded==0) {
+ fputs("Error encoding webp", stderr);
+ return 1;
+ }
size_t written = fwrite(out, sizeof(uint8_t), encoded, fp);
- assert(written==encoded);
+ if (written!=encoded) {
+ fputs("Error writing output file", stderr);
+ return 1;
+ }
WebPFree(out);
free(merged_orig);
fclose(fp);
+ return 0;
}
int save_file(AnimatedImage *img, char *out, int cols, int width) {
- char out_name[PATH_MAX];
+ char out_name[NAME_MAX];
char *in_name = basename(img->path);
char *in_dir = dirname(img->path);
- int n = snprintf(out_name, NAME_MAX, "atlas_%s", in_name);
- if (n<=0) return 1;
- printf("[path:%s][%s -> %s(%d)]\ndimensions: %dx%d\nframes: %d\n", in_dir, in_name, out_name, cols, img->width, img->height, img->frame_count);
- write_webp(img, out_name, cols);
-
- return 0;
+ int n = snprintf(out_name, NAME_MAX-1, "atlas_%s", in_name);
+ if (n<=0) {
+ fputs("Error generating output name", stderr);
+ return 1;
+ }
+ printf("[path:%s][%s -> %s(%d)]\ndimensions: %dx%d\nframes: %d\nout: %s\nmax width: %d\n", in_dir, in_name, out_name, cols, img->width, img->height, img->frame_count, out, width);
+ return write_webp(img, out_name, cols);
}
void print_usage(void) {