MCPcopy Create free account
hub / github.com/defold/defold / Image_Load

Function Image_Load

engine/gamesys/src/gamesys/scripts/script_image.cpp:138–202  ·  view source on GitHub ↗

load image from buffer * Load image (PNG or JPEG) from buffer. * * @name image.load * @param buffer [type:string] image data buffer * @param [options] [type:table] An optional table containing parameters for loading the image. Supported entries: * * `premultiply_alpha` * : [type:boolean] True if alpha should be premultiplied into the color components. Defaults to `f

Source from the content-addressed store, hash-verified

136 * ```
137 */
138 int Image_Load(lua_State* L)
139 {
140 int top = lua_gettop(L);
141 luaL_checktype(L, 1, LUA_TSTRING);
142 size_t buffer_len = 0;
143 const char* buffer = lua_tolstring(L, 1, &buffer_len);
144
145 bool premult = false;
146 bool flip_vertically = false;
147
148 if (top >= 2)
149 {
150 // Parse as options table
151 if (lua_istable(L, 2))
152 {
153 lua_pushvalue(L, 2);
154
155 lua_getfield(L, -1, "premultiply_alpha");
156 if (!lua_isnil(L, -1))
157 premult = dmScript::CheckBoolean(L, -1);
158 lua_pop(L, 1);
159
160 lua_getfield(L, -1, "flip_vertically");
161 if (!lua_isnil(L, -1))
162 flip_vertically = dmScript::CheckBoolean(L, -1);
163 lua_pop(L, 1);
164
165 lua_pop(L, 1);
166 }
167 // backwards compatability
168 else
169 {
170 premult = dmScript::CheckBoolean(L, 2);
171 }
172 }
173
174 dmImage::Image image;
175 dmImage::Result r = dmImage::Load(buffer, buffer_len, premult, flip_vertically, &image);
176 if (r == dmImage::RESULT_OK) {
177
178 int bytes_per_pixel = dmImage::BytesPerPixel(image.m_Type);
179 if (bytes_per_pixel == 0) {
180 dmImage::Free(&image);
181 luaL_error(L, "unknown image type %d", image.m_Type);
182 }
183
184 lua_newtable(L);
185
186 PushImageParameters(L, image);
187
188 lua_pushliteral(L, "buffer");
189 lua_pushlstring(L, (const char*) image.m_Buffer, bytes_per_pixel * image.m_Width * image.m_Height);
190 lua_rawset(L, -3);
191
192 dmImage::Free(&image);
193 }
194 else
195 {

Callers

nothing calls this directly

Calls 15

lua_gettopFunction · 0.85
luaL_checktypeFunction · 0.85
lua_tolstringFunction · 0.85
lua_pushvalueFunction · 0.85
lua_getfieldFunction · 0.85
CheckBooleanFunction · 0.85
BytesPerPixelFunction · 0.85
luaL_errorFunction · 0.85
PushImageParametersFunction · 0.85
lua_pushlstringFunction · 0.85
lua_rawsetFunction · 0.85
lua_pushnilFunction · 0.85

Tested by

no test coverage detected