Skip to content

Commit

Permalink
Fix compiler warnings. Reformat C code (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcara committed Dec 7, 2023
1 parent 4af78a9 commit 7088d85
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 68 deletions.
18 changes: 18 additions & 0 deletions .clang-format
@@ -0,0 +1,18 @@
---
BasedOnStyle: Google
Language: Cpp
AlwaysBreakAfterReturnType: None
AlwaysBreakAfterDefinitionReturnType: TopLevel
BreakBeforeBraces: Stroustrup
DerivePointerAlignment: false
PointerAlignment: Right
SortIncludes: false
SortUsingDeclarations: false
IndentWidth: 4
TabWidth: 4
UseTab: Never
AlignConsecutiveMacros: true
ColumnLimit: 79
ReflowComments: true
SpaceBeforeParens: ControlStatements
...
71 changes: 40 additions & 31 deletions src/buildHistogram.c
Expand Up @@ -9,17 +9,16 @@
#include <Python.h>
#include "numpy/arrayobject.h"

int populate1DHist_(float *image, int image_elements,
unsigned int *histogram, int histogram_elements,
float minValue, float maxValue, float binWidth)
int
populate1DHist_(float *image, int image_elements, unsigned int *histogram,
int histogram_elements, float minValue, float maxValue,
float binWidth)
{
int i, idx;
float f, hist_edge, v;
unsigned int *last_bin;

f = 1.0f / binWidth;
hist_edge = minValue + binWidth * histogram_elements;
last_bin = histogram + (histogram_elements - 1);

if (maxValue > hist_edge) {
maxValue = hist_edge;
Expand All @@ -35,57 +34,67 @@ int populate1DHist_(float *image, int image_elements,
return 1;
}

static PyObject * populate1DHist(PyObject *obj, PyObject *args)
static PyObject *
populate1DHist(PyObject *obj, PyObject *args)
{
PyObject *oimage, *ohistogram;
PyArrayObject *image, *histogram;
float minValue, maxValue, binWidth;
int status;

if (!PyArg_ParseTuple(args,"OOfff:populate1DHist",&oimage,&ohistogram,&minValue,&maxValue,&binWidth))
return NULL;
(void)obj;

image = (PyArrayObject *)PyArray_ContiguousFromObject(oimage, NPY_FLOAT32, 1, 2);
if (!PyArg_ParseTuple(args, "OOfff:populate1DHist", &oimage, &ohistogram,
&minValue, &maxValue, &binWidth))
return NULL;

image = (PyArrayObject *)PyArray_ContiguousFromObject(oimage, NPY_FLOAT32,
1, 2);

if (!image) return NULL;

histogram = (PyArrayObject *)PyArray_ContiguousFromObject(ohistogram, NPY_UINT32, 1, 1);
histogram = (PyArrayObject *)PyArray_ContiguousFromObject(
ohistogram, NPY_UINT32, 1, 1);

if (!histogram) return NULL;

status = populate1DHist_((float *)PyArray_DATA(image), PyArray_Size((PyObject*)image),
(unsigned int *)PyArray_DATA(histogram), PyArray_Size((PyObject*)histogram),
minValue, maxValue, binWidth);
status = populate1DHist_(
(float *)PyArray_DATA(image), PyArray_Size((PyObject *)image),
(unsigned int *)PyArray_DATA(histogram),
PyArray_Size((PyObject *)histogram), minValue, maxValue, binWidth);

Py_XDECREF(image);
Py_XDECREF(histogram);

return Py_BuildValue("i", status);
}

static PyMethodDef buildHistogram_methods[] =
{
{"populate1DHist", populate1DHist, METH_VARARGS,
"populate1Dhist(image, histogram, minValue, maxValue, binWidth)"},
{0, 0} /* sentinel */
#pragma clang diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
static PyMethodDef buildHistogram_methods[] = {
{"populate1DHist", populate1DHist, METH_VARARGS,
"populate1Dhist(image, histogram, minValue, maxValue, binWidth)"},
{0, 0} /* sentinel */
};
#pragma GCC diagnostic pop

static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"buildHistogram", /* m_name */
"C build histogram module", /* m_doc */
-1, /* m_size */
buildHistogram_methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
PyModuleDef_HEAD_INIT,
"buildHistogram", /* m_name */
"C build histogram module", /* m_doc */
-1, /* m_size */
buildHistogram_methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};

PyMODINIT_FUNC PyInit_buildHistogram(void)
PyMODINIT_FUNC
PyInit_buildHistogram(void)
{
PyObject* m;
PyObject *m;
import_array();
m = PyModule_Create(&moduledef);
return m;
m = PyModule_Create(&moduledef);
return m;
}
84 changes: 47 additions & 37 deletions src/computeMean.c
Expand Up @@ -14,9 +14,10 @@
#include <Python.h>
#include "numpy/arrayobject.h"

int computeMean_(float *image, int nelements, float clipmin, float clipmax,
int *numGoodPixels, float *mean, float *stddev,
float *minValue, float *maxValue)
int
computeMean_(float *image, int nelements, float clipmin, float clipmax,
int *numGoodPixels, float *mean, float *stddev, float *minValue,
float *maxValue)
{
int i;
float tmpMinValue, tmpMaxValue;
Expand All @@ -34,7 +35,7 @@ int computeMean_(float *image, int nelements, float clipmin, float clipmax,
tmpMaxValue = (clipmax < image[0]) ? clipmax : image[0];

for (i = 0; i < nelements; i++) {
if ( (image[i] >= clipmin) && (image[i] <= clipmax) ) {
if ((image[i] >= clipmin) && (image[i] <= clipmax)) {
/* Find lowest value in the clipped image */
if (image[i] <= tmpMinValue) {
tmpMinValue = image[i];
Expand All @@ -60,29 +61,33 @@ int computeMean_(float *image, int nelements, float clipmin, float clipmax,
*maxValue = tmpMaxValue;
*mean = (float)(sum / *numGoodPixels);

for (i=0; i < nelements; i++) {
if ( (image[i] >= *minValue) && (image[i] <= *maxValue) ) {
sumdiff = sumdiff + ( (image[i] - *mean) * (image[i] - *mean) );
}
for (i = 0; i < nelements; i++) {
if ((image[i] >= *minValue) && (image[i] <= *maxValue)) {
sumdiff = sumdiff + ((image[i] - *mean) * (image[i] - *mean));
}
}

*stddev = (float)sqrt( sumdiff / (*numGoodPixels - 1));
*stddev = (float)sqrt(sumdiff / (*numGoodPixels - 1));

return 1;
}

static PyObject * computeMean(PyObject *obj, PyObject *args)
static PyObject *
computeMean(PyObject *obj, PyObject *args)
{
PyObject *oimage;
PyArrayObject *image;
int status=0;
int numGoodPixels;
float clipmin, clipmax, mean, stddev, minValue, maxValue;

if (!PyArg_ParseTuple(args,"Off:computeMean",&oimage, &clipmin, &clipmax))
return NULL;
(void)obj;

image = (PyArrayObject *)PyArray_ContiguousFromObject(oimage, NPY_FLOAT32, 1, 2);
if (!PyArg_ParseTuple(args, "Off:computeMean", &oimage, &clipmin,
&clipmax))
return NULL;

image = (PyArrayObject *)PyArray_ContiguousFromObject(oimage, NPY_FLOAT32,
1, 2);

if (!image) return NULL;

Expand All @@ -92,37 +97,42 @@ static PyObject * computeMean(PyObject *obj, PyObject *args)
minValue = 0;
maxValue = 0;

status = computeMean_((float *)PyArray_DATA(image), PyArray_Size((PyObject*)image),
clipmin, clipmax,
&numGoodPixels, &mean, &stddev, &minValue, &maxValue);
computeMean_((float *)PyArray_DATA(image), PyArray_Size((PyObject *)image),
clipmin, clipmax, &numGoodPixels, &mean, &stddev, &minValue,
&maxValue);
Py_XDECREF(image);

return Py_BuildValue("iffff",numGoodPixels,mean,stddev,minValue,maxValue);
return Py_BuildValue("iffff", numGoodPixels, mean, stddev, minValue,
maxValue);
}

static PyMethodDef computeMean_methods[] =
{
{"computeMean", computeMean, METH_VARARGS,
"computeMean(image, clipmin, clipmax, numGoodPixels, mean, stddev, minValue, maxValue)"},
{0, 0} /* sentinel */
#pragma clang diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
static PyMethodDef computeMean_methods[] = {
{"computeMean", computeMean, METH_VARARGS,
"computeMean(image, clipmin, clipmax, numGoodPixels, mean, stddev, "
"minValue, maxValue)"},
{0, 0} /* sentinel */
};
#pragma GCC diagnostic pop

static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"computeMean", /* m_name */
"C compute mean module", /* m_doc */
-1, /* m_size */
computeMean_methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
PyModuleDef_HEAD_INIT,
"computeMean", /* m_name */
"C compute mean module", /* m_doc */
-1, /* m_size */
computeMean_methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};

PyMODINIT_FUNC PyInit_computeMean(void)
PyMODINIT_FUNC
PyInit_computeMean(void)
{
PyObject* m;
import_array();
m = PyModule_Create(&moduledef);
return m;
PyObject *m;
import_array();
m = PyModule_Create(&moduledef);
return m;
}

0 comments on commit 7088d85

Please sign in to comment.