2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
module stbi
|
|
|
|
|
2020-01-26 14:41:43 +03:00
|
|
|
#flag -I @VROOT/thirdparty/stb_image
|
2019-06-23 15:08:17 +03:00
|
|
|
#include "stb_image.h"
|
2020-01-26 14:41:43 +03:00
|
|
|
#flag @VROOT/thirdparty/stb_image/stbi.o
|
|
|
|
|
2019-10-27 14:05:50 +03:00
|
|
|
pub struct Image {
|
2020-05-15 14:15:04 +03:00
|
|
|
pub mut:
|
2019-06-22 21:20:28 +03:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
nr_channels int
|
|
|
|
ok bool
|
|
|
|
data voidptr
|
|
|
|
ext string
|
|
|
|
}
|
|
|
|
|
2021-01-31 11:23:43 +03:00
|
|
|
fn C.stbi_load(filename charptr, x &int, y &int, channels_in_file &int, desired_channels int) byteptr
|
|
|
|
|
|
|
|
fn C.stbi_load_from_file(f voidptr, x &int, y &int, channels_in_file &int, desired_channels int) byteptr
|
|
|
|
|
|
|
|
fn C.stbi_load_from_memory(buffer byteptr, len int, x &int, y &int, channels_in_file &int, desired_channels int) byteptr
|
|
|
|
|
|
|
|
fn C.stbi_image_free(retval_from_stbi_load byteptr)
|
|
|
|
|
|
|
|
fn C.stbi_set_flip_vertically_on_load(should_flip int)
|
2019-11-24 06:27:02 +03:00
|
|
|
|
2020-08-04 02:26:56 +03:00
|
|
|
fn init() {
|
|
|
|
set_flip_vertically_on_load(false)
|
|
|
|
}
|
|
|
|
|
2020-08-22 18:09:22 +03:00
|
|
|
pub fn load(path string) ?Image {
|
2020-05-20 12:04:28 +03:00
|
|
|
ext := path.all_after_last('.')
|
2021-01-31 11:23:43 +03:00
|
|
|
mut res := Image{
|
2019-06-22 21:20:28 +03:00
|
|
|
ok: true
|
|
|
|
ext: ext
|
|
|
|
data: 0
|
|
|
|
}
|
2019-08-23 00:00:31 +03:00
|
|
|
flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 }
|
2021-01-31 11:23:43 +03:00
|
|
|
res.data = C.stbi_load(path.str, &res.width, &res.height, &res.nr_channels, flag)
|
2019-06-22 21:20:28 +03:00
|
|
|
if isnil(res.data) {
|
2020-08-22 18:09:22 +03:00
|
|
|
return error('stbi image failed to load from "$path"')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-08-22 18:09:22 +03:00
|
|
|
pub fn load_from_memory(buf byteptr, bufsize int) ?Image {
|
2021-01-31 11:23:43 +03:00
|
|
|
mut res := Image{
|
2020-01-09 14:00:39 +03:00
|
|
|
ok: true
|
|
|
|
data: 0
|
|
|
|
}
|
|
|
|
flag := C.STBI_rgb_alpha
|
2021-01-31 11:23:43 +03:00
|
|
|
res.data = C.stbi_load_from_memory(buf, bufsize, &res.width, &res.height, &res.nr_channels,
|
|
|
|
flag)
|
2020-01-09 14:00:39 +03:00
|
|
|
if isnil(res.data) {
|
2020-08-22 18:09:22 +03:00
|
|
|
return error('stbi image failed to load from memory')
|
2020-01-09 14:00:39 +03:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-11-21 21:07:47 +03:00
|
|
|
pub fn (img &Image) free() {
|
2019-06-22 21:20:28 +03:00
|
|
|
C.stbi_image_free(img.data)
|
|
|
|
}
|
|
|
|
|
2020-07-05 20:28:28 +03:00
|
|
|
/*
|
2019-06-26 18:49:50 +03:00
|
|
|
pub fn (img Image) tex_image_2d() {
|
2019-08-23 00:00:31 +03:00
|
|
|
mut rgb_flag := C.GL_RGB
|
2019-06-22 21:20:28 +03:00
|
|
|
if img.ext == 'png' {
|
2019-08-23 00:00:31 +03:00
|
|
|
rgb_flag = C.GL_RGBA
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-08-23 00:00:31 +03:00
|
|
|
C.glTexImage2D(C.GL_TEXTURE_2D, 0, rgb_flag, img.width, img.height, 0,
|
|
|
|
rgb_flag, C.GL_UNSIGNED_BYTE, img.data)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2020-07-05 20:28:28 +03:00
|
|
|
*/
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2019-06-26 18:49:50 +03:00
|
|
|
pub fn set_flip_vertically_on_load(val bool) {
|
2019-06-22 21:20:28 +03:00
|
|
|
C.stbi_set_flip_vertically_on_load(val)
|
|
|
|
}
|