ImageMix.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 VideoTexture library
  21. *
  22. * Contributor(s):
  23. *
  24. * ***** END GPL LICENSE BLOCK *****
  25. */
  26. /** \file ImageMix.h
  27. * \ingroup bgevideotex
  28. */
  29. #ifndef __IMAGEMIX_H__
  30. #define __IMAGEMIX_H__
  31. #include "Common.h"
  32. #include "ImageBase.h"
  33. #include "FilterBase.h"
  34. /// class for source mixing
  35. class ImageSourceMix : public ImageSource
  36. {
  37. public:
  38. /// constructor
  39. ImageSourceMix (const char *id) : ImageSource(id), m_offset(0), m_weight(0x100) {}
  40. /// destructor
  41. virtual ~ImageSourceMix (void) {}
  42. /// get offset
  43. long long getOffset (void) { return m_offset; }
  44. /// set offset
  45. void setOffset (unsigned int * firstImg) { m_offset = m_image - firstImg; }
  46. /// get weight
  47. short getWeight (void) { return m_weight; }
  48. /// set weight
  49. void setWeight (short weight) { m_weight = weight; }
  50. protected:
  51. /// buffer offset to the first source buffer
  52. long long m_offset;
  53. /// source weight
  54. short m_weight;
  55. };
  56. /// class for image mixer
  57. class ImageMix : public ImageBase
  58. {
  59. public:
  60. /// constructor
  61. ImageMix (void) : ImageBase(false) {}
  62. /// destructor
  63. virtual ~ImageMix (void) {}
  64. /// get weight
  65. short getWeight(const char *id);
  66. /// set weight
  67. bool setWeight(const char *id, short weight);
  68. protected:
  69. /// create new source
  70. virtual ImageSource *newSource(const char *id) { return new ImageSourceMix(id); }
  71. /// calculate image from sources and set its availability
  72. virtual void calcImage (unsigned int texId, double ts);
  73. };
  74. /// pixel filter for image mixer
  75. class FilterImageMix : public FilterBase
  76. {
  77. public:
  78. /// constructor
  79. FilterImageMix (ImageSourceList & sources) : m_sources(sources) {}
  80. /// destructor
  81. virtual ~FilterImageMix (void) {}
  82. protected:
  83. /// source list
  84. ImageSourceList & m_sources;
  85. /// filter pixel, source int buffer
  86. virtual unsigned int filter (unsigned int * src, short x, short y,
  87. short * size, unsigned int pixSize, unsigned int val = 0)
  88. {
  89. // resulting pixel color
  90. int color[] = {0, 0, 0, 0};
  91. // iterate sources
  92. for (ImageSourceList::iterator it = m_sources.begin(); it != m_sources.end(); ++it)
  93. {
  94. // get pointer to mixer source
  95. ImageSourceMix * mixSrc = static_cast<ImageSourceMix*>(*it);
  96. // add weighted source pixel to result
  97. color[0] += mixSrc->getWeight() * (src[mixSrc->getOffset()] & 0xFF);
  98. color[1] += mixSrc->getWeight() * ((src[mixSrc->getOffset()] >> 8) & 0xFF);
  99. color[2] += mixSrc->getWeight() * ((src[mixSrc->getOffset()] >> 16) & 0xFF);
  100. color[3] += mixSrc->getWeight() * ((src[mixSrc->getOffset()] >> 24) & 0xFF);
  101. }
  102. // return resulting color
  103. return ((color[0] >> 8) & 0xFF) | (color[1] & 0xFF00)
  104. | ((color[2] << 8) & 0xFF0000) | ((color[3] << 16) & 0xFF000000);
  105. }
  106. };
  107. #endif