2022-01-04 10:21:08 +01:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-06-23 04:21:30 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
module stbi
|
|
|
|
|
2021-04-19 18:01:47 +02:00
|
|
|
#flag -I @VEXEROOT/thirdparty/stb_image
|
2019-06-23 14:08:17 +02:00
|
|
|
#include "stb_image.h"
|
2021-12-07 23:31:37 +01:00
|
|
|
#include "stb_image_write.h"
|
|
|
|
#include "stb_v_header.h"
|
2021-04-19 18:01:47 +02:00
|
|
|
#flag @VEXEROOT/thirdparty/stb_image/stbi.o
|
2020-01-26 12:41:43 +01:00
|
|
|
|
2019-10-27 12:05:50 +01:00
|
|
|
pub struct Image {
|
2020-05-15 13:15:04 +02:00
|
|
|
pub mut:
|
2019-06-22 20:20:28 +02:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
nr_channels int
|
|
|
|
ok bool
|
|
|
|
data voidptr
|
|
|
|
ext string
|
|
|
|
}
|
|
|
|
|
2021-12-07 23:31:37 +01: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 09:23:43 +01:00
|
|
|
|
2021-12-07 23:31:37 +01:00
|
|
|
pub fn set_flip_vertically_on_load(val bool) {
|
|
|
|
C.stbi_set_flip_vertically_on_load(val)
|
|
|
|
}
|
2021-01-31 09:23:43 +01:00
|
|
|
|
2021-12-07 23:31:37 +01:00
|
|
|
pub fn set_flip_vertically_on_write(val bool) {
|
|
|
|
C.stbi_flip_vertically_on_write(val)
|
|
|
|
}
|
2021-01-31 09:23:43 +01:00
|
|
|
|
2021-12-07 23:31:37 +01: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 09:23:43 +01:00
|
|
|
|
2021-12-07 23:31:37 +01: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 17:25:45 +02:00
|
|
|
fn C.stbi_image_free(retval_from_stbi_load &u8)
|
2019-11-24 04:27:02 +01:00
|
|
|
|
2021-12-07 23:31:37 +01:00
|
|
|
pub fn (img &Image) free() {
|
|
|
|
C.stbi_image_free(img.data)
|
2020-08-04 01:26:56 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 23:31:37 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Load functions
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-15 17:25:45 +02: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-07 23:31:37 +01:00
|
|
|
|
|
|
|
// load load an image from a path
|
2020-08-22 17:09:22 +02:00
|
|
|
pub fn load(path string) ?Image {
|
2020-05-20 11:04:28 +02:00
|
|
|
ext := path.all_after_last('.')
|
2021-01-31 09:23:43 +01:00
|
|
|
mut res := Image{
|
2019-06-22 20:20:28 +02:00
|
|
|
ok: true
|
|
|
|
ext: ext
|
|
|
|
data: 0
|
|
|
|
}
|
2021-03-15 23:39:29 +01:00
|
|
|
// flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 }
|
2022-03-13 08:57:34 +01:00
|
|
|
desired_channels := if ext in ['png', 'jpg', 'jpeg'] { 4 } else { 0 }
|
2021-05-08 12:32:29 +02:00
|
|
|
res.data = C.stbi_load(&char(path.str), &res.width, &res.height, &res.nr_channels,
|
|
|
|
desired_channels)
|
2021-03-15 23:39:29 +01:00
|
|
|
if desired_channels == 4 && res.nr_channels == 3 {
|
|
|
|
// Fix an alpha png bug
|
|
|
|
res.nr_channels = 4
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
if isnil(res.data) {
|
2021-12-07 23:31:37 +01:00
|
|
|
return error('stbi_image failed to load from "$path"')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-12-07 23:31:37 +01:00
|
|
|
// load_from_memory load an image from a memory buffer
|
2022-04-15 17:25:45 +02:00
|
|
|
pub fn load_from_memory(buf &u8, bufsize int) ?Image {
|
2021-01-31 09:23:43 +01:00
|
|
|
mut res := Image{
|
2020-01-09 12:00:39 +01:00
|
|
|
ok: true
|
|
|
|
data: 0
|
|
|
|
}
|
|
|
|
flag := C.STBI_rgb_alpha
|
2021-01-31 09:23:43 +01:00
|
|
|
res.data = C.stbi_load_from_memory(buf, bufsize, &res.width, &res.height, &res.nr_channels,
|
|
|
|
flag)
|
2020-01-09 12:00:39 +01:00
|
|
|
if isnil(res.data) {
|
2021-12-07 23:31:37 +01:00
|
|
|
return error('stbi_image failed to load from memory')
|
2020-01-09 12:00:39 +01:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-12-07 23:31:37 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Write functions
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-15 17:25:45 +02: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-07 23:31:37 +01: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-04-15 17:25:45 +02:00
|
|
|
pub fn stbi_write_png(path string, w int, h int, comp int, buf &u8, row_stride_in_bytes int) ? {
|
2021-12-07 23:31:37 +01:00
|
|
|
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 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 23:31:37 +01:00
|
|
|
// stbi_write_png write on path a BMP file
|
2022-04-15 17:25:45 +02:00
|
|
|
pub fn stbi_write_bmp(path string, w int, h int, comp int, buf &u8) ? {
|
2021-12-07 23:31:37 +01:00
|
|
|
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 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 23:31:37 +01:00
|
|
|
// stbi_write_png write on path a TGA file
|
2022-04-15 17:25:45 +02:00
|
|
|
pub fn stbi_write_tga(path string, w int, h int, comp int, buf &u8) ? {
|
2021-12-07 23:31:37 +01:00
|
|
|
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.
|
2022-04-15 17:25:45 +02:00
|
|
|
pub fn stbi_write_jpg(path string, w int, h int, comp int, buf &u8, quality int) ? {
|
2021-12-07 23:31:37 +01:00
|
|
|
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 20:20:28 +02:00
|
|
|
}
|
2021-12-07 23:31:37 +01: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"')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|