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
|
|
|
|
|
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
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
fn C.stbi_image_free(retval_from_stbi_load &byte)
|
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
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
fn C.stbi_load(filename &char, x &int, y &int, channels_in_file &int, desired_channels int) &byte
|
|
|
|
fn C.stbi_load_from_file(f voidptr, x &int, y &int, channels_in_file &int, desired_channels int) &byte
|
|
|
|
fn C.stbi_load_from_memory(buffer &byte, len int, x &int, y &int, channels_in_file &int, desired_channels int) &byte
|
|
|
|
|
|
|
|
// load load an image from a path
|
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
|
|
|
|
}
|
2021-03-16 01:39:29 +03:00
|
|
|
// flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 }
|
|
|
|
desired_channels := if ext == 'png' { 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) {
|
2021-12-08 01:31:37 +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
|
2021-05-08 13:32:29 +03:00
|
|
|
pub fn load_from_memory(buf &byte, 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) {
|
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
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
fn C.stbi_write_png(filename &char, w int, h int, comp int, buffer &byte, stride_in_bytes int) int
|
|
|
|
fn C.stbi_write_bmp(filename &char, w int, h int, comp int, buffer &byte) int
|
|
|
|
fn C.stbi_write_tga(filename &char, w int, h int, comp int, buffer &byte) int
|
|
|
|
fn C.stbi_write_jpg(filename &char, w int, h int, comp int, buffer &byte, quality int) int
|
|
|
|
|
|
|
|
// 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
|
|
|
|
pub fn stbi_write_png(path string, w int, h int, comp int, buf &byte, row_stride_in_bytes int) ? {
|
|
|
|
if 0 == C.stbi_write_png(&char(path.str), w, h, comp, buf, row_stride_in_bytes) {
|
|
|
|
return error('stbi_image failed to write png 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 BMP file
|
|
|
|
pub fn stbi_write_bmp(path string, w int, h int, comp int, buf &byte) ? {
|
|
|
|
if 0 == C.stbi_write_bmp(&char(path.str), w, h, comp, buf) {
|
|
|
|
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
|
|
|
|
pub fn stbi_write_tga(path string, w int, h int, comp int, buf &byte) ? {
|
|
|
|
if 0 == C.stbi_write_tga(&char(path.str), w, h, comp, buf) {
|
|
|
|
return error('stbi_image failed to write tga file to "$path"')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stbi_write_png write on path a JPG file
|
|
|
|
// quality select teh compression quality of the JPG
|
|
|
|
// quality is between 1 and 100. Higher quality looks better but results in a bigger image.
|
|
|
|
pub fn stbi_write_jpg(path string, w int, h int, comp int, buf &byte, quality int) ? {
|
|
|
|
if 0 == C.stbi_write_jpg(&char(path.str), w, h, comp, buf, quality) {
|
|
|
|
return error('stbi_image failed to write jpg file to "$path"')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2021-12-08 01:31:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
pub fn stbi_write_hdr(path string, w int, h int, comp int, buf &byte) ? {
|
|
|
|
if 0 == C.stbi_write_hdr(&char(path.str), w , h , comp , buf){
|
|
|
|
return error('stbi_image failed to write hdr file to "$path"')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|