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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2007 The Zdeno Ash Miklas
*
* This source file is part of VideoTexture library
*
* Contributor(s):
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file gameengine/VideoTexture/ImageBase.cpp
* \ingroup bgevideotex
*/
#include "ImageBase.h"
extern "C" {
#include "bgl.h"
}
#include <vector>
#include <string.h>
#include "EXP_PyObjectPlus.h"
#include <structmember.h>
#include "FilterBase.h"
#include "Exception.h"
#if (defined(WIN32) || defined(WIN64)) && !defined(FREE_WINDOWS)
#define strcasecmp _stricmp
#endif
// ImageBase class implementation
ExceptionID ImageHasExports;
ExceptionID InvalidColorChannel;
ExceptionID InvalidImageMode;
ExpDesc ImageHasExportsDesc(ImageHasExports, "Image has exported buffers, cannot resize");
ExpDesc InvalidColorChannelDesc(InvalidColorChannel, "Invalid or too many color channels specified. At most 4 values within R, G, B, A, 0, 1");
ExpDesc InvalidImageModeDesc(InvalidImageMode, "Invalid image mode, only RGBA and BGRA are supported");
// constructor
ImageBase::ImageBase (bool staticSrc) : m_image(NULL), m_imgSize(0),
m_avail(false), m_scale(false), m_scaleChange(false), m_flip(false),
m_zbuff(false),
m_depth(false),
m_staticSources(staticSrc), m_pyfilter(NULL)
{
m_size[0] = m_size[1] = 0;
m_exports = 0;
}
// destructor
ImageBase::~ImageBase (void)
{
// release image
if (m_image)
delete [] m_image;
}
// release python objects
bool ImageBase::release (void)
{
// iterate sources
for (ImageSourceList::iterator it = m_sources.begin(); it != m_sources.end(); ++it)
{
// release source object
delete *it;
*it = NULL;
}
// release filter object
Py_XDECREF(m_pyfilter);
m_pyfilter = NULL;
return true;
}
// get image
unsigned int * ImageBase::getImage (unsigned int texId, double ts)
{
// if image is not available
if (!m_avail)
{
// if there are any sources
if (!m_sources.empty())
{
// get images from sources
for (ImageSourceList::iterator it = m_sources.begin(); it != m_sources.end(); ++it)
// get source image
(*it)->getImage(ts);
// init image
init(m_sources[0]->getSize()[0], m_sources[0]->getSize()[1]);
}
// calculate new image
calcImage(texId, ts);
}
// if image is available, return it, otherwise NULL
return m_avail ? m_image : NULL;
}
bool ImageBase::loadImage(unsigned int *buffer, unsigned int size, unsigned int format, double ts)
{
unsigned int *d, *s, v, len;
if (getImage(0, ts) != NULL && size >= getBuffSize()) {
switch (format) {
case GL_RGBA:
memcpy(buffer, m_image, getBuffSize());
break;
case GL_BGRA:
len = (unsigned int)m_size[0] * m_size[1];
for (s=m_image, d=buffer; len; len--) {
v = *s++;
*d++ = VT_SWAPBR(v);
}
break;
default:
THRWEXCP(InvalidImageMode,S_OK);
}
return true;
}
return false;
}
// refresh image source
void ImageBase::refresh (void)
{
// invalidate this image
m_avail = false;
// refresh all sources
for (ImageSourceList::iterator it = m_sources.begin(); it != m_sources.end(); ++it)
(*it)->refresh();
}
// get source object
PyImage * ImageBase::getSource (const char *id)
{
// find source
ImageSourceList::iterator src = findSource(id);
// return it, if found
return src != m_sources.end() ? (*src)->getSource() : NULL;
}
// set source object
bool ImageBase::setSource (const char *id, PyImage *source)
{
// find source
ImageSourceList::iterator src = findSource(id);
// check source loop
if (source != NULL && source->m_image->loopDetect(this))
return false;
// if found, set new object
if (src != m_sources.end())
// if new object is not empty or sources are static
if (source != NULL || m_staticSources)
// replace previous source
(*src)->setSource(source);
// otherwise delete source
else
m_sources.erase(src);
// if source is not found and adding is allowed
else
if (!m_staticSources)
{
// create new source
ImageSource * newSrc = newSource(id);
newSrc->setSource(source);
// if source was created, add it to source list
if (newSrc != NULL) m_sources.push_back(newSrc);
}
// otherwise source wasn't set
else
return false;
// source was set
return true;
}
// set pixel filter
void ImageBase::setFilter (PyFilter * filt)
{
// reference new filter
if (filt != NULL) Py_INCREF(filt);
// release previous filter
Py_XDECREF(m_pyfilter);
// set new filter
m_pyfilter = filt;
}
void ImageBase::swapImageBR()
{
unsigned int size, v, *s;
if (m_avail) {
size = 1 * m_size[0] * m_size[1];
for (s=m_image; size; size--) {
v = *s;
*s++ = VT_SWAPBR(v);
}
}
}
// initialize image data
void ImageBase::init (short width, short height)
{
// if image has to be scaled
if (m_scale)
{
// recalc sizes of image
width = calcSize(width);
height = calcSize(height);
}
// if sizes differ
if (width != m_size[0] || height != m_size[1])
{
if (m_exports > 0)
THRWEXCP(ImageHasExports,S_OK);
// new buffer size
unsigned int newSize = width * height;
// if new buffer is larger than previous
if (newSize > m_imgSize)
{
// set new buffer size
m_imgSize = newSize;
// release previous and create new buffer
if (m_image)
delete [] m_image;
m_image = new unsigned int[m_imgSize];
}
// new image size
m_size[0] = width;
m_size[1] = height;
// scale was processed
m_scaleChange = false;
}
}
// find source
ImageSourceList::iterator ImageBase::findSource (const char *id)
{
// iterate sources
ImageSourceList::iterator it;
for (it = m_sources.begin(); it != m_sources.end(); ++it)
// if id matches, return iterator
if ((*it)->is(id)) return it;
// source not found
return it;
}
// check sources sizes
bool ImageBase::checkSourceSizes (void)
{
// reference size
short * refSize = NULL;
// iterate sources
for (ImageSourceList::iterator it = m_sources.begin(); it != m_sources.end(); ++it)
{
// get size of current source
short * curSize = (*it)->getSize();
// if size is available and is not empty
if (curSize[0] != 0 && curSize[1] != 0) {
// if reference size is not set
if (refSize == NULL) {
// set current size as reference
refSize = curSize;
// otherwise check with current size
}
else if (curSize[0] != refSize[0] || curSize[1] != refSize[1]) {
// if they don't match, report it
return false;
}
}
}
// all sizes match
return true;
}
// compute nearest power of 2 value
short ImageBase::calcSize (short size)
{
// while there is more than 1 bit in size value
while ((size & (size - 1)) != 0)
// clear last bit
size = size & (size - 1);
// return result
return size;
}
// perform loop detection
bool ImageBase::loopDetect (ImageBase * img)
{
// if this object is the same as parameter, loop is detected
if (this == img) return true;
// check all sources
for (ImageSourceList::iterator it = m_sources.begin(); it != m_sources.end(); ++it)
// if source detected loop, return this result
if ((*it)->getSource() != NULL && (*it)->getSource()->m_image->loopDetect(img))
return true;
// no loop detected
return false;
}
// ImageSource class implementation
// constructor
ImageSource::ImageSource (const char *id) : m_source(NULL), m_image(NULL)
{
// copy id
int idx;
for (idx = 0; id[idx] != '\0' && idx < SourceIdSize - 1; ++idx)
m_id[idx] = id[idx];
m_id[idx] = '\0';
}
// destructor
ImageSource::~ImageSource (void)
{
// release source
setSource(NULL);
}
// compare id
bool ImageSource::is (const char *id)
{
for (char *myId = m_id; *myId != '\0'; ++myId, ++id)
if (*myId != *id) return false;
return *id == '\0';
}
// set source object
void ImageSource::setSource (PyImage *source)
{
// reference new source
if (source != NULL) Py_INCREF(source);
// release previous source
Py_XDECREF(m_source);
// set new source
m_source = source;
}
// get image from source
unsigned int * ImageSource::getImage (double ts)
{
// if source is available
if (m_source != NULL)
// get image from source
m_image = m_source->m_image->getImage(0, ts);
// otherwise reset buffer
else
m_image = NULL;
// return image
return m_image;
}
// refresh source
void ImageSource::refresh (void)
{
// if source is available, refresh it
if (m_source != NULL) m_source->m_image->refresh();
}
// list of image types
PyTypeList pyImageTypes;
// functions for python interface
// object allocation
PyObject *Image_allocNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
// allocate object
PyImage *self = reinterpret_cast<PyImage*>(type->tp_alloc(type, 0));
// initialize object structure
self->m_image = NULL;
// return allocated object
return reinterpret_cast<PyObject*>(self);
}
// object deallocation
void Image_dealloc(PyImage *self)
{
// release object attributes
if (self->m_image != NULL)
{
if (self->m_image->m_exports > 0)
{
PyErr_SetString(PyExc_SystemError,
"deallocated Image object has exported buffers");
PyErr_Print();
}
// if release requires deleting of object, do it
if (self->m_image->release())
delete self->m_image;
self->m_image = NULL;
}
}
// get image data
PyObject *Image_getImage(PyImage *self, char *mode)
{
try
{
unsigned int * image = self->m_image->getImage();
if (image)
{
// build BGL buffer
int dimensions = self->m_image->getBuffSize();
Buffer * buffer;
if (mode == NULL || !strcasecmp(mode, "RGBA"))
{
buffer = BGL_MakeBuffer( GL_BYTE, 1, &dimensions, image);
}
else if (!strcasecmp(mode, "F"))
{
// this mode returns the image as an array of float.
// This makes sense ONLY for the depth buffer:
// source = VideoTexture.ImageViewport()
// source.depth = True
// depth = VideoTexture.imageToArray(source, 'F')
// adapt dimension from byte to float
dimensions /= sizeof(float);
buffer = BGL_MakeBuffer( GL_FLOAT, 1, &dimensions, image);
}
else
{
int i, c, ncolor, pixels;
int offset[4];
unsigned char *s, *d;
// scan the mode to get the channels requested, no more than 4
for (i=ncolor=0; mode[i] != 0 && ncolor < 4; i++)
{
switch (toupper(mode[i]))
{
case 'R':
offset[ncolor++] = 0;
break;
case 'G':
offset[ncolor++] = 1;
break;
case 'B':
offset[ncolor++] = 2;
break;
case 'A':
offset[ncolor++] = 3;
break;
case '0':
offset[ncolor++] = -1;
break;
case '1':
offset[ncolor++] = -2;
break;
// if you add more color code, change the switch further down
default:
THRWEXCP(InvalidColorChannel,S_OK);
}
}
if (mode[i] != 0) {
THRWEXCP(InvalidColorChannel,S_OK);
}
// first get the number of pixels
pixels = dimensions / 4;
// multiple by the number of channels, each is one byte
dimensions = pixels * ncolor;
// get an empty buffer
buffer = BGL_MakeBuffer( GL_BYTE, 1, &dimensions, NULL);
// and fill it
for (i = 0, d = (unsigned char *)buffer->buf.asbyte, s = (unsigned char *)image;
i < pixels;
i++, d += ncolor, s += 4)
{
for (c=0; c<ncolor; c++)
{
switch (offset[c])
{
case 0: d[c] = s[0]; break;
case 1: d[c] = s[1]; break;
case 2: d[c] = s[2]; break;
case 3: d[c] = s[3]; break;
case -1: d[c] = 0; break;
case -2: d[c] = 0xFF; break;
}
}
}
}
return (PyObject *)buffer;
}
}
catch (Exception & exp)
{
exp.report();
return NULL;
}
Py_RETURN_NONE;
}
// get image size
PyObject *Image_getSize (PyImage *self, void *closure)
{
return Py_BuildValue("(hh)", self->m_image->getSize()[0],
self->m_image->getSize()[1]);
}
// refresh image
PyObject *Image_refresh (PyImage *self, PyObject *args)
{
Py_buffer buffer;
bool done = true;
char *mode = NULL;
double ts = -1.0;
unsigned int format;
memset(&buffer, 0, sizeof(buffer));
if (PyArg_ParseTuple(args, "|s*sd:refresh", &buffer, &mode, &ts)) {
if (buffer.buf) {
// a target buffer is provided, verify its format
if (buffer.readonly) {
PyErr_SetString(PyExc_TypeError, "Buffers passed in argument must be writable");
}
else if (!PyBuffer_IsContiguous(&buffer, 'C')) {
PyErr_SetString(PyExc_TypeError, "Buffers passed in argument must be contiguous in memory");
}
else if (((intptr_t)buffer.buf & 3) != 0) {
PyErr_SetString(PyExc_TypeError, "Buffers passed in argument must be aligned to 4 bytes boundary");
}
else {
// ready to get the image into our buffer
try {
if (mode == NULL || !strcmp(mode, "RGBA"))
format = GL_RGBA;
else if (!strcmp(mode, "BGRA"))
format = GL_BGRA;
else
THRWEXCP(InvalidImageMode,S_OK);
done = self->m_image->loadImage((unsigned int *)buffer.buf, buffer.len, format, ts);
}
catch (Exception & exp) {
exp.report();
}
}
PyBuffer_Release(&buffer);
if (PyErr_Occurred()) {
return NULL;
}
}
}
else {
return NULL;
}
self->m_image->refresh();
if (done)
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
// get scale
PyObject *Image_getScale (PyImage *self, void *closure)
{
if (self->m_image != NULL && self->m_image->getScale()) Py_RETURN_TRUE;
else Py_RETURN_FALSE;
}
// set scale
int Image_setScale(PyImage *self, PyObject *value, void *closure)
{
// check parameter, report failure
if (value == NULL || !PyBool_Check(value))
{
PyErr_SetString(PyExc_TypeError, "The value must be a bool");
return -1;
}
// set scale
if (self->m_image != NULL) self->m_image->setScale(value == Py_True);
// success
return 0;
}
// get flip
PyObject *Image_getFlip (PyImage *self, void *closure)
{
if (self->m_image != NULL && self->m_image->getFlip()) Py_RETURN_TRUE;
else Py_RETURN_FALSE;
}
// set flip
int Image_setFlip(PyImage *self, PyObject *value, void *closure)
{
// check parameter, report failure
if (value == NULL || !PyBool_Check(value))
{
PyErr_SetString(PyExc_TypeError, "The value must be a bool");
return -1;
}
// set scale
if (self->m_image != NULL) self->m_image->setFlip(value == Py_True);
// success
return 0;
}
// get zbuff
PyObject *Image_getZbuff(PyImage *self, void *closure)
{
if (self->m_image != NULL && self->m_image->getZbuff()) Py_RETURN_TRUE;
else Py_RETURN_FALSE;
}
// set zbuff
int Image_setZbuff(PyImage *self, PyObject *value, void *closure)
{
// check parameter, report failure
if (value == NULL || !PyBool_Check(value))
{
PyErr_SetString(PyExc_TypeError, "The value must be a bool");
return -1;
}
// set scale
if (self->m_image != NULL) self->m_image->setZbuff(value == Py_True);
// success
return 0;
}
// get depth
PyObject *Image_getDepth(PyImage *self, void *closure)
{
if (self->m_image != NULL && self->m_image->getDepth()) Py_RETURN_TRUE;
else Py_RETURN_FALSE;
}
// set depth
int Image_setDepth(PyImage *self, PyObject *value, void *closure)
{
// check parameter, report failure
if (value == NULL || !PyBool_Check(value))
{
PyErr_SetString(PyExc_TypeError, "The value must be a bool");
return -1;
}
// set scale
if (self->m_image != NULL) self->m_image->setDepth(value == Py_True);
// success
return 0;
}
// get filter source object
PyObject *Image_getSource(PyImage *self, PyObject *args)
{
// get arguments
char *id;
if (!PyArg_ParseTuple(args, "s:getSource", &id))
return NULL;
if (self->m_image != NULL)
{
// get source object
PyObject *src = reinterpret_cast<PyObject*>(self->m_image->getSource(id));
// if source is available
if (src != NULL)
{
// return source
Py_INCREF(src);
return src;
}
}
// source was not found
Py_RETURN_NONE;
}
// set filter source object
PyObject *Image_setSource(PyImage *self, PyObject *args)
{
// get arguments
char *id;
PyObject *obj;
if (!PyArg_ParseTuple(args, "sO:setSource", &id, &obj))
return NULL;
if (self->m_image != NULL)
{
// check type of object
if (pyImageTypes.in(Py_TYPE(obj)))
{
// convert to image struct
PyImage * img = reinterpret_cast<PyImage*>(obj);
// set source
if (!self->m_image->setSource(id, img))
{
// if not set, retport error
PyErr_SetString(PyExc_RuntimeError, "Invalid source or id");
return NULL;
}
}
// else report error
else
{
PyErr_SetString(PyExc_RuntimeError, "Invalid type of object");
return NULL;
}
}
// return none
Py_RETURN_NONE;
}
// get pixel filter object
PyObject *Image_getFilter(PyImage *self, void *closure)
{
// if image object is available
if (self->m_image != NULL)
{
// pixel filter object
PyObject *filt = reinterpret_cast<PyObject*>(self->m_image->getFilter());
// if filter is present
if (filt != NULL)
{
// return it
Py_INCREF(filt);
return filt;
}
}
// otherwise return none
Py_RETURN_NONE;
}
// set pixel filter object
int Image_setFilter(PyImage *self, PyObject *value, void *closure)
{
// if image object is available
if (self->m_image != NULL)
{
// check new value
if (value == NULL || !pyFilterTypes.in(Py_TYPE(value)))
{
// report value error
PyErr_SetString(PyExc_TypeError, "Invalid type of value");
return -1;
}
// set new value
self->m_image->setFilter(reinterpret_cast<PyFilter*>(value));
}
// return success
return 0;
}
PyObject *Image_valid(PyImage *self, void *closure)
{
if (self->m_image->isImageAvailable())
{
Py_RETURN_TRUE;
}
else
{
Py_RETURN_FALSE;
}
}
static int Image_getbuffer(PyImage *self, Py_buffer *view, int flags)
{
unsigned int * image;
int ret;
try {
// can throw in case of resize
image = self->m_image->getImage();
}
catch (Exception & exp) {
exp.report();
return -1;
}
if (!image) {
PyErr_SetString(PyExc_BufferError, "Image buffer is not available");
return -1;
}
if (view == NULL)
{
self->m_image->m_exports++;
return 0;
}
ret = PyBuffer_FillInfo(view, (PyObject *)self, image, self->m_image->getBuffSize(), 0, flags);
if (ret >= 0)
self->m_image->m_exports++;
return ret;
}
static void Image_releaseBuffer(PyImage *self, Py_buffer *buffer)
{
self->m_image->m_exports--;
}
PyBufferProcs imageBufferProcs =
{
(getbufferproc)Image_getbuffer,
(releasebufferproc)Image_releaseBuffer
};
|