Skip to content

Commit

Permalink
Follow-up on removing xmalloc by returning NULLs
Browse files Browse the repository at this point in the history
Took a closer look at src directory files blur, convmask, hopfield,
lambda, threshold, weights and followed-up with NULL returns for
malloc() if no more memory available.
Also looked at adding static for functions not called outside of those
files. You'll notice the *.h files are a little smaller.
gimp-plugin/main-gimp and src/image need similar fixes but there are
other parts that need further investigation.
In summary, took care of the easier stuff first :-P
  • Loading branch information
JoesCat committed Oct 22, 2024
1 parent 218bb5e commit 9ce672e
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 94 deletions.
22 changes: 13 additions & 9 deletions src/blur.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

#include <math.h>
#include <stdlib.h>
#include "blur.h"

#ifndef SQR
Expand All @@ -33,7 +32,7 @@ typedef struct
} point_t;

/* Return the integral of sqrt(radius^2 - z^2) for z = 0 to x */
static double circle_integral (double x, double radius) {
static double circle_integral(double x, double radius) {
double sin, sq_diff;
if (radius < 1e-6) {
return (0.0);
Expand All @@ -54,7 +53,7 @@ static double circle_integral (double x, double radius) {
}
}

static double circle_intensity (int x, int y, double radius) {
static double circle_intensity(int x, int y, double radius) {
double xlo, xhi, ylo, yhi, xc1, xc2;
double symmetry_factor;

Expand Down Expand Up @@ -101,15 +100,16 @@ convmask_t* blur_create_defocus(convmask_t* blur, double radius) {
double val;

r = (int)(radius + 0.5);
convmask_create(blur, r); //memfull?
if (!(convmask_create(blur, r)))
return NULL;

if (r < 1) {
convmask_set(blur, 0, 0, 1.0);
return blur;
} else {
for (i = 0; i <= r; i++) {
for (j = 0; j <= r; j++) {
val = (double)circle_intensity(i, j, radius);
val = circle_intensity(i, j, radius);
if (val < 0.0) val = 0.0;
convmask_set_circle(blur, i, j, val);
}
Expand All @@ -126,7 +126,8 @@ convmask_t* blur_create_gauss(convmask_t* blur, double variance) {
int i, j, radius;

if (variance < 1e-6) {
convmask_create(blur, 0); //memfull?
if (!(convmask_create(blur, 0)))
return NULL;
convmask_set(blur, 0, 0, 1.0);
return blur;
} else {
Expand All @@ -135,7 +136,8 @@ convmask_t* blur_create_gauss(convmask_t* blur, double variance) {
var = variance;
epsilon = sqrt(-2.0 * log(1e-2));
radius = (int)(var * epsilon + 0.5);
convmask_create(blur, radius); //memfull?
if (!(convmask_create(blur, radius)))
return NULL;
var *= var * 2.0;
mult = var * M_PI;
for (i = 0; i <= radius; i++) {
Expand Down Expand Up @@ -258,12 +260,14 @@ convmask_t* blur_create_motion(convmask_t* blur, double radius, double angle) {
point_t coords_line[4];

if (radius < 1e-4) {
convmask_create(blur, 0.01); //memfull?
if (!(convmask_create(blur, 0.01)))
return NULL;
convmask_set(blur, 0, 0, 1.0);
return blur;
} else {
r = (int)(radius + 1.0);
convmask_create(blur, r); //memfull?
if (!(convmask_create(blur, r)))
return NULL;
make_coords_line(coords_line, radius, angle);
for (i = -r; i <= r; i++) {
for (j = -r; j <= r; j++) {
Expand Down
29 changes: 15 additions & 14 deletions src/convmask.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
*
*/

#include <stdlib.h>
#include "convmask.h"

static double convmask_get_0(convmask_t* convmask, int i, int j) {
return ((abs(i) <= convmask->radius && abs(j) <= convmask->radius) ? convmask_get(convmask, i, j) : 0.0);
}

convmask_t* convmask_create(convmask_t* convmask, int radius) {
convmask->radius = radius;
radius *= 2;
Expand Down Expand Up @@ -99,17 +102,6 @@ convmask_t* convmask_normalize(convmask_t* convmask) {
return convmask;
}

void convmask_print(convmask_t* convmask, FILE* file) {
int i, j;
fprintf(file, "%s\n", "CONVMASK:");
for (i = -convmask->radius; i <= convmask->radius; i++) {
for (j = -convmask->radius; j <= convmask->radius; j++) {
fprintf(file, "%1.4f ", (float)convmask_get(convmask, j, i));
}
fprintf(file, "\n");
}
}

void convmask_set(convmask_t* convmask, int i, int j, double value) {
convmask->coef[j*convmask->r21 + convmask->speeder + i] = value;
}
Expand All @@ -118,6 +110,15 @@ double convmask_get(convmask_t* convmask, int i, int j) {
return (convmask->coef[j*convmask->r21 + convmask->speeder + i]);
}

double convmask_get_0(convmask_t* convmask, int i, int j) {
return ((abs(i) <= convmask->radius && abs(j) <= convmask->radius) ? convmask_get(convmask, i, j) : 0.0);
#if defined(NDEBUG)
void convmask_print(convmask_t* convmask, FILE* file) {
int i, j;
fprintf(file, "CONVMASK:\n");
for (i = -convmask->radius; i <= convmask->radius; i++) {
for (j = -convmask->radius; j <= convmask->radius; j++) {
fprintf(file, " %1.4f", (float)convmask_get(convmask, j, i));
}
fprintf(file, "\n");
}
}
#endif
11 changes: 6 additions & 5 deletions src/convmask.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#ifndef _CONVMASK_H
#define _CONVMASK_H

#include <stdio.h>
#include "compiler.h"

C_DECL_BEGIN
Expand All @@ -39,13 +38,15 @@ convmask_t* convmask_create(convmask_t* convmask, int radius);
void convmask_destroy(convmask_t* convmask);
convmask_t* convmask_normalize(convmask_t* convmask);
convmask_t* convmask_convolve(convmask_t* ct, convmask_t* c1, convmask_t* c2);
void convmask_set(convmask_t* convmask, int i, int j, double value);
double convmask_get(convmask_t* convmask, int i, int j);

void convmask_set_circle(convmask_t* convmask, int i, int j, double value);
#if defined(NDEBUG)
#include <stdio.h>
#include <stdlib.h>
void convmask_print(convmask_t* convmask, FILE* file);

void convmask_set(convmask_t* convmask, int i, int j, double value);
double convmask_get(convmask_t* convmask, int i, int j);
double convmask_get_0(convmask_t* convmask, int i, int j);
#endif

C_DECL_END

Expand Down
26 changes: 17 additions & 9 deletions src/hopfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*
*/

#include <stdlib.h>
#include "hopfield.h"

#define hardlim(x) ((x)>=0?1:-1)
Expand Down Expand Up @@ -362,26 +361,35 @@ static double hopfield_iteration_mirror_lambda(hopfield_t* hopfield) {
return Sum;
}

/* Public functions */

hopfield_t* hopfield_create_mirror(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld) {
static hopfield_t* hopfield_create_mirror(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld) {
hopfield->image = image;
hopfield->mirror = 1;
weights_create(&(hopfield->weights), convmask);
threshold_create_mirror(&(hopfield->threshold), convmask, image);
if (!(weights_create(&(hopfield->weights), convmask)))
return NULL;
if (!(threshold_create_mirror(&(hopfield->threshold), convmask, image))) {
weights_destroy(&(hopfield->weights));
return NULL;
}
hopfield->lambdafld = lambdafld;
return hopfield;
}

hopfield_t* hopfield_create_period(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld) {
static hopfield_t* hopfield_create_period(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld) {
hopfield->image = image;
hopfield->mirror = 0;
weights_create(&(hopfield->weights), convmask);
threshold_create_period(&(hopfield->threshold), convmask, image);
if (!(weights_create(&(hopfield->weights), convmask)))
return NULL;
if (!(threshold_create_mirror(&(hopfield->threshold), convmask, image))) {
weights_destroy(&(hopfield->weights));
return NULL;
}
hopfield->lambdafld = lambdafld;
hopfield->lambdafld = lambdafld;
return hopfield;
}

/* Public functions */

hopfield_t* hopfield_create(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld) {
if (hopfield->mirror) return hopfield_create_mirror(hopfield, convmask, image, lambdafld);
else return hopfield_create_period(hopfield, convmask, image, lambdafld);
Expand Down
10 changes: 4 additions & 6 deletions src/hopfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@
C_DECL_BEGIN

typedef struct {
int mirror;
int mirror;
image_t *image;
weights_t weights;
double lambda;
weights_t weights;
double lambda;
lambda_t *lambdafld;
threshold_t threshold;
threshold_t threshold;
} hopfield_t;

hopfield_t* hopfield_create_mirror(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld);
hopfield_t* hopfield_create_period(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld);
hopfield_t* hopfield_create(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld);
void hopfield_set_mirror(hopfield_t* hopfield, int mirror);
void hopfield_destroy(hopfield_t* hopfield);
Expand Down
62 changes: 44 additions & 18 deletions src/lambda.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*
*/

#include <stdlib.h>
#include "lambda.h"

static void get_variance_mirror(image_t* variance, image_t* img, double* pmin, double* pmax, int winsize) {
Expand Down Expand Up @@ -121,21 +120,28 @@ void lambda_set_nl(lambda_t* lambda, int nl) {
lambda->nl = nl;
}

lambda_t* lambda_calculate_period(lambda_t* lambda, image_t* image) {
static lambda_t* lambda_calculate_period(lambda_t* lambda, image_t* image) {
image_t imgenh, *imgcal;
image_t variance;
double minvar, maxvar;
double akoef, bkoef;
int i, size;

if (lambda->filter) {
imgcal = image_create_copyparam(&imgenh, image);
image_convolve_period(imgcal, image, lambda->filter);
if (!(imgcal = image_create_copyparam(&imgenh, image)))
return NULL;
if (!(image_convolve_period(imgcal, image, lambda->filter))) {
image_destroy(imgcal);
return NULL;
}
} else {
imgcal = image;
}

image_create_copyparam(&variance, imgcal);
if (!(image_create_copyparam(&variance, imgcal))) {
if (imgcal == &imgenh) image_destroy(imgcal);
return NULL;
}

get_variance_period(&variance, imgcal, &minvar, &maxvar, lambda->winsize);

Expand All @@ -155,21 +161,28 @@ lambda_t* lambda_calculate_period(lambda_t* lambda, image_t* image) {
return lambda;
}

lambda_t* lambda_calculate_period_nl(lambda_t* lambda, image_t* image) {
static lambda_t* lambda_calculate_period_nl(lambda_t* lambda, image_t* image) {
image_t imgenh, *imgcal;
image_t variance;
double minvar, maxvar;
double alpha;
int i, size;

if (lambda->filter) {
imgcal = image_create_copyparam(&imgenh, image);
image_convolve_period(imgcal, image, lambda->filter);
if (!(imgcal = image_create_copyparam(&imgenh, image)))
return NULL;
if (!(image_convolve_period(imgcal, image, lambda->filter))) {
image_destroy(imgcal);
return NULL;
}
} else {
imgcal = image;
}

image_create_copyparam(&variance, imgcal);
if (!(image_create_copyparam(&variance, imgcal))) {
if (imgcal == &imgenh) image_destroy(imgcal);
return NULL;
}

get_variance_period(&variance, imgcal, &minvar, &maxvar, lambda->winsize);

Expand All @@ -188,22 +201,28 @@ lambda_t* lambda_calculate_period_nl(lambda_t* lambda, image_t* image) {
return lambda;
}


lambda_t* lambda_calculate_mirror(lambda_t* lambda, image_t* image) {
static lambda_t* lambda_calculate_mirror(lambda_t* lambda, image_t* image) {
image_t imgenh, *imgcal;
image_t variance;
double minvar, maxvar;
double akoef, bkoef;
int i, size;

if (lambda->filter) {
imgcal = image_create_copyparam(&imgenh, image);
image_convolve_mirror(imgcal, image, lambda->filter);
if (!(imgcal = image_create_copyparam(&imgenh, image)))
return NULL;
if (!(image_convolve_period(imgcal, image, lambda->filter))) {
image_destroy(imgcal);
return NULL;
}
} else {
imgcal = image;
}

image_create_copyparam(&variance, imgcal);
if (!(image_create_copyparam(&variance, imgcal))) {
if (imgcal == &imgenh) image_destroy(imgcal);
return NULL;
}

get_variance_mirror(&variance, imgcal, &minvar, &maxvar, lambda->winsize);

Expand All @@ -223,21 +242,28 @@ lambda_t* lambda_calculate_mirror(lambda_t* lambda, image_t* image) {
return lambda;
}

lambda_t* lambda_calculate_mirror_nl(lambda_t* lambda, image_t* image) {
static lambda_t* lambda_calculate_mirror_nl(lambda_t* lambda, image_t* image) {
image_t imgenh, *imgcal;
image_t variance;
double minvar, maxvar;
double alpha;
int i, size;

if (lambda->filter) {
imgcal = image_create_copyparam(&imgenh, image);
image_convolve_mirror(imgcal, image, lambda->filter);
if (!(imgcal = image_create_copyparam(&imgenh, image)))
return NULL;
if (!(image_convolve_period(imgcal, image, lambda->filter))) {
image_destroy(imgcal);
return NULL;
}
} else {
imgcal = image;
}

image_create_copyparam(&variance, imgcal);
if (!(image_create_copyparam(&variance, imgcal))) {
if (imgcal == &imgenh) image_destroy(imgcal);
return NULL;
}

get_variance_mirror(&variance, imgcal, &minvar, &maxvar, lambda->winsize);

Expand Down
4 changes: 0 additions & 4 deletions src/lambda.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ typedef struct {
lambda_t* lambda_create(lambda_t* lambda, int x, int y, double minlambda, int winsize, convmask_t* filter);
void lambda_destroy(lambda_t* lambda);

lambda_t* lambda_calculate_period(lambda_t* lambda, image_t* image);
lambda_t* lambda_calculate_period_nl(lambda_t* lambda, image_t* image);
lambda_t* lambda_calculate_mirror(lambda_t* lambda, image_t* image);
lambda_t* lambda_calculate_mirror_nl(lambda_t* lambda, image_t* image);
lambda_t* lambda_calculate(lambda_t* lambda, image_t* image);

void lambda_set_mirror(lambda_t* lambda, int mirror);
Expand Down
1 change: 0 additions & 1 deletion src/threshold.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*
*/

#include <stdlib.h>
#include "threshold.h"

threshold_t* threshold_create_mirror(threshold_t* threshold, convmask_t* convmask, image_t* image) {
Expand Down
Loading

0 comments on commit 9ce672e

Please sign in to comment.