mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
stbi: add a resize_uint8 function for resizing images in memory (#18484)
This commit is contained in:
parent
a3f24caffc
commit
2bedb6ffd7
2634
thirdparty/stb_image/stb_image_resize.h
vendored
Normal file
2634
thirdparty/stb_image/stb_image_resize.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
2
thirdparty/stb_image/stbi.c
vendored
2
thirdparty/stb_image/stbi.c
vendored
@ -1,5 +1,6 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#define STB_IMAGE_RESIZE_IMPLEMENTATION
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
@ -16,6 +17,7 @@ extern void stbi__callback_free(void *ptr);
|
||||
|
||||
#include "stb_image.h"
|
||||
#include "stb_image_write.h"
|
||||
#include "stb_image_resize.h"
|
||||
|
||||
/*
|
||||
void set_png_compression_level(int level);
|
||||
|
@ -32,6 +32,7 @@ fn cb_free(p voidptr) {
|
||||
#flag -I @VEXEROOT/thirdparty/stb_image
|
||||
#include "stb_image.h"
|
||||
#include "stb_image_write.h"
|
||||
#include "stb_image_resize.h"
|
||||
#include "stb_v_header.h"
|
||||
#flag @VEXEROOT/thirdparty/stb_image/stbi.o
|
||||
|
||||
@ -141,6 +142,36 @@ pub fn load_from_memory(buf &u8, bufsize int) !Image {
|
||||
return res
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Resize functions
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
fn C.stbir_resize_uint8(input_pixels &u8, input_w int, input_h int, input_stride_in_bytes int, output_pixels &u8, output_w int, output_h int, output_stride_in_bytes int, num_channels int) int
|
||||
|
||||
// resize_uint8 resizes `img` to dimensions of `output_w` and `output_h`
|
||||
pub fn resize_uint8(img &Image, output_w int, output_h int) !Image {
|
||||
mut res := Image{
|
||||
ok: true
|
||||
ext: img.ext
|
||||
data: 0
|
||||
width: output_w
|
||||
height: output_h
|
||||
nr_channels: img.nr_channels
|
||||
}
|
||||
|
||||
res.data = cb_malloc(usize(output_w * output_h * img.nr_channels))
|
||||
if res.data == 0 {
|
||||
return error('stbi_image failed to resize file')
|
||||
}
|
||||
|
||||
if 0 == C.stbir_resize_uint8(img.data, img.width, img.height, 0, res.data, output_w,
|
||||
output_h, 0, img.nr_channels) {
|
||||
return error('stbi_image failed to resize file')
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Write functions
|
||||
|
@ -42,3 +42,30 @@ fn test_stbi_read_write() {
|
||||
assert 0 == delta
|
||||
os.rm(out_path) or {}
|
||||
}
|
||||
|
||||
fn test_stbi_resize() {
|
||||
vroot := @VEXEROOT
|
||||
path := os.join_path(vroot, 'examples', 'assets', 'logo.png')
|
||||
println('Source path: ${path}')
|
||||
d_s := stbi.load(path) or { panic(err) }
|
||||
println('Image source data:\n ${d_s}')
|
||||
|
||||
new_width, new_height := 100, 100
|
||||
|
||||
d_r := stbi.resize_uint8(d_s, new_width, new_height) or { panic(err) }
|
||||
println('Resized Image source data:\n ${d_s}')
|
||||
|
||||
out_path := os.join_path(tfolder, 'test.png')
|
||||
println('Out path: ${out_path}')
|
||||
stbi.stbi_write_png(out_path, d_r.width, d_r.height, 4, d_r.data, d_r.width * 4) or {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
d_d := stbi.load(out_path) or { panic(err) }
|
||||
println('Image dest data:\n ${d_d}')
|
||||
|
||||
assert d_d.width == new_width
|
||||
assert d_d.height == new_height
|
||||
assert d_d.nr_channels == d_r.nr_channels
|
||||
os.rm(out_path) or {}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user