Skip to content

Instantly share code, notes, and snippets.

@sfan5
Last active February 9, 2020 02:14
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfan5/7827430 to your computer and use it in GitHub Desktop.
Save sfan5/7827430 to your computer and use it in GitHub Desktop.
A small python wrapper for xbrz
#You may have to tweak some things here
CC = gcc
CXX = g++
FLAGS = -I/usr/include/python2.7
CFLAGS =
CXXFLAGS =
LDFLAGS =
#Do not change anything beyond this point
CXXFLAGS += -std=c++11
FLAGS += -Wall -fpic
OBJS = xbrz.o wrap.o xbrzmodule.o
all: xbrzmodule
xbrzmodule: $(OBJS)
gcc $(OBJS) $(LDFLAGS) -shared -o xbrz.so
%.o: %.cpp
$(CXX) $(FLAGS) $(CXXFLAGS) -c -o $@ $^
%.o: %.c
$(CC) $(FLAGS) $(CFLAGS) -c -o $@ $^
clean:
rm -f $(OBJS)
.PHONY: clean
#include "xbrz.h"
extern "C" void xbrzscale(size_t factor, const uint32_t* src, uint32_t* trg, int srcWidth, int srcHeight)
{
xbrz::scale(factor, src, trg, srcWidth, srcHeight);
}
extern "C" void nnscale(size_t factor, const uint32_t* src, uint32_t* trg, int srcWidth, int srcHeight)
{
xbrz::nearestNeighborScale(src, srcWidth, srcHeight, trg, srcWidth * factor, srcHeight * factor);
}
#include <Python.h>
void xbrzscale(size_t factor, const uint32_t* src, uint32_t* trg, int srcWidth, int srcHeight);
void nnscale(size_t factor, const uint32_t* src, uint32_t* trg, int srcWidth, int srcHeight);
static PyObject *xbrz_scale(PyObject *self, PyObject *args)
{
int fac, srcw, srch;
PyByteArrayObject *bbuf, *bobuf;
char *buf, *obuf;
if (!PyArg_ParseTuple(args, "iOii", &fac, &bbuf, &srcw, &srch))
return NULL;
buf = PyByteArray_AsString((PyObject*) bbuf);
if (fac < 2 || fac > 5)
return NULL;
obuf = malloc(srcw * fac * srch * fac * 4);
if (!obuf)
return NULL;
xbrzscale(fac, (const uint32_t*) buf, (uint32_t*) obuf, srcw, srch);
bobuf = (PyByteArrayObject*) PyByteArray_FromStringAndSize(obuf, srcw * fac * srch * fac * 4);
return Py_BuildValue("O", bobuf);
}
static PyObject *xbrz_nnscale(PyObject *self, PyObject *args)
{
int fac, srcw, srch;
PyByteArrayObject *bbuf, *bobuf;
char *buf, *obuf;
if (!PyArg_ParseTuple(args, "iOii", &fac, &bbuf, &srcw, &srch))
return NULL;
buf = PyByteArray_AsString((PyObject*) bbuf);
if (fac < 2 || fac > 5)
return NULL;
obuf = malloc(srcw * fac * srch * fac * 4);
if (!obuf)
return NULL;
nnscale(fac, (const uint32_t*) buf, (uint32_t*) obuf, srcw, srch);
bobuf = (PyByteArrayObject*) PyByteArray_FromStringAndSize(obuf, srcw * fac * srch * fac * 4);
return Py_BuildValue("O", bobuf);
}
static PyMethodDef XbrzMethods[] = {
{"scale", xbrz_scale, METH_VARARGS, "xBRZ image scaling algorithm"},
// bytearray scale(factor[int], input[bytearray], inputWidth[int], inputHeight[int])
{"nnscale", xbrz_nnscale, METH_VARARGS, "Nearest-Neighbor image scalingalgorithm"},
// bytearray nnscale(factor[int], input[bytearray], inputWidth[int], inputHeight[int])
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC initxbrz(void)
{
(void) Py_InitModule("xbrz", XbrzMethods);
}
#!/usr/bin/env python
import sys
from PIL import Image
import xbrz
if len(sys.argv) <= 3:
print("Usage: %s <input> <factor> <output>" % sys.argv[0])
print("xbrzscale scales an image using xBRZ while making sure no transparent pixels are outputted (using NN scaling)")
else:
fc = int(sys.argv[2])
ini = Image.open(sys.argv[1])
ini = ini.convert("RGBA")
insz = ini.size
out_m = Image.fromstring("RGBA", (insz[0] * fc, insz[1] * fc), str(xbrz.scale(fc, bytearray(ini.tostring()), insz[0], insz[1])))
out = Image.fromstring("RGBA", (insz[0] * fc, insz[1] * fc), str(xbrz.nnscale(fc, bytearray(ini.tostring()), insz[0], insz[1])))
out.paste(out_m, (0, 0), out_m)
out.save(sys.argv[3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment