wrap_Image.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Copyright (c) 2006-2014 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: Integers.
  33. if (lua_isnumber(L, 1))
  34. {
  35. int w = luaL_checkint(L, 1);
  36. int h = luaL_checkint(L, 2);
  37. if (w <= 0 || h <= 0)
  38. return luaL_error(L, "Invalid image size.");
  39. ImageData *t = nullptr;
  40. luax_catchexcept(L, [&](){ t = instance()->newImageData(w, h); });
  41. luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, t);
  42. return 1;
  43. }
  44. // Case 2: File(Data).
  45. love::filesystem::FileData *data = love::filesystem::luax_getfiledata(L, 1);
  46. ImageData *t = nullptr;
  47. luax_catchexcept(L,
  48. [&]() { t = instance()->newImageData(data); },
  49. [&]() { data->release(); }
  50. );
  51. luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, t);
  52. return 1;
  53. }
  54. int w_newCompressedData(lua_State *L)
  55. {
  56. love::filesystem::FileData *data = love::filesystem::luax_getfiledata(L, 1);
  57. CompressedData *t = nullptr;
  58. luax_catchexcept(L,
  59. [&]() { t = instance()->newCompressedData(data); },
  60. [&]() { data->release(); }
  61. );
  62. luax_pushtype(L, "CompressedData", IMAGE_COMPRESSED_DATA_T, t);
  63. return 1;
  64. }
  65. int w_isCompressed(lua_State *L)
  66. {
  67. love::filesystem::FileData *data = love::filesystem::luax_getfiledata(L, 1);
  68. bool compressed = instance()->isCompressed(data);
  69. data->release();
  70. luax_pushboolean(L, compressed);
  71. return 1;
  72. }
  73. // List of functions to wrap.
  74. static const luaL_Reg functions[] =
  75. {
  76. { "newImageData", w_newImageData },
  77. { "newCompressedData", w_newCompressedData },
  78. { "isCompressed", w_isCompressed },
  79. { 0, 0 }
  80. };
  81. static const lua_CFunction types[] =
  82. {
  83. luaopen_imagedata,
  84. luaopen_compresseddata,
  85. 0
  86. };
  87. extern "C" int luaopen_love_image(lua_State *L)
  88. {
  89. Image *instance = instance();
  90. if (instance == nullptr)
  91. {
  92. luax_catchexcept(L, [&](){ instance = new love::image::magpie::Image(); });
  93. }
  94. else
  95. instance->retain();
  96. WrappedModule w;
  97. w.module = instance;
  98. w.name = "image";
  99. w.flags = MODULE_IMAGE_T;
  100. w.functions = functions;
  101. w.types = types;
  102. return luax_register_module(L, w);
  103. }
  104. } // image
  105. } // love