wrap_Image.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Copyright (c) 2006-2016 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. #include "wrap_Image.h"
  21. #include "common/Data.h"
  22. #include "common/StringMap.h"
  23. #include "magpie/Image.h"
  24. #include "filesystem/wrap_Filesystem.h"
  25. namespace love
  26. {
  27. namespace image
  28. {
  29. #define instance() (Module::getInstance<Image>(Module::M_IMAGE))
  30. int w_newImageData(lua_State *L)
  31. {
  32. // Case 1: width & height.
  33. if (lua_isnumber(L, 1))
  34. {
  35. int w = (int) luaL_checknumber(L, 1);
  36. int h = (int) luaL_checknumber(L, 2);
  37. if (w <= 0 || h <= 0)
  38. return luaL_error(L, "Invalid image size.");
  39. ImageData::Format format = ImageData::FORMAT_RGBA8;
  40. if (!lua_isnoneornil(L, 3))
  41. {
  42. const char *fstr = luaL_checkstring(L, 3);
  43. if (!ImageData::getConstant(fstr, format))
  44. return luaL_error(L, "Invalid ImageData format: %s", fstr);
  45. }
  46. size_t numbytes = 0;
  47. const char *bytes = nullptr;
  48. if (!lua_isnoneornil(L, 4))
  49. bytes = luaL_checklstring(L, 4, &numbytes);
  50. ImageData *t = nullptr;
  51. luax_catchexcept(L, [&](){ t = instance()->newImageData(w, h, format); });
  52. if (bytes)
  53. {
  54. if (numbytes != t->getSize())
  55. {
  56. t->release();
  57. return luaL_error(L, "The size of the raw byte string must match the ImageData's actual size in bytes.");
  58. }
  59. memcpy(t->getData(), bytes, t->getSize());
  60. }
  61. luax_pushtype(L, IMAGE_IMAGE_DATA_ID, t);
  62. t->release();
  63. return 1;
  64. }
  65. // Case 2: File(Data).
  66. love::filesystem::FileData *data = love::filesystem::luax_getfiledata(L, 1);
  67. ImageData *t = nullptr;
  68. luax_catchexcept(L,
  69. [&]() { t = instance()->newImageData(data); },
  70. [&](bool) { data->release(); }
  71. );
  72. luax_pushtype(L, IMAGE_IMAGE_DATA_ID, t);
  73. t->release();
  74. return 1;
  75. }
  76. int w_newCompressedData(lua_State *L)
  77. {
  78. love::filesystem::FileData *data = love::filesystem::luax_getfiledata(L, 1);
  79. CompressedImageData *t = nullptr;
  80. luax_catchexcept(L,
  81. [&]() { t = instance()->newCompressedData(data); },
  82. [&](bool) { data->release(); }
  83. );
  84. luax_pushtype(L, IMAGE_COMPRESSED_IMAGE_DATA_ID, t);
  85. t->release();
  86. return 1;
  87. }
  88. int w_isCompressed(lua_State *L)
  89. {
  90. love::filesystem::FileData *data = love::filesystem::luax_getfiledata(L, 1);
  91. bool compressed = instance()->isCompressed(data);
  92. data->release();
  93. luax_pushboolean(L, compressed);
  94. return 1;
  95. }
  96. // List of functions to wrap.
  97. static const luaL_Reg functions[] =
  98. {
  99. { "newImageData", w_newImageData },
  100. { "newCompressedData", w_newCompressedData },
  101. { "isCompressed", w_isCompressed },
  102. { 0, 0 }
  103. };
  104. static const lua_CFunction types[] =
  105. {
  106. luaopen_imagedata,
  107. luaopen_compressedimagedata,
  108. 0
  109. };
  110. extern "C" int luaopen_love_image(lua_State *L)
  111. {
  112. Image *instance = instance();
  113. if (instance == nullptr)
  114. {
  115. luax_catchexcept(L, [&](){ instance = new love::image::magpie::Image(); });
  116. }
  117. else
  118. instance->retain();
  119. WrappedModule w;
  120. w.module = instance;
  121. w.name = "image";
  122. w.type = MODULE_IMAGE_ID;
  123. w.functions = functions;
  124. w.types = types;
  125. return luax_register_module(L, w);
  126. }
  127. } // image
  128. } // love