From 38eae482ba5c9848aa75d6f1b73d6d507cf30fb9 Mon Sep 17 00:00:00 2001 From: tth Date: Wed, 7 Aug 2019 11:55:29 +0200 Subject: [PATCH] clean the code --- floatimg.h | 7 ++++++- lib/fimg-core.c | 37 +++++++++++++++++++++++++++---------- lib/t.c | 6 ++++-- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/floatimg.h b/floatimg.h index 65c652ad..37c9df37 100644 --- a/floatimg.h +++ b/floatimg.h @@ -2,7 +2,7 @@ * floatimg.h */ -#define FIMG_VERSION 62 +#define FIMG_VERSION 64 /* * in memory descriptor @@ -27,6 +27,11 @@ typedef struct { int w, h, t; } FimgFileHead; + +#define FIMG_TYPE_GRAY 1 +#define FIMG_TYPE_RGB 3 +#define FIMG_TYPE_RGBA 4 + /* * core module */ diff --git a/lib/fimg-core.c b/lib/fimg-core.c index 37bf94cf..b424c801 100644 --- a/lib/fimg-core.c +++ b/lib/fimg-core.c @@ -14,14 +14,26 @@ extern int verbosity; /* must be declared around main() */ /* ---------------------------------------------------------------- */ -static int fimg_type_is_valid(int t) +static int fimg_type_is_valid(int type) { -switch (t) { - case 1: case 3: case 4: return 1; +switch (type) { + case FIMG_TYPE_GRAY: + case FIMG_TYPE_RGB: + case FIMG_TYPE_RGBA: return 1; } return 0; } /* --------------------------------------------------------------------- */ +static char *fimg_str_type(int type) +{ +switch (type) { + case FIMG_TYPE_GRAY: return "gray"; + case FIMG_TYPE_RGB: return "rgb"; + case FIMG_TYPE_RGBA: return "rgba"; + } +return "???"; +} +/* --------------------------------------------------------------------- */ int fimg_print_version(int k) { fprintf(stderr, "*** FloatImg library, alpha v%d (%s, %s)\n", @@ -52,9 +64,11 @@ if( ! fimg_type_is_valid(head->type) ) { return -1; } -printf(" size %d x %d x %d\n", - head->width, head->height, head->type); +printf(" type %d\n", head->type); +printf(" dims %d x %d\n", + head->width, head->height); printf(" fval/count %f %d\n", head->fval, head->count); + printf(" pixels@ %p %p %p %p\n", head->R, head->G, head->B, head->A); @@ -78,7 +92,7 @@ if ( ! fimg_type_is_valid(t) ) { memset(fimg, 0, sizeof(FloatImg)); surface = w * h; -size = surface * t * sizeof(float); +size = surface * t * sizeof(float); #if DEBUG_LEVEL > 1 fprintf(stderr, "surface is %d pixels, need %d bytes\n", surface, size); #endif @@ -97,11 +111,12 @@ fimg->width = w; fimg->height = h; fimg->type = t; fimg->R = fptr; -if ( (t==3) || (t==4) ) { +if ( (t==FIMG_TYPE_RGB) || (t==FIMG_TYPE_RGBA) ) { fimg->G = fptr + surface; fimg->B = fptr + surface + surface; } -if ( t==4 ) fimg->A = fptr + (3 * surface); +if ( t==FIMG_TYPE_RGBA ) fimg->A = fptr + (3 * surface); +/* ok this a really WTF fragment of code */ return 0; } @@ -120,10 +135,12 @@ if (NULL == fimg) { } if ( ! fimg_type_is_valid(fimg->type) ) { + fprintf(stderr, "%s : type %d invalid\n", __func__, + fimg->type); return -2; } if (NULL == fimg->R) { - fprintf(stderr, "%s : %p already freeed\n", __func__, fimg); + fprintf(stderr, "%s : %p already freed\n", __func__, fimg); return -3; } free(fimg->R); @@ -147,7 +164,7 @@ if ( ! fimg_type_is_valid(fimg->type) ) { size = fimg->width * fimg->height * fimg->type * sizeof(float); memset(fimg->R, 0, size); -return -1; +return 0; } /* --------------------------------------------------------------------- */ diff --git a/lib/t.c b/lib/t.c index ad8078e9..dd95758f 100644 --- a/lib/t.c +++ b/lib/t.c @@ -1,7 +1,7 @@ #include #include #include -#include "string.h" +#include #include "../floatimg.h" @@ -16,9 +16,11 @@ int datas[3]; verbosity = 1; +fimg_print_version(0); + foo = fimg_create(&fimg, 640, 480, 3); -printf("retour du truc ---> %d\n", foo); +printf("retour fimg_create ---> %d\n", foo); fimg_printhead(&fimg);