REFACTOR: Completely remade the error handling format. Using pre-defined

error codes and error messages to make it easier to debug problems.
This commit is contained in:
2026-09-03 14:14:41 -04:00
parent ff25fad5ae
commit 56d26fe723
8 changed files with 188 additions and 67 deletions
+88 -39
View File
@@ -4,20 +4,21 @@
#include <stdlib.h>
#include <turbojpeg.h>
#include "image/utils.h"
unsigned int*
ppm_load(const char* file, PPM* m_attr)
{
printf("Starting decode...\n");
unsigned char red;
unsigned char green;
unsigned char blue;
LOG("Decoding");
unsigned char red, green, blue;
char fmt[3];
int max_val;
FILE* fptr;
fptr = fopen(file, "r");
if (!fptr)
{
printf("Invalid file name: %s\n", file);
LOGERROR(ERR_IMG_NAME_INVALID);
LOG(file);
return NULL;
}
fscanf(fptr, "%s", fmt);
@@ -29,12 +30,14 @@ ppm_load(const char* file, PPM* m_attr)
unsigned char* img_buf = malloc(m_attr->len * 3);
if (!img_buf)
{
LOGERROR(ERR_BUF_NULL);
return NULL;
}
unsigned int* clr_buf = malloc(m_attr->len * sizeof(unsigned int));
if (!clr_buf)
{
free(img_buf);
LOGERROR(ERR_BUF_NULL);
return NULL;
}
if (m_attr->fmt == '3')
@@ -60,7 +63,7 @@ ppm_load(const char* file, PPM* m_attr)
fclose(fptr);
free(img_buf);
printf("Decoding done!\n");
LOG("Decoding Finished");
return clr_buf;
}
@@ -69,15 +72,16 @@ ppm_save(const unsigned int* buf, const PPM img, char* name)
{
if (!buf)
{
return 10;
LOGERROR(ERR_BUF_NULL);
return ERR_BUF_NULL;
}
unsigned char red, green, blue;
unsigned char* dst_buf = malloc(img.len * 3);
if (!dst_buf)
{
return 10;
LOGERROR(ERR_BUF_NULL);
return ERR_BUF_NULL;
}
printf("Starting encode...\n");
LOG("Encoding");
FILE* fptr;
if (img.fmt == '3')
@@ -93,7 +97,8 @@ ppm_save(const unsigned int* buf, const PPM img, char* name)
if (!fptr)
{
free(dst_buf);
return 10;
LOGERROR(ERR_FILE_NULL);
return ERR_FILE_NULL;
}
fprintf(fptr, "P%c\n%d %d\n255\n", img.fmt, img.width, img.height);
@@ -101,10 +106,8 @@ ppm_save(const unsigned int* buf, const PPM img, char* name)
{
for (int i = 0; i < img.len; i++)
{
red = DECODE_R(buf[i]);
green = DECODE_G(buf[i]);
blue = DECODE_B(buf[i]);
fprintf(fptr, "%d %d %d ", red, green, blue);
fprintf(fptr, "%d %d %d ", DECODE_R(buf[i]),
DECODE_G(buf[i]), DECODE_B(buf[i]));
if (i % 7 == 0)
{
fprintf(fptr, "\n");
@@ -124,20 +127,20 @@ ppm_save(const unsigned int* buf, const PPM img, char* name)
}
fclose(fptr);
free(dst_buf);
printf("Encode Done!\n");
printf("Image: %s is saved.\n", name);
return 1;
LOG("Encoding Finished");
return SUCCESS;
}
unsigned int*
jpeg_load(JPEG* m_attr)
{
printf("Starting Decode...\n");
LOG("Decoding");
tjhandle tjdecompress = tj3Init(TJINIT_DECOMPRESS);
unsigned char* jpeg_buf = NULL;
FILE* infile = fopen(m_attr->name, "rb");
if (!infile)
{
LOGERROR(ERR_FILE_NULL);
return NULL;
}
fseek(infile, 0, SEEK_END);
@@ -148,26 +151,33 @@ jpeg_load(JPEG* m_attr)
fclose(infile);
if (tj3DecompressHeader(tjdecompress, jpeg_buf, m_attr->len) < 0)
{
printf("Header error: %s\n", tj3GetErrorStr(tjdecompress));
LOGERROR(ERR_JHEADER);
LOG(tj3GetErrorStr(tjdecompress));
free(jpeg_buf);
return NULL;
}
m_attr->width = tj3Get(tjdecompress, TJPARAM_JPEGWIDTH);
m_attr->height = tj3Get(tjdecompress, TJPARAM_JPEGHEIGHT);
m_attr->len = m_attr->width * m_attr->height;
unsigned int* pixels =
malloc(m_attr->width * m_attr->height * sizeof(unsigned int));
if (!pixels)
{
LOGERROR(ERR_BUF_NULL);
return NULL;
}
tj3Decompress8(tjdecompress, jpeg_buf, m_attr->len,
(unsigned char*)pixels, 0, TJPF_BGRA);
tj3Destroy(tjdecompress);
free(jpeg_buf);
printf("Decode Successful!\n");
LOG("Decoding Finished");
return pixels;
}
int
jpeg_save(const unsigned int* src_buf, const JPEG* attr, const char* name)
{
printf("Starting Decode...\n");
LOG("Encoding");
tjhandle tjcompress = tj3Init(TJINIT_COMPRESS);
unsigned char* dst_buf = NULL;
size_t dst_size = 0;
@@ -176,27 +186,52 @@ jpeg_save(const unsigned int* src_buf, const JPEG* attr, const char* name)
if (tj3Compress8(tjcompress, (unsigned char*)src_buf, attr->width, 0,
attr->height, TJPF_BGRA, &dst_buf, &dst_size) < 0)
{
printf("TurboJPEG Error: %s\n", tj3GetErrorStr(tjcompress));
LOGERROR(ERR_JHEADER);
LOG(tj3GetErrorStr(tjcompress));
return ERR_JHEADER;
}
FILE* fptr = fopen(name, "wb");
if (!fptr || dst_buf == NULL || dst_size <= 0)
if (!fptr)
{
tj3Free(dst_buf);
return 25;
LOGERROR(ERR_FILE_NULL);
return ERR_FILE_NULL;
}
if (dst_buf == NULL)
{
tj3Free(dst_buf);
LOGERROR(ERR_BUF_NULL);
return ERR_BUF_NULL;
}
if (dst_size <= 0)
{
tj3Free(dst_buf);
LOGERROR(ERR_INVALID_SIZE);
return ERR_INVALID_SIZE;
}
fwrite(dst_buf, 1, dst_size, fptr);
fclose(fptr);
tj3Destroy(tjcompress);
tj3Free(dst_buf);
printf("Encode Successful!\n");
return 1;
LOG("Encoding Finished");
return SUCCESS;
}
void
transcode_to_jpeg(PPM* in, JPEG* out)
{
if (!in || !out)
{
LOGERROR(ERR_IMG_NULL);
return;
}
unsigned int* dst = ppm_load(in->name, in);
if (!dst)
{
LOGERROR(ERR_BUF_NULL);
return;
}
out->width = in->width;
out->height = in->height;
jpeg_save(dst, out, out->name);
@@ -204,11 +239,21 @@ transcode_to_jpeg(PPM* in, JPEG* out)
}
void
transcode_to_ppm(JPEG* input, PPM* out)
transcode_to_ppm(JPEG* in, PPM* out)
{
unsigned int* dst = jpeg_load(input);
out->width = input->width;
out->height = input->height;
if (!in || !out)
{
LOGERROR(ERR_IMG_NULL);
return;
}
unsigned int* dst = jpeg_load(in);
if (!dst)
{
LOGERROR(ERR_BUF_NULL);
return;
}
out->width = in->width;
out->height = in->height;
out->len = out->width * out->height;
ppm_save(dst, *out, out->name);
free(dst);
@@ -217,6 +262,12 @@ transcode_to_ppm(JPEG* input, PPM* out)
PPM
inherit_from_jpeg(const JPEG* in, const char fmt)
{
PPM img;
if (!in)
{
LOGERROR(ERR_IMG_NULL);
return img;
}
PPM image = { .name = in->name,
.width = in->width,
.height = in->height,
@@ -228,6 +279,12 @@ inherit_from_jpeg(const JPEG* in, const char fmt)
JPEG
inherit_from_ppm(const PPM* in)
{
JPEG blank;
if (!in)
{
LOGERROR(ERR_IMG_NULL);
return blank;
}
JPEG image = {
.name = in->name,
.width = in->width,
@@ -236,11 +293,3 @@ inherit_from_ppm(const PPM* in)
};
return image;
}
/** The Second image gets merged into the first.
* The First image obtains attributes from the second image that were not found.
*/
// PPM
// merge_ppm(const PPM* in_1, const PPM* in_2) {
// if(in)
// }