From 138da8e13003082424a2b97817c447cae974b6eb Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 16 Mar 2021 01:39:29 +0300 Subject: [PATCH] stbi/sokol: fix png loading bug --- thirdparty/sokol/sokol_gfx.h | 3 +-- vlib/stbi/stbi.v | 9 +++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/thirdparty/sokol/sokol_gfx.h b/thirdparty/sokol/sokol_gfx.h index bbeb80cd08..897257be8d 100644 --- a/thirdparty/sokol/sokol_gfx.h +++ b/thirdparty/sokol/sokol_gfx.h @@ -9490,8 +9490,7 @@ _SOKOL_PRIVATE void _sg_mtl_copy_image_content(const _sg_image_t* img, __unsafe_ for (int slice_index = 0; slice_index < num_slices; slice_index++) { const int mtl_slice_index = (img->cmn.type == SG_IMAGETYPE_CUBE) ? face_index : slice_index; const int slice_offset = slice_index * bytes_per_slice; -// SOKOL_ASSERT((slice_offset + bytes_per_slice) <= (int)content->subimage[face_index][mip_index].size); - if (!((slice_offset + bytes_per_slice) <= (int)content->subimage[face_index][mip_index].size)) continue; + SOKOL_ASSERT((slice_offset + bytes_per_slice) <= (int)content->subimage[face_index][mip_index].size); [mtl_tex replaceRegion:region mipmapLevel:mip_index slice:mtl_slice_index diff --git a/vlib/stbi/stbi.v b/vlib/stbi/stbi.v index 53b4331462..ddf8868962 100644 --- a/vlib/stbi/stbi.v +++ b/vlib/stbi/stbi.v @@ -39,8 +39,13 @@ pub fn load(path string) ?Image { ext: ext data: 0 } - flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 } - res.data = C.stbi_load(path.str, &res.width, &res.height, &res.nr_channels, flag) + // flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 } + desired_channels := if ext == 'png' { 4 } else { 0 } + res.data = C.stbi_load(path.str, &res.width, &res.height, &res.nr_channels, desired_channels) + if desired_channels == 4 && res.nr_channels == 3 { + // Fix an alpha png bug + res.nr_channels = 4 + } if isnil(res.data) { return error('stbi image failed to load from "$path"') }