Error checking

This commit is contained in:
Andrew Haynes
2026-03-22 13:32:10 -04:00
parent 319a6e3b2c
commit 05a15d38af
3 changed files with 50 additions and 134 deletions
+50 -51
View File
@@ -121,12 +121,9 @@ static inline unsigned int
u32_brighten(const unsigned int clr, const float factor)
{
unsigned int alpha = (clr & BYTE_A);
unsigned int red =
CLAMP_MAX((unsigned int)((float)DECODE_R(clr) * factor));
unsigned int green =
CLAMP_MAX((unsigned int)((float)DECODE_G(clr) * factor));
unsigned int blue =
CLAMP_MAX((unsigned int)((float)DECODE_B(clr) * factor));
unsigned int red = CLAMP_MAX(DECODE_R(clr) * factor);
unsigned int green = CLAMP_MAX(DECODE_G(clr) * factor);
unsigned int blue = CLAMP_MAX(DECODE_B(clr) * factor);
return (clr & BYTE_A) | (red << SHIFT_16) | (green << SHIFT_8) | blue;
}
@@ -143,12 +140,12 @@ u32_mix(const unsigned int clr_a, const unsigned int clr_b)
static inline unsigned int
u32_lerp(const unsigned clr_a, const unsigned int clr_b, const float ratio)
{
unsigned char blue = (unsigned char)((DECODE_B(clr_a) * (1 - ratio)) +
((DECODE_B(clr_b) * ratio)));
unsigned char green = (unsigned char)((DECODE_G(clr_a) * (1 - ratio)) +
((DECODE_G(clr_b) * ratio)));
unsigned char red = (unsigned char)((DECODE_R(clr_a) * (1 - ratio)) +
((DECODE_R(clr_b) * ratio)));
unsigned char blue =
((DECODE_B(clr_a) * (1 - ratio)) + ((DECODE_B(clr_b) * ratio)));
unsigned char green =
((DECODE_G(clr_a) * (1 - ratio)) + ((DECODE_G(clr_b) * ratio)));
unsigned char red =
((DECODE_R(clr_a) * (1 - ratio)) + ((DECODE_R(clr_b) * ratio)));
return ENCODE_ARGB(DECODE_A(clr_a), red, green, blue);
}
@@ -233,9 +230,8 @@ ppm_flip_v(const unsigned int* buf, const PPM img)
}
static inline unsigned int
u32_contrast(unsigned int clr, float factor)
u32_contrast(const unsigned int clr, const float factor)
{
unsigned int mask = 0;
int red = CLAMP_MIN(CLAMP_MAX(
(int)(factor * (float)(int)(DECODE_R(clr) - HALF_MAX)) + HALF_MAX));
int green = CLAMP_MIN(CLAMP_MAX(
@@ -246,11 +242,11 @@ u32_contrast(unsigned int clr, float factor)
}
static inline unsigned int*
u32_rotate(const unsigned int* buf, PPM* img, const int len)
u32_rotate(const unsigned int* buf, PPM* m_img, const int len)
{
unsigned int* mask = malloc(len * sizeof(unsigned int));
int old_width = img->width;
int old_height = img->height;
int old_width = m_img->width;
int old_height = m_img->height;
for (int row = 0; row < old_height; row++)
{
for (int col = 0; col < old_width; col++)
@@ -262,8 +258,8 @@ u32_rotate(const unsigned int* buf, PPM* img, const int len)
buf[(row * old_width) + col];
}
}
img->width = old_height;
img->height = old_width;
m_img->width = old_height;
m_img->height = old_width;
return mask;
}
@@ -357,7 +353,7 @@ box_blur(const unsigned int* src_buf, const PPM* img, const int radius)
}
static inline unsigned int*
ppm_load(const char* file, PPM* img)
ppm_load(const char* file, PPM* m_attr)
{
printf("Starting decode...\n");
unsigned char red;
@@ -373,35 +369,35 @@ ppm_load(const char* file, PPM* img)
return NULL;
}
fscanf(fptr, "%s", fmt);
img->fmt = fmt[1];
fscanf(fptr, "%d %d", &img->width, &img->height);
img->len = img->width * img->height;
m_attr->fmt = fmt[1];
fscanf(fptr, "%d %d", &m_attr->width, &m_attr->height);
m_attr->len = m_attr->width * m_attr->height;
fscanf(fptr, "%d", &max_val);
fgetc(fptr);
unsigned char* img_buf = malloc(img->len * 3);
unsigned char* img_buf = malloc(m_attr->len * 3);
if (!img_buf)
{
return NULL;
}
unsigned int* clr_buf = malloc(img->len * sizeof(unsigned int));
unsigned int* clr_buf = malloc(m_attr->len * sizeof(unsigned int));
if (!clr_buf)
{
free(img_buf);
return NULL;
}
if (img->fmt == '3')
if (m_attr->fmt == '3')
{
for (int i = 0; i < img->len; i++)
for (int i = 0; i < m_attr->len; i++)
{
fscanf(fptr, "%hhu %hhu %hhu", &red, &green, &blue);
clr_buf[i] = ENCODE_RGB(red, green, blue);
}
}
else if (img->fmt == '6')
else if (m_attr->fmt == '6')
{
fread(img_buf, 3, img->len, fptr);
fread(img_buf, 3, m_attr->len, fptr);
for (int i = 0; i < img->len; i++)
for (int i = 0; i < m_attr->len; i++)
{
red = img_buf[i * 3];
green = img_buf[i * 3 + 1];
@@ -416,18 +412,18 @@ ppm_load(const char* file, PPM* img)
return clr_buf;
}
static inline void
static inline int
ppm_save(const unsigned int* buf, const PPM img)
{
if (!buf)
{
return;
return 0;
}
unsigned char red, green, blue;
unsigned char* dst_buf = malloc(img.len * 3);
if (!dst_buf)
{
return;
return 0;
}
printf("Starting encode...\n");
FILE* fptr;
@@ -443,7 +439,7 @@ ppm_save(const unsigned int* buf, const PPM img)
if (!fptr)
{
free(dst_buf);
return;
return 0;
}
fprintf(fptr, "P%c\n%d %d\n255\n", img.fmt, img.width, img.height);
@@ -474,36 +470,37 @@ ppm_save(const unsigned int* buf, const PPM img)
fclose(fptr);
free(dst_buf);
printf("Encode Done!\n");
return 1;
}
unsigned int*
jpeg_load(JPEG* attr)
jpeg_load(JPEG* m_attr)
{
printf("Starting Decode...\n");
tjhandle tjdecompress = tj3Init(TJINIT_DECOMPRESS);
unsigned char* jpeg_buf = NULL;
FILE* infile = fopen(attr->name, "rb");
FILE* infile = fopen(m_attr->name, "rb");
if (!infile)
{
return NULL;
}
fseek(infile, 0, SEEK_END);
attr->len = ftell(infile);
m_attr->len = ftell(infile);
fseek(infile, 0, SEEK_SET);
jpeg_buf = (unsigned char*)malloc(attr->len);
fread(jpeg_buf, 1, attr->len, infile);
jpeg_buf = (unsigned char*)malloc(m_attr->len);
fread(jpeg_buf, 1, m_attr->len, infile);
fclose(infile);
if (tj3DecompressHeader(tjdecompress, jpeg_buf, attr->len) < 0)
if (tj3DecompressHeader(tjdecompress, jpeg_buf, m_attr->len) < 0)
{
printf("Header error: %s\n", tj3GetErrorStr(tjdecompress));
free(jpeg_buf);
return NULL;
}
attr->width = tj3Get(tjdecompress, TJPARAM_JPEGWIDTH);
attr->height = tj3Get(tjdecompress, TJPARAM_JPEGHEIGHT);
m_attr->width = tj3Get(tjdecompress, TJPARAM_JPEGWIDTH);
m_attr->height = tj3Get(tjdecompress, TJPARAM_JPEGHEIGHT);
unsigned int* pixels =
malloc(attr->width * attr->height * sizeof(unsigned int));
tj3Decompress8(tjdecompress, jpeg_buf, attr->len,
malloc(m_attr->width * m_attr->height * sizeof(unsigned int));
tj3Decompress8(tjdecompress, jpeg_buf, m_attr->len,
(unsigned char*)pixels, 0, TJPF_BGRA);
tj3Destroy(tjdecompress);
free(jpeg_buf);
@@ -511,8 +508,8 @@ jpeg_load(JPEG* attr)
return pixels;
}
void
jpeg_save(unsigned int* src_buf, JPEG* attr, char* name)
int
jpeg_save(const unsigned int* src_buf, const JPEG* attr, const char* name)
{
printf("Starting Decode...\n");
tjhandle tjcompress = tj3Init(TJINIT_COMPRESS);
@@ -528,9 +525,8 @@ jpeg_save(unsigned int* src_buf, JPEG* attr, char* name)
FILE* fptr = fopen(name, "wb");
if (!fptr || dst_buf == NULL || dst_size <= 0)
{
free(src_buf);
tj3Free(dst_buf);
return;
return 0;
}
fwrite(dst_buf, 1, dst_size, fptr);
fclose(fptr);
@@ -538,6 +534,7 @@ jpeg_save(unsigned int* src_buf, JPEG* attr, char* name)
tj3Destroy(tjcompress);
tj3Free(dst_buf);
printf("Encode Successful!\n");
return 1;
}
void
@@ -567,9 +564,11 @@ main()
};
unsigned int* src_buf = ppm_load("../assets/8ktest.ppm", &out);
unsigned int* blurred = box_blur(src_buf, &out, 10);
ppm_save(blurred, out);
unsigned int* flip = ppm_flip_h(src_buf, out);
// unsigned int* blurred = box_blur(src_buf, &out, 10);
ppm_save(flip, out);
free(src_buf);
free(blurred);
// free(blurred);
free(flip);
return 0;
}