├── .gitignore
├── .gitmodules
├── CMakeLists.txt
├── LICENCE
├── README.md
├── build.bat
├── images
├── lua_qoi.png
└── lua_qoi.qoi
├── lua-qoi.c
└── test
├── all.lua
├── basic.lua
├── circle-no-alpha.lua
├── circle.lua
├── memory.lua
└── multiple.lua
/.gitignore:
--------------------------------------------------------------------------------
1 | build/*.dll
2 | build/*.exp
3 | build/*.lib
4 | build/*.obj
5 | .vscode
6 | build/
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "qoi"]
2 | path = qoi
3 | url = https://github.com/phoboslab/qoi
4 | [submodule "lua-compat-5.3"]
5 | path = lua-compat-5.3
6 | url = https://github.com/keplerproject/lua-compat-5.3
7 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.10)
2 |
3 | project(lua-qoi)
4 |
5 | #find_package(Lua51 REQUIRED)
6 |
7 | # For other Lua versions, replace 5.1 with 5.2, 5.3 or 5.4
8 | find_package(Lua 5.1 EXACT REQUIRED)
9 |
10 | include_directories(${LUA_INCLUDE_DIR})
11 |
12 | #link_directories(${LUA_LIBRARY_DIR})
13 |
14 | #link_libraries(lua51)
15 |
16 | if(MSVC)
17 | add_definitions(-D_CRT_SECURE_NO_WARNINGS)
18 | endif()
19 |
20 | add_library(lua-qoi SHARED lua-qoi.c)
21 | target_link_libraries(lua-qoi ${LUA_LIBRARIES})
--------------------------------------------------------------------------------
/LICENCE:
--------------------------------------------------------------------------------
1 | Copyright 2021 Cr4xy
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Lua-QOI
2 | #
3 |
4 | Lua bindings to the QOI (“Quite OK Image”) library (https://github.com/phoboslab/qoi)
5 |
6 | ## Example
7 | ```lua
8 | local qoi = require "lua-qoi"
9 | local image = qoi.read("hello.qoi")
10 | local x, y = image.width / 2, image.height / 2
11 | image:setPixel(x, y, 255, 255, 255)
12 | image:write("world.qoi")
13 | ```
14 | See more examples in the [test](test) directory.
15 | #
16 | ## Functions
17 | ### **`qoi.read(path, [channels: 0]) -> image`**
18 | Reads a QOI image from the given path. The channels argument is optional and defaults to 0, meaning the channels from the file are used.
19 | ### **`qoi.new(width, height, [channels: 4]) -> image`**
20 | Creates a new QOI image with the given dimensions. The `channels` parameter is optional and defaults to 4.
21 | ### **`qoi.decode(data) -> image`**
22 | Decodes a QOI image from a binary string.
23 | ### **`image:write(path)`**
24 | ### or `qoi.write(image, path)`
25 | Writes a QOI image to the given path.
26 | ### **`image:encode() -> string`**
27 | ### or `qoi.encode(image) -> string`
28 | Encodes a QOI image to a binary string.
29 | ### **`image:getPixel(x, y) -> r, g, b, a`**
30 | ### or `qoi.getPixel(image, x, y) -> r, g, b, a`
31 | Returns the pixel at the given coordinates. The returned values are in the range [0, 255].
32 | ### **`image:setPixel(x, y, r, g, b, [a: 255])`**
33 | ### or `qoi.setPixel(image, x, y, r, g, b, [a: 255])`
34 | Sets the pixel at the given coordinates. The values must be in the range [0, 255]. The `a` parameter is optional and defaults to 255.
35 | #
36 | ## Image properties
37 | ### **`image.width`**
38 | Image width.
39 | ### **`image.height`**
40 | Image height.
41 | ### **`image.channels`**
42 | Image channels.
43 | #
44 | ## Building
45 | To build this library, run:
46 | ### Win32:
47 | ```bash
48 | $ build.bat
49 | ```
50 | > For building for a specific Lua version, change `find_package(Lua 5.1 EXACT REQUIRED)` to what you need in CMakeLists.txt. For example, `find_package(Lua 5.3 EXACT REQUIRED)` for Lua 5.3.
51 | #
52 | ## Testing
53 | To test this library, run:
54 | ```bash
55 | $ cd test
56 | $ lua all.lua
57 | ```
58 | > This library has been tested with Lua 5.1, 5.3 and 5.4.
59 | #
60 | ## License
61 | This library is released under the MIT license.
--------------------------------------------------------------------------------
/build.bat:
--------------------------------------------------------------------------------
1 | mkdir build
2 | pushd build
3 | cmake -A Win32 ..
4 | msbuild /t:Build /p:Configuration=Release lua-qoi.sln
5 | popd
--------------------------------------------------------------------------------
/images/lua_qoi.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Cr4xy/lua-qoi/59a10fa1824427c63d809d24f6d868b6557d7f3e/images/lua_qoi.png
--------------------------------------------------------------------------------
/images/lua_qoi.qoi:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Cr4xy/lua-qoi/59a10fa1824427c63d809d24f6d868b6557d7f3e/images/lua_qoi.qoi
--------------------------------------------------------------------------------
/lua-qoi.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Lua Bindings to the QOI library
3 | * 2021 Cr4xy Initial version
4 | *
5 | * SPDX-License-Identifier: MIT
6 | */
7 | #include
8 | #include
9 | #include
10 | #include "lua-compat-5.3/c-api/compat-5.3.h"
11 |
12 | #define QOI_IMPLEMENTATION
13 | #include "qoi/qoi.h"
14 |
15 | #define boxptr(L, p) (*(void**)(lua_newuserdata(L, sizeof(void*))) = (p))
16 | #define unboxptr(L, i) (*(void**)(lua_touserdata(L, i)))
17 |
18 | #define LUA_QOI_IMAGE_PTR_TYPENAME "qoiImagePtr_handle"
19 |
20 | struct qoiImgData {
21 | qoi_desc desc;
22 | void *data;
23 | };
24 |
25 | typedef struct qoiImgData* qoiImgDataPtr;
26 |
27 | static int typerror(lua_State *L, int narg, const char *tname) {
28 | const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg));
29 | return luaL_argerror(L, narg, msg);
30 | }
31 |
32 | static qoiImgDataPtr getImagePtr(lua_State *L, int i) {
33 | int pop = 0;
34 | if (lua_istable(L, i)) {
35 | lua_getfield(L, i, "__ptr");
36 | pop = 1;
37 | }
38 | void *ud = luaL_checkudata(L, -1, LUA_QOI_IMAGE_PTR_TYPENAME);
39 | if (ud != NULL) {
40 | qoiImgDataPtr im = unboxptr(L, -1);
41 | if (im == NULL) {
42 | lua_pop(L, pop);
43 | luaL_error(L, "attempt to use an invalid " LUA_QOI_IMAGE_PTR_TYPENAME);
44 | return NULL;
45 | }
46 | lua_pop(L, pop);
47 | return im;
48 | }
49 | typerror(L, i, LUA_QOI_IMAGE_PTR_TYPENAME);
50 | lua_pop(L, pop);
51 | return NULL;
52 | }
53 |
54 | static int LqoiImageSetPixel(lua_State *L);
55 | static int LqoiImageGetPixel(lua_State *L);
56 | static int LqoiImageWrite(lua_State *L);
57 | static int LqoiImageEncode(lua_State *L);
58 |
59 | static void pushImagePtr(lua_State *L, qoiImgDataPtr im) {
60 | lua_newtable(L);
61 | lua_pushstring(L, "__ptr");
62 | boxptr(L, im);
63 | luaL_getmetatable(L, LUA_QOI_IMAGE_PTR_TYPENAME);
64 | lua_setmetatable(L, -2);
65 | lua_settable(L, -3);
66 | lua_pushstring(L, "width");
67 | lua_pushnumber(L, im->desc.width);
68 | lua_settable(L, -3);
69 | lua_pushstring(L, "height");
70 | lua_pushnumber(L, im->desc.height);
71 | lua_settable(L, -3);
72 | lua_pushstring(L, "channels");
73 | lua_pushnumber(L, im->desc.channels);
74 | lua_settable(L, -3);
75 |
76 | lua_pushstring(L, "setPixel");
77 | lua_pushcfunction(L, LqoiImageSetPixel);
78 | lua_settable(L, -3);
79 | lua_pushstring(L, "getPixel");
80 | lua_pushcfunction(L, LqoiImageGetPixel);
81 | lua_settable(L, -3);
82 | lua_pushstring(L, "write");
83 | lua_pushcfunction(L, LqoiImageWrite);
84 | lua_settable(L, -3);
85 | lua_pushstring(L, "encode");
86 | lua_pushcfunction(L, LqoiImageEncode);
87 | lua_settable(L, -3);
88 |
89 | luaL_getmetatable(L, LUA_QOI_IMAGE_PTR_TYPENAME);
90 | lua_setmetatable(L, -2);
91 | }
92 |
93 | static int LqoiRead(lua_State *L) {
94 | const char *path = luaL_checkstring(L, 1);
95 | int channels = 0;
96 | if (lua_isnumber(L, 2)) {
97 | channels = lua_tonumber(L, 2);
98 | }
99 | qoi_desc desc;
100 | qoiImgDataPtr img = (qoiImgDataPtr)lua_newuserdata(L, sizeof(struct qoiImgData));
101 | void *rgba_pixels = qoi_read(path, &desc, channels);
102 | if (rgba_pixels == NULL) {
103 | lua_pushnil(L);
104 | return 1;
105 | }
106 | img->desc = desc;
107 | img->data = rgba_pixels;
108 | pushImagePtr(L, img);
109 | return 1;
110 | }
111 |
112 | static int LqoiImageWrite(lua_State *L) {
113 | qoiImgDataPtr img = getImagePtr(L, 1);
114 | const char *path = luaL_checkstring(L, 2);
115 | if (img == NULL) {
116 | lua_pushnil(L);
117 | return 1;
118 | }
119 | if (qoi_write(path, img->data, &img->desc) != 0) {
120 | lua_pushboolean(L, 1);
121 | return 1;
122 | }
123 | lua_pushnil(L);
124 | return 1;
125 | }
126 |
127 | static int LqoiImageEncode(lua_State *L) {
128 | qoiImgDataPtr img = getImagePtr(L, 1);
129 | if (img == NULL) {
130 | lua_pushnil(L);
131 | return 1;
132 | }
133 | size_t len = 0;
134 | void* encoded = qoi_encode(img->data, &img->desc, &len);
135 | lua_pushlstring(L, encoded, len);
136 | return 1;
137 | }
138 |
139 | static int LqoiDecode(lua_State *L) {
140 | const char *data = luaL_checkstring(L, 1);
141 | //#if LUA_VERSION_NUM == 501
142 | //size_t len = lua_objlen(L, 1);
143 | //#else
144 | size_t len = lua_rawlen(L, 1);
145 | //#endif
146 | qoi_desc desc;
147 | qoiImgDataPtr img = (qoiImgDataPtr)lua_newuserdata(L, sizeof(struct qoiImgData));
148 | void *rgba_pixels = qoi_decode(data, len, &desc, 4);
149 | if (rgba_pixels == NULL) {
150 | lua_pushnil(L);
151 | return 1;
152 | }
153 | img->desc = desc;
154 | img->data = rgba_pixels;
155 | pushImagePtr(L, img);
156 | return 1;
157 | }
158 |
159 | static int LqoiNew(lua_State *L) {
160 | int width = luaL_checkinteger(L, 1);
161 | int height = luaL_checkinteger(L, 2);
162 | int channels = 4;
163 | if (lua_isnumber(L, 3)) {
164 | channels = lua_tonumber(L, 3);
165 | }
166 | qoi_desc desc = {
167 | .width = width,
168 | .height = height,
169 | .channels = channels,
170 | .colorspace = QOI_SRGB,
171 | };
172 | qoiImgDataPtr img = (qoiImgDataPtr)lua_newuserdata(L, sizeof(struct qoiImgData));
173 | void *rgba_pixels = malloc(width * height * channels * sizeof(unsigned char));
174 | memset(rgba_pixels, 0, width * height * channels * sizeof(unsigned char));
175 | if (rgba_pixels == NULL) {
176 | lua_pushnil(L);
177 | return 1;
178 | }
179 | img->desc = desc;
180 | img->data = rgba_pixels;
181 | pushImagePtr(L, img);
182 | return 1;
183 | }
184 |
185 | static int LqoiImageDestroy(lua_State *L) {
186 | qoiImgDataPtr img = getImagePtr(L, 1);
187 | if (img == NULL || img->data == NULL) {
188 | return 0;
189 | }
190 | free(img->data);
191 | img->data = NULL;
192 | return 0;
193 | }
194 |
195 | static int LqoiImageToString(lua_State *L) {
196 | qoiImgDataPtr img = getImagePtr(L, 1);
197 | if (img == NULL) {
198 | return 0;
199 | }
200 | lua_pushfstring(L, "qoiImage: %p (%dx%d)", img, img->desc.width, img->desc.height);
201 | return 1;
202 | }
203 |
204 | static int LqoiImageGetPixel(lua_State *L) {
205 | qoiImgDataPtr img = getImagePtr(L, 1);
206 | int x = luaL_checkinteger(L, 2);
207 | int y = luaL_checkinteger(L, 3);
208 | if (img == NULL) {
209 | return 0;
210 | }
211 | unsigned char* rgba_pixels = (unsigned char*)img->data;
212 |
213 | for (int i = 0; i < img->desc.channels; i++) {
214 | lua_pushnumber(L, rgba_pixels[(y * img->desc.width + x) * img->desc.channels + i]);
215 | }
216 | return img->desc.channels;
217 | }
218 |
219 | static int LqoiImageSetPixel(lua_State *L) {
220 | qoiImgDataPtr img = getImagePtr(L, 1);
221 | int x = luaL_checkinteger(L, 2);
222 | int y = luaL_checkinteger(L, 3);
223 | int r = luaL_checkinteger(L, 4);
224 | int g = luaL_checkinteger(L, 5);
225 | int b = luaL_checkinteger(L, 6);
226 | int a = 255;
227 | if (lua_isnumber(L, 7)) {
228 | a = lua_tonumber(L, 7);
229 | }
230 | if (img == NULL) {
231 | return 0;
232 | }
233 | unsigned char* rgba_pixels = (unsigned char*)img->data;
234 | for (int i = 0; i < img->desc.channels; i++) {
235 | rgba_pixels[(y * img->desc.width + x) * img->desc.channels + i] = (i == 0) ? r : (i == 1) ? g : (i == 2) ? b : a;
236 | }
237 | return 0;
238 | }
239 |
240 | static const luaL_Reg LqoiLib[] = {
241 | {"new", LqoiNew},
242 | {"read", LqoiRead},
243 | {"decode", LqoiDecode},
244 | {"encode", LqoiImageEncode},
245 | {"write", LqoiImageWrite},
246 | {"destroy", LqoiImageDestroy},
247 | {"getPixel", LqoiImageGetPixel},
248 | {"setPixel", LqoiImageSetPixel},
249 | { NULL, NULL }
250 | };
251 |
252 | #ifndef QOI_API
253 | #define QOI_API
254 | #endif
255 |
256 | QOI_API __declspec(dllexport) int luaopen_qoi(lua_State *L) {
257 | luaL_newmetatable(L, LUA_QOI_IMAGE_PTR_TYPENAME);
258 | lua_pushliteral(L, "__index");
259 | lua_pushvalue(L, -3);
260 | lua_settable(L, -3);
261 | lua_pushliteral(L, "__tostring");
262 | lua_pushcfunction(L, LqoiImageToString);
263 | lua_settable(L, -3);
264 | lua_pushliteral(L, "__gc");
265 | lua_pushcfunction(L, LqoiImageDestroy);
266 | lua_settable(L, -3);
267 |
268 | luaL_newlib(L, LqoiLib);
269 | return 1;
270 | }
--------------------------------------------------------------------------------
/test/all.lua:
--------------------------------------------------------------------------------
1 | package.cpath = package.cpath .. ";../build/?.dll;../build/Release/?.dll"
2 | require("basic")
3 | require("circle")
4 | require("circle-no-alpha")
5 | require("memory")
6 | require("multiple")
7 | print("All tests successful.")
--------------------------------------------------------------------------------
/test/basic.lua:
--------------------------------------------------------------------------------
1 | local qoi = require "lua-qoi"
2 | local image = qoi.read("../images/lua_qoi.qoi")
3 | local x, y = image.width / 2, image.height / 2
4 | image:setPixel(x, y, 255, 255, 255, 255)
5 | image:write("test.qoi")
6 | os.remove("test.qoi")
7 | print("basic.lua successful.")
--------------------------------------------------------------------------------
/test/circle-no-alpha.lua:
--------------------------------------------------------------------------------
1 | local qoi = require "lua-qoi"
2 | local image = qoi.new(128, 128, 3)
3 | local x, y = image.width / 2, image.height / 2
4 | for a = -math.pi, math.pi, 0.1 do
5 | local xo = math.floor(math.sin(a) * image.width / 2 * 0.9)
6 | local yo = math.floor(math.cos(a) * image.height / 2 * 0.9)
7 | image:setPixel(x + xo, y + yo, 255, 255, 255)
8 | end
9 | qoi.write(image, "circle-no-alpha.qoi")
10 | os.remove("circle-no-alpha.qoi")
11 | print("circle-no-alpha.lua successful.")
--------------------------------------------------------------------------------
/test/circle.lua:
--------------------------------------------------------------------------------
1 | local qoi = require "lua-qoi"
2 | local image = qoi.new(128, 128)
3 | local x, y = image.width / 2, image.height / 2
4 | for a = -math.pi, math.pi, 0.3 do
5 | local xo = math.floor(math.sin(a) * image.width / 2 * 0.9)
6 | local yo = math.floor(math.cos(a) * image.height / 2 * 0.9)
7 | image:setPixel(x + xo, y + yo, 255, 255, 255)
8 |
9 | a = a + 0.1
10 | xo = math.floor(math.sin(a) * image.width / 2 * 0.9)
11 | yo = math.floor(math.cos(a) * image.height / 2 * 0.9)
12 | image:setPixel(x + xo, y + yo, 255, 0, 0, 255)
13 |
14 | a = a + 0.1
15 | xo = math.floor(math.sin(a) * image.width / 2 * 0.9)
16 | yo = math.floor(math.cos(a) * image.height / 2 * 0.9)
17 | image:setPixel(x + xo, y + yo, 255, 255, 0, 128)
18 | end
19 | qoi.write(image, "circle.qoi")
20 | os.remove("circle.qoi")
21 | print("circle.lua successful.")
--------------------------------------------------------------------------------
/test/memory.lua:
--------------------------------------------------------------------------------
1 | local qoi = require "lua-qoi"
2 | local image = qoi.new(128, 128)
3 | local x, y = image.width / 2, image.height / 2
4 | for a = -math.pi, math.pi, 0.1 do
5 | local xo = math.floor(math.sin(a) * image.width / 2 * 0.9)
6 | local yo = math.floor(math.cos(a) * image.height / 2 * 0.9)
7 | image:setPixel(x + xo, y + yo, 255, 255, 255, 255)
8 | end
9 | local bin = qoi.encode(image)
10 | local bin2 = image:encode()
11 | assert(bin == bin2)
12 | --print("encoded image size", #bin)
13 | local image2 = qoi.decode(bin)
14 | --print("decoded image dimensions", image2.width, image2.height)
15 | assert(image2.width == image.width)
16 | assert(image2.height == image.height)
17 | for angle = -math.pi, math.pi, 0.1 do
18 | local xo = math.floor(math.sin(angle) * image.width / 2 * 0.9)
19 | local yo = math.floor(math.cos(angle) * image.height / 2 * 0.9)
20 | local r, g, b, a = image:getPixel(x + xo, y + yo)
21 | assert(r == 255 and g == 255 and b == 255 and a == 255)
22 | end
23 | print("memory.lua successful.")
--------------------------------------------------------------------------------
/test/multiple.lua:
--------------------------------------------------------------------------------
1 | local qoi = require "lua-qoi"
2 | local image1 = qoi.read("../images/lua_qoi.qoi")
3 | local image2 = qoi.new(42, 50, 3)
4 | local image3 = qoi.read("../images/lua_qoi.qoi")
5 | local image4 = qoi.new(20, 20, 4)
6 | --print(image1.width .. "x" .. image1.height .. " " .. image1.channels)
7 | assert(image1.width == 512 and image1.height == 512 and image1.channels == 4)
8 | image1:write("image1.qoi")
9 | --print(image2.width .. "x" .. image2.height .. " " .. image2.channels)
10 | assert(image2.width == 42 and image2.height == 50 and image2.channels == 3)
11 | image2:write("image2.qoi")
12 | --print(image3.width .. "x" .. image3.height .. " " .. image3.channels)
13 | assert(image3.width == 512 and image3.height == 512 and image3.channels == 4)
14 | image3:write("image3.qoi")
15 | --print(image4.width .. "x" .. image4.height .. " " .. image4.channels)
16 | assert(image4.width == 20 and image4.height == 20 and image4.channels == 4)
17 | image4:write("image4.qoi")
18 |
19 | os.remove("image1.qoi")
20 | os.remove("image2.qoi")
21 | os.remove("image3.qoi")
22 | os.remove("image4.qoi")
23 |
24 | print("multiple.lua successful.")
--------------------------------------------------------------------------------