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

Function Image_LoadBuffer

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

load image from a string into a buffer object * Load image (PNG or JPEG) from a string buffer. * * @name image.load_buffer * @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 in

Source from the content-addressed store, hash-verified

247 *
248 */
249 static int Image_LoadBuffer(lua_State* L)
250 {
251 int top = lua_gettop(L);
252 luaL_checktype(L, 1, LUA_TSTRING);
253 size_t buffer_len = 0;
254 const char* buffer = lua_tolstring(L, 1, &buffer_len);
255
256 bool premult = false;
257 bool flip_vertically = false;
258
259 if (top >= 2)
260 {
261 // Parse as options table
262 if (lua_istable(L, 2))
263 {
264 lua_pushvalue(L, 2);
265
266 lua_getfield(L, -1, "premultiply_alpha");
267 if (!lua_isnil(L, -1))
268 premult = dmScript::CheckBoolean(L, -1);
269 lua_pop(L, 1);
270
271 lua_getfield(L, -1, "flip_vertically");
272 if (!lua_isnil(L, -1))
273 flip_vertically = dmScript::CheckBoolean(L, -1);
274 lua_pop(L, 1);
275
276 lua_pop(L, 1);
277 }
278 // backwards compatability
279 else
280 {
281 premult = dmScript::CheckBoolean(L, 2);
282 }
283 }
284
285 dmImage::Image image;
286 dmImage::Result r = dmImage::Load(buffer, buffer_len, premult, flip_vertically, &image);
287 if (r == dmImage::RESULT_OK) {
288
289 uint8_t bytes_per_pixel = dmImage::BytesPerPixel(image.m_Type);
290 if (bytes_per_pixel == 0) {
291 dmImage::Free(&image);
292 luaL_error(L, "unknown image type %d", image.m_Type);
293 }
294
295 lua_newtable(L);
296
297 PushImageParameters(L, image);
298
299 uint32_t imagesize = image.m_Width * image.m_Height;
300 uint32_t datasize = bytes_per_pixel * imagesize;
301
302 lua_pushliteral(L, "buffer");
303
304 dmBuffer::StreamDeclaration streams_decl[] = {
305 { dmHashString64("data"), dmBuffer::VALUE_TYPE_UINT8, bytes_per_pixel }
306 };

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
PushBufferFunction · 0.85
lua_rawsetFunction · 0.85
lua_pushnilFunction · 0.85

Tested by

no test coverage detected