1// Aseprite
2// Copyright (c) 2018-2020 Igara Studio S.A.
3// Copyright (C) 2018 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/script/engine.h"
13#include "app/script/luacpp.h"
14#include "doc/image_spec.h"
15
16namespace app {
17namespace script {
18
19namespace {
20
21doc::ImageSpec ImageSpec_new(lua_State* L, int index)
22{
23 doc::ImageSpec spec(doc::ColorMode::RGB, 1, 1, 0);
24 // Copy other size
25 if (auto spec2 = may_get_obj<doc::ImageSpec>(L, index)) {
26 spec = *spec2;
27 }
28 // Convert { width, height } into a Size
29 else if (lua_istable(L, index)) {
30 if (lua_getfield(L, index, "colorMode") != LUA_TNIL)
31 spec.setColorMode((doc::ColorMode)lua_tointeger(L, -1));
32 lua_pop(L, 1);
33
34 if (lua_getfield(L, index, "width") != LUA_TNIL)
35 spec.setWidth(lua_tointeger(L, -1));
36 lua_pop(L, 1);
37
38 if (lua_getfield(L, index, "height") != LUA_TNIL)
39 spec.setHeight(lua_tointeger(L, -1));
40 lua_pop(L, 1);
41
42 if (lua_getfield(L, index, "transparentColor") != LUA_TNIL)
43 spec.setMaskColor(lua_tointeger(L, -1));
44 lua_pop(L, 1);
45 }
46 return spec;
47}
48
49int ImageSpec_new(lua_State* L)
50{
51 push_obj(L, ImageSpec_new(L, 1));
52 return 1;
53}
54
55int ImageSpec_gc(lua_State* L)
56{
57 get_obj<doc::ImageSpec>(L, 1)->~ImageSpec();
58 return 0;
59}
60
61int ImageSpec_eq(lua_State* L)
62{
63 auto a = get_obj<doc::ImageSpec>(L, 1);
64 auto b = get_obj<doc::ImageSpec>(L, 2);
65 lua_pushboolean(L, *a == *b);
66 return 1;
67}
68
69int ImageSpec_get_colorMode(lua_State* L)
70{
71 const auto spec = get_obj<doc::ImageSpec>(L, 1);
72 lua_pushinteger(L, (int)spec->colorMode());
73 return 1;
74}
75
76int ImageSpec_get_colorSpace(lua_State* L)
77{
78 const auto spec = get_obj<doc::ImageSpec>(L, 1);
79 if (spec->colorSpace())
80 push_color_space(L, *spec->colorSpace());
81 else
82 lua_pushnil(L);
83 return 1;
84}
85
86int ImageSpec_get_width(lua_State* L)
87{
88 const auto spec = get_obj<doc::ImageSpec>(L, 1);
89 lua_pushinteger(L, spec->width());
90 return 1;
91}
92
93int ImageSpec_get_height(lua_State* L)
94{
95 const auto spec = get_obj<doc::ImageSpec>(L, 1);
96 lua_pushinteger(L, spec->height());
97 return 1;
98}
99
100int ImageSpec_get_transparentColor(lua_State* L)
101{
102 const auto spec = get_obj<doc::ImageSpec>(L, 1);
103 lua_pushinteger(L, spec->maskColor());
104 return 1;
105}
106
107int ImageSpec_set_colorMode(lua_State* L)
108{
109 auto spec = get_obj<doc::ImageSpec>(L, 1);
110 spec->setColorMode((doc::ColorMode)lua_tointeger(L, 2));
111 return 0;
112}
113
114int ImageSpec_set_colorSpace(lua_State* L)
115{
116 auto spec = get_obj<doc::ImageSpec>(L, 1);
117 auto cs = get_obj<gfx::ColorSpace>(L, 2);
118 spec->setColorSpace(base::make_ref<gfx::ColorSpace>(*cs));
119 return 0;
120}
121
122int ImageSpec_set_width(lua_State* L)
123{
124 auto spec = get_obj<doc::ImageSpec>(L, 1);
125 spec->setWidth(lua_tointeger(L, 2));
126 return 0;
127}
128
129int ImageSpec_set_height(lua_State* L)
130{
131 auto spec = get_obj<doc::ImageSpec>(L, 1);
132 spec->setHeight(lua_tointeger(L, 2));
133 return 0;
134}
135
136int ImageSpec_set_transparentColor(lua_State* L)
137{
138 auto spec = get_obj<doc::ImageSpec>(L, 1);
139 spec->setMaskColor(lua_tointeger(L, 2));
140 return 0;
141}
142
143const luaL_Reg ImageSpec_methods[] = {
144 { "__gc", ImageSpec_gc },
145 { "__eq", ImageSpec_eq },
146 { nullptr, nullptr }
147};
148
149const Property ImageSpec_properties[] = {
150 { "colorMode", ImageSpec_get_colorMode, ImageSpec_set_colorMode },
151 { "colorSpace", ImageSpec_get_colorSpace, ImageSpec_set_colorSpace },
152 { "width", ImageSpec_get_width, ImageSpec_set_width },
153 { "height", ImageSpec_get_height, ImageSpec_set_height },
154 { "transparentColor", ImageSpec_get_transparentColor, ImageSpec_set_transparentColor },
155 { nullptr, nullptr, nullptr }
156};
157
158} // anonymous namespace
159
160DEF_MTNAME(doc::ImageSpec);
161
162void register_image_spec_class(lua_State* L)
163{
164 using doc::ImageSpec;
165 REG_CLASS(L, ImageSpec);
166 REG_CLASS_NEW(L, ImageSpec);
167 REG_CLASS_PROPERTIES(L, ImageSpec);
168}
169
170doc::ImageSpec convert_args_into_image_spec(lua_State* L, int index)
171{
172 return ImageSpec_new(L, index);
173}
174
175} // namespace script
176} // namespace app
177