Skip to content

Commit

Permalink
update color sequence interpolation architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
oclyke committed Nov 29, 2023
1 parent 7899c2b commit 6edd652
Show file tree
Hide file tree
Showing 33 changed files with 192 additions and 140 deletions.
1 change: 1 addition & 0 deletions include/pysicgl/submodules/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

PyMODINIT_FUNC PyInit_color(void);
1 change: 1 addition & 0 deletions include/pysicgl/submodules/composition.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

PyMODINIT_FUNC PyInit_composition(void);
1 change: 1 addition & 0 deletions include/pysicgl/submodules/functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

PyMODINIT_FUNC PyInit_functional(void);
3 changes: 3 additions & 0 deletions include/pysicgl/submodules/functional/color.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

PyObject* color_from_rgba(PyObject* self, PyObject* args);
PyObject* color_to_rgba(PyObject* self, PyObject* args);
PyObject* interpolate_color_sequence(
PyObject* self_in, PyObject* args, PyObject* kwds);
1 change: 1 addition & 0 deletions include/pysicgl/submodules/functional/color_correction.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

PyObject* gamma_correct(PyObject* self, PyObject* args);
1 change: 1 addition & 0 deletions include/pysicgl/submodules/functional/drawing/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

PyObject* global_pixel(PyObject* self_in, PyObject* args);
PyObject* global_line(PyObject* self_in, PyObject* args);
Expand Down
1 change: 1 addition & 0 deletions include/pysicgl/submodules/functional/drawing/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

PyObject* interface_compose(PyObject* self_in, PyObject* args);
PyObject* interface_blit(PyObject* self_in, PyObject* args);
Expand Down
1 change: 1 addition & 0 deletions include/pysicgl/submodules/functional/drawing/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

PyObject* screen_fill(PyObject* self_in, PyObject* args);
PyObject* screen_pixel(PyObject* self_in, PyObject* args);
Expand Down
1 change: 1 addition & 0 deletions include/pysicgl/submodules/functional/operations.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

PyObject* scalar_field(PyObject* self_in, PyObject* args, PyObject* kwds);
PyObject* compose(PyObject* self_in, PyObject* args);
Expand Down
1 change: 1 addition & 0 deletions include/pysicgl/submodules/interpolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

PyMODINIT_FUNC PyInit_interpolation(void);
3 changes: 3 additions & 0 deletions include/pysicgl/types/color_sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

#include "pysicgl/types/color_sequence_interpolator.h"
#include "sicgl/color_sequence.h"

// declare the type
extern PyTypeObject ColorSequenceType;

typedef struct {
PyObject_HEAD color_sequence_t sequence;
ColorSequenceInterpolatorObject* interpolator;

// iterator state
// protected by the GIL
Expand Down
2 changes: 1 addition & 1 deletion include/pysicgl/types/color_sequence_interpolator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes must come first
// python includes first (clang-format)

#include "sicgl/color_sequence.h"

Expand Down
2 changes: 1 addition & 1 deletion include/pysicgl/types/compositor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes must come first
// python includes first (clang-format)

#include <stdbool.h>

Expand Down
2 changes: 1 addition & 1 deletion include/pysicgl/types/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes must come first
// python includes first (clang-format)

#include "pysicgl/types/screen.h"
#include "sicgl/interface.h"
Expand Down
2 changes: 1 addition & 1 deletion include/pysicgl/types/scalar_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes must come first
// python includes first (clang-format)

#include "sicgl/field.h"

Expand Down
2 changes: 1 addition & 1 deletion include/pysicgl/types/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes must come first
// python includes first (clang-format)

#include <stdbool.h>

Expand Down
2 changes: 1 addition & 1 deletion src/module.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes must come first
// python includes first (clang-format)

#include "pysicgl/submodules/color.h"
#include "pysicgl/submodules/composition.h"
Expand Down
2 changes: 1 addition & 1 deletion src/submodules/composition/module.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes must come first
// python includes first (clang-format)

#include "pysicgl/types/compositor.h"
#include "sicgl/compositors.h"
Expand Down
99 changes: 97 additions & 2 deletions src/submodules/functional/color.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#define PY_SSIZE_T_CLEAN
#include "sicgl/color.h"

#include <Python.h>
// python includes first (clang-format)

#include "pysicgl/types/color_sequence.h"
#include "sicgl/color.h"

PyObject* color_to_rgba(PyObject* self, PyObject* args) {
(void)self;
Expand Down Expand Up @@ -31,3 +33,96 @@ PyObject* color_from_rgba(PyObject* self, PyObject* args) {
PyLong_AsLong(PyTuple_GetItem(obj, 2)),
PyLong_AsLong(PyTuple_GetItem(obj, 3))));
}

PyObject* interpolate_color_sequence(
PyObject* self_in, PyObject* args, PyObject* kwds) {
(void)self_in;
int ret = 0;
ColorSequenceObject* color_sequence_obj;
PyObject* samples_obj;
char* keywords[] = {
"color_sequence",
"samples",
NULL,
};
if (!PyArg_ParseTupleAndKeywords(
args, kwds, "O!O", keywords, &ColorSequenceType, &color_sequence_obj,
&samples_obj)) {
return NULL;
}

// determine the interpolation function
sequence_map_fn interp_fn = color_sequence_obj->interpolator->fn;

// use this sequences' interpolation method to handle the input
if (PyLong_Check(samples_obj)) {
// input is a single sample, return the interpolated color directly
color_t color;
ret = interp_fn(
&color_sequence_obj->sequence, (double)PyLong_AsLong(samples_obj),
&color);
if (0 != ret) {
PyErr_SetNone(PyExc_OSError);
return NULL;
}
return PyLong_FromLong(color);

} else if (PyFloat_Check(samples_obj)) {
// input is a single sample, return the interpolated color directly
color_t color;
ret = interp_fn(
&color_sequence_obj->sequence, PyFloat_AsDouble(samples_obj), &color);
if (0 != ret) {
PyErr_SetNone(PyExc_OSError);
return NULL;
}
return PyLong_FromLong(color);

} else if (PyList_Check(samples_obj)) {
// input is a list of samples, return a tuple of interpolated colors
size_t num_samples = PyList_Size(samples_obj);
PyObject* result = PyTuple_New(num_samples);
for (size_t idx = 0; idx < num_samples; idx++) {
color_t color;
ret = interp_fn(
&color_sequence_obj->sequence,
PyFloat_AsDouble(PyList_GetItem(samples_obj, idx)), &color);
if (0 != ret) {
PyErr_SetNone(PyExc_OSError);
return NULL;
}
ret = PyTuple_SetItem(result, idx, PyLong_FromLong(color));
if (0 != ret) {
return NULL;
}
}
return result;

} else if (PyTuple_Check(samples_obj)) {
// input is a tuple of samples, return a tuple of interpolated colors
size_t num_samples = PyTuple_Size(samples_obj);
PyObject* result = PyTuple_New(num_samples);
for (size_t idx = 0; idx < num_samples; idx++) {
color_t color;
ret = interp_fn(
&color_sequence_obj->sequence,
PyFloat_AsDouble(PyTuple_GetItem(samples_obj, idx)), &color);
if (0 != ret) {
PyErr_SetNone(PyExc_OSError);
return NULL;
}
ret = PyTuple_SetItem(result, idx, PyLong_FromLong(color));
if (0 != ret) {
return NULL;
}
}

} else {
PyErr_SetNone(PyExc_TypeError);
return NULL;
}

// should never get here
PyErr_SetNone(PyExc_NotImplementedError);
return NULL;
}
1 change: 1 addition & 0 deletions src/submodules/functional/color_correction.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

#include "pysicgl/types/interface.h"
#include "sicgl/gamma.h"
Expand Down
4 changes: 2 additions & 2 deletions src/submodules/functional/drawing/global.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#define PY_SSIZE_T_CLEAN
#include "sicgl/domain/global.h"

#include <Python.h>
// python includes first (clang-format)

#include "pysicgl/types/interface.h"
#include "sicgl/blit.h"
#include "sicgl/domain/global.h"

PyObject* global_pixel(PyObject* self_in, PyObject* args) {
(void)self_in;
Expand Down
2 changes: 1 addition & 1 deletion src/submodules/functional/drawing/interface.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes must come first
// python includes first (clang-format)

#include "pysicgl/types/interface.h"
#include "sicgl/blit.h"
Expand Down
2 changes: 1 addition & 1 deletion src/submodules/functional/drawing/screen.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes must come first
// python includes first (clang-format)

#include "pysicgl/types/interface.h"
#include "sicgl/domain/screen.h"
Expand Down
5 changes: 5 additions & 0 deletions src/submodules/functional/module.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

#include "pysicgl/submodules/functional/color.h"
#include "pysicgl/submodules/functional/color_correction.h"
Expand Down Expand Up @@ -79,6 +80,10 @@ static PyMethodDef funcs[] = {
"Return the color comprised of the RGBA input 4-tuple."},
{"color_to_rgba", (PyCFunction)color_to_rgba, METH_VARARGS,
"Return the individual RGBA components of the input color as a 4-tuple."},
{"interpolate_color_sequence", (PyCFunction)interpolate_color_sequence,
METH_VARARGS | METH_KEYWORDS,
"Interpolate the color sequence at one or more points using the given "
"interpolation type."},

// color correction
{"gamma_correct", (PyCFunction)gamma_correct, METH_VARARGS,
Expand Down
14 changes: 6 additions & 8 deletions src/submodules/functional/operations.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes first (clang-format)

#include "pysicgl/types/color_sequence.h"
#include "pysicgl/types/color_sequence_interpolator.h"
Expand Down Expand Up @@ -36,22 +37,18 @@ PyObject* scalar_field(PyObject* self_in, PyObject* args, PyObject* kwds) {
ScreenObject* field_obj;
ScalarFieldObject* scalar_field_obj;
ColorSequenceObject* color_sequence_obj;
ColorSequenceInterpolatorObject* interpolator_obj;
double offset = 0.0;
char* keywords[] = {
"interface", "field", "scalars", "color_sequence",
"interpolator", "offset", NULL,
"interface", "screen", "scalar_field", "color_sequence", "offset", NULL,
};
if (!PyArg_ParseTupleAndKeywords(
args, kwds, "O!O!O!O!O!|d", keywords, &InterfaceType, &interface_obj,
args, kwds, "O!O!O!O!|d", keywords, &InterfaceType, &interface_obj,
&ScreenType, &field_obj, &ScalarFieldType, &scalar_field_obj,
&ColorSequenceType, &color_sequence_obj,
&ColorSequenceInterpolatorType, &interpolator_obj, &offset)) {
&ColorSequenceType, &color_sequence_obj, &offset)) {
return NULL;
}

Py_INCREF(color_sequence_obj);
Py_INCREF(interpolator_obj);
Py_INCREF(scalar_field_obj);

// check length of scalars is sufficient for the field
Expand All @@ -68,6 +65,8 @@ PyObject* scalar_field(PyObject* self_in, PyObject* args, PyObject* kwds) {
return NULL;
}

ColorSequenceInterpolatorObject* interpolator_obj =
color_sequence_obj->interpolator;
ret = sicgl_scalar_field(
&interface_obj->interface, field_obj->screen, scalar_field_obj->scalars,
offset, &color_sequence_obj->sequence, interpolator_obj->fn);
Expand All @@ -77,7 +76,6 @@ PyObject* scalar_field(PyObject* self_in, PyObject* args, PyObject* kwds) {
}

Py_DECREF(scalar_field_obj);
Py_DECREF(interpolator_obj);
Py_DECREF(color_sequence_obj);

Py_INCREF(Py_None);
Expand Down
2 changes: 1 addition & 1 deletion src/submodules/interpolation/module.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// python includes must come first
// python includes first (clang-format)

#include "pysicgl/types/color_sequence_interpolator.h"
#include "sicgl/color_sequence.h"
Expand Down
Loading

0 comments on commit 6edd652

Please sign in to comment.