diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index c195e0aeb2..8b7dcb411d 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -272,6 +272,23 @@ pub fn create_image(file string) Image { return img } +pub fn create_image_from_memory(buf byteptr, bufsize int) Image { + stb_img := stbi.load_from_memory(buf, bufsize) + mut img := Image{ + width: stb_img.width + height: stb_img.height + nr_channels: stb_img.nr_channels + ok: stb_img.ok + data: stb_img.data + ext: stb_img.ext + } + return img +} + +pub fn create_image_from_byte_array(b []byte) Image { + return create_image_from_memory(b.data, b.len) +} + pub fn (mut img Image) init_sokol_image() &Image { mut img_desc := C.sg_image_desc{ width: img.width @@ -291,13 +308,6 @@ pub fn (mut img Image) init_sokol_image() &Image { return img } -pub fn create_image_from_memory(buf byteptr) u32 { - // texture := gl.gen_texture() - // img := stbi.load_from_memory(buf) - // img.free() - return 0 // texture -} - pub fn (gg &Context) begin() { if gg.render_text && gg.font_inited { gg.ft.flush() diff --git a/vlib/stbi/stbi.v b/vlib/stbi/stbi.v index 4bc4412b76..ff5fd01a02 100644 --- a/vlib/stbi/stbi.v +++ b/vlib/stbi/stbi.v @@ -38,23 +38,20 @@ pub fn load(path string) Image { flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 } res.data = C.stbi_load(path.str, &res.width, &res.height, &res.nr_channels, flag) if isnil(res.data) { - println('stbi image failed to load') - exit(1) + panic('stbi image failed to load') } return res } -//pub fn load_from_memory(buf []byte) Image { -pub fn load_from_memory(buf byteptr) Image { +pub fn load_from_memory(buf byteptr, bufsize int) Image { mut res := Image { ok: true data: 0 } flag := C.STBI_rgb_alpha - res.data = C.stbi_load_from_memory(buf, 3812, &res.width, &res.height, &res.nr_channels, flag) + res.data = C.stbi_load_from_memory(buf, bufsize, &res.width, &res.height, &res.nr_channels, flag) if isnil(res.data) { - println('stbi image failed to load from memory') - exit(1) + panic('stbi image failed to load from memory') } return res }