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.

211 lines
5.0KB

  1. /*
  2. * DISTRHO Plugin Toolkit (DPT)
  3. * Copyright (C) 2012-2013 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. #include <cstdio>
  18. START_NAMESPACE_DGL
  19. // -----------------------------------------------------------------------
  20. Image::Image() noexcept
  21. : fRawData(nullptr),
  22. fSize(0, 0),
  23. fFormat(0),
  24. fType(0),
  25. fTextureId(0)
  26. {
  27. }
  28. Image::Image(const char* rawData, int width, int height, GLenum format, GLenum type) noexcept
  29. : fRawData(rawData),
  30. fSize(width, height),
  31. fFormat(format),
  32. fType(type),
  33. fTextureId(0)
  34. {
  35. }
  36. Image::Image(const char* rawData, const Size<int>& size, GLenum format, GLenum type) noexcept
  37. : fRawData(rawData),
  38. fSize(size),
  39. fFormat(format),
  40. fType(type),
  41. fTextureId(0)
  42. {
  43. }
  44. Image::Image(const Image& image) noexcept
  45. : fRawData(image.fRawData),
  46. fSize(image.fSize),
  47. fFormat(image.fFormat),
  48. fType(image.fType),
  49. fTextureId(0)
  50. {
  51. }
  52. Image::~Image()
  53. {
  54. if (fTextureId != 0)
  55. {
  56. glDeleteTextures(1, &fTextureId);
  57. fTextureId = 0;
  58. }
  59. }
  60. void Image::loadFromMemory(const char* rawData, int width, int height, GLenum format, GLenum type) noexcept
  61. {
  62. loadFromMemory(rawData, Size<int>(width, height), format, type);
  63. }
  64. void Image::loadFromMemory(const char* rawData, const Size<int>& size, GLenum format, GLenum type) noexcept
  65. {
  66. fRawData = rawData;
  67. fSize = size;
  68. fFormat = format;
  69. fType = type;
  70. }
  71. bool Image::isValid() const noexcept
  72. {
  73. return (fRawData != nullptr && getWidth() > 0 && getHeight() > 0);
  74. }
  75. int Image::getWidth() const noexcept
  76. {
  77. return fSize.getWidth();
  78. }
  79. int Image::getHeight() const noexcept
  80. {
  81. return fSize.getHeight();
  82. }
  83. const Size<int>& Image::getSize() const noexcept
  84. {
  85. return fSize;
  86. }
  87. const char* Image::getRawData() const noexcept
  88. {
  89. return fRawData;
  90. }
  91. GLenum Image::getFormat() const noexcept
  92. {
  93. return fFormat;
  94. }
  95. GLenum Image::getType() const noexcept
  96. {
  97. return fType;
  98. }
  99. void Image::draw() const
  100. {
  101. draw(0, 0);
  102. }
  103. void Image::draw(int x, int y) const
  104. {
  105. if (! isValid())
  106. return;
  107. if (fTextureId == 0)
  108. glGenTextures(1, &fTextureId);
  109. if (fTextureId == 0)
  110. {
  111. printf("texture Id still 0\n");
  112. return;
  113. }
  114. glEnable(GL_TEXTURE_2D);
  115. glBindTexture(GL_TEXTURE_2D, fTextureId);
  116. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  117. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  118. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWidth(), getHeight(), 0, fFormat, fType, fRawData);
  119. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  120. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  121. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  122. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  123. float trans[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  124. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, trans);
  125. glPushMatrix();
  126. const GLint w2 = getWidth()/2;
  127. const GLint h2 = getHeight()/2;
  128. glTranslatef(x+w2, y+h2, 0.0f);
  129. glBegin(GL_QUADS);
  130. glTexCoord2f(0.0f, 1.0f);
  131. glVertex2i(-w2, -h2);
  132. glTexCoord2f(1.0f, 1.0f);
  133. glVertex2i(getWidth()-w2, -h2);
  134. glTexCoord2f(1.0f, 0.0f);
  135. glVertex2i(getWidth()-w2, getHeight()-h2);
  136. glTexCoord2f(0.0f, 0.0f);
  137. glVertex2i(-w2, getHeight()-h2);
  138. glEnd();
  139. glPopMatrix();
  140. glBindTexture(GL_TEXTURE_2D, 0);
  141. glDisable(GL_TEXTURE_2D);
  142. // glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  143. // glPixelStorei(GL_PACK_ALIGNMENT, 1);
  144. // glRasterPos2i(x, fSize.getHeight()+y);
  145. // glDrawPixels(fSize.getWidth(), fSize.getHeight(), fFormat, fType, fRawData);
  146. }
  147. void Image::draw(const Point<int>& pos) const
  148. {
  149. draw(pos.getX(), pos.getY());
  150. }
  151. // -----------------------------------------------------------------------
  152. Image& Image::operator=(const Image& image) noexcept
  153. {
  154. fRawData = image.fRawData;
  155. fSize = image.fSize;
  156. fFormat = image.fFormat;
  157. fType = image.fType;
  158. return *this;
  159. }
  160. bool Image::operator==(const Image& image) const noexcept
  161. {
  162. return (fRawData == image.fRawData);
  163. }
  164. bool Image::operator!=(const Image& image) const noexcept
  165. {
  166. return (fRawData != image.fRawData);
  167. }
  168. // -----------------------------------------------------------------------
  169. END_NAMESPACE_DGL