Error checking
This commit is contained in:
-59
@@ -1,59 +0,0 @@
|
|||||||
---
|
|
||||||
Checks: >
|
|
||||||
-*,
|
|
||||||
bugprone-assert-side-effect,
|
|
||||||
bugprone-assignment-in-if-condition,
|
|
||||||
bugprone-bool-pointer-implicit-conversion,
|
|
||||||
bugprone-dangling-handle,
|
|
||||||
bugprone-dynamic-static-initializers,
|
|
||||||
bugprone-infinite-loop,
|
|
||||||
bugprone-integer-division,
|
|
||||||
bugprone-macro-repeated-side-effects,
|
|
||||||
bugprone-misplaced-operator-in-strlen-in-alloc,
|
|
||||||
bugprone-misplaced-pointer-arithmetic-in-alloc,
|
|
||||||
bugprone-misplaced-widening-cast,
|
|
||||||
bugprone-not-null-terminated-result,
|
|
||||||
bugprone-posix-return,
|
|
||||||
bugprone-redundant-branch-condition,
|
|
||||||
bugprone-string-literal-with-embedded-nul,
|
|
||||||
bugprone-suspicious-memset-usage,
|
|
||||||
bugprone-suspicious-semicolon,
|
|
||||||
bugprone-suspicious-string-compare,
|
|
||||||
bugprone-too-small-loop-variable,
|
|
||||||
bugprone-unused-return-value,
|
|
||||||
cert-err33-c,
|
|
||||||
clang-analyzer-core.*,
|
|
||||||
clang-analyzer-valist.*,
|
|
||||||
clang-analyzer-unix.Malloc,
|
|
||||||
clang-diagnostic-*,
|
|
||||||
google-readability-casting,
|
|
||||||
misc-misleading-bidirectional,
|
|
||||||
misc-misleading-identifier,
|
|
||||||
misc-misplaced-const,
|
|
||||||
misc-redundant-expression,
|
|
||||||
objc-*,
|
|
||||||
performance-type-promotion-in-math-fn,
|
|
||||||
readability-avoid-const-params-in-decls,
|
|
||||||
readability-braces-around-statements,
|
|
||||||
readability-const-return-type,
|
|
||||||
readability-duplicate-include,
|
|
||||||
readability-inconsistent-declaration-parameter-name,
|
|
||||||
readability-misplaced-array-index,
|
|
||||||
readability-non-const-parameter,
|
|
||||||
readability-redundant-control-flow,
|
|
||||||
readability-redundant-declaration,
|
|
||||||
readability-redundant-function-ptr-dereference,
|
|
||||||
readability-redundant-preprocessor,
|
|
||||||
readability-simplify-boolean-expr
|
|
||||||
|
|
||||||
CheckOptions:
|
|
||||||
- key: bugprone-assert-side-effect.AssertMacros
|
|
||||||
value: "SDL_assert, SDL_assert_release, SDL_assert_paranoid, SDL_assert_always, SDL_COMPILE_TIME_ASSERT"
|
|
||||||
- key: bugprone-misplaced-widening-cast.CheckImplicitCasts
|
|
||||||
value: true
|
|
||||||
- key: bugprone-not-null-terminated-result.WantToUseSafeFunctions
|
|
||||||
value: false # Do not recommend _s functions
|
|
||||||
|
|
||||||
FormatStyle: "file"
|
|
||||||
HeaderFilterRegex: "*.h$"
|
|
||||||
WarningsAsErrors: ""
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
---
|
|
||||||
# Enable a broad range of useful C checks
|
|
||||||
Checks: >
|
|
||||||
-*,
|
|
||||||
bugprone-*,
|
|
||||||
concurrency-*,
|
|
||||||
misc-*,
|
|
||||||
modernize-*,
|
|
||||||
performance-*,
|
|
||||||
readability-*,
|
|
||||||
portability-*,
|
|
||||||
-readability-braces-around-statements,
|
|
||||||
-readability-implicit-bool-conversion
|
|
||||||
|
|
||||||
# Treat all enabled checks as errors (optional, shows up as Red in Neovim)
|
|
||||||
WarningsAsErrors: ""
|
|
||||||
|
|
||||||
# Project-specific settings
|
|
||||||
CheckOptions:
|
|
||||||
- key: readability-identifier-naming.VariableCase
|
|
||||||
value: lower_case
|
|
||||||
- key: readability-identifier-naming.FunctionCase
|
|
||||||
value: lower_case
|
|
||||||
---
|
|
||||||
+50
-51
@@ -121,12 +121,9 @@ static inline unsigned int
|
|||||||
u32_brighten(const unsigned int clr, const float factor)
|
u32_brighten(const unsigned int clr, const float factor)
|
||||||
{
|
{
|
||||||
unsigned int alpha = (clr & BYTE_A);
|
unsigned int alpha = (clr & BYTE_A);
|
||||||
unsigned int red =
|
unsigned int red = CLAMP_MAX(DECODE_R(clr) * factor);
|
||||||
CLAMP_MAX((unsigned int)((float)DECODE_R(clr) * factor));
|
unsigned int green = CLAMP_MAX(DECODE_G(clr) * factor);
|
||||||
unsigned int green =
|
unsigned int blue = CLAMP_MAX(DECODE_B(clr) * factor);
|
||||||
CLAMP_MAX((unsigned int)((float)DECODE_G(clr) * factor));
|
|
||||||
unsigned int blue =
|
|
||||||
CLAMP_MAX((unsigned int)((float)DECODE_B(clr) * factor));
|
|
||||||
return (clr & BYTE_A) | (red << SHIFT_16) | (green << SHIFT_8) | blue;
|
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
|
static inline unsigned int
|
||||||
u32_lerp(const unsigned clr_a, const unsigned int clr_b, const float ratio)
|
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)) +
|
unsigned char blue =
|
||||||
((DECODE_B(clr_b) * ratio)));
|
((DECODE_B(clr_a) * (1 - ratio)) + ((DECODE_B(clr_b) * ratio)));
|
||||||
unsigned char green = (unsigned char)((DECODE_G(clr_a) * (1 - ratio)) +
|
unsigned char green =
|
||||||
((DECODE_G(clr_b) * ratio)));
|
((DECODE_G(clr_a) * (1 - ratio)) + ((DECODE_G(clr_b) * ratio)));
|
||||||
unsigned char red = (unsigned char)((DECODE_R(clr_a) * (1 - ratio)) +
|
unsigned char red =
|
||||||
((DECODE_R(clr_b) * ratio)));
|
((DECODE_R(clr_a) * (1 - ratio)) + ((DECODE_R(clr_b) * ratio)));
|
||||||
return ENCODE_ARGB(DECODE_A(clr_a), red, green, blue);
|
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
|
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 red = CLAMP_MIN(CLAMP_MAX(
|
||||||
(int)(factor * (float)(int)(DECODE_R(clr) - HALF_MAX)) + HALF_MAX));
|
(int)(factor * (float)(int)(DECODE_R(clr) - HALF_MAX)) + HALF_MAX));
|
||||||
int green = CLAMP_MIN(CLAMP_MAX(
|
int green = CLAMP_MIN(CLAMP_MAX(
|
||||||
@@ -246,11 +242,11 @@ u32_contrast(unsigned int clr, float factor)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline unsigned int*
|
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));
|
unsigned int* mask = malloc(len * sizeof(unsigned int));
|
||||||
int old_width = img->width;
|
int old_width = m_img->width;
|
||||||
int old_height = img->height;
|
int old_height = m_img->height;
|
||||||
for (int row = 0; row < old_height; row++)
|
for (int row = 0; row < old_height; row++)
|
||||||
{
|
{
|
||||||
for (int col = 0; col < old_width; col++)
|
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];
|
buf[(row * old_width) + col];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
img->width = old_height;
|
m_img->width = old_height;
|
||||||
img->height = old_width;
|
m_img->height = old_width;
|
||||||
return mask;
|
return mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,7 +353,7 @@ box_blur(const unsigned int* src_buf, const PPM* img, const int radius)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline unsigned int*
|
static inline unsigned int*
|
||||||
ppm_load(const char* file, PPM* img)
|
ppm_load(const char* file, PPM* m_attr)
|
||||||
{
|
{
|
||||||
printf("Starting decode...\n");
|
printf("Starting decode...\n");
|
||||||
unsigned char red;
|
unsigned char red;
|
||||||
@@ -373,35 +369,35 @@ ppm_load(const char* file, PPM* img)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
fscanf(fptr, "%s", fmt);
|
fscanf(fptr, "%s", fmt);
|
||||||
img->fmt = fmt[1];
|
m_attr->fmt = fmt[1];
|
||||||
fscanf(fptr, "%d %d", &img->width, &img->height);
|
fscanf(fptr, "%d %d", &m_attr->width, &m_attr->height);
|
||||||
img->len = img->width * img->height;
|
m_attr->len = m_attr->width * m_attr->height;
|
||||||
fscanf(fptr, "%d", &max_val);
|
fscanf(fptr, "%d", &max_val);
|
||||||
fgetc(fptr);
|
fgetc(fptr);
|
||||||
unsigned char* img_buf = malloc(img->len * 3);
|
unsigned char* img_buf = malloc(m_attr->len * 3);
|
||||||
if (!img_buf)
|
if (!img_buf)
|
||||||
{
|
{
|
||||||
return NULL;
|
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)
|
if (!clr_buf)
|
||||||
{
|
{
|
||||||
free(img_buf);
|
free(img_buf);
|
||||||
return NULL;
|
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);
|
fscanf(fptr, "%hhu %hhu %hhu", &red, &green, &blue);
|
||||||
clr_buf[i] = ENCODE_RGB(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];
|
red = img_buf[i * 3];
|
||||||
green = img_buf[i * 3 + 1];
|
green = img_buf[i * 3 + 1];
|
||||||
@@ -416,18 +412,18 @@ ppm_load(const char* file, PPM* img)
|
|||||||
return clr_buf;
|
return clr_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline int
|
||||||
ppm_save(const unsigned int* buf, const PPM img)
|
ppm_save(const unsigned int* buf, const PPM img)
|
||||||
{
|
{
|
||||||
if (!buf)
|
if (!buf)
|
||||||
{
|
{
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
unsigned char red, green, blue;
|
unsigned char red, green, blue;
|
||||||
unsigned char* dst_buf = malloc(img.len * 3);
|
unsigned char* dst_buf = malloc(img.len * 3);
|
||||||
if (!dst_buf)
|
if (!dst_buf)
|
||||||
{
|
{
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
printf("Starting encode...\n");
|
printf("Starting encode...\n");
|
||||||
FILE* fptr;
|
FILE* fptr;
|
||||||
@@ -443,7 +439,7 @@ ppm_save(const unsigned int* buf, const PPM img)
|
|||||||
if (!fptr)
|
if (!fptr)
|
||||||
{
|
{
|
||||||
free(dst_buf);
|
free(dst_buf);
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(fptr, "P%c\n%d %d\n255\n", img.fmt, img.width, img.height);
|
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);
|
fclose(fptr);
|
||||||
free(dst_buf);
|
free(dst_buf);
|
||||||
printf("Encode Done!\n");
|
printf("Encode Done!\n");
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int*
|
unsigned int*
|
||||||
jpeg_load(JPEG* attr)
|
jpeg_load(JPEG* m_attr)
|
||||||
{
|
{
|
||||||
printf("Starting Decode...\n");
|
printf("Starting Decode...\n");
|
||||||
tjhandle tjdecompress = tj3Init(TJINIT_DECOMPRESS);
|
tjhandle tjdecompress = tj3Init(TJINIT_DECOMPRESS);
|
||||||
unsigned char* jpeg_buf = NULL;
|
unsigned char* jpeg_buf = NULL;
|
||||||
FILE* infile = fopen(attr->name, "rb");
|
FILE* infile = fopen(m_attr->name, "rb");
|
||||||
if (!infile)
|
if (!infile)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
fseek(infile, 0, SEEK_END);
|
fseek(infile, 0, SEEK_END);
|
||||||
attr->len = ftell(infile);
|
m_attr->len = ftell(infile);
|
||||||
fseek(infile, 0, SEEK_SET);
|
fseek(infile, 0, SEEK_SET);
|
||||||
jpeg_buf = (unsigned char*)malloc(attr->len);
|
jpeg_buf = (unsigned char*)malloc(m_attr->len);
|
||||||
fread(jpeg_buf, 1, attr->len, infile);
|
fread(jpeg_buf, 1, m_attr->len, infile);
|
||||||
fclose(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));
|
printf("Header error: %s\n", tj3GetErrorStr(tjdecompress));
|
||||||
free(jpeg_buf);
|
free(jpeg_buf);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
attr->width = tj3Get(tjdecompress, TJPARAM_JPEGWIDTH);
|
m_attr->width = tj3Get(tjdecompress, TJPARAM_JPEGWIDTH);
|
||||||
attr->height = tj3Get(tjdecompress, TJPARAM_JPEGHEIGHT);
|
m_attr->height = tj3Get(tjdecompress, TJPARAM_JPEGHEIGHT);
|
||||||
unsigned int* pixels =
|
unsigned int* pixels =
|
||||||
malloc(attr->width * attr->height * sizeof(unsigned int));
|
malloc(m_attr->width * m_attr->height * sizeof(unsigned int));
|
||||||
tj3Decompress8(tjdecompress, jpeg_buf, attr->len,
|
tj3Decompress8(tjdecompress, jpeg_buf, m_attr->len,
|
||||||
(unsigned char*)pixels, 0, TJPF_BGRA);
|
(unsigned char*)pixels, 0, TJPF_BGRA);
|
||||||
tj3Destroy(tjdecompress);
|
tj3Destroy(tjdecompress);
|
||||||
free(jpeg_buf);
|
free(jpeg_buf);
|
||||||
@@ -511,8 +508,8 @@ jpeg_load(JPEG* attr)
|
|||||||
return pixels;
|
return pixels;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
jpeg_save(unsigned int* src_buf, JPEG* attr, char* name)
|
jpeg_save(const unsigned int* src_buf, const JPEG* attr, const char* name)
|
||||||
{
|
{
|
||||||
printf("Starting Decode...\n");
|
printf("Starting Decode...\n");
|
||||||
tjhandle tjcompress = tj3Init(TJINIT_COMPRESS);
|
tjhandle tjcompress = tj3Init(TJINIT_COMPRESS);
|
||||||
@@ -528,9 +525,8 @@ jpeg_save(unsigned int* src_buf, JPEG* attr, char* name)
|
|||||||
FILE* fptr = fopen(name, "wb");
|
FILE* fptr = fopen(name, "wb");
|
||||||
if (!fptr || dst_buf == NULL || dst_size <= 0)
|
if (!fptr || dst_buf == NULL || dst_size <= 0)
|
||||||
{
|
{
|
||||||
free(src_buf);
|
|
||||||
tj3Free(dst_buf);
|
tj3Free(dst_buf);
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
fwrite(dst_buf, 1, dst_size, fptr);
|
fwrite(dst_buf, 1, dst_size, fptr);
|
||||||
fclose(fptr);
|
fclose(fptr);
|
||||||
@@ -538,6 +534,7 @@ jpeg_save(unsigned int* src_buf, JPEG* attr, char* name)
|
|||||||
tj3Destroy(tjcompress);
|
tj3Destroy(tjcompress);
|
||||||
tj3Free(dst_buf);
|
tj3Free(dst_buf);
|
||||||
printf("Encode Successful!\n");
|
printf("Encode Successful!\n");
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -567,9 +564,11 @@ main()
|
|||||||
};
|
};
|
||||||
|
|
||||||
unsigned int* src_buf = ppm_load("../assets/8ktest.ppm", &out);
|
unsigned int* src_buf = ppm_load("../assets/8ktest.ppm", &out);
|
||||||
unsigned int* blurred = box_blur(src_buf, &out, 10);
|
unsigned int* flip = ppm_flip_h(src_buf, out);
|
||||||
ppm_save(blurred, out);
|
// unsigned int* blurred = box_blur(src_buf, &out, 10);
|
||||||
|
ppm_save(flip, out);
|
||||||
free(src_buf);
|
free(src_buf);
|
||||||
free(blurred);
|
// free(blurred);
|
||||||
|
free(flip);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user