|
1086
|
1 /**
|
|
|
2 * Copyright (c) 2006-2012 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
|
|
|
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
|
|
|
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
|
|
|
71 int w_ImageData_setPixel(lua_State *L)
|
|
|
72 {
|
|
|
73 ImageData *t = luax_checkimagedata(L, 1);
|
|
|
74 int x = luaL_checkint(L, 2);
|
|
|
75 int y = luaL_checkint(L, 3);
|
|
|
76 pixel c;
|
|
|
77 c.r = luaL_checkint(L, 4);
|
|
|
78 c.g = luaL_checkint(L, 5);
|
|
|
79 c.b = luaL_checkint(L, 6);
|
|
|
80 c.a = luaL_checkint(L, 7);
|
|
|
81 try
|
|
|
82 {
|
|
|
83 t->setPixel(x, y, c);
|
|
|
84 }
|
|
|
85 catch(love::Exception &e)
|
|
|
86 {
|
|
|
87 return luaL_error(L, "%s", e.what());
|
|
|
88 }
|
|
|
89 return 0;
|
|
|
90 }
|
|
|
91
|
|
|
92 int w_ImageData_mapPixel(lua_State *L)
|
|
|
93 {
|
|
|
94 ImageData *t = luax_checkimagedata(L, 1);
|
|
|
95
|
|
|
96 if (!lua_isfunction(L, 2))
|
|
|
97 return luaL_error(L, "Function expected");
|
|
|
98
|
|
|
99 int w = t->getWidth();
|
|
|
100 int h = t->getHeight();
|
|
|
101
|
|
|
102 for (int i = 0; i < w; i++)
|
|
|
103 {
|
|
|
104 for (int j = 0; j < h; j++)
|
|
|
105 {
|
|
|
106 lua_pushvalue(L, 2);
|
|
|
107 lua_pushnumber(L, i);
|
|
|
108 lua_pushnumber(L, j);
|
|
|
109 pixel c = t->getPixel(i, j);
|
|
|
110 lua_pushnumber(L, c.r);
|
|
|
111 lua_pushnumber(L, c.g);
|
|
|
112 lua_pushnumber(L, c.b);
|
|
|
113 lua_pushnumber(L, c.a);
|
|
|
114 lua_call(L, 6, 4);
|
|
|
115 c.r = luaL_optint(L, -4, 0);
|
|
|
116 c.g = luaL_optint(L, -3, 0);
|
|
|
117 c.b = luaL_optint(L, -2, 0);
|
|
|
118 c.a = luaL_optint(L, -1, 0);
|
|
|
119 t->setPixel(i, j, c);
|
|
|
120 lua_pop(L, 4);
|
|
|
121 }
|
|
|
122 }
|
|
|
123 return 0;
|
|
|
124 }
|
|
|
125
|
|
|
126 int w_ImageData_getString(lua_State *L)
|
|
|
127 {
|
|
|
128 ImageData *t = luax_checkimagedata(L, 1);
|
|
|
129 lua_pushlstring(L, (const char *)t->getData(), t->getSize());
|
|
|
130 return 1;
|
|
|
131 }
|
|
|
132
|
|
|
133 int w_ImageData_paste(lua_State *L)
|
|
|
134 {
|
|
|
135 ImageData *t = luax_checkimagedata(L, 1);
|
|
|
136 ImageData *src = luax_checkimagedata(L, 2);
|
|
|
137 int dx = luaL_checkint(L, 3);
|
|
|
138 int dy = luaL_checkint(L, 4);
|
|
|
139 int sx = luaL_optint(L, 5, 0);
|
|
|
140 int sy = luaL_optint(L, 6, 0);
|
|
|
141 int sw = luaL_optint(L, 7, src->getWidth());
|
|
|
142 int sh = luaL_optint(L, 8, src->getHeight());
|
|
|
143 t->paste((love::image::ImageData *)src, dx, dy, sx, sy, sw, sh);
|
|
|
144 return 0;
|
|
|
145 }
|
|
|
146
|
|
|
147 int w_ImageData_encode(lua_State *L)
|
|
|
148 {
|
|
|
149 std::string ext;
|
|
|
150 const char *fmt;
|
|
|
151 ImageData::Format format = ImageData::FORMAT_MAX_ENUM;
|
|
|
152 ImageData *t = luax_checkimagedata(L, 1);
|
|
|
153
|
|
|
154 if (lua_isstring(L, 2))
|
|
|
155 luax_convobj(L, 2, "filesystem", "newFile");
|
|
|
156 love::filesystem::File *file = luax_checktype<love::filesystem::File>(L, 2, "File", FILESYSTEM_FILE_T);
|
|
|
157
|
|
|
158 if (lua_isnoneornil(L, 3))
|
|
|
159 {
|
|
|
160 ext = file->getExtension();
|
|
|
161 fmt = ext.c_str();
|
|
|
162 ImageData::getConstant(fmt, format);
|
|
|
163 }
|
|
|
164 else
|
|
|
165 {
|
|
|
166 fmt = luaL_checkstring(L, 3);
|
|
|
167 if (!ImageData::getConstant(fmt, format))
|
|
|
168 luaL_error(L, "Invalid image format.");
|
|
|
169 }
|
|
|
170
|
|
|
171 try
|
|
|
172 {
|
|
|
173 t->encode(file, format);
|
|
|
174 }
|
|
|
175 catch(love::Exception &e)
|
|
|
176 {
|
|
|
177 return luaL_error(L, e.what());
|
|
|
178 }
|
|
|
179 return 0;
|
|
|
180 }
|
|
|
181
|
|
|
182 static const luaL_Reg functions[] =
|
|
|
183 {
|
|
|
184
|
|
|
185 // Data
|
|
|
186 { "getPointer", w_Data_getPointer },
|
|
|
187 { "getSize", w_Data_getSize },
|
|
|
188
|
|
|
189 { "getWidth", w_ImageData_getWidth },
|
|
|
190 { "getHeight", w_ImageData_getHeight },
|
|
|
191 { "getPixel", w_ImageData_getPixel },
|
|
|
192 { "setPixel", w_ImageData_setPixel },
|
|
|
193 { "mapPixel", w_ImageData_mapPixel },
|
|
|
194 { "getString", w_ImageData_getString },
|
|
|
195 { "paste", w_ImageData_paste },
|
|
|
196 { "encode", w_ImageData_encode },
|
|
|
197 { 0, 0 }
|
|
|
198 };
|
|
|
199
|
|
|
200 extern "C" int luaopen_imagedata(lua_State *L)
|
|
|
201 {
|
|
|
202 return luax_register_type(L, "ImageData", functions);
|
|
|
203 }
|
|
|
204
|
|
|
205 } // image
|
|
|
206 } // love
|