- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfpng-python.cpp
More file actions
Latest commit
180 lines (147 loc) · 5.65 KB
/
Copy pathfpng-python.cpp
File metadata and controls
180 lines (147 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include<Python.h>
#defineNPY_NO_DEPRECATED_APINPY_1_7_API_VERSION
#include<numpy/arrayobject.h>
#include"fpng.h"
static PyObject* fpng_init(PyObject *self, PyObject *args) {
fpng::fpng_init();
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* fpng_encode_image_to_memory(PyObject *self, PyObject *args)
{
PyArrayObject *arrData;
if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &arrData)){
PyErr_SetString(PyExc_ValueError, "Invalid arguments. RGB uint8 numpy array as argument!");
returnNULL;
}
if (NULL == arrData){
Py_INCREF(Py_None);
return Py_None;
}
if (PyArray_NDIM(arrData) != 3){
PyErr_SetString(PyExc_ValueError, "Array NDIM > 3");
returnNULL;
}
if (PyArray_TYPE(arrData) != NPY_UINT8) {
PyErr_SetString(PyExc_ValueError, "Invalid array type ");
returnNULL;
}
unsignedint flags = 0;
std::vector<uint8_t> out;
bool success = fpng::fpng_encode_image_to_memory(PyArray_DATA(arrData), PyArray_DIM(arrData, 1), PyArray_DIM(arrData, 0), PyArray_DIM(arrData, 2), out, flags);
PyObject* encoded;
if (success) {
char* bytes = newchar[out.size()];
std::copy(out.begin(),out.end(),bytes);
encoded = PyBytes_FromStringAndSize(bytes, out.size());
} else {
Py_INCREF(Py_None);
encoded = Py_None;
}
returnPyTuple_Pack(2, PyBool_FromLong(success), encoded);
}
static PyObject* fpng_encode_image_to_file(PyObject *self, PyObject *args)
{
PyArrayObject *arrData;
unsignedchar *file_path;
if (!PyArg_ParseTuple(args, "sO!", &file_path, &PyArray_Type, &arrData)){
PyErr_SetString(PyExc_ValueError, "Invalid arguments. Pass file destination and RGB uint8 numpy array as arguments!");
returnNULL;
}
if (NULL == arrData){
Py_INCREF(Py_None);
return Py_None;
}
if (PyArray_NDIM(arrData) != 3){
PyErr_SetString(PyExc_ValueError, "Array NDIM > 3");
returnNULL;
}
if (PyArray_TYPE(arrData) != NPY_UINT8) {
PyErr_SetString(PyExc_ValueError, "Invalid array type - should be np.uint8");
returnNULL;
}
unsignedint flags = 0;
bool success = fpng::fpng_encode_image_to_file((constchar*)file_path, PyArray_DATA(arrData), PyArray_DIM(arrData, 1), PyArray_DIM(arrData, 0), PyArray_DIM(arrData, 2), flags);
returnPyBool_FromLong(success);
}
static PyObject* fpng_get_info(PyObject *self, PyObject *args)
{
constchar* image_bytes;
uint32_t image_size;
if (!PyArg_ParseTuple(args, "y#", &image_bytes, &image_size)){
PyErr_SetString(PyExc_ValueError, "Invalid arguments. Pass file bytes as argument!");
returnNULL;
}
uint32_t width, height, channels;
int retcode = fpng::fpng_get_info(image_bytes, image_size, width, height, channels);
returnPy_BuildValue("(iiii)", retcode, (int)width, (int)height, (int)channels);
}
static PyObject* fpng_decode_file(PyObject *self, PyObject *args)
{
constchar *file_path;
if (!PyArg_ParseTuple(args, "s", &file_path)){
PyErr_SetString(PyExc_ValueError, "Invalid arguments. Pass file path as argument!");
returnNULL;
}
std::vector<uint8_t> out;
uint32_t width, height, channels;
unsignedint desired_channels = 3;
int ret = fpng::fpng_decode_file(file_path, out, width, height, channels, desired_channels);
PyObject* img;
if (ret == 0) {
npy_intp dims[3] = {height, width, channels};
PyArrayObject* numpyArray = (PyArrayObject*)PyArray_SimpleNewFromData(3, dims, NPY_UINT8, (uint8_t*)out.data());
img = PyArray_Return(numpyArray);
} else {
Py_INCREF(Py_None);
img = Py_None;
}
returnPyTuple_Pack(2, PyLong_FromLong(ret), img);
}
static PyObject* fpng_decode_memory(PyObject *self, PyObject *args)
{
constchar* image_bytes;
uint32_t image_size;
if (!PyArg_ParseTuple(args, "y#", &image_bytes, &image_size)){
PyErr_SetString(PyExc_ValueError, "Invalid arguments. Pass file bytes as argument!");
returnNULL;
}
std::vector<uint8_t> out;
uint32_t width, height, channels;
unsignedint desired_channels = 3;
int ret = fpng::fpng_decode_memory(image_bytes, image_size, out, width, height, channels, desired_channels);
PyObject* img;
if (ret == 0) {
npy_intp dims[3] = {height, width, channels};
PyArrayObject* numpyArray = (PyArrayObject*)PyArray_SimpleNewFromData(3, dims, NPY_UINT8, (uint8_t*)out.data());
img = PyArray_Return(numpyArray);
} else {
Py_INCREF(Py_None);
img = Py_None;
}
returnPyTuple_Pack(2, PyLong_FromLong(ret), img);
}
static PyMethodDef FpngMethods[] = {
{"encode_image_to_file", fpng_encode_image_to_file, METH_VARARGS, "Encode numpy array to file as png using fpng."},
{"encode_image_to_memory", fpng_encode_image_to_memory, METH_VARARGS, "Encode numpy array to memory as png using fpng."},
{"decode_file", fpng_decode_file, METH_VARARGS, "Decode file from png to numpy array using fpng."},
{"decode_memory", fpng_decode_memory, METH_VARARGS, "Decode png file from bytes."},
{"get_info", fpng_get_info, METH_VARARGS, "Get png info about bytes. Returns fpng decode success/error indicator, along with width, height, channel data."},
{"init", fpng_init, METH_VARARGS, "Initialize fpng."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
staticstructPyModuleDef pyfpngmodule =
{
PyModuleDef_HEAD_INIT,
"pyfpng",
"Python bindings for fpng",
-1,
FpngMethods
};
PyMODINIT_FUNC
PyInit_pyfpng(void)
{
import_array();
fpng::fpng_init();
returnPyModule_Create(&pyfpngmodule);
}