DISTRHO Plugin Framework
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.

193 lines
4.8KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 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. glDeleteTextures(1, &fTextureId);
  64. fTextureId = 0;
  65. }
  66. }
  67. void Image::loadFromMemory(const char* const rawData, const uint width, const uint height, const GLenum format, const GLenum type) noexcept
  68. {
  69. loadFromMemory(rawData, Size<uint>(width, height), format, type);
  70. }
  71. void Image::loadFromMemory(const char* const rawData, const Size<uint>& size, const GLenum format, const GLenum type) noexcept
  72. {
  73. fRawData = rawData;
  74. fSize = size;
  75. fFormat = format;
  76. fType = type;
  77. fIsReady = false;
  78. }
  79. bool Image::isValid() const noexcept
  80. {
  81. return (fRawData != nullptr && fSize.getWidth() > 0 && fSize.getHeight() > 0);
  82. }
  83. uint Image::getWidth() const noexcept
  84. {
  85. return fSize.getWidth();
  86. }
  87. uint Image::getHeight() const noexcept
  88. {
  89. return fSize.getHeight();
  90. }
  91. const Size<uint>& Image::getSize() const noexcept
  92. {
  93. return fSize;
  94. }
  95. const char* Image::getRawData() const noexcept
  96. {
  97. return fRawData;
  98. }
  99. GLenum Image::getFormat() const noexcept
  100. {
  101. return fFormat;
  102. }
  103. GLenum Image::getType() const noexcept
  104. {
  105. return fType;
  106. }
  107. void Image::draw()
  108. {
  109. drawAt(0, 0);
  110. }
  111. void Image::drawAt(const int x, const int y)
  112. {
  113. drawAt(Point<int>(x, y));
  114. }
  115. void Image::drawAt(const Point<int>& pos)
  116. {
  117. if (fTextureId == 0 || ! isValid())
  118. return;
  119. glEnable(GL_TEXTURE_2D);
  120. glBindTexture(GL_TEXTURE_2D, fTextureId);
  121. if (! fIsReady)
  122. {
  123. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  124. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  125. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  126. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  127. static const float trans[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  128. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, trans);
  129. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  130. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  131. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
  132. static_cast<GLsizei>(fSize.getWidth()), static_cast<GLsizei>(fSize.getHeight()), 0,
  133. fFormat, fType, fRawData);
  134. fIsReady = true;
  135. }
  136. Rectangle<int>(pos, static_cast<int>(fSize.getWidth()), static_cast<int>(fSize.getHeight())).draw();
  137. glBindTexture(GL_TEXTURE_2D, 0);
  138. glDisable(GL_TEXTURE_2D);
  139. }
  140. // -----------------------------------------------------------------------
  141. Image& Image::operator=(const Image& image) noexcept
  142. {
  143. fRawData = image.fRawData;
  144. fSize = image.fSize;
  145. fFormat = image.fFormat;
  146. fType = image.fType;
  147. fIsReady = false;
  148. return *this;
  149. }
  150. bool Image::operator==(const Image& image) const noexcept
  151. {
  152. return (fRawData == image.fRawData && fSize == image.fSize);
  153. }
  154. bool Image::operator!=(const Image& image) const noexcept
  155. {
  156. return !operator==(image);
  157. }
  158. // -----------------------------------------------------------------------
  159. END_NAMESPACE_DGL