Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
4.9KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "../Image.hpp"
  17. START_NAMESPACE_DGL
  18. // -----------------------------------------------------------------------
  19. Image::Image()
  20. : fRawData(nullptr),
  21. fSize(0, 0),
  22. fFormat(0),
  23. fType(0),
  24. fTextureId(0),
  25. fIsReady(false)
  26. {
  27. glGenTextures(1, &fTextureId);
  28. }
  29. Image::Image(const char* const rawData, const uint width, const uint height, const GLenum format, const GLenum type)
  30. : fRawData(rawData),
  31. fSize(width, height),
  32. fFormat(format),
  33. fType(type),
  34. fTextureId(0),
  35. fIsReady(false)
  36. {
  37. glGenTextures(1, &fTextureId);
  38. }
  39. Image::Image(const char* const rawData, const Size<uint>& size, const GLenum format, const GLenum type)
  40. : fRawData(rawData),
  41. fSize(size),
  42. fFormat(format),
  43. fType(type),
  44. fTextureId(0),
  45. fIsReady(false)
  46. {
  47. glGenTextures(1, &fTextureId);
  48. }
  49. Image::Image(const Image& image)
  50. : fRawData(image.fRawData),
  51. fSize(image.fSize),
  52. fFormat(image.fFormat),
  53. fType(image.fType),
  54. fTextureId(0),
  55. fIsReady(false)
  56. {
  57. glGenTextures(1, &fTextureId);
  58. }
  59. Image::~Image()
  60. {
  61. if (fTextureId != 0)
  62. {
  63. #ifndef DISTRHO_OS_MAC // FIXME
  64. glDeleteTextures(1, &fTextureId);
  65. #endif
  66. fTextureId = 0;
  67. }
  68. }
  69. void Image::loadFromMemory(const char* const rawData, const uint width, const uint height, const GLenum format, const GLenum type) noexcept
  70. {
  71. loadFromMemory(rawData, Size<uint>(width, height), format, type);
  72. }
  73. void Image::loadFromMemory(const char* const rawData, const Size<uint>& size, const GLenum format, const GLenum type) noexcept
  74. {
  75. fRawData = rawData;
  76. fSize = size;
  77. fFormat = format;
  78. fType = type;
  79. fIsReady = false;
  80. }
  81. bool Image::isValid() const noexcept
  82. {
  83. return (fRawData != nullptr && fSize.getWidth() > 0 && fSize.getHeight() > 0);
  84. }
  85. uint Image::getWidth() const noexcept
  86. {
  87. return fSize.getWidth();
  88. }
  89. uint Image::getHeight() const noexcept
  90. {
  91. return fSize.getHeight();
  92. }
  93. const Size<uint>& Image::getSize() const noexcept
  94. {
  95. return fSize;
  96. }
  97. const char* Image::getRawData() const noexcept
  98. {
  99. return fRawData;
  100. }
  101. GLenum Image::getFormat() const noexcept
  102. {
  103. return fFormat;
  104. }
  105. GLenum Image::getType() const noexcept
  106. {
  107. return fType;
  108. }
  109. void Image::draw()
  110. {
  111. drawAt(0, 0);
  112. }
  113. void Image::drawAt(const int x, const int y)
  114. {
  115. drawAt(Point<int>(x, y));
  116. }
  117. void Image::drawAt(const Point<int>& pos)
  118. {
  119. if (fTextureId == 0 || ! isValid())
  120. return;
  121. glEnable(GL_TEXTURE_2D);
  122. glBindTexture(GL_TEXTURE_2D, fTextureId);
  123. if (! fIsReady)
  124. {
  125. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  126. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  127. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  128. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  129. static const float trans[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  130. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, trans);
  131. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  132. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  133. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
  134. static_cast<GLsizei>(fSize.getWidth()), static_cast<GLsizei>(fSize.getHeight()), 0,
  135. fFormat, fType, fRawData);
  136. fIsReady = true;
  137. }
  138. Rectangle<int>(pos, static_cast<int>(fSize.getWidth()), static_cast<int>(fSize.getHeight())).draw();
  139. glBindTexture(GL_TEXTURE_2D, 0);
  140. glDisable(GL_TEXTURE_2D);
  141. }
  142. // -----------------------------------------------------------------------
  143. Image& Image::operator=(const Image& image) noexcept
  144. {
  145. fRawData = image.fRawData;
  146. fSize = image.fSize;
  147. fFormat = image.fFormat;
  148. fType = image.fType;
  149. fIsReady = false;
  150. return *this;
  151. }
  152. bool Image::operator==(const Image& image) const noexcept
  153. {
  154. return (fRawData == image.fRawData && fSize == image.fSize);
  155. }
  156. bool Image::operator!=(const Image& image) const noexcept
  157. {
  158. return !operator==(image);
  159. }
  160. // -----------------------------------------------------------------------
  161. END_NAMESPACE_DGL