wrap_ImageData.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * Copyright (c) 2006-2013 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_ImageData.h"
  21. #include "common/wrap_Data.h"
  22. #include "filesystem/File.h"
  23. namespace love
  24. {
  25. namespace image
  26. {
  27. ImageData *luax_checkimagedata(lua_State *L, int idx)
  28. {
  29. return luax_checktype<ImageData>(L, idx, "ImageData", IMAGE_IMAGE_DATA_T);
  30. }
  31. int w_ImageData_getWidth(lua_State *L)
  32. {
  33. ImageData *t = luax_checkimagedata(L, 1);
  34. lua_pushinteger(L, t->getWidth());
  35. return 1;
  36. }
  37. int w_ImageData_getHeight(lua_State *L)
  38. {
  39. ImageData *t = luax_checkimagedata(L, 1);
  40. lua_pushinteger(L, t->getHeight());
  41. return 1;
  42. }
  43. int w_ImageData_getDimensions(lua_State *L)
  44. {
  45. ImageData *t = luax_checkimagedata(L, 1);
  46. lua_pushinteger(L, t->getWidth());
  47. lua_pushinteger(L, t->getHeight());
  48. return 2;
  49. }
  50. int w_ImageData_getPixel(lua_State *L)
  51. {
  52. ImageData *t = luax_checkimagedata(L, 1);
  53. int x = luaL_checkint(L, 2);
  54. int y = luaL_checkint(L, 3);
  55. pixel c;
  56. try
  57. {
  58. c = t->getPixel(x, y);
  59. }
  60. catch(love::Exception &e)
  61. {
  62. return luaL_error(L, "%s", e.what());
  63. }
  64. lua_pushnumber(L, c.r);
  65. lua_pushnumber(L, c.g);
  66. lua_pushnumber(L, c.b);
  67. lua_pushnumber(L, c.a);
  68. return 4;
  69. }
  70. int w_ImageData_setPixel(lua_State *L)
  71. {
  72. ImageData *t = luax_checkimagedata(L, 1);
  73. int x = luaL_checkint(L, 2);
  74. int y = luaL_checkint(L, 3);
  75. pixel c;
  76. c.r = luaL_checkint(L, 4);
  77. c.g = luaL_checkint(L, 5);
  78. c.b = luaL_checkint(L, 6);
  79. c.a = luaL_optint(L, 7, 255);
  80. try
  81. {
  82. t->setPixel(x, y, c);
  83. }
  84. catch(love::Exception &e)
  85. {
  86. return luaL_error(L, "%s", e.what());
  87. }
  88. return 0;
  89. }
  90. int w_ImageData_mapPixel(lua_State *L)
  91. {
  92. ImageData *t = luax_checkimagedata(L, 1);
  93. if (!lua_isfunction(L, 2))
  94. return luaL_error(L, "Function expected");
  95. int w = t->getWidth();
  96. int h = t->getHeight();
  97. for (int i = 0; i < w; i++)
  98. {
  99. for (int j = 0; j < h; j++)
  100. {
  101. lua_pushvalue(L, 2);
  102. lua_pushnumber(L, i);
  103. lua_pushnumber(L, j);
  104. pixel c = t->getPixel(i, j);
  105. lua_pushnumber(L, c.r);
  106. lua_pushnumber(L, c.g);
  107. lua_pushnumber(L, c.b);
  108. lua_pushnumber(L, c.a);
  109. lua_call(L, 6, 4);
  110. c.r = luaL_optint(L, -4, 0);
  111. c.g = luaL_optint(L, -3, 0);
  112. c.b = luaL_optint(L, -2, 0);
  113. c.a = luaL_optint(L, -1, 255);
  114. t->setPixel(i, j, c);
  115. lua_pop(L, 4);
  116. }
  117. }
  118. return 0;
  119. }
  120. int w_ImageData_paste(lua_State *L)
  121. {
  122. ImageData *t = luax_checkimagedata(L, 1);
  123. ImageData *src = luax_checkimagedata(L, 2);
  124. int dx = luaL_checkint(L, 3);
  125. int dy = luaL_checkint(L, 4);
  126. int sx = luaL_optint(L, 5, 0);
  127. int sy = luaL_optint(L, 6, 0);
  128. int sw = luaL_optint(L, 7, src->getWidth());
  129. int sh = luaL_optint(L, 8, src->getHeight());
  130. t->paste((love::image::ImageData *)src, dx, dy, sx, sy, sw, sh);
  131. return 0;
  132. }
  133. int w_ImageData_encode(lua_State *L)
  134. {
  135. std::string ext;
  136. const char *fmt;
  137. ImageData::Format format = ImageData::FORMAT_MAX_ENUM;
  138. ImageData *t = luax_checkimagedata(L, 1);
  139. if (lua_isstring(L, 2))
  140. luax_convobj(L, 2, "filesystem", "newFile");
  141. love::filesystem::File *file = luax_checktype<love::filesystem::File>(L, 2, "File", FILESYSTEM_FILE_T);
  142. if (lua_isnoneornil(L, 3))
  143. {
  144. ext = file->getExtension();
  145. fmt = ext.c_str();
  146. if (!ImageData::getConstant(fmt, format))
  147. return luaL_error(L, "Invalid image format '%s'.", fmt);
  148. }
  149. else
  150. {
  151. fmt = luaL_checkstring(L, 3);
  152. if (!ImageData::getConstant(fmt, format))
  153. return luaL_error(L, "Invalid image format '%s'.", fmt);
  154. }
  155. try
  156. {
  157. t->encode(file, format);
  158. }
  159. catch(love::Exception &e)
  160. {
  161. return luaL_error(L, e.what());
  162. }
  163. return 0;
  164. }
  165. static const luaL_Reg functions[] =
  166. {
  167. // Data
  168. { "getString", w_Data_getString },
  169. { "getSize", w_Data_getSize },
  170. { "getWidth", w_ImageData_getWidth },
  171. { "getHeight", w_ImageData_getHeight },
  172. { "getDimensions", w_ImageData_getDimensions },
  173. { "getPixel", w_ImageData_getPixel },
  174. { "setPixel", w_ImageData_setPixel },
  175. { "mapPixel", w_ImageData_mapPixel },
  176. { "paste", w_ImageData_paste },
  177. { "encode", w_ImageData_encode },
  178. { 0, 0 }
  179. };
  180. extern "C" int luaopen_imagedata(lua_State *L)
  181. {
  182. return luax_register_type(L, "ImageData", functions);
  183. }
  184. } // image
  185. } // love