Initial push

This commit is contained in:
2025-09-12 12:07:47 -05:00
parent 77a8ac4941
commit edbd080ad6
48 changed files with 1603 additions and 2 deletions

View File

@@ -0,0 +1,15 @@
#!/bin/bash
# . CONFIG.sh
# set -o xtrace ## To debug scripts
# set -o errexit ## To exit on error
# set -o errunset ## To exit if a variable is referenced but not set
function main() {
cd "$(dirname "")"
echo "Working Dir: " $(pwd)
python3 setup.py build && python3 setup.py install --user
}
main "$@";

View File

@@ -0,0 +1,80 @@
#include <Python.h>
#include <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <stdlib.h>
static PyObject* pixbuf_to_cairo_data(PyObject* self, PyObject* args) {
PyObject *py_pixbuf;
if (!PyArg_ParseTuple(args, "O", &py_pixbuf)) {
return NULL;
}
GdkPixbuf *pixbuf = (GdkPixbuf *) PyLong_AsVoidPtr(py_pixbuf);
if (!GDK_IS_PIXBUF(pixbuf)) {
PyErr_SetString(PyExc_TypeError, "Invalid GdkPixbuf pointer.");
return NULL;
}
int width = gdk_pixbuf_get_width(pixbuf);
int height = gdk_pixbuf_get_height(pixbuf);
int rowstride = gdk_pixbuf_get_rowstride(pixbuf);
int n_channels = gdk_pixbuf_get_n_channels(pixbuf);
gboolean has_alpha = gdk_pixbuf_get_has_alpha(pixbuf);
const guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
int cairo_stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
Py_ssize_t buffer_size = height * cairo_stride;
guchar *cairo_data = malloc(buffer_size);
if (!cairo_data) {
PyErr_NoMemory();
return NULL;
}
for (int y = 0; y < height; ++y) {
const guchar *src = pixels + y * rowstride;
guchar *dst = cairo_data + y * cairo_stride;
for (int x = 0; x < width; ++x) {
const guchar *p = src + x * n_channels;
guchar *q = dst + x * 4;
guchar r = p[0];
guchar g = p[1];
guchar b = p[2];
guchar a = 255;
if (has_alpha) {
a = p[3];
r = (r * a) / 255;
g = (g * a) / 255;
b = (b * a) / 255;
}
q[0] = b;
q[1] = g;
q[2] = r;
q[3] = a;
}
}
return PyBytes_FromStringAndSize((const char *) cairo_data, buffer_size);
}
static PyMethodDef Methods[] = {
{"pixbuf_to_cairo_data", pixbuf_to_cairo_data, METH_VARARGS, "Convert GdkPixbuf* to compatible Cairo ImageSurface data format."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"pixbuf2cairo",
NULL,
-1,
Methods
};
PyMODINIT_FUNC PyInit_pixbuf2cairo(void) {
return PyModule_Create(&moduledef);
}

View File

@@ -0,0 +1,31 @@
from setuptools import setup, Extension
import gi
gi.require_version("GdkPixbuf", "2.0")
from gi.repository import GdkPixbuf
from gi.repository import GObject
pkg_config_args = [
"--cflags", "--libs",
"gdk-pixbuf-2.0",
"cairo"
]
from subprocess import check_output
def get_pkgconfig_flags(flag_type):
return check_output(["pkg-config", flag_type] + pkg_config_args).decode().split()
ext = Extension(
"pixbuf2cairo",
sources=["pixbuf2cairo.c"],
include_dirs=[],
extra_compile_args=get_pkgconfig_flags("--cflags"),
extra_link_args=get_pkgconfig_flags("--libs")
)
setup(
name="pixbuf2cairo",
version="0.1",
ext_modules=[ext]
)