-
Notifications
You must be signed in to change notification settings - Fork 2
/
oil-python.c
814 lines (685 loc) · 26 KB
/
oil-python.c
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
#include "oil-python.h"
/*
* file IO implementation for python file-like objects
*/
static size_t oil_python_read(void *file, void *data, size_t length) {
void *buffer = NULL;
Py_ssize_t buflength = 0;
PyObject *result = PyObject_CallMethod(file, "read", "i", length);
if (!result)
return 0;
if (!PyString_Check(result)) {
Py_DECREF(result);
return 0;
}
PyString_AsStringAndSize(result, (char **)&buffer, &buflength);
if (!buffer || !buflength || buflength > length) {
Py_DECREF(result);
return 0;
}
memcpy(data, buffer, buflength);
Py_DECREF(result);
return buflength;
}
static size_t oil_python_write(void *file, void *data, size_t length) {
PyObject *result = PyObject_CallMethod(file, "write", "s#", data, length);
if (!result)
return 0;
Py_DECREF(result);
return length;
}
static void oil_python_flush(void *file) {
PyObject *result = PyObject_CallMethod(file, "flush", "");
if (result) {
Py_DECREF(result);
}
}
/* forward declaration of Matrix type */
static PyTypeObject PyOILMatrixType;
/* init and dealloc for the Matrix type */
static int PyOILMatrix_set_data(PyOILMatrix *self, PyObject *arg, void *unused);
static PyOILMatrix *PyOILMatrix_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) {
PyObject *data = NULL;
PyOILMatrix *self;
/* for now, do not allow initialization */
if (!PyArg_ParseTuple(args, "|O", &data)) {
return NULL;
}
self = (PyOILMatrix *)(subtype->tp_alloc(subtype, 0));
if (!self)
return NULL;
if (data) {
if (PyOILMatrix_set_data(self, data, NULL)) {
Py_DECREF(self);
return NULL;
}
} else {
oil_matrix_set_identity(&(self->matrix));
}
return self;
}
static void PyOILMatrix_dealloc(PyOILMatrix *self) {
self->ob_type->tp_free((PyObject *)self);
}
/* methods for Matrix type */
static PyObject *PyOILMatrix_get_data(PyOILMatrix *self, PyObject *args) {
int i;
PyObject *tuples[4];
PyObject *full_tuple;
/* args == NULL means we're called as an attribute-getter */
if (args != NULL) {
if (!PyArg_ParseTuple(args, "")) {
return NULL;
}
}
for (i = 0; i < 4; i++) {
PyObject *t0 = PyFloat_FromDouble(self->matrix.data[i][0]);
PyObject *t1 = PyFloat_FromDouble(self->matrix.data[i][1]);
PyObject *t2 = PyFloat_FromDouble(self->matrix.data[i][2]);
PyObject *t3 = PyFloat_FromDouble(self->matrix.data[i][3]);
tuples[i] = PyTuple_Pack(4, t0, t1, t2, t3);
Py_DECREF(t0);
Py_DECREF(t1);
Py_DECREF(t2);
Py_DECREF(t3);
}
full_tuple = PyTuple_Pack(4, tuples[0], tuples[1], tuples[2], tuples[3]);
Py_DECREF(tuples[0]);
Py_DECREF(tuples[1]);
Py_DECREF(tuples[2]);
Py_DECREF(tuples[3]);
return full_tuple;
}
static PyObject *PyOILMatrix_transform(PyOILMatrix *self, PyObject *args) {
float x, y, z;
if (!PyArg_ParseTuple(args, "fff", &x, &y, &z)) {
return NULL;
}
oil_matrix_transform(&(self->matrix), &x, &y, &z);
return Py_BuildValue("fff", x, y, z);
}
static int PyOILMatrix_set_data(PyOILMatrix *self, PyObject *arg, void *unused) {
int i;
float data[4][4];
PyObject *argfast = PySequence_Fast(arg, "matrix data is not a 4x4 sequence of sequences");
if (!argfast)
return 1;
if (PySequence_Fast_GET_SIZE(argfast) != 4) {
Py_DECREF(argfast);
PyErr_SetString(PyExc_ValueError, "matrix data is not a 4x4 sequence of sequences");
return 1;
}
for (i = 0; i < 4; i++) {
int j;
PyObject *argi = PySequence_Fast_GET_ITEM(argfast, i);
PyObject *argifast = PySequence_Fast(argi, "matrix data is not a 4x4 sequence of sequences");
if (!argifast) {
Py_DECREF(argfast);
return 1;
}
if (PySequence_Fast_GET_SIZE(argifast) != 4) {
Py_DECREF(argfast);
Py_DECREF(argifast);
PyErr_SetString(PyExc_ValueError, "matrix data is not a 4x4 sequence of sequences");
return 1;
}
for (j = 0; j < 4; j++) {
PyObject *val = PySequence_Fast_GET_ITEM(argifast, j);
PyObject *fval = PyNumber_Float(val);
if (!fval) {
Py_DECREF(argfast);
Py_DECREF(argifast);
PyErr_SetString(PyExc_ValueError, "matrix data is not composed of float-compatible numbers");
return 1;
}
data[i][j] = PyFloat_AsDouble(fval);
}
Py_DECREF(argifast);
}
Py_DECREF(argfast);
oil_matrix_set_data(&(self->matrix), (float *)data);
return 0;
}
static PyObject *PyOILMatrix_str(PyOILMatrix *self) {
PyObject *full_tuple = PyOILMatrix_get_data(self, NULL);
PyObject *repr;
repr = PyObject_Repr(full_tuple);
Py_DECREF(full_tuple);
return repr;
}
static PyObject *PyOILMatrix_repr(PyOILMatrix *self) {
PyObject *repr = PyOILMatrix_str(self);
PyObject *full_repr = PyString_FromFormat("OIL.Matrix(%s)", PyString_AsString(repr));
Py_DECREF(repr);
return full_repr;
}
static inline PyObject *PyOILMatrix_binop(PyOILMatrix *a, PyOILMatrix *b, void (*op)(OILMatrix *, const OILMatrix *, const OILMatrix *), int inplace) {
if (!PyObject_TypeCheck((PyObject *)a, &PyOILMatrixType) ||
!PyObject_TypeCheck((PyObject *)b, &PyOILMatrixType)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
if (!inplace) {
PyOILMatrix *result;
result = (PyOILMatrix *)(PyOILMatrixType.tp_alloc(&PyOILMatrixType, 0));
if (!result)
return NULL;
op(&(result->matrix), &(a->matrix), &(b->matrix));
return (PyObject *)result;
}
op(&(a->matrix), &(a->matrix), &(b->matrix));
Py_INCREF(a);
return (PyObject *)a;
}
static PyObject *PyOILMatrix_add(PyOILMatrix *a, PyOILMatrix *b) {
return PyOILMatrix_binop(a, b, oil_matrix_add, 0);
}
static PyObject *PyOILMatrix_inplace_add(PyOILMatrix *a, PyOILMatrix *b) {
return PyOILMatrix_binop(a, b, oil_matrix_add, 1);
}
static PyObject *PyOILMatrix_subtract(PyOILMatrix *a, PyOILMatrix *b) {
return PyOILMatrix_binop(a, b, oil_matrix_subtract, 0);
}
static PyObject *PyOILMatrix_inplace_subtract(PyOILMatrix *a, PyOILMatrix *b) {
return PyOILMatrix_binop(a, b, oil_matrix_subtract, 1);
}
static PyObject *PyOILMatrix_multiply(PyOILMatrix *a, PyOILMatrix *b) {
return PyOILMatrix_binop(a, b, oil_matrix_multiply, 0);
}
static PyObject *PyOILMatrix_inplace_multiply(PyOILMatrix *a, PyOILMatrix *b) {
return PyOILMatrix_binop(a, b, oil_matrix_multiply, 1);
}
static PyObject *PyOILMatrix_negative(PyOILMatrix *mat) {
PyOILMatrix *result;
result = (PyOILMatrix *)(PyOILMatrixType.tp_alloc(&PyOILMatrixType, 0));
if (!result)
return NULL;
oil_matrix_negate(&(result->matrix), &(mat->matrix));
return (PyObject *)result;
}
static PyObject *PyOILMatrix_positive(PyOILMatrix *mat) {
Py_INCREF(mat);
return (PyObject *)mat;
}
static int PyOILMatrix_nonzero(PyOILMatrix *mat) {
return !oil_matrix_is_zero(&(mat->matrix));
}
static PyObject *PyOILMatrix_get_inverse(PyOILMatrix *self, PyObject *args) {
PyOILMatrix *other;
/* args == NULL means we're called as an attribute-getter */
if (args != NULL) {
if (!PyArg_ParseTuple(args, "")) {
return NULL;
}
}
other = (PyOILMatrix *)(PyOILMatrixType.tp_alloc(&PyOILMatrixType, 0));
if (!other)
return NULL;
if (!oil_matrix_invert(&(other->matrix), &(self->matrix))) {
Py_DECREF(other);
PyErr_SetString(PyExc_ValueError, "cannot invert matrix");
return NULL;
}
return (PyObject *)other;
}
static PyObject *PyOILMatrix_invert(PyOILMatrix *self, PyObject *args) {
if (!PyArg_ParseTuple(args, "")) {
return NULL;
}
if (!oil_matrix_invert(&(self->matrix), &(self->matrix))) {
PyErr_SetString(PyExc_ValueError, "cannot invert matrix");
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *PyOILMatrix_translate(PyOILMatrix *self, PyObject *args) {
float x, y, z;
if (!PyArg_ParseTuple(args, "fff", &x, &y, &z))
return NULL;
oil_matrix_translate(&(self->matrix), x, y, z);
Py_INCREF(self);
return (PyObject *)self;
}
static PyObject *PyOILMatrix_scale(PyOILMatrix *self, PyObject *args) {
float x, y, z;
if (!PyArg_ParseTuple(args, "fff", &x, &y, &z))
return NULL;
oil_matrix_scale(&(self->matrix), x, y, z);
Py_INCREF(self);
return (PyObject *)self;
}
static PyObject *PyOILMatrix_rotate(PyOILMatrix *self, PyObject *args) {
float x, y, z;
if (!PyArg_ParseTuple(args, "fff", &x, &y, &z))
return NULL;
oil_matrix_rotate(&(self->matrix), x, y, z);
Py_INCREF(self);
return (PyObject *)self;
}
static PyObject *PyOILMatrix_orthographic(PyOILMatrix *self, PyObject *args) {
float x1, x2, y1, y2, z1, z2;
if (!PyArg_ParseTuple(args, "ffffff", &x1, &x2, &y1, &y2, &z1, &z2))
return NULL;
oil_matrix_orthographic(&(self->matrix), x1, x2, y1, y2, z1, z2);
Py_INCREF(self);
return (PyObject *)self;
}
static PyMethodDef PyOILMatrix_methods[] = {
{"get_data", (PyCFunction)PyOILMatrix_get_data, METH_VARARGS,
"Returns a nested tuple of the matrix data."},
{"transform", (PyCFunction)PyOILMatrix_transform, METH_VARARGS,
"Transform 3 coordinates."},
{"get_inverse", (PyCFunction)PyOILMatrix_get_inverse, METH_VARARGS,
"Returns the inverse of the matrix."},
{"invert", (PyCFunction)PyOILMatrix_invert, METH_VARARGS,
"Invert the matrix in-place."},
{"translate", (PyCFunction)PyOILMatrix_translate, METH_VARARGS,
"Multiply on a translation matrix."},
{"scale", (PyCFunction)PyOILMatrix_scale, METH_VARARGS,
"Multiply on a scaling matrix."},
{"rotate", (PyCFunction)PyOILMatrix_rotate, METH_VARARGS,
"Multiply on a rotation matrix."},
{"orthographic", (PyCFunction)PyOILMatrix_orthographic, METH_VARARGS,
"Multiply on an orthographic matrix."},
{NULL, NULL, 0, NULL}
};
static PyGetSetDef PyOILMatrix_getset[] = {
{"data", (getter)PyOILMatrix_get_data, (setter)PyOILMatrix_set_data,
"A nested tuple of matrix data.", NULL},
{"inverse", (getter)PyOILMatrix_get_inverse, NULL,
"Return the inverse of this matrix.", NULL},
{NULL, NULL, 0, NULL}
};
/* the Matrix math ops */
static PyNumberMethods PyOILMatrixNumberMethods = {
(binaryfunc)PyOILMatrix_add, /* nb_add */
(binaryfunc)PyOILMatrix_subtract, /* nb_subtract */
(binaryfunc)PyOILMatrix_multiply, /* nb_multiply */
NULL, /* nb_divide */
NULL, /* nb_remainder */
NULL, /* nb_divmod */
NULL, /* nb_power */
(unaryfunc)PyOILMatrix_negative, /* nb_negative */
(unaryfunc)PyOILMatrix_positive, /* nb_positive */
NULL, /* nb_absolute */
(inquiry)PyOILMatrix_nonzero, /* nb_nonzero */
NULL, /* nb_invert */
NULL, /* nb_lshift */
NULL, /* nb_rshift */
NULL, /* nb_and */
NULL, /* nb_xor */
NULL, /* nb_or */
NULL, /* nb_coerce */
NULL, /* nb_int */
NULL, /* nb_long */
NULL, /* nb_float */
NULL, /* nb_oct */
NULL, /* nb_hex */
(binaryfunc)PyOILMatrix_inplace_add, /* nb_inplace_add */
(binaryfunc)PyOILMatrix_inplace_subtract, /* nb_inplace_subtract */
(binaryfunc)PyOILMatrix_inplace_multiply, /* nb_inplace_multiply */
NULL, /* nb_inplace_divide */
NULL, /* nb_inplace_remainder */
NULL, /* nb_inplace_power */
NULL, /* nb_inplace_lshift */
NULL, /* nb_inplace_rshift */
NULL, /* nb_inplace_and */
NULL, /* nb_inplace_xor */
NULL, /* nb_inplace_or */
};
/* the Matrix type */
static PyTypeObject PyOILMatrixType = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"Matrix", /* tp_name */
sizeof(PyOILMatrix), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)PyOILMatrix_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
(reprfunc)PyOILMatrix_repr, /* tp_repr */
&(PyOILMatrixNumberMethods), /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
(reprfunc)PyOILMatrix_str, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags*/
/* tp_doc */
"Encapsulates matrix data and operations.",
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
PyOILMatrix_methods, /* tp_methods */
NULL, /* tp_members */
PyOILMatrix_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
(newfunc)PyOILMatrix_new, /* tp_new */
};
/* forward declaration for the Image type */
static PyTypeObject PyOILImageType;
/* init and dealloc for the Image type */
static PyOILImage *PyOILImage_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) {
PyOILImage *self;
unsigned int width = 0, height = 0;
if (!PyArg_ParseTuple(args, "II", &width, &height)) {
return NULL;
}
if (width == 0 || height == 0) {
PyErr_SetString(PyExc_ValueError, "cannot create image with 0 size");
return NULL;
}
self = (PyOILImage *)(subtype->tp_alloc(subtype, 0));
if (!self)
return NULL;
self->im = oil_image_new(width, height);
if (self->im == NULL) {
PyErr_SetString(PyExc_RuntimeError, "cannot create image");
Py_DECREF(self);
return NULL;
}
return self;
}
static void PyOILImage_dealloc(PyOILImage *self) {
if (self->im) {
oil_image_free(self->im);
self->im = NULL;
}
self->ob_type->tp_free((PyObject *)self);
}
/* methods for Image type */
static PyObject *PyOILImage_load(PyTypeObject *type, PyObject *args) {
PyObject *src = NULL;
const char *path = NULL;
PyOILImage *self;
if (!PyArg_ParseTuple(args, "O", &src)) {
return NULL;
}
if (PyString_Check(src)) {
path = PyString_AsString(src);
}
self = (PyOILImage *)(type->tp_alloc(type, 0));
if (!self)
return NULL;
if (path) {
self->im = oil_image_load(path);
} else {
OILFile file;
file.file = src;
file.read = oil_python_read;
file.write = oil_python_write;
file.flush = oil_python_flush;
self->im = oil_image_load_ex(&file);
}
if (self->im == NULL) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_IOError, "cannot load image");
Py_DECREF(self);
return NULL;
}
return (PyObject *)self;
}
static PyObject *PyOILImage_save(PyOILImage *self, PyObject *args, PyObject *kwargs) {
OILFormatOptions opts = {0, 0};
static char *argnames[] = {"dest", "indexed", "palette_size", NULL};
PyObject *dest = NULL;
const char *path = NULL;
int save_success = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iI", argnames,
&dest, &opts.indexed, &opts.palette_size)) {
return NULL;
}
if (PyString_Check(dest)) {
path = PyString_AsString(dest);
}
if (path) {
save_success = oil_image_save(self->im, path, &opts);
} else {
OILFile file;
file.file = dest;
file.read = oil_python_read;
file.write = oil_python_write;
file.flush = oil_python_flush;
save_success = oil_image_save_ex(self->im, &file, &opts);
}
if (!save_success) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_IOError, "cannot save image");
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *PyOILImage_get_size(PyOILImage *self, PyObject *args) {
unsigned int width = 0, height = 0;
/* args == NULL means we're called as an attribute-getter */
if (args != NULL) {
if (!PyArg_ParseTuple(args, "")) {
return NULL;
}
}
oil_image_get_size(self->im, &width, &height);
return Py_BuildValue("II", width, height);
}
static PyObject *PyOILImage_composite(PyOILImage *self, PyObject *args) {
PyOILImage *src;
unsigned char alpha = 255;
int dx = 0, dy = 0;
unsigned int sx = 0, sy = 0, xsize = 0, ysize = 0;
if (!PyArg_ParseTuple(args, "O!|biiIIII", &PyOILImageType, &src, &alpha, &dx, &dy, &sx, &sy, &xsize, &ysize)) {
return NULL;
}
if (!oil_image_composite(self->im, src->im, alpha, dx, dy, sx, sy, xsize, ysize)) {
PyErr_SetString(PyExc_RuntimeError, "cannot composite image");
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *PyOILImage_draw_triangles(PyOILImage *self, PyObject *args) {
unsigned int i;
PyOILMatrix *matrix = NULL;
PyOILImage *tex = NULL;
PyObject *pyvertices = NULL;
PyObject *pyindices = NULL;
unsigned int flags = 0;
OILVertex *vertices = NULL;
unsigned int vertices_length = 0;
unsigned int *indices = NULL;
unsigned int indices_length = 0;
if (!PyArg_ParseTuple(args, "O!O!OO|I", &PyOILMatrixType, &matrix, &PyOILImageType, &tex, &pyvertices, &pyindices, &flags)) {
return NULL;
}
pyvertices = PySequence_Fast(pyvertices, "vertices are not a sequence");
if (pyvertices)
pyindices = PySequence_Fast(pyindices, "indices are not a sequence");
if (!pyvertices || !pyindices) {
Py_XDECREF(pyvertices);
Py_XDECREF(pyindices);
return NULL;
}
vertices_length = PySequence_Fast_GET_SIZE(pyvertices);
vertices = malloc(sizeof(OILVertex) * vertices_length);
indices_length = PySequence_Fast_GET_SIZE(pyindices);
indices = malloc(sizeof(unsigned int) * indices_length);
if (!vertices || !indices) {
if (vertices)
free(vertices);
if (indices)
free(indices);
Py_DECREF(pyvertices);
Py_DECREF(pyindices);
PyErr_SetString(PyExc_RuntimeError, "out of memory");
return NULL;
}
for (i = 0; i < vertices_length; i++) {
PyObject *vert = PySequence_Fast_GET_ITEM(pyvertices, i);
if (!PyArg_ParseTuple(vert, "(fff)(ff)(bbbb)", &(vertices[i].x), &(vertices[i].y), &(vertices[i].z), &(vertices[i].s), &(vertices[i].t), &(vertices[i].color.r), &(vertices[i].color.g), &(vertices[i].color.b), &(vertices[i].color.a))) {
free(vertices);
free(indices);
Py_DECREF(pyvertices);
Py_DECREF(pyindices);
PyErr_SetString(PyExc_ValueError, "vertex has invalid form");
return NULL;
}
}
for (i = 0; i < indices_length; i++) {
PyObject *pyindex = PySequence_Fast_GET_ITEM(pyindices, i);
pyindex = PyNumber_Index(pyindex);
if (!pyindex) {
free(vertices);
free(indices);
Py_DECREF(pyvertices);
Py_DECREF(pyindices);
PyErr_SetString(PyExc_ValueError, "index is not valid");
return NULL;
}
indices[i] = PyInt_AsLong(pyindex);
Py_DECREF(pyindex);
}
Py_DECREF(pyvertices);
Py_DECREF(pyindices);
oil_image_draw_triangles(self->im, &(matrix->matrix), tex->im, vertices, vertices_length, indices, indices_length, flags);
free(vertices);
free(indices);
Py_RETURN_NONE;
}
static PyObject *PyOILImage_resize_half(PyOILImage *self, PyObject *args) {
PyOILImage *src = NULL;
if (!PyArg_ParseTuple(args, "O!", &PyOILImageType, &src)) {
return NULL;
}
if (!oil_image_resize_half(self->im, src->im)) {
PyErr_SetString(PyExc_RuntimeError, "cannot resize image in half (likely a size mismatch)");
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *PyOILImage_clear(PyOILImage *self, PyObject *args) {
if (!PyArg_ParseTuple(args, ""))
return NULL;
oil_image_clear(self->im);
Py_RETURN_NONE;
}
static PyMethodDef PyOILImage_methods[] = {
{"load", (PyCFunction)PyOILImage_load, METH_VARARGS | METH_CLASS,
"Load the given path name into an Image object."},
{"save", (PyCFunction)PyOILImage_save, METH_KEYWORDS,
"Save the Image object to a file."},
{"get_size", (PyCFunction)PyOILImage_get_size, METH_VARARGS,
"Return a (width, height) tuple."},
{"composite", (PyCFunction)PyOILImage_composite, METH_VARARGS,
"Composite another image on top of this one."},
{"draw_triangles", (PyCFunction)PyOILImage_draw_triangles, METH_VARARGS,
"Draw 3D triangles on top of the image."},
{"resize_half", (PyCFunction)PyOILImage_resize_half, METH_VARARGS,
"Shrink the given image by half and copy onto self."},
{"clear", (PyCFunction)PyOILImage_clear, METH_VARARGS,
"Clear the image."},
{NULL, NULL, 0, NULL}
};
static PyGetSetDef PyOILImage_getset[] = {
{"size", (getter)PyOILImage_get_size, NULL,
"Return a (width, height) tuple.", NULL},
{NULL, NULL, 0, NULL}
};
/* the Image type */
static PyTypeObject PyOILImageType = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"Image", /* tp_name */
sizeof(PyOILImage), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)PyOILImage_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags*/
/* tp_doc */
"Encapsulates image data and image operations.",
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
PyOILImage_methods, /* tp_methods */
NULL, /* tp_members */
PyOILImage_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
(newfunc)PyOILImage_new, /* tp_new */
};
static PyObject *OIL_backend_set(PyObject *self, PyObject *args) {
unsigned int backend = OIL_BACKEND_CPU;
if (!PyArg_ParseTuple(args, "I", &backend)) {
return NULL;
}
if (backend >= OIL_BACKEND_MAX) {
PyErr_SetString(PyExc_ValueError, "invalid backend");
return NULL;
}
if (!oil_backend_set(backend)) {
PyErr_SetString(PyExc_RuntimeError, "could not set backend");
return NULL;
}
Py_RETURN_NONE;
}
static PyMethodDef OIL_methods[] = {
{"backend_set", OIL_backend_set, METH_VARARGS,
"Set the OIL backend to use"},
{NULL, NULL, 0, NULL}
};
/* helper to add a type to the module */
#define ADD_TYPE(type) do { \
if (PyType_Ready(&type) < 0) \
return; \
PyModule_AddObject(mod, type.tp_name, (PyObject *)&type); \
} while (0)
PyMODINIT_FUNC initOIL(void) {
PyObject *mod;
mod = Py_InitModule3("OIL", OIL_methods,
"OIL is an image handling library for Python.");
if (mod == NULL)
return;
/* the sizeof(...) bits are to prevent compiler warnings about unused
helper functions in oil-python.h */
ADD_TYPE(PyOILMatrixType);
(void)sizeof(py_oil_get_matrix_type());
ADD_TYPE(PyOILImageType);
(void)sizeof(py_oil_get_image_type());
/* add in the flag enums */
PyModule_AddIntConstant(mod, "DEPTH_TEST", OIL_DEPTH_TEST);
/* add in the backend enums */
#define BACKEND(name, symbol) PyModule_AddIntConstant(mod, "BACKEND_" #name, OIL_BACKEND_##name);
#include "oil-backends.def"
#undef BACKEND
}