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.

151 lines
4.1KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2019 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. : ImageBase(),
  21. fFormat(0),
  22. fType(0),
  23. fTextureId(0),
  24. fIsReady(false)
  25. {
  26. glGenTextures(1, &fTextureId);
  27. }
  28. Image::Image(const Image& image)
  29. : ImageBase(image),
  30. fFormat(image.fFormat),
  31. fType(image.fType),
  32. fTextureId(0),
  33. fIsReady(false)
  34. {
  35. glGenTextures(1, &fTextureId);
  36. }
  37. Image::Image(const char* const rawData, const uint width, const uint height, const GLenum format, const GLenum type)
  38. : ImageBase(rawData, width, height),
  39. fFormat(format),
  40. fType(type),
  41. fTextureId(0),
  42. fIsReady(false)
  43. {
  44. glGenTextures(1, &fTextureId);
  45. }
  46. Image::Image(const char* const rawData, const Size<uint>& size, const GLenum format, const GLenum type)
  47. : ImageBase(rawData, size),
  48. fFormat(format),
  49. fType(type),
  50. fTextureId(0),
  51. fIsReady(false)
  52. {
  53. glGenTextures(1, &fTextureId);
  54. }
  55. Image::~Image()
  56. {
  57. if (fTextureId != 0)
  58. {
  59. #ifndef DISTRHO_OS_MAC // FIXME
  60. glDeleteTextures(1, &fTextureId);
  61. #endif
  62. fTextureId = 0;
  63. }
  64. }
  65. void Image::loadFromMemory(const char* const rawData,
  66. const uint width,
  67. const uint height,
  68. const GLenum format,
  69. const GLenum type) noexcept
  70. {
  71. loadFromMemory(rawData, Size<uint>(width, height), format, type);
  72. }
  73. void Image::loadFromMemory(const char* const rawData,
  74. const Size<uint>& size,
  75. const GLenum format,
  76. const GLenum type) noexcept
  77. {
  78. fRawData = rawData;
  79. fSize = size;
  80. fFormat = format;
  81. fType = type;
  82. fIsReady = false;
  83. }
  84. GLenum Image::getFormat() const noexcept
  85. {
  86. return fFormat;
  87. }
  88. GLenum Image::getType() const noexcept
  89. {
  90. return fType;
  91. }
  92. Image& Image::operator=(const Image& image) noexcept
  93. {
  94. fRawData = image.fRawData;
  95. fSize = image.fSize;
  96. fFormat = image.fFormat;
  97. fType = image.fType;
  98. fIsReady = false;
  99. return *this;
  100. }
  101. void Image::_drawAt(const Point<int>& pos)
  102. {
  103. if (fTextureId == 0 || ! isValid())
  104. return;
  105. glEnable(GL_TEXTURE_2D);
  106. glBindTexture(GL_TEXTURE_2D, fTextureId);
  107. if (! fIsReady)
  108. {
  109. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  110. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  111. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  112. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  113. static const float trans[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  114. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, trans);
  115. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  116. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  117. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
  118. static_cast<GLsizei>(fSize.getWidth()), static_cast<GLsizei>(fSize.getHeight()), 0,
  119. fFormat, fType, fRawData);
  120. fIsReady = true;
  121. }
  122. Rectangle<int>(pos, static_cast<int>(fSize.getWidth()), static_cast<int>(fSize.getHeight())).draw();
  123. glBindTexture(GL_TEXTURE_2D, 0);
  124. glDisable(GL_TEXTURE_2D);
  125. }
  126. // -----------------------------------------------------------------------
  127. END_NAMESPACE_DGL