ImageBase.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * Copyright (c) 2007 The Zdeno Ash Miklas
  19. *
  20. * This source file is part of blendTex library
  21. *
  22. * Contributor(s):
  23. *
  24. * ***** END GPL LICENSE BLOCK *****
  25. */
  26. /** \file ImageBase.h
  27. * \ingroup bgevideotex
  28. */
  29. #ifndef __IMAGEBASE_H__
  30. #define __IMAGEBASE_H__
  31. #include "Common.h"
  32. #include <vector>
  33. #include "EXP_PyObjectPlus.h"
  34. #include "PyTypeList.h"
  35. #include "FilterBase.h"
  36. #include "GPU_glew.h"
  37. // forward declarations
  38. struct PyImage;
  39. class ImageSource;
  40. /// type for list of image sources
  41. typedef std::vector<ImageSource*> ImageSourceList;
  42. /// base class for image filters
  43. class ImageBase
  44. {
  45. public:
  46. /// constructor
  47. ImageBase (bool staticSrc = false);
  48. /// destructor
  49. virtual ~ImageBase(void);
  50. /// release contained objects, if returns true, object should be deleted
  51. virtual bool release(void);
  52. /// is an image available
  53. bool isImageAvailable(void)
  54. { return m_avail; }
  55. /// get image
  56. unsigned int *getImage(unsigned int texId = 0, double timestamp=-1.0);
  57. /// get image size
  58. short * getSize(void) { return m_size; }
  59. /// get image buffer size
  60. unsigned long getBuffSize(void)
  61. { return m_size[0] * m_size[1] * sizeof(unsigned int); }
  62. /// refresh image - invalidate its current content
  63. virtual void refresh(void);
  64. /// get scale
  65. bool getScale(void) { return m_scale; }
  66. /// set scale
  67. void setScale(bool scale) { m_scale = scale; m_scaleChange = true; }
  68. /// get vertical flip
  69. bool getFlip(void) { return m_flip; }
  70. /// set vertical flip
  71. void setFlip(bool flip) { m_flip = flip; }
  72. /// get Z buffer
  73. bool getZbuff(void) { return m_zbuff; }
  74. /// set Z buffer
  75. void setZbuff(bool zbuff) { m_zbuff = zbuff; }
  76. /// get depth
  77. bool getDepth(void) { return m_depth; }
  78. /// set depth
  79. void setDepth(bool depth) { m_depth = depth; }
  80. /// get source object
  81. PyImage * getSource(const char *id);
  82. /// set source object, return true, if source was set
  83. bool setSource(const char *id, PyImage *source);
  84. /// get pixel filter
  85. PyFilter * getFilter(void) { return m_pyfilter; }
  86. /// set pixel filter
  87. void setFilter(PyFilter * filt);
  88. /// calculate size(nearest power of 2)
  89. static short calcSize(short size);
  90. /// calculate image from sources and send it to a target buffer instead of a texture
  91. /// format is GL_RGBA or GL_BGRA
  92. virtual bool loadImage(unsigned int *buffer, unsigned int size, unsigned int format, double ts);
  93. /// swap the B and R channel in-place in the image buffer
  94. void swapImageBR();
  95. /// number of buffer pointing to m_image, public because not handled by this class
  96. int m_exports;
  97. protected:
  98. /// image buffer
  99. unsigned int * m_image;
  100. /// image buffer size
  101. unsigned int m_imgSize;
  102. /// image size
  103. short m_size[2];
  104. /// image is available
  105. bool m_avail;
  106. /// scale image to power 2 sizes
  107. bool m_scale;
  108. /// scale was changed
  109. bool m_scaleChange;
  110. /// flip image vertically
  111. bool m_flip;
  112. /// use the Z buffer as a texture
  113. bool m_zbuff;
  114. /// extract the Z buffer with unisgned int precision
  115. bool m_depth;
  116. /// source image list
  117. ImageSourceList m_sources;
  118. /// flag for disabling addition and deletion of sources
  119. bool m_staticSources;
  120. /// pixel filter
  121. PyFilter * m_pyfilter;
  122. /// initialize image data
  123. void init(short width, short height);
  124. /// find source
  125. ImageSourceList::iterator findSource(const char *id);
  126. /// create new source
  127. virtual ImageSource *newSource(const char *id) { return NULL; }
  128. /// check source sizes
  129. bool checkSourceSizes(void);
  130. /// calculate image from sources and set its availability
  131. virtual void calcImage(unsigned int texId, double ts) {}
  132. /// perform loop detection
  133. bool loopDetect(ImageBase * img);
  134. /// template for image conversion
  135. template<class FLT, class SRC> void convImage(FLT & filter, SRC srcBuff,
  136. short * srcSize)
  137. {
  138. // destination buffer
  139. unsigned int * dstBuff = m_image;
  140. // pixel size from filter
  141. unsigned int pixSize = filter.firstPixelSize();
  142. // if no scaling is needed
  143. if (srcSize[0] == m_size[0] && srcSize[1] == m_size[1])
  144. // if flipping isn't required
  145. if (!m_flip)
  146. // copy bitmap
  147. for (short y = 0; y < m_size[1]; ++y)
  148. for (short x = 0; x < m_size[0]; ++x, ++dstBuff, srcBuff += pixSize)
  149. // copy pixel
  150. *dstBuff = filter.convert(srcBuff, x, y, srcSize, pixSize);
  151. // otherwise flip image top to bottom
  152. else
  153. {
  154. // go to last row of image
  155. srcBuff += srcSize[0] * (srcSize[1] - 1) * pixSize;
  156. // copy bitmap
  157. for (short y = m_size[1] - 1; y >= 0; --y, srcBuff -= 2 * srcSize[0] * pixSize)
  158. for (short x = 0; x < m_size[0]; ++x, ++dstBuff, srcBuff += pixSize)
  159. // copy pixel
  160. *dstBuff = filter.convert(srcBuff, x, y, srcSize, pixSize);
  161. }
  162. // else scale picture (nearest neighbor)
  163. else
  164. {
  165. // interpolation accumulator
  166. int accHeight = srcSize[1] >> 1;
  167. // if flipping is required
  168. if (m_flip)
  169. // go to last row of image
  170. srcBuff += srcSize[0] * (srcSize[1] - 1) * pixSize;
  171. // process image rows
  172. for (int y = 0; y < srcSize[1]; ++y)
  173. {
  174. // increase height accum
  175. accHeight += m_size[1];
  176. // if pixel row has to be drawn
  177. if (accHeight >= srcSize[1])
  178. {
  179. // decrease accum
  180. accHeight -= srcSize[1];
  181. // width accum
  182. int accWidth = srcSize[0] >> 1;
  183. // process row
  184. for (int x = 0; x < srcSize[0]; ++x)
  185. {
  186. // increase width accum
  187. accWidth += m_size[0];
  188. // if pixel has to be drawn
  189. if (accWidth >= srcSize[0])
  190. {
  191. // decrease accum
  192. accWidth -= srcSize[0];
  193. // convert pixel
  194. *dstBuff = filter.convert(srcBuff, x, m_flip ? srcSize[1] - y - 1 : y,
  195. srcSize, pixSize);
  196. // next pixel
  197. ++dstBuff;
  198. }
  199. // shift source pointer
  200. srcBuff += pixSize;
  201. }
  202. }
  203. // if pixel row will not be drawn
  204. else
  205. // move source pointer to next row
  206. srcBuff += pixSize * srcSize[0];
  207. // if y flipping is required
  208. if (m_flip)
  209. // go to previous row of image
  210. srcBuff -= 2 * pixSize * srcSize[0];
  211. }
  212. }
  213. }
  214. // template for specific filter preprocessing
  215. template <class F, class SRC> void filterImage (F & filt, SRC srcBuff, short *srcSize)
  216. {
  217. // find first filter in chain
  218. FilterBase * firstFilter = NULL;
  219. if (m_pyfilter != NULL) firstFilter = m_pyfilter->m_filter->findFirst();
  220. // if first filter is available
  221. if (firstFilter != NULL)
  222. {
  223. // python wrapper for filter
  224. PyFilter pyFilt;
  225. pyFilt.m_filter = &filt;
  226. // set specified filter as first in chain
  227. firstFilter->setPrevious(&pyFilt, false);
  228. // convert video image
  229. convImage(*(m_pyfilter->m_filter), srcBuff, srcSize);
  230. // delete added filter
  231. firstFilter->setPrevious(NULL, false);
  232. }
  233. // otherwise use given filter for conversion
  234. else convImage(filt, srcBuff, srcSize);
  235. // source was processed
  236. m_avail = true;
  237. }
  238. };
  239. // python structure for image filter
  240. struct PyImage
  241. {
  242. PyObject_HEAD
  243. // source object
  244. ImageBase * m_image;
  245. };
  246. // size of id
  247. const int SourceIdSize = 32;
  248. /// class for source of image
  249. class ImageSource
  250. {
  251. public:
  252. /// constructor
  253. ImageSource (const char *id);
  254. /// destructor
  255. virtual ~ImageSource (void);
  256. /// get id
  257. const char * getId (void) { return m_id; }
  258. /// compare id to argument
  259. bool is (const char *id);
  260. /// get source object
  261. PyImage * getSource (void) { return m_source; }
  262. /// set source object
  263. void setSource (PyImage *source);
  264. /// get image from source
  265. unsigned int * getImage (double ts=-1.0);
  266. /// get buffered image
  267. unsigned int * getImageBuf (void) { return m_image; }
  268. /// refresh source
  269. void refresh (void);
  270. /// get image size
  271. short * getSize (void)
  272. {
  273. static short defSize [] = {0, 0};
  274. return m_source != NULL ? m_source->m_image->getSize() : defSize;
  275. }
  276. protected:
  277. /// id of source
  278. char m_id [SourceIdSize];
  279. /// pointer to source structure
  280. PyImage * m_source;
  281. /// buffered image from source
  282. unsigned int * m_image;
  283. private:
  284. /// default constructor is forbidden
  285. ImageSource (void) {}
  286. };
  287. // list of python image types
  288. extern PyTypeList pyImageTypes;
  289. // functions for python interface
  290. // object initialization
  291. template <class T> static int Image_init(PyObject *pySelf, PyObject *args, PyObject *kwds)
  292. {
  293. PyImage *self = reinterpret_cast<PyImage *>(pySelf);
  294. // create source object
  295. if (self->m_image != NULL) delete self->m_image;
  296. self->m_image = new T();
  297. // initialization succeded
  298. return 0;
  299. }
  300. // object allocation
  301. PyObject *Image_allocNew(PyTypeObject *type, PyObject *args, PyObject *kwds);
  302. // object deallocation
  303. void Image_dealloc(PyImage *self);
  304. // get image data
  305. PyObject *Image_getImage(PyImage *self, char *mode);
  306. // get image size
  307. PyObject *Image_getSize(PyImage *self, void *closure);
  308. // refresh image - invalidate current content
  309. PyObject *Image_refresh(PyImage *self, PyObject *args);
  310. // get scale
  311. PyObject *Image_getScale(PyImage *self, void *closure);
  312. // set scale
  313. int Image_setScale(PyImage *self, PyObject *value, void *closure);
  314. // get flip
  315. PyObject *Image_getFlip(PyImage *self, void *closure);
  316. // set flip
  317. int Image_setFlip(PyImage *self, PyObject *value, void *closure);
  318. // get filter source object
  319. PyObject *Image_getSource(PyImage *self, PyObject *args);
  320. // set filter source object
  321. PyObject *Image_setSource(PyImage *self, PyObject *args);
  322. // get Z buffer
  323. PyObject *Image_getZbuff(PyImage *self, void *closure);
  324. // set Z buffer
  325. int Image_setZbuff(PyImage *self, PyObject *value, void *closure);
  326. // get depth
  327. PyObject *Image_getDepth(PyImage *self, void *closure);
  328. // set depth
  329. int Image_setDepth(PyImage *self, PyObject *value, void *closure);
  330. // get pixel filter object
  331. PyObject *Image_getFilter(PyImage *self, void *closure);
  332. // set pixel filter object
  333. int Image_setFilter(PyImage *self, PyObject *value, void *closure);
  334. // check if a buffer can be extracted
  335. PyObject *Image_valid(PyImage *self, void *closure);
  336. // for buffer access to PyImage objects
  337. extern PyBufferProcs imageBufferProcs;
  338. #endif