1/**
2 * Copyright (c) 2006-2023 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_CompressedImageData.h"
22#include "data/wrap_Data.h"
23
24namespace love
25{
26namespace image
27{
28
29CompressedImageData *luax_checkcompressedimagedata(lua_State *L, int idx)
30{
31 return luax_checktype<CompressedImageData>(L, idx);
32}
33
34int w_CompressedImageData_clone(lua_State *L)
35{
36 CompressedImageData *t = luax_checkcompressedimagedata(L, 1), *c = nullptr;
37 luax_catchexcept(L, [&](){ c = t->clone(); });
38 luax_pushtype(L, c);
39 c->release();
40 return 1;
41}
42
43int w_CompressedImageData_getWidth(lua_State *L)
44{
45 CompressedImageData *t = luax_checkcompressedimagedata(L, 1);
46 int miplevel = (int) luaL_optinteger(L, 2, 1);
47 int width = 0;
48
49 luax_catchexcept(L, [&](){ width = t->getWidth(miplevel - 1); });
50
51 lua_pushinteger(L, width);
52 return 1;
53}
54
55int w_CompressedImageData_getHeight(lua_State *L)
56{
57 CompressedImageData *t = luax_checkcompressedimagedata(L, 1);
58 int miplevel = (int) luaL_optinteger(L, 2, 1);
59 int height = 0;
60
61 luax_catchexcept(L, [&](){ height = t->getHeight(miplevel - 1); });
62
63 lua_pushinteger(L, height);
64 return 1;
65}
66
67int w_CompressedImageData_getDimensions(lua_State *L)
68{
69 CompressedImageData *t = luax_checkcompressedimagedata(L, 1);
70 int miplevel = (int) luaL_optinteger(L, 2, 1);
71 int width = 0, height = 0;
72
73 luax_catchexcept(L, [&]()
74 {
75 width = t->getWidth(miplevel - 1);
76 height = t->getHeight(miplevel - 1);
77 });
78
79 lua_pushinteger(L, width);
80 lua_pushinteger(L, height);
81 return 2;
82}
83
84int w_CompressedImageData_getMipmapCount(lua_State *L)
85{
86 CompressedImageData *t = luax_checkcompressedimagedata(L, 1);
87 lua_pushinteger(L, t->getMipmapCount());
88 return 1;
89}
90
91int w_CompressedImageData_getFormat(lua_State *L)
92{
93 CompressedImageData *t = luax_checkcompressedimagedata(L, 1);
94
95 PixelFormat format = t->getFormat();
96 const char *str;
97
98 if (getConstant(format, str))
99 lua_pushstring(L, str);
100 else
101 lua_pushstring(L, "unknown");
102
103 return 1;
104}
105
106static const luaL_Reg w_CompressedImageData_functions[] =
107{
108 { "clone", w_CompressedImageData_clone },
109 { "getWidth", w_CompressedImageData_getWidth },
110 { "getHeight", w_CompressedImageData_getHeight },
111 { "getDimensions", w_CompressedImageData_getDimensions },
112 { "getMipmapCount", w_CompressedImageData_getMipmapCount },
113 { "getFormat", w_CompressedImageData_getFormat },
114 { 0, 0 },
115};
116
117extern "C" int luaopen_compressedimagedata(lua_State *L)
118{
119 int ret = luax_register_type(L, &CompressedImageData::type, data::w_Data_functions, w_CompressedImageData_functions, nullptr);
120 love::data::luax_rundatawrapper(L, CompressedImageData::type);
121 return ret;
122}
123
124} // image
125} // love
126