Image.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * Copyright (c) 2006-2024 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "Image.h"
  22. #include "common/config.h"
  23. #include "magpie/PNGHandler.h"
  24. #include "magpie/STBHandler.h"
  25. #include "magpie/EXRHandler.h"
  26. #include "magpie/ddsHandler.h"
  27. #include "magpie/PVRHandler.h"
  28. #include "magpie/KTXHandler.h"
  29. #include "magpie/PKMHandler.h"
  30. #include "magpie/ASTCHandler.h"
  31. namespace love
  32. {
  33. namespace image
  34. {
  35. love::Type Image::type("image", &Module::type);
  36. Image::Image()
  37. : Module(M_IMAGE, "love.image.magpie")
  38. {
  39. using namespace magpie;
  40. float16Init(); // Makes sure half-float conversions can be used.
  41. formatHandlers = {
  42. new PNGHandler,
  43. new STBHandler,
  44. new EXRHandler,
  45. new DDSHandler,
  46. new PVRHandler,
  47. new KTXHandler,
  48. new PKMHandler,
  49. new ASTCHandler,
  50. };
  51. }
  52. Image::~Image()
  53. {
  54. // ImageData objects reference the FormatHandlers in our list, so we should
  55. // release them instead of deleting them completely here.
  56. for (FormatHandler *handler : formatHandlers)
  57. handler->release();
  58. }
  59. love::image::ImageData *Image::newImageData(Data *data)
  60. {
  61. return new ImageData(data);
  62. }
  63. love::image::ImageData *Image::newImageData(int width, int height, PixelFormat format)
  64. {
  65. return new ImageData(width, height, format);
  66. }
  67. love::image::ImageData *Image::newImageData(int width, int height, PixelFormat format, void *data, bool own)
  68. {
  69. return new ImageData(width, height, format, data, own);
  70. }
  71. love::image::CompressedImageData *Image::newCompressedData(Data *data)
  72. {
  73. return new CompressedImageData(formatHandlers, data);
  74. }
  75. bool Image::isCompressed(Data *data)
  76. {
  77. for (FormatHandler *handler : formatHandlers)
  78. {
  79. if (handler->canParseCompressed(data))
  80. return true;
  81. }
  82. return false;
  83. }
  84. const std::list<FormatHandler *> &Image::getFormatHandlers() const
  85. {
  86. return formatHandlers;
  87. }
  88. ImageData *Image::newPastedImageData(ImageData *src, int sx, int sy, int w, int h)
  89. {
  90. ImageData *res = newImageData(w, h, src->getFormat());
  91. try
  92. {
  93. res->paste(src, 0, 0, sx, sy, w, h);
  94. }
  95. catch (love::Exception &)
  96. {
  97. res->release();
  98. throw;
  99. }
  100. return res;
  101. }
  102. std::vector<StrongRef<ImageData>> Image::newCubeFaces(love::image::ImageData *src)
  103. {
  104. // The faces array is always ordered +x, -x, +y, -y, +z, -z.
  105. std::vector<StrongRef<ImageData>> faces;
  106. int totalW = src->getWidth();
  107. int totalH = src->getHeight();
  108. if (totalW % 3 == 0 && totalH % 4 == 0 && totalW / 3 == totalH / 4)
  109. {
  110. // +y
  111. // +z +x -z
  112. // -y
  113. // -x
  114. int w = totalW / 3;
  115. int h = totalH / 4;
  116. faces.emplace_back(newPastedImageData(src, 1*w, 1*h, w, h), Acquire::NORETAIN);
  117. faces.emplace_back(newPastedImageData(src, 1*w, 3*h, w, h), Acquire::NORETAIN);
  118. faces.emplace_back(newPastedImageData(src, 1*w, 0*h, w, h), Acquire::NORETAIN);
  119. faces.emplace_back(newPastedImageData(src, 1*w, 2*h, w, h), Acquire::NORETAIN);
  120. faces.emplace_back(newPastedImageData(src, 0*w, 1*h, w, h), Acquire::NORETAIN);
  121. faces.emplace_back(newPastedImageData(src, 2*w, 1*h, w, h), Acquire::NORETAIN);
  122. }
  123. else if (totalW % 4 == 0 && totalH % 3 == 0 && totalW / 4 == totalH / 3)
  124. {
  125. // +y
  126. // -x +z +x -z
  127. // -y
  128. int w = totalW / 4;
  129. int h = totalH / 3;
  130. faces.emplace_back(newPastedImageData(src, 2*w, 1*h, w, h), Acquire::NORETAIN);
  131. faces.emplace_back(newPastedImageData(src, 0*w, 1*h, w, h), Acquire::NORETAIN);
  132. faces.emplace_back(newPastedImageData(src, 1*w, 0*h, w, h), Acquire::NORETAIN);
  133. faces.emplace_back(newPastedImageData(src, 1*w, 2*h, w, h), Acquire::NORETAIN);
  134. faces.emplace_back(newPastedImageData(src, 1*w, 1*h, w, h), Acquire::NORETAIN);
  135. faces.emplace_back(newPastedImageData(src, 3*w, 1*h, w, h), Acquire::NORETAIN);
  136. }
  137. else if (totalH % 6 == 0 && totalW == totalH / 6)
  138. {
  139. // +x
  140. // -x
  141. // +y
  142. // -y
  143. // +z
  144. // -z
  145. int w = totalW;
  146. int h = totalH / 6;
  147. for (int i = 0; i < 6; i++)
  148. faces.emplace_back(newPastedImageData(src, 0, i * h, w, h), Acquire::NORETAIN);
  149. }
  150. else if (totalW % 6 == 0 && totalW / 6 == totalH)
  151. {
  152. // +x -x +y -y +z -z
  153. int w = totalW / 6;
  154. int h = totalH;
  155. for (int i = 0; i < 6; i++)
  156. faces.emplace_back(newPastedImageData(src, i * w, 0, w, h), Acquire::NORETAIN);
  157. }
  158. else
  159. throw love::Exception("Unknown cubemap image dimensions!");
  160. return faces;
  161. }
  162. std::vector<StrongRef<ImageData>> Image::newVolumeLayers(ImageData *src)
  163. {
  164. std::vector<StrongRef<ImageData>> layers;
  165. int totalW = src->getWidth();
  166. int totalH = src->getHeight();
  167. if (totalW % totalH == 0)
  168. {
  169. for (int i = 0; i < totalW / totalH; i++)
  170. layers.emplace_back(newPastedImageData(src, i * totalH, 0, totalH, totalH), Acquire::NORETAIN);
  171. }
  172. else if (totalH % totalW == 0)
  173. {
  174. for (int i = 0; i < totalH / totalW; i++)
  175. layers.emplace_back(newPastedImageData(src, 0, i * totalW, totalW, totalW), Acquire::NORETAIN);
  176. }
  177. else
  178. throw love::Exception("Cannot extract volume layers from source ImageData.");
  179. return layers;
  180. }
  181. } // image
  182. } // love