From e997a6b7afe1fe2d7ee0abab2bfb5525417240b2 Mon Sep 17 00:00:00 2001 From: hgn Date: Sat, 29 Oct 2022 04:50:54 +0100 Subject: [PATCH] update QOI version and texture api --- dep/phoboslab/qoi.h | 48 +++++++----------------- src/vg/vg_tex.h | 91 +++++++++++++++++++++++++++++---------------- 2 files changed, 71 insertions(+), 68 deletions(-) diff --git a/dep/phoboslab/qoi.h b/dep/phoboslab/qoi.h index 988f9ed..6734ac4 100644 --- a/dep/phoboslab/qoi.h +++ b/dep/phoboslab/qoi.h @@ -1,30 +1,10 @@ /* -QOI - The "Quite OK Image" format for fast, lossless image compression - -Dominic Szablewski - https://phoboslab.org - +Copyright (c) 2021, Dominic Szablewski - https://phoboslab.org +SPDX-License-Identifier: MIT --- LICENSE: The MIT License(MIT) - -Copyright(c) 2021 Dominic Szablewski - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files(the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies -of the Software, and to permit persons to whom the Software is furnished to do -so, subject to the following conditions : -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +QOI - The "Quite OK Image" format for fast, lossless image compression -- About @@ -424,13 +404,12 @@ void *qoi_encode(const void *data, const qoi_desc *desc, int *out_len) { channels = desc->channels; for (px_pos = 0; px_pos < px_len; px_pos += channels) { + px.rgba.r = pixels[px_pos + 0]; + px.rgba.g = pixels[px_pos + 1]; + px.rgba.b = pixels[px_pos + 2]; + if (channels == 4) { - px = *(qoi_rgba_t *)(pixels + px_pos); - } - else { - px.rgba.r = pixels[px_pos + 0]; - px.rgba.g = pixels[px_pos + 1]; - px.rgba.b = pixels[px_pos + 2]; + px.rgba.a = pixels[px_pos + 3]; } if (px.v == px_prev.v) { @@ -598,13 +577,12 @@ void *qoi_decode(const void *data, int size, qoi_desc *desc, int channels) { index[QOI_COLOR_HASH(px) % 64] = px; } + pixels[px_pos + 0] = px.rgba.r; + pixels[px_pos + 1] = px.rgba.g; + pixels[px_pos + 2] = px.rgba.b; + if (channels == 4) { - *(qoi_rgba_t*)(pixels + px_pos) = px; - } - else { - pixels[px_pos + 0] = px.rgba.r; - pixels[px_pos + 1] = px.rgba.g; - pixels[px_pos + 2] = px.rgba.b; + pixels[px_pos + 3] = px.rgba.a; } } diff --git a/src/vg/vg_tex.h b/src/vg/vg_tex.h index 7a43450..0896b75 100644 --- a/src/vg/vg_tex.h +++ b/src/vg/vg_tex.h @@ -11,9 +11,20 @@ #define VG_TEXTURE_NEAREST 0x8 #define VG_TEXTURE_ALLOCATED_INTERNAL 0x10 -/* TODO: Update this implementation */ +VG_STATIC void *vg_qoi_malloc( size_t size ) +{ + return vg_linear_alloc( vg_mem.scratch, size ); +} + +VG_STATIC void vg_qoi_free( void *ptr ) +{ + +} + #define QOI_IMPLEMENTATION #define QOI_NO_STDIO +#define QOI_MALLOC(sz) vg_qoi_malloc( sz ) +#define QOI_FREE(p) vg_qoi_free( p ) #include "phoboslab/qoi.h" @@ -77,53 +88,67 @@ static inline void vg_tex2d_clamp(void) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); } -VG_STATIC GLuint vg_tex2d_rgba( const char *path ) +VG_STATIC GLuint vg_tex2d_new(void) { GLuint texture_name; glGenTextures( 1, &texture_name ); glBindTexture( GL_TEXTURE_2D, texture_name ); + return texture_name; +} + +VG_STATIC void vg_tex2d_set_error(void) +{ + u32 tex_err[4] = + { + 0xffff00ff, + 0xff000000, + 0xff000000, + 0xffff00ff + }; + + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, + 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_err ); +} + +VG_STATIC void vg_tex2d_qoi( void *mem, u32 size, const char *name ) +{ + qoi_desc info; + u8 *tex_buffer = qoi_decode( mem, size, &info, 4 ); + + if( tex_buffer ) + { + vg_info( "Texture decoded: [%u %u] %s\n", + info.width, info.height, name ); + + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, info.width, info.height, + 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_buffer ); + + QOI_FREE(tex_buffer); + } + else + { + vg_error( "File size: %u\n", size ); + vg_tex2d_set_error(); + } +} + +VG_STATIC GLuint vg_tex2d_rgba( const char *path ) +{ + GLuint texture_name = vg_tex2d_new(); + vg_linear_clear( vg_mem.scratch ); u32 size; void *file = vg_file_read( vg_mem.scratch, path, &size ); if( file ) { - qoi_desc info; - u8 *tex_buffer = qoi_decode( file, size, &info, 4 ); - - if( tex_buffer ) - { - vg_info( "Texture decoded: [%u %u] %s\n", - info.width, info.height, path ); - - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, info.width, info.height, - 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_buffer ); - - /* TODO: pass through linear_alloc function */ - QOI_FREE(tex_buffer); - } - else - { - vg_error( "File size: %u\n", size ); - goto temp_error; - } + vg_tex2d_qoi( file, size, path ); } else { -temp_error: vg_error( "Loading texture failed (%s)\n", path ); - - u32 tex_err[4] = - { - 0xffff00ff, - 0xff000000, - 0xff000000, - 0xffff00ff - }; - - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, - 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_err ); + vg_tex2d_set_error(); } return texture_name; -- 2.25.1