2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 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
|
|
|
|
|
2022-10-11 15:19:36 +03:00
|
|
|
[if trace_stbi_allocations ?]
|
|
|
|
fn trace_allocation(message string) {
|
|
|
|
eprintln(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
[export: 'stbi__callback_malloc']
|
|
|
|
fn cb_malloc(s usize) voidptr {
|
|
|
|
res := unsafe { malloc(isize(s)) }
|
2022-11-15 16:53:13 +03:00
|
|
|
trace_allocation('> stbi__callback_malloc: ${s} => ${ptr_str(res)}')
|
2022-10-11 15:19:36 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
[export: 'stbi__callback_realloc']
|
|
|
|
fn cb_realloc(p voidptr, s usize) voidptr {
|
|
|
|
res := unsafe { v_realloc(p, isize(s)) }
|
2022-11-15 16:53:13 +03:00
|
|
|
trace_allocation('> stbi__callback_realloc: ${ptr_str(p)} , ${s} => ${ptr_str(res)}')
|
2022-10-11 15:19:36 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
[export: 'stbi__callback_free']
|
|
|
|
fn cb_free(p voidptr) {
|
|
|
|
trace_allocation('> stbi__callback_free: ${ptr_str(p)}')
|
|
|
|
unsafe { free(p) }
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:01:47 +03:00
|
|
|
#flag -I @VEXEROOT/thirdparty/stb_image
|
2019-06-23 15:08:17 +03:00
|
|
|
#include "stb_image.h"
|
2021-12-08 01:31:37 +03:00
|
|
|
#include "stb_image_write.h"
|
|
|
|
#include "stb_v_header.h"
|
2021-04-19 19:01:47 +03:00
|
|
|
#flag @VEXEROOT/thirdparty/stb_image/stbi.o
|
2020-01-26 14:41:43 +03:00
|
|
|
|
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-12-08 01:31:37 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Configuration functions
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
fn C.stbi_set_flip_vertically_on_load(should_flip int)
|
|
|
|
fn C.stbi_flip_vertically_on_write(flag int)
|
|
|
|
fn C.set_png_compression_level(level int)
|
|
|
|
fn C.write_force_png_filter(level int)
|
|
|
|
fn C.write_tga_with_rle(level int)
|
2021-01-31 11:23:43 +03:00
|
|
|
|
2021-12-08 01:31:37 +03:00
|
|
|
pub fn set_flip_vertically_on_load(val bool) {
|
|
|
|
C.stbi_set_flip_vertically_on_load(val)
|
|
|
|
}
|
2021-01-31 11:23:43 +03:00
|
|
|
|
2021-12-08 01:31:37 +03:00
|
|
|
pub fn set_flip_vertically_on_write(val bool) {
|
|
|
|
C.stbi_flip_vertically_on_write(val)
|
|
|
|
}
|
2021-01-31 11:23:43 +03:00
|
|
|
|
2021-12-08 01:31:37 +03:00
|
|
|
// set_png_compression_level set the PNG compression level during the writing process
|
|
|
|
// defaults to 8; set to higher for more compression
|
|
|
|
pub fn set_png_compression_level(level int) {
|
|
|
|
C.set_png_compression_level(level)
|
|
|
|
}
|
2021-01-31 11:23:43 +03:00
|
|
|
|
2021-12-08 01:31:37 +03:00
|
|
|
// write_force_png_filter defaults to -1; set to 0..5 to force a filter mode
|
|
|
|
// the filter algorithms that can be applied before compression. The purpose of these filters is to prepare the image data for optimum compression.
|
|
|
|
// Type Name
|
|
|
|
//
|
|
|
|
// 0 None
|
|
|
|
// 1 Sub
|
|
|
|
// 2 Up
|
|
|
|
// 3 Average
|
|
|
|
// 4 Paeth
|
|
|
|
pub fn write_force_png_filter(level int) {
|
|
|
|
C.write_force_png_filter(level)
|
|
|
|
}
|
|
|
|
|
|
|
|
// stbi_write_tga_with_rle enable/disable the TGA RLE during the writing process
|
|
|
|
// defaults to true; set to false to disable RLE in tga
|
|
|
|
pub fn write_tga_with_rle(flag bool) {
|
|
|
|
C.write_tga_with_rle(if flag { 1 } else { 0 })
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Utility functions
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-15 18:25:45 +03:00
|
|
|
fn C.stbi_image_free(retval_from_stbi_load &u8)
|
2019-11-24 06:27:02 +03:00
|
|
|
|
2021-12-08 01:31:37 +03:00
|
|
|
pub fn (img &Image) free() {
|
|
|
|
C.stbi_image_free(img.data)
|
2020-08-04 02:26:56 +03:00
|
|
|
}
|
|
|
|
|
2021-12-08 01:31:37 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Load functions
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-15 18:25:45 +03:00
|
|
|
fn C.stbi_load(filename &char, x &int, y &int, channels_in_file &int, desired_channels int) &u8
|
|
|
|
fn C.stbi_load_from_file(f voidptr, x &int, y &int, channels_in_file &int, desired_channels int) &u8
|
|
|
|
fn C.stbi_load_from_memory(buffer &u8, len int, x &int, y &int, channels_in_file &int, desired_channels int) &u8
|
2021-12-08 01:31:37 +03:00
|
|
|
|
|
|
|
// load load an image from a path
|
2022-10-16 09:28:57 +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
|
|
|
|
}
|
2021-03-16 01:39:29 +03:00
|
|
|
// flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 }
|
2022-03-13 10:57:34 +03:00
|
|
|
desired_channels := if ext in ['png', 'jpg', 'jpeg'] { 4 } else { 0 }
|
2021-05-08 13:32:29 +03:00
|
|
|
res.data = C.stbi_load(&char(path.str), &res.width, &res.height, &res.nr_channels,
|
|
|
|
desired_channels)
|
2021-03-16 01:39:29 +03:00
|
|
|
if desired_channels == 4 && res.nr_channels == 3 {
|
|
|
|
// Fix an alpha png bug
|
|
|
|
res.nr_channels = 4
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
if isnil(res.data) {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('stbi_image failed to load from "${path}"')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-12-08 01:31:37 +03:00
|
|
|
// load_from_memory load an image from a memory buffer
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn load_from_memory(buf &u8, 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
|
|
|
|
}
|
2022-10-10 10:37:39 +03:00
|
|
|
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,
|
2022-10-10 10:37:39 +03:00
|
|
|
flag)
|
2020-01-09 14:00:39 +03:00
|
|
|
if isnil(res.data) {
|
2021-12-08 01:31:37 +03:00
|
|
|
return error('stbi_image failed to load from memory')
|
2020-01-09 14:00:39 +03:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-12-08 01:31:37 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Write functions
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-15 18:25:45 +03:00
|
|
|
fn C.stbi_write_png(filename &char, w int, h int, comp int, buffer &u8, stride_in_bytes int) int
|
|
|
|
fn C.stbi_write_bmp(filename &char, w int, h int, comp int, buffer &u8) int
|
|
|
|
fn C.stbi_write_tga(filename &char, w int, h int, comp int, buffer &u8) int
|
|
|
|
fn C.stbi_write_jpg(filename &char, w int, h int, comp int, buffer &u8, quality int) int
|
2021-12-08 01:31:37 +03:00
|
|
|
|
|
|
|
// fn C.stbi_write_hdr(filename &char, w int, h int, comp int, buffer &byte) int // buffer &byte => buffer &f32
|
|
|
|
|
|
|
|
// stbi_write_png write on path a PNG file
|
|
|
|
// row_stride_in_bytes is usually equal to: w * comp
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn stbi_write_png(path string, w int, h int, comp int, buf &u8, row_stride_in_bytes int) ! {
|
2021-12-08 01:31:37 +03:00
|
|
|
if 0 == C.stbi_write_png(&char(path.str), w, h, comp, buf, row_stride_in_bytes) {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('stbi_image failed to write png file to "${path}"')
|
2021-12-08 01:31:37 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2021-12-08 01:31:37 +03:00
|
|
|
// stbi_write_png write on path a BMP file
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn stbi_write_bmp(path string, w int, h int, comp int, buf &u8) ! {
|
2021-12-08 01:31:37 +03:00
|
|
|
if 0 == C.stbi_write_bmp(&char(path.str), w, h, comp, buf) {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('stbi_image failed to write bmp file to "${path}"')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 01:31:37 +03:00
|
|
|
// stbi_write_png write on path a TGA file
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn stbi_write_tga(path string, w int, h int, comp int, buf &u8) ! {
|
2021-12-08 01:31:37 +03:00
|
|
|
if 0 == C.stbi_write_tga(&char(path.str), w, h, comp, buf) {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('stbi_image failed to write tga file to "${path}"')
|
2021-12-08 01:31:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stbi_write_png write on path a JPG file
|
2022-05-06 09:10:42 +03:00
|
|
|
// quality select the compression quality of the JPG
|
2021-12-08 01:31:37 +03:00
|
|
|
// quality is between 1 and 100. Higher quality looks better but results in a bigger image.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn stbi_write_jpg(path string, w int, h int, comp int, buf &u8, quality int) ! {
|
2021-12-08 01:31:37 +03:00
|
|
|
if 0 == C.stbi_write_jpg(&char(path.str), w, h, comp, buf, quality) {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('stbi_image failed to write jpg file to "${path}"')
|
2021-12-08 01:31:37 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2021-12-08 01:31:37 +03:00
|
|
|
|
|
|
|
/*
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn stbi_write_hdr(path string, w int, h int, comp int, buf &byte) ! {
|
2021-12-08 01:31:37 +03:00
|
|
|
if 0 == C.stbi_write_hdr(&char(path.str), w , h , comp , buf){
|
|
|
|
return error('stbi_image failed to write hdr file to "$path"')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|