Redesign image API to be format agnostic (#40)
continuous-integration/drone/push Build is passing Details

pull/42/head
cosmonaut 2023-04-19 06:42:44 +00:00
parent 3f5fe1ff67
commit 74909b49c3
4 changed files with 566 additions and 289 deletions

View File

@ -44,115 +44,45 @@
extern "C" {
#endif /* __cplusplus */
/* Decodes PNG data into raw RGBA8 texture data.
*
* w: Filled with the width of the image.
* h: Filled with the height of the image.
* numChannels: Filled with the number of channels in the image.
*
* Returns a block of memory suitable for use with Refresh_SetTextureData2D.
* Be sure to free the memory with Refresh_Image_FreePNG after use!
*/
REFRESHAPI uint8_t* Refresh_Image_LoadPNGFromFile(
char const *filename,
int32_t *w,
int32_t *h,
int32_t *numChannels
);
/* Image Read API */
/* Decodes PNG data into raw RGBA8 texture data.
/* Decodes image data into raw RGBA8 texture data.
*
* w: Filled with the width of the image.
* h: Filled with the height of the image.
* numChannels: Filled with the number of channels in the image.
* len: Filled with the length of pixel data in bytes.
*
* Returns a block of memory suitable for use with Refresh_SetTextureData2D.
* Be sure to free the memory with Refresh_Image_FreePNG after use!
* Be sure to free the memory with Refresh_Image_Free after use!
*/
REFRESHAPI uint8_t* Refresh_Image_LoadPNGFromMemory(
uint8_t *buffer,
REFRESHAPI uint8_t* Refresh_Image_Load(
uint8_t *bufferPtr,
int32_t bufferLength,
int32_t *w,
int32_t *h,
int32_t *numChannels
int32_t *len
);
/* Frees memory returned by Refresh_Image_LoadPNG functions. (Do NOT free the memory yourself!)
/* Frees memory returned by Refresh_Image_Load. Do NOT free the memory yourself!
*
* mem: A pointer previously returned by Refresh_Image_LoadPNG.
*/
REFRESHAPI void Refresh_Image_FreePNG(uint8_t *mem);
/* Decodes QOI data into raw RGBA8 texture data.
*
* w: Filled with the width of the image.
* h: Filled with the height of the image.
* numChannels: Filled with the number of channels in the image.
*
* Returns a block of memory suitable for use with Refresh_SetTextureData2D.
* Be sure to free the memory with Refresh_Image_FreeQOI after use!
*/
REFRESHAPI uint8_t* Refresh_Image_LoadQOIFromFile(
char const *filename,
int32_t *w,
int32_t *h,
int32_t *numChannels
);
/* Decodes QOI data into raw RGBA8 texture data.
*
* bufferLength: The length of the input buffer to be decoded.
* w: Filled with the width of the image.
* h: Filled with the height of the image.
* numChannels: Filled with the number of channels in the image.
*
* Returns a block of memory suitable for use with Refresh_SetTextureData2D.
* Be sure to free the memory with Refresh_Image_FreeQOI after use!
*/
REFRESHAPI uint8_t* Refresh_Image_LoadQOIFromMemory(
uint8_t *buffer,
int32_t bufferLength,
int32_t *w,
int32_t *h,
int32_t *numChannels
);
/* Frees memory returned by Refresh_Image_LoadQOI functions. (Do NOT free the memory yourself!)
*
* mem: A pointer previously returned by Refresh_Image_LoadQOI.
*/
REFRESHAPI void Refresh_Image_FreeQOI(uint8_t *mem);
REFRESHAPI void Refresh_Image_Free(uint8_t *mem);
/* Image Write API */
/* Encodes 32-bit color data into PNG data.
/* Returns a buffer of PNG encoded from RGBA8 color data.
*
* filename: The filename that the image will be written to.
* w: The width of the PNG data.
* h: The height of the PNG data.
* bgra: Whether the data is in BGRA8 format. Otherwise will assume RBGA8.
* data: The raw color data.
* w: The width of the color data.
* h: The height of the color data.
* len: Filled with the length of PNG data in bytes.
*/
REFRESHAPI void Refresh_Image_SavePNG(
char const *filename,
const char* filename,
uint8_t* data,
int32_t w,
int32_t h,
uint8_t bgra,
uint8_t *data
);
/* Encodes 32-bit color data into PNG data.
*
* filename: The filename that the image will be written to.
* w: The width of the PNG data.
* h: The height of the PNG data.
* data: The raw color data.
*/
REFRESHAPI void Refresh_Image_SaveQOI(
char const *filename,
int32_t w,
int32_t h,
uint8_t *data
int32_t h
);
#ifdef __cplusplus

View File

@ -9639,7 +9639,6 @@ static void VULKAN_INTERNAL_CleanCommandBuffer(
uint32_t i;
VulkanUniformBuffer *uniformBuffer;
DescriptorSetData *descriptorSetData;
VkResult result;
/* Bound uniform buffers are now available */
@ -10599,7 +10598,7 @@ static uint8_t VULKAN_INTERNAL_CreateLogicalDevice(
return 1;
}
static void VULKAN_INTERNAL_LoadEntryPoints()
static void VULKAN_INTERNAL_LoadEntryPoints(void)
{
/* Load Vulkan entry points */
if (SDL_Vulkan_LoadLibrary(NULL) < 0)

View File

@ -76,6 +76,7 @@
/* These are per the Texture2D.FromStream spec */
#define STBI_ONLY_PNG
#define STBI_ONLY_QOI
/* These are per the Texture2D.SaveAs* spec */
#define STBIW_ONLY_PNG
@ -146,12 +147,6 @@ SDL_SIMDRealloc(void *mem, const size_t len)
#endif
#include "stb_image.h"
#define QOI_IMPLEMENTATION
#define QOI_MALLOC SDL_SIMDAlloc
#define QOI_FREE SDL_SIMDFree
#define QOI_ZEROARR SDL_zero
#include "qoi.h"
#define MINIZ_NO_STDIO
#define MINIZ_NO_TIME
#define MINIZ_SDL_MALLOC
@ -191,110 +186,73 @@ static unsigned char* dgibson_stbi_zlib_compress(
/* Image Read API */
uint8_t* Refresh_Image_LoadPNGFromFile(
char const *filename,
int32_t *w,
int32_t *h,
int32_t *numChannels
) {
return stbi_load(filename, w, h, numChannels, STBI_rgb_alpha);
}
uint8_t* Refresh_Image_LoadPNGFromMemory(
uint8_t *buffer,
uint8_t* Refresh_Image_Load(
uint8_t *bufferPtr,
int32_t bufferLength,
int32_t *w,
int32_t *h,
int32_t *numChannels
int32_t *len
) {
return stbi_load_from_memory(buffer, bufferLength, w, h, numChannels, STBI_rgb_alpha);
}
uint8_t* result;
uint8_t* pixels;
int32_t format;
int32_t i;
void Refresh_Image_FreePNG(uint8_t *mem)
result = stbi_load_from_memory(
bufferPtr,
bufferLength,
w,
h,
&format,
STBI_rgb_alpha
);
if (result == NULL)
{
stbi_image_free(mem);
SDL_LogWarn(SDL_LOG_CATEGORY_ERROR, "Image loading failed: %s", stbi_failure_reason());
}
uint8_t *Refresh_Image_LoadQOIFromFile(
char const *filename,
int32_t *w,
int32_t *h,
int32_t *numChannels
) {
qoi_desc desc;
uint8_t *pixels = qoi_read(filename, &desc, 0);
*w = desc.width;
*h = desc.height;
*numChannels = desc.channels;
return pixels;
}
uint8_t* Refresh_Image_LoadQOIFromMemory(
uint8_t *buffer,
int32_t bufferLength,
int32_t *w,
int32_t *h,
int32_t *numChannels
) {
qoi_desc desc;
uint8_t *pixels = qoi_decode(buffer, bufferLength, &desc, 0);
*w = desc.width;
*h = desc.height;
*numChannels = desc.channels;
return pixels;
}
void Refresh_Image_FreeQOI(uint8_t *mem)
/* Ensure that the alpha pixels are... well, actual alpha.
* You think this looks stupid, but be assured: Your paint program is
* almost certainly even stupider.
* -flibit
*/
pixels = result;
*len = (*w) * (*h) *4;
for (i = 0; i < *len; i += 4, pixels += 4)
{
QOI_FREE(mem);
if (pixels[3] == 0)
{
pixels[0] = 0;
pixels[1] = 1;
pixels[2] = 2;
}
}
return result;
}
void Refresh_Image_Free(uint8_t *mem)
{
SDL_SIMDFree(mem);
}
/* Image Write API */
void Refresh_Image_SavePNG(
const char* filename,
uint8_t* data,
int32_t w,
int32_t h,
uint8_t bgra,
uint8_t *data
int32_t h
) {
uint32_t i;
uint8_t *bgraData;
if (bgra)
{
bgraData = SDL_malloc(w * h * 4);
for (i = 0; i < w * h * 4; i += 4)
{
bgraData[i] = data[i + 2];
bgraData[i + 1] = data[i + 1];
bgraData[i + 2] = data[i];
bgraData[i + 3] = data[i + 3];
}
stbi_write_png(filename, w, h, 4, bgraData, w * 4);
SDL_free(bgraData);
}
else
{
stbi_write_png(filename, w, h, 4, data, w * 4);
}
}
void Refresh_Image_SaveQOI(
char const *filename,
int32_t w,
int32_t h,
uint8_t *data
) {
qoi_desc desc;
desc.width = w;
desc.height = h;
desc.channels = 4;
desc.colorspace = QOI_LINEAR;
qoi_write(filename, data, &desc);
stbi_write_png(
filename,
w,
h,
4,
data,
w * 4
);
}
/* vim: set noexpandtab shiftwidth=8 tabstop=8: */

File diff suppressed because it is too large Load Diff