big refactoring step 1, expect more bugs

This commit is contained in:
tth
2021-03-09 11:55:48 +01:00
parent 7ada60d113
commit b9d963dfa2
14 changed files with 179 additions and 100 deletions

View File

@@ -19,7 +19,8 @@ int fimg_type_is_valid(int type)
switch (type) {
case FIMG_TYPE_GRAY:
case FIMG_TYPE_RGB:
case FIMG_TYPE_RGBA: return 1;
case FIMG_TYPE_RGBA:
case FIMG_TYPE_RGBZ: return 1;
}
return 0;
}
@@ -30,13 +31,14 @@ switch (type) {
case FIMG_TYPE_GRAY: return "gray";
case FIMG_TYPE_RGB: return "rgb";
case FIMG_TYPE_RGBA: return "rgba";
case FIMG_TYPE_RGBZ: return "rgbz";
}
return "???";
}
/* --------------------------------------------------------------------- */
int fimg_print_version(int k)
{
fprintf(stderr, "*** FloatImg library, alpha v%d (%s, %s)\n",
fprintf(stderr, "*** FloatImg library, alpha %d (%s, %s)\n",
FIMG_VERSION, __DATE__, __TIME__);
if (51 == k) {
@@ -52,6 +54,7 @@ void fimg_print_sizeof(void)
{
fprintf(stderr, " sz FloatImg = %lu\n", sizeof(FloatImg));
fprintf(stderr, " sz filehead = %lu\n", sizeof(FimgFileHead));
fprintf(stderr, " sz filter = %lu\n", sizeof(FimgFilter3x3));
}
/* --------------------------------------------------------------------- */
void fimg_printhead(FloatImg *h)
@@ -81,19 +84,35 @@ printf(" pixels@ %p %p %p %p\n",
return 0;
}
/* ---------------------------------------------------------------- */
/*
* values for the parameter 't' are defined in 'floatimg.h'
*/
int fimg_create(FloatImg *fimg, int w, int h, int t)
static float *plane_alloc(int size)
{
int surface, size;
float *fptr;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p %d %d %d )\n", __func__, fimg, w, h, t);
fprintf(stderr, ">>> %s ( %d )\n", __func__, size);
#endif
if ( ! fimg_type_is_valid(t) ) {
fptr = calloc(size, sizeof(float));
if (NULL==fptr) {
fprintf(stderr, "no more memory available, ABEND\n");
abort();
}
return fptr;
}
/* ---------------------------------------------------------------- */
/*
* values for the parameter 'type' are defined in 'floatimg.h'
*/
int fimg_create(FloatImg *fimg, int w, int h, int type)
{
int size;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %d %d %d )\n", __func__, fimg, w, h, type);
#endif
if ( ! fimg_type_is_valid(type) ) {
fprintf(stderr, "%s: type %d invalid\n", __func__, type);
return -2;
}
@@ -101,35 +120,24 @@ if ( ! fimg_type_is_valid(t) ) {
* what to do if we've got a descriptor for an image
* already allocated ?
*/
memset(fimg, 0, sizeof(FloatImg));
surface = w * h;
size = surface * t * sizeof(float);
#if DEBUG_LEVEL > 1
fprintf(stderr, "surface is %d pixels, need %d bytes\n", surface, size);
#endif
fptr = (float *)malloc(size);
if (NULL==fptr) {
fprintf(stderr, "%s : no mem, exiting.\n", __func__);
exit(1);
}
#if DEBUG_LEVEL > 1
fprintf(stderr, " %s: got %d bytes at %p\n", __func__, size, fptr);
#endif
size = w * h;
fimg->width = w; fimg->height = h;
fimg->type = t;
fimg->type = type;
fimg->R = fptr;
if ( (t==FIMG_TYPE_RGB) || (t==FIMG_TYPE_RGBA) ) {
fimg->G = fptr + surface;
fimg->B = fptr + surface + surface;
/* the red channel is allway allocated */
fimg->R = (float *)plane_alloc(size);
if (FIMG_TYPE_RGB == type) {
fimg->G = (float *)plane_alloc(size);
fimg->B = (float *)plane_alloc(size);
}
if ( t==FIMG_TYPE_RGBA ) fimg->A = fptr + (3 * surface);
/* ok this a really WTF fragment of code */
if (FIMG_TYPE_RGBA == type) {
fimg->A = (float *)plane_alloc(size);
}
return 0;
}
@@ -156,6 +164,8 @@ if (NULL == fimg->R) {
fprintf(stderr, "%s : %p already freed ?\n", __func__, fimg);
return -3;
}
free(fimg->R);
memset(fimg, 0, sizeof(FloatImg));
@@ -167,7 +177,7 @@ int fimg_clone(FloatImg *old, FloatImg *new, int flags)
int foo;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p %p 0x%x)\n", __func__, old, new, flags);
fprintf(stderr, ">>> %s ( %p %p 0x%x )\n", __func__, old, new, flags);
#endif
if ( ! fimg_type_is_valid(old->type) ) {
@@ -197,7 +207,7 @@ int size;
int foo;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p %p )\n", __func__, from, to);
fprintf(stderr, ">>> %s ( %p %p )\n", __func__, from, to);
#endif
foo = fimg_images_not_compatible(from, to);
@@ -206,8 +216,10 @@ if (foo) {
return foo;
}
size = from->width * from->height * from->type * sizeof(float);
size = from->width * from->height * sizeof(float);
memcpy(to->R, from->R, size);
memcpy(to->G, from->G, size);
memcpy(to->B, from->B, size);
return 0;
}