Updated Image Loading and Displaying Examples (markdown)

omar
2024-09-30 11:16:58 +02:00
parent 7ea19a1818
commit 15641b9936

@@ -949,25 +949,14 @@ Add at the top of one of your source file:
#include "stb_image.h"
// Simple helper function to load an image into a WebGPU texture with common settings
bool LoadTextureFromMemory(const void * data,
size_t data_size,
WGPUDevice device,
WGPUQueue queue,
WGPUTexture * out_texture,
WGPUTextureView * out_texture_view,
int * out_width,
int * out_height) {
bool LoadTextureFromMemory(const void* data, size_t data_size, WGPUDevice device, WGPUQueue queue, WGPUTexture* out_texture, WGPUTextureView* out_texture_view, int* out_width, int* out_height)
{
// Load image
int image_width = 0;
int image_height = 0;
// Ask stbi to output 4 channels since WebGPU doesn't have 3-channels texture format
const int output_channels = 4;
void * const image_data = stbi_load_from_memory((const unsigned char *)data,
(int)data_size,
&image_width,
&image_height,
NULL,
output_channels);
void* const image_data = stbi_load_from_memory((const unsigned char *)data, (int)data_size, &image_width, &image_height, NULL, output_channels);
if (image_data == NULL)
return false;
@@ -1031,13 +1020,8 @@ bool LoadTextureFromMemory(const void * data,
}
// Open and read a file, then forward to LoadTextureFromMemory()
bool LoadTextureFromFile(const char * file_name,
WGPUDevice device,
WGPUQueue queue,
WGPUTexture * out_texture,
WGPUTextureView * out_texture_view,
int * out_width,
int * out_height) {
bool LoadTextureFromFile(const char* file_name, WGPUDevice device, WGPUQueue queue, WGPUTexture* out_texture, WGPUTextureView* out_texture_view, int* out_width, int* out_height)
{
FILE * f = fopen(file_name, "rb");
if (f == NULL)
return false;
@@ -1048,14 +1032,7 @@ bool LoadTextureFromFile(const char * file_name,
fseek(f, 0, SEEK_SET);
void * file_data = IM_ALLOC(file_size);
fread(file_data, 1, (size_t)file_size, f);
bool ret = LoadTextureFromMemory(file_data,
(size_t)file_size,
device,
queue,
out_texture,
out_texture_view,
out_width,
out_height);
bool ret = LoadTextureFromMemory(file_data, (size_t)file_size, device, queue, out_texture, out_texture_view, out_width, out_height);
IM_FREE(file_data);
return ret;
}
@@ -1070,13 +1047,7 @@ WGPUTexture my_texture = NULL;
WGPUTextureView my_texture_view = NULL;
int my_image_width = 0;
int my_image_height = 0;
bool ret = LoadTextureFromFile("../../MyImage01.jpg",
device,
queue,
&my_texture,
&my_texture_view,
&my_image_width,
&my_image_height);
bool ret = LoadTextureFromFile("../../MyImage01.jpg", device, queue, &my_texture, &my_texture_view, &my_image_width, &my_image_height);
IM_ASSERT(ret);
```