Mercurial > love-experiments
annotate src/modules/image/wrap_ImageData.cpp @ 1361:70f7c7bb0ac1
Added love.graphics.getDimensions and Image/Canvas/ImageData:getDimensions (issue #566)
| author | Alex Szpakowski <slime73@gmail.com> |
|---|---|
| date | Mon, 11 Mar 2013 19:03:37 -0300 |
| parents | b7f097053d74 |
| children | a923d8f90638 |
| rev | line source |
|---|---|
| 1096 | 1 /** |
|
1282
b7f097053d74
Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents:
1096
diff
changeset
|
2 * Copyright (c) 2006-2013 LOVE Development Team |
| 1096 | 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 | |
| 21 #include "wrap_ImageData.h" | |
| 22 | |
| 23 #include "common/wrap_Data.h" | |
| 24 #include "filesystem/File.h" | |
| 25 | |
| 26 namespace love | |
| 27 { | |
| 28 namespace image | |
| 29 { | |
| 30 | |
| 31 ImageData *luax_checkimagedata(lua_State *L, int idx) | |
| 32 { | |
| 33 return luax_checktype<ImageData>(L, idx, "ImageData", IMAGE_IMAGE_DATA_T); | |
| 34 } | |
| 35 | |
| 36 int w_ImageData_getWidth(lua_State *L) | |
| 37 { | |
| 38 ImageData *t = luax_checkimagedata(L, 1); | |
| 39 lua_pushinteger(L, t->getWidth()); | |
| 40 return 1; | |
| 41 } | |
| 42 | |
| 43 int w_ImageData_getHeight(lua_State *L) | |
| 44 { | |
| 45 ImageData *t = luax_checkimagedata(L, 1); | |
| 46 lua_pushinteger(L, t->getHeight()); | |
| 47 return 1; | |
| 48 } | |
| 49 | |
|
1361
70f7c7bb0ac1
Added love.graphics.getDimensions and Image/Canvas/ImageData:getDimensions (issue #566)
Alex Szpakowski <slime73@gmail.com>
parents:
1282
diff
changeset
|
50 int w_ImageData_getDimensions(lua_State *L) |
|
70f7c7bb0ac1
Added love.graphics.getDimensions and Image/Canvas/ImageData:getDimensions (issue #566)
Alex Szpakowski <slime73@gmail.com>
parents:
1282
diff
changeset
|
51 { |
|
70f7c7bb0ac1
Added love.graphics.getDimensions and Image/Canvas/ImageData:getDimensions (issue #566)
Alex Szpakowski <slime73@gmail.com>
parents:
1282
diff
changeset
|
52 ImageData *t = luax_checkimagedata(L, 1); |
|
70f7c7bb0ac1
Added love.graphics.getDimensions and Image/Canvas/ImageData:getDimensions (issue #566)
Alex Szpakowski <slime73@gmail.com>
parents:
1282
diff
changeset
|
53 lua_pushinteger(L, t->getWidth()); |
|
70f7c7bb0ac1
Added love.graphics.getDimensions and Image/Canvas/ImageData:getDimensions (issue #566)
Alex Szpakowski <slime73@gmail.com>
parents:
1282
diff
changeset
|
54 lua_pushinteger(L, t->getHeight()); |
|
70f7c7bb0ac1
Added love.graphics.getDimensions and Image/Canvas/ImageData:getDimensions (issue #566)
Alex Szpakowski <slime73@gmail.com>
parents:
1282
diff
changeset
|
55 return 2; |
|
70f7c7bb0ac1
Added love.graphics.getDimensions and Image/Canvas/ImageData:getDimensions (issue #566)
Alex Szpakowski <slime73@gmail.com>
parents:
1282
diff
changeset
|
56 } |
|
70f7c7bb0ac1
Added love.graphics.getDimensions and Image/Canvas/ImageData:getDimensions (issue #566)
Alex Szpakowski <slime73@gmail.com>
parents:
1282
diff
changeset
|
57 |
| 1096 | 58 int w_ImageData_getPixel(lua_State *L) |
| 59 { | |
| 60 ImageData *t = luax_checkimagedata(L, 1); | |
| 61 int x = luaL_checkint(L, 2); | |
| 62 int y = luaL_checkint(L, 3); | |
| 63 pixel c; | |
| 64 try | |
| 65 { | |
| 66 c = t->getPixel(x, y); | |
| 67 } | |
| 68 catch(love::Exception &e) | |
| 69 { | |
| 70 return luaL_error(L, "%s", e.what()); | |
| 71 } | |
| 72 lua_pushnumber(L, c.r); | |
| 73 lua_pushnumber(L, c.g); | |
| 74 lua_pushnumber(L, c.b); | |
| 75 lua_pushnumber(L, c.a); | |
| 76 return 4; | |
| 77 } | |
| 78 | |
| 79 int w_ImageData_setPixel(lua_State *L) | |
| 80 { | |
| 81 ImageData *t = luax_checkimagedata(L, 1); | |
| 82 int x = luaL_checkint(L, 2); | |
| 83 int y = luaL_checkint(L, 3); | |
| 84 pixel c; | |
| 85 c.r = luaL_checkint(L, 4); | |
| 86 c.g = luaL_checkint(L, 5); | |
| 87 c.b = luaL_checkint(L, 6); | |
| 88 c.a = luaL_checkint(L, 7); | |
| 89 try | |
| 90 { | |
| 91 t->setPixel(x, y, c); | |
| 92 } | |
| 93 catch(love::Exception &e) | |
| 94 { | |
| 95 return luaL_error(L, "%s", e.what()); | |
| 96 } | |
| 97 return 0; | |
| 98 } | |
| 99 | |
| 100 int w_ImageData_mapPixel(lua_State *L) | |
| 101 { | |
| 102 ImageData *t = luax_checkimagedata(L, 1); | |
| 103 | |
| 104 if (!lua_isfunction(L, 2)) | |
| 105 return luaL_error(L, "Function expected"); | |
| 106 | |
| 107 int w = t->getWidth(); | |
| 108 int h = t->getHeight(); | |
| 109 | |
| 110 for (int i = 0; i < w; i++) | |
| 111 { | |
| 112 for (int j = 0; j < h; j++) | |
| 113 { | |
| 114 lua_pushvalue(L, 2); | |
| 115 lua_pushnumber(L, i); | |
| 116 lua_pushnumber(L, j); | |
| 117 pixel c = t->getPixel(i, j); | |
| 118 lua_pushnumber(L, c.r); | |
| 119 lua_pushnumber(L, c.g); | |
| 120 lua_pushnumber(L, c.b); | |
| 121 lua_pushnumber(L, c.a); | |
| 122 lua_call(L, 6, 4); | |
| 123 c.r = luaL_optint(L, -4, 0); | |
| 124 c.g = luaL_optint(L, -3, 0); | |
| 125 c.b = luaL_optint(L, -2, 0); | |
| 126 c.a = luaL_optint(L, -1, 0); | |
| 127 t->setPixel(i, j, c); | |
| 128 lua_pop(L, 4); | |
| 129 } | |
| 130 } | |
| 131 return 0; | |
| 132 } | |
| 133 | |
| 134 int w_ImageData_getString(lua_State *L) | |
| 135 { | |
| 136 ImageData *t = luax_checkimagedata(L, 1); | |
| 137 lua_pushlstring(L, (const char *)t->getData(), t->getSize()); | |
| 138 return 1; | |
| 139 } | |
| 140 | |
| 141 int w_ImageData_paste(lua_State *L) | |
| 142 { | |
| 143 ImageData *t = luax_checkimagedata(L, 1); | |
| 144 ImageData *src = luax_checkimagedata(L, 2); | |
| 145 int dx = luaL_checkint(L, 3); | |
| 146 int dy = luaL_checkint(L, 4); | |
| 147 int sx = luaL_optint(L, 5, 0); | |
| 148 int sy = luaL_optint(L, 6, 0); | |
| 149 int sw = luaL_optint(L, 7, src->getWidth()); | |
| 150 int sh = luaL_optint(L, 8, src->getHeight()); | |
| 151 t->paste((love::image::ImageData *)src, dx, dy, sx, sy, sw, sh); | |
| 152 return 0; | |
| 153 } | |
| 154 | |
| 155 int w_ImageData_encode(lua_State *L) | |
| 156 { | |
| 157 std::string ext; | |
| 158 const char *fmt; | |
| 159 ImageData::Format format = ImageData::FORMAT_MAX_ENUM; | |
| 160 ImageData *t = luax_checkimagedata(L, 1); | |
| 161 | |
| 162 if (lua_isstring(L, 2)) | |
| 163 luax_convobj(L, 2, "filesystem", "newFile"); | |
| 164 love::filesystem::File *file = luax_checktype<love::filesystem::File>(L, 2, "File", FILESYSTEM_FILE_T); | |
| 165 | |
| 166 if (lua_isnoneornil(L, 3)) | |
| 167 { | |
| 168 ext = file->getExtension(); | |
| 169 fmt = ext.c_str(); | |
| 170 ImageData::getConstant(fmt, format); | |
| 171 } | |
| 172 else | |
| 173 { | |
| 174 fmt = luaL_checkstring(L, 3); | |
| 175 if (!ImageData::getConstant(fmt, format)) | |
| 176 luaL_error(L, "Invalid image format."); | |
| 177 } | |
| 178 | |
| 179 try | |
| 180 { | |
| 181 t->encode(file, format); | |
| 182 } | |
| 183 catch(love::Exception &e) | |
| 184 { | |
| 185 return luaL_error(L, e.what()); | |
| 186 } | |
| 187 return 0; | |
| 188 } | |
| 189 | |
| 190 static const luaL_Reg functions[] = | |
| 191 { | |
| 192 | |
| 193 // Data | |
| 194 { "getPointer", w_Data_getPointer }, | |
| 195 { "getSize", w_Data_getSize }, | |
| 196 | |
| 197 { "getWidth", w_ImageData_getWidth }, | |
| 198 { "getHeight", w_ImageData_getHeight }, | |
|
1361
70f7c7bb0ac1
Added love.graphics.getDimensions and Image/Canvas/ImageData:getDimensions (issue #566)
Alex Szpakowski <slime73@gmail.com>
parents:
1282
diff
changeset
|
199 { "getDimensions", w_ImageData_getDimensions }, |
| 1096 | 200 { "getPixel", w_ImageData_getPixel }, |
| 201 { "setPixel", w_ImageData_setPixel }, | |
| 202 { "mapPixel", w_ImageData_mapPixel }, | |
| 203 { "getString", w_ImageData_getString }, | |
| 204 { "paste", w_ImageData_paste }, | |
| 205 { "encode", w_ImageData_encode }, | |
| 206 { 0, 0 } | |
| 207 }; | |
| 208 | |
| 209 extern "C" int luaopen_imagedata(lua_State *L) | |
| 210 { | |
| 211 return luax_register_type(L, "ImageData", functions); | |
| 212 } | |
| 213 | |
| 214 } // image | |
| 215 } // love |
