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 
21 // LOVE
22 #include "wrap_Image.h"
23 
24 namespace love
25 {
26 namespace graphics
27 {
28 namespace opengl
29 {
30 
luax_checkimage(lua_State * L,int idx)31 Image *luax_checkimage(lua_State *L, int idx)
32 {
33 	return luax_checktype<Image>(L, idx, GRAPHICS_IMAGE_ID);
34 }
35 
w_Image_setMipmapFilter(lua_State * L)36 int w_Image_setMipmapFilter(lua_State *L)
37 {
38 	Image *t = luax_checkimage(L, 1);
39 	Texture::Filter f = t->getFilter();
40 
41 	if (lua_isnoneornil(L, 2))
42 		f.mipmap = Texture::FILTER_NONE; // mipmapping is disabled if no argument is given
43 	else
44 	{
45 		const char *mipmapstr = luaL_checkstring(L, 2);
46 		if (!Texture::getConstant(mipmapstr, f.mipmap))
47 			return luaL_error(L, "Invalid filter mode: %s", mipmapstr);
48 	}
49 
50 	luax_catchexcept(L, [&](){ t->setFilter(f); });
51 	t->setMipmapSharpness((float) luaL_optnumber(L, 3, 0.0));
52 
53 	return 0;
54 }
55 
w_Image_getMipmapFilter(lua_State * L)56 int w_Image_getMipmapFilter(lua_State *L)
57 {
58 	Image *t = luax_checkimage(L, 1);
59 
60 	const Texture::Filter &f = t->getFilter();
61 
62 	const char *mipmapstr;
63 	if (Texture::getConstant(f.mipmap, mipmapstr))
64 		lua_pushstring(L, mipmapstr);
65 	else
66 		lua_pushnil(L); // only return a mipmap filter if mipmapping is enabled
67 
68 	lua_pushnumber(L, t->getMipmapSharpness());
69 	return 2;
70 }
71 
w_Image_isCompressed(lua_State * L)72 int w_Image_isCompressed(lua_State *L)
73 {
74 	Image *i = luax_checkimage(L, 1);
75 	luax_pushboolean(L, i->isCompressed());
76 	return 1;
77 }
78 
w_Image_refresh(lua_State * L)79 int w_Image_refresh(lua_State *L)
80 {
81 	Image *i = luax_checkimage(L, 1);
82 
83 	int xoffset = (int) luaL_optnumber(L, 2, 0);
84 	int yoffset = (int) luaL_optnumber(L, 3, 0);
85 	int w = (int) luaL_optnumber(L, 4, i->getWidth());
86 	int h = (int) luaL_optnumber(L, 5, i->getHeight());
87 
88 	luax_catchexcept(L, [&](){ i->refresh(xoffset, yoffset, w, h); });
89 	return 0;
90 }
91 
w_Image_getData(lua_State * L)92 int w_Image_getData(lua_State *L)
93 {
94 	Image *i = luax_checkimage(L, 1);
95 	int n = 0;
96 
97 	if (i->isCompressed())
98 	{
99 		for (const auto &cdata : i->getCompressedData())
100 		{
101 			luax_pushtype(L, IMAGE_COMPRESSED_IMAGE_DATA_ID, cdata.get());
102 			n++;
103 		}
104 	}
105 	else
106 	{
107 		for (const auto &data : i->getImageData())
108 		{
109 			luax_pushtype(L, IMAGE_IMAGE_DATA_ID, data.get());
110 			n++;
111 		}
112 	}
113 
114 	return n;
115 }
116 
imageFlagName(Image::FlagType flagtype)117 static const char *imageFlagName(Image::FlagType flagtype)
118 {
119 	const char *name = nullptr;
120 	Image::getConstant(flagtype, name);
121 	return name;
122 }
123 
w_Image_getFlags(lua_State * L)124 int w_Image_getFlags(lua_State *L)
125 {
126 	Image *i = luax_checkimage(L, 1);
127 	Image::Flags flags = i->getFlags();
128 
129 	lua_createtable(L, 0, 2);
130 
131 	lua_pushboolean(L, flags.mipmaps);
132 	lua_setfield(L, -2, imageFlagName(Image::FLAG_TYPE_MIPMAPS));
133 
134 	lua_pushboolean(L, flags.linear);
135 	lua_setfield(L, -2, imageFlagName(Image::FLAG_TYPE_LINEAR));
136 
137 	return 1;
138 }
139 
140 static const luaL_Reg w_Image_functions[] =
141 {
142 	{ "setMipmapFilter", w_Image_setMipmapFilter },
143 	{ "getMipmapFilter", w_Image_getMipmapFilter },
144 	{ "isCompressed", w_Image_isCompressed },
145 	{ "refresh", w_Image_refresh },
146 	{ "getData", w_Image_getData },
147 	{ "getFlags", w_Image_getFlags },
148 	{ 0, 0 }
149 };
150 
luaopen_image(lua_State * L)151 extern "C" int luaopen_image(lua_State *L)
152 {
153 	return luax_register_type(L, GRAPHICS_IMAGE_ID, "Image", w_Texture_functions, w_Image_functions, nullptr);
154 }
155 
156 } // opengl
157 } // graphics
158 } // love
159