mirror of
https://github.com/mattiasgustavsson/libs.git
synced 2026-11-30 16:48:22 +01:00
Added more libs
This commit is contained in:
@@ -0,0 +1,315 @@
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
Licensing information can be found at the end of the file.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
array.h - v0.1 - Dynamic array library for C/C++.
|
||||
|
||||
Do this:
|
||||
#define ARRAY_IMPLEMENTATION
|
||||
before you include this file in *one* C/C++ file to create the implementation.
|
||||
*/
|
||||
|
||||
#ifndef array_h
|
||||
#define array_h
|
||||
|
||||
#ifndef ARRAY_BOOL_T
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdbool.h>
|
||||
#define ARRAY_BOOL_T bool
|
||||
#endif
|
||||
|
||||
#define array_t( type ) struct { int count; type* items; }
|
||||
#define array_param_t( type ) void
|
||||
#define array_create( type ) ARRAY_CAST( (void*)internal_array_create( sizeof( type ), NULL ) )
|
||||
#define array_create_memctx( type, memctx ) ARRAY_CAST( (void*)internal_array_create( sizeof( type ), (memctx) ) )
|
||||
#define array_destroy( array ) internal_array_destroy( (struct internal_array_t*) (array) )
|
||||
#define array_add( array, item ) ARRAY_CAST( internal_array_add( (struct internal_array_t*) (array), (void*) (item) ) )
|
||||
#define array_remove( array, index ) internal_array_remove( (struct internal_array_t*) (array), (index) )
|
||||
#define array_remove_ordered( array, index ) internal_array_remove_ordered( (struct internal_array_t*) (array), (index) )
|
||||
#define array_get( array, index, item ) internal_array_get( (struct internal_array_t*) (array), (index), (void*) (item) )
|
||||
#define array_set( array, index, item ) internal_array_set( (struct internal_array_t*) (array), (index), (void*) (item) )
|
||||
#define array_count( array ) internal_array_count( (struct internal_array_t*) (array) )
|
||||
#define array_sort( array, compare ) internal_array_sort( (struct internal_array_t*) (array), (compare) )
|
||||
#define array_bsearch( array, key, compare ) internal_array_bsearch( (struct internal_array_t*) (array), (void*) (key), (compare) )
|
||||
#define array_find( array, item ) internal_array_find( (struct internal_array_t*) (array), (void*) (item) )
|
||||
#define array_item( array, index ) ARRAY_CAST( internal_array_item( (struct internal_array_t*) (array), (index) ) )
|
||||
|
||||
|
||||
// In C, a void* can be implicitly cast to any other kind of pointer, while in C++ you need an explicit cast. In most
|
||||
// cases, the explicit cast works for both C and C++, but if we consider the case where we have nested structs, then
|
||||
// the way you refer to them differs between C and C++ (in C++, `parent_type::nested_type`, in C just `nested_type`).
|
||||
// In addition, with the automatic cast in C, it is possible to use unnamed nested structs and still dynamically
|
||||
// allocate arrays of that type - this would be desirable when the code is compiled from C++ as well.
|
||||
// This VOID_CAST macro allows for automatic cast from void* in C++. In C, it does nothing, but for C++ it uses a
|
||||
// simple template function to define a cast-to-anything operator.
|
||||
// Use like this:
|
||||
// struct {
|
||||
// struct {
|
||||
// int x;
|
||||
// } *nested;
|
||||
// } parent;
|
||||
// parent.nested = VOID_CAST( malloc( sizeof( *parent.nested ) * count ) );
|
||||
//
|
||||
#ifndef ARRAY_CAST
|
||||
#ifdef __cplusplus
|
||||
struct array_cast {
|
||||
inline array_cast( void* x_ ) : x( x_ ) { }
|
||||
inline array_cast( void const* x_ ) : x( (void*) x_ ) { }
|
||||
template< typename T > inline operator T() { return (T)x; } // cast to whatever requested
|
||||
void* x;
|
||||
};
|
||||
#define ARRAY_CAST( x ) array_cast( x )
|
||||
#else
|
||||
#define ARRAY_CAST( x ) x
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
struct internal_array_t;
|
||||
|
||||
struct internal_array_t* internal_array_create( int item_size, void* memctx );
|
||||
void internal_array_destroy( struct internal_array_t* array );
|
||||
void* internal_array_add( struct internal_array_t* array, void* item );
|
||||
void internal_array_remove( struct internal_array_t* array, int index );
|
||||
void internal_array_remove_ordered( struct internal_array_t* array, int index );
|
||||
ARRAY_BOOL_T internal_array_get( struct internal_array_t* array, int index, void* item );
|
||||
ARRAY_BOOL_T internal_array_set( struct internal_array_t* array, int index, void const* item );
|
||||
int internal_array_count( struct internal_array_t* array );
|
||||
void internal_array_sort( struct internal_array_t* array, int (*compare)( void const*, void const* ) );
|
||||
int internal_array_bsearch( struct internal_array_t* array, void* key, int (*compare)( void const*, void const* ) );
|
||||
int internal_array_find( struct internal_array_t* array, void* item );
|
||||
void* internal_array_item( struct internal_array_t* array, int index );
|
||||
|
||||
#endif /* array_h */
|
||||
|
||||
|
||||
/*
|
||||
----------------------
|
||||
IMPLEMENTATION
|
||||
----------------------
|
||||
*/
|
||||
|
||||
#ifdef ARRAY_IMPLEMENTATION
|
||||
#undef ARRAY_IMPLEMENTATION
|
||||
|
||||
|
||||
#ifndef ARRAY_MALLOC
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdlib.h>
|
||||
#define ARRAY_MALLOC( ctx, size ) ( malloc( size ) )
|
||||
#define ARRAY_FREE( ctx, ptr ) ( free( ptr ) )
|
||||
#endif
|
||||
|
||||
#ifndef ARRAY_MEMCPY
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <string.h>
|
||||
#define ARRAY_MEMCPY( dst, src, cnt ) ( memcpy( (dst), (src), (cnt) ) )
|
||||
#endif
|
||||
|
||||
#ifndef ARRAY_MEMMOVE
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <string.h>
|
||||
#define ARRAY_MEMMOVE( dst, src, cnt ) ( memcpy( (dst), (src), (cnt) ) )
|
||||
#endif
|
||||
|
||||
#ifndef ARRAY_MEMCMP
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <string.h>
|
||||
#define ARRAY_MEMCMP( a, b, cnt ) ( memcmp( (a), (b), (cnt) ) )
|
||||
#endif
|
||||
|
||||
#ifndef ARRAY_QSORT
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdlib.h>
|
||||
#define ARRAY_QSORT( base, num, size, cmp ) ( qsort( (base), (num), (size), (cmp) ) )
|
||||
#endif
|
||||
|
||||
#ifndef ARRAY_BSEARCH
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdlib.h>
|
||||
#define ARRAY_BSEARCH( key, base, num, size, cmp ) ( bsearch( (key), (base), (num), (size), (cmp) ) )
|
||||
#endif
|
||||
|
||||
|
||||
struct internal_array_t {
|
||||
int count;
|
||||
void* items;
|
||||
void* memctx;
|
||||
int item_size;
|
||||
int capacity;
|
||||
};
|
||||
|
||||
|
||||
struct internal_array_t* internal_array_create( int item_size, void* memctx ) {
|
||||
struct internal_array_t* array = (struct internal_array_t*) ARRAY_MALLOC( memctx, sizeof( struct internal_array_t ) );
|
||||
array->memctx = memctx;
|
||||
array->item_size = item_size;
|
||||
array->capacity = 256;
|
||||
array->count = 0;
|
||||
array->items = ARRAY_MALLOC( memctx, (size_t) array->capacity * item_size );
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
void internal_array_destroy( struct internal_array_t* array ) {
|
||||
ARRAY_FREE( array->memctx, array->items );
|
||||
ARRAY_FREE( array->memctx, array );
|
||||
}
|
||||
|
||||
|
||||
void* internal_array_add( struct internal_array_t* array, void* item ) {
|
||||
if( array->count >= array->capacity ) {
|
||||
array->capacity *= 2;
|
||||
void* items = array->items;
|
||||
array->items = ARRAY_MALLOC( array->memctx, (size_t) array->capacity * array->item_size );
|
||||
ARRAY_MEMCPY( array->items, items, (size_t)array->count * array->item_size );
|
||||
ARRAY_FREE( array->memctx, items );
|
||||
}
|
||||
ARRAY_MEMCPY( (void*)( ( (uintptr_t) array->items ) + array->count * array->item_size ), item,
|
||||
(size_t)array->item_size );
|
||||
++array->count;
|
||||
return (void*)( ( (uintptr_t) array->items ) + ( array->count - 1 ) * array->item_size );
|
||||
}
|
||||
|
||||
|
||||
void internal_array_remove( struct internal_array_t* array, int index ) {
|
||||
if( index >= 0 && index < array->count ) {
|
||||
--array->count;
|
||||
ARRAY_MEMMOVE( (void*)( ( (uintptr_t) array->items ) + index * array->item_size ),
|
||||
(void*)( ( (uintptr_t) array->items ) + array->count * array->item_size ), (size_t) array->item_size );
|
||||
}
|
||||
}
|
||||
|
||||
void internal_array_remove_ordered( struct internal_array_t* array, int index ) {
|
||||
if( index >= 0 && index < array->count ) {
|
||||
--array->count;
|
||||
ARRAY_MEMMOVE( (void*)( ( (uintptr_t) array->items ) + index * array->item_size ),
|
||||
(void*)( ( (uintptr_t) array->items ) + ( index + 1 ) * array->item_size ),
|
||||
(size_t)array->item_size * ( array->count - index ) );
|
||||
}
|
||||
}
|
||||
|
||||
ARRAY_BOOL_T internal_array_get( struct internal_array_t* array, int index, void* item ) {
|
||||
ARRAY_BOOL_T result = index >= 0 && index < array->count;
|
||||
if( result ) {
|
||||
ARRAY_MEMCPY( item, (void*)( ( (uintptr_t) array->items ) + index * array->item_size ),
|
||||
(size_t) array->item_size );
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
ARRAY_BOOL_T internal_array_set( struct internal_array_t* array, int index, void const* item ) {
|
||||
ARRAY_BOOL_T result = index >= 0 && index < array->count;
|
||||
if( result ) {
|
||||
ARRAY_MEMCPY( (void*)( ( (uintptr_t) array->items ) + index * array->item_size ), item,
|
||||
(size_t) array->item_size );
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int internal_array_count( struct internal_array_t* array ) {
|
||||
int count = array->count;
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
void internal_array_sort( struct internal_array_t* array, int (*compare)( void const*, void const* ) ) {
|
||||
ARRAY_QSORT( array->items, (size_t) array->count, (size_t) array->item_size, compare );
|
||||
}
|
||||
|
||||
|
||||
int internal_array_bsearch( struct internal_array_t* array, void* key, int (*compare)( void const*, void const* ) ) {
|
||||
void* item = ARRAY_BSEARCH( key, array->items, (size_t) array->count, (size_t) array->item_size, compare );
|
||||
int result = -1;
|
||||
if( item ) {
|
||||
result = (int)( ( ((uintptr_t)item) - ((uintptr_t)array->items) ) / array->item_size );
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
int internal_array_find( struct internal_array_t* array, void* item ) {
|
||||
for( int i = 0; i < array->count; ++i ) {
|
||||
if( ARRAY_MEMCMP( (void*)( ( (uintptr_t) array->items ) + i * array->item_size ), item,
|
||||
(size_t) array->item_size) == 0 ) {
|
||||
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
void* internal_array_item( struct internal_array_t* array, int index ) {
|
||||
if( index >= 0 && index < array->count ) {
|
||||
return (void*)( ( (uintptr_t) array->items ) + index * array->item_size );
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* ARRAY_IMPLEMENTATION */
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This software is available under 2 licenses - you may choose the one you like.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE A - MIT License
|
||||
|
||||
Copyright (c) 2022 Mattias Gustavsson
|
||||
|
||||
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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||
software, either in source code form or as a compiled binary, for any purpose,
|
||||
commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||
software dedicate any and all copyright interest in the software to the public
|
||||
domain. We make this dedication for the benefit of the public at large and to
|
||||
the detriment of our heirs and successors. We intend this dedication to be an
|
||||
overt act of relinquishment in perpetuity of all present and future rights to
|
||||
this software under copyright law.
|
||||
|
||||
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 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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -0,0 +1,437 @@
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
Licensing information can be found at the end of the file.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
buffer.h - v0.1 - Memory buffer with read/write operations, for C/C++.
|
||||
|
||||
Do this:
|
||||
#define BUFFER_IMPLEMENTATION
|
||||
before you include this file in *one* C/C++ file to create the implementation.
|
||||
*/
|
||||
|
||||
#ifndef buffer_h
|
||||
#define buffer_h
|
||||
|
||||
// If you want buffer to swap endianness on read/write, do this before include: #define BUFFER_BIG_ENDIAN
|
||||
|
||||
#ifndef BUFFER_I8_T
|
||||
#define BUFFER_I8_T char
|
||||
#endif
|
||||
|
||||
#ifndef BUFFER_I16_T
|
||||
#define BUFFER_I16_T short
|
||||
#endif
|
||||
|
||||
#ifndef BUFFER_I32_T
|
||||
#define BUFFER_I32_T int
|
||||
#endif
|
||||
|
||||
#ifndef BUFFER_I64_T
|
||||
#define BUFFER_I64_T long long
|
||||
#endif
|
||||
|
||||
#ifndef BUFFER_U8_T
|
||||
#define BUFFER_U8_T unsigned char
|
||||
#endif
|
||||
|
||||
#ifndef BUFFER_U16_T
|
||||
#define BUFFER_U16_T unsigned short
|
||||
#endif
|
||||
|
||||
#ifndef BUFFER_U32_T
|
||||
#define BUFFER_U32_T unsigned int
|
||||
#endif
|
||||
|
||||
#ifndef BUFFER_U64_T
|
||||
#define BUFFER_U64_T unsigned long long
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct buffer_t buffer_t;
|
||||
|
||||
buffer_t* buffer_create( void );
|
||||
buffer_t* buffer_load( const char* filename );
|
||||
buffer_t* buffer_map( void* data, size_t size );
|
||||
void buffer_destroy( buffer_t* buffer );
|
||||
bool buffer_save( buffer_t* buffer, char const* filename );
|
||||
void buffer_resize( buffer_t* buffer, size_t size );
|
||||
size_t buffer_position( buffer_t* buffer );
|
||||
size_t buffer_position_set( buffer_t* buffer, size_t position );
|
||||
size_t buffer_size( buffer_t* buffer );
|
||||
void* buffer_data( buffer_t* buffer );
|
||||
|
||||
int buffer_read_char( buffer_t* buffer, char* value, int count );
|
||||
int buffer_read_i8( buffer_t* buffer, BUFFER_I8_T* value, int count );
|
||||
int buffer_read_i16( buffer_t* buffer, BUFFER_I16_T* value, int count );
|
||||
int buffer_read_i32( buffer_t* buffer, BUFFER_I32_T* value, int count );
|
||||
int buffer_read_i64( buffer_t* buffer, BUFFER_I64_T* value, int count );
|
||||
int buffer_read_u8( buffer_t* buffer, BUFFER_U8_T* value, int count );
|
||||
int buffer_read_u16( buffer_t* buffer, BUFFER_U16_T* value, int count );
|
||||
int buffer_read_u32( buffer_t* buffer, BUFFER_U32_T* value, int count );
|
||||
int buffer_read_u64( buffer_t* buffer, BUFFER_U64_T* value, int count );
|
||||
int buffer_read_float( buffer_t* buffer, float* value, int count );
|
||||
int buffer_read_double( buffer_t* buffer, double* value, int count );
|
||||
int buffer_read_bool( buffer_t* buffer, bool* value, int count );
|
||||
|
||||
int buffer_write_char( buffer_t* buffer, char const* value, int count );
|
||||
int buffer_write_i8( buffer_t* buffer, BUFFER_I8_T const* value, int count );
|
||||
int buffer_write_i16( buffer_t* buffer, BUFFER_I16_T const* value, int count );
|
||||
int buffer_write_i32( buffer_t* buffer, BUFFER_I32_T const* value, int count );
|
||||
int buffer_write_i64( buffer_t* buffer, BUFFER_I64_T const* value, int count );
|
||||
int buffer_write_u8( buffer_t* buffer, BUFFER_U8_T const* value, int count );
|
||||
int buffer_write_u16( buffer_t* buffer, BUFFER_U16_T const* value, int count );
|
||||
int buffer_write_u32( buffer_t* buffer, BUFFER_U32_T const* value, int count );
|
||||
int buffer_write_u64( buffer_t* buffer, BUFFER_U64_T const* value, int count );
|
||||
int buffer_write_float( buffer_t* buffer, float const* value, int count );
|
||||
int buffer_write_double( buffer_t* buffer, double const* value, int count );
|
||||
int buffer_write_bool( buffer_t* buffer, bool const* value, int count );
|
||||
|
||||
#endif /* buffer_h */
|
||||
|
||||
|
||||
#ifdef BUFFER_IMPLEMENTATION
|
||||
#undef BUFFER_IMPLEMENTATION
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static BUFFER_U32_T buffer_pow2ceil( BUFFER_U32_T v ) {
|
||||
--v;
|
||||
v |= v >> 1;
|
||||
v |= v >> 2;
|
||||
v |= v >> 4;
|
||||
v |= v >> 8;
|
||||
v |= v >> 16;
|
||||
++v;
|
||||
v += ( v == 0 );
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
struct buffer_t {
|
||||
size_t capacity;
|
||||
size_t size;
|
||||
size_t position;
|
||||
void* data;
|
||||
int is_mapped;
|
||||
};
|
||||
|
||||
|
||||
buffer_t* buffer_create( void ) {
|
||||
buffer_t* buffer = (buffer_t*) malloc( sizeof( buffer_t) );
|
||||
buffer->capacity = 4096;
|
||||
buffer->size = 0;
|
||||
buffer->position = 0;
|
||||
buffer->data = malloc( buffer->capacity );
|
||||
buffer->is_mapped = 0;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
buffer_t* buffer_load( const char* filename ) {
|
||||
FILE* fp = fopen( filename, "rb" );
|
||||
if( !fp ) {
|
||||
return NULL;
|
||||
}
|
||||
fseek( fp, 0, SEEK_END );
|
||||
int size = (int) ftell( fp );
|
||||
fseek( fp, 0, SEEK_SET );
|
||||
if( size == 0 ) {
|
||||
fclose( fp );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int pow2size = (int)buffer_pow2ceil( (BUFFER_U32_T) size );
|
||||
void* data = malloc( pow2size );
|
||||
int read = (int) fread( data, 1, size, fp );
|
||||
fclose( fp );
|
||||
if( read != size ) {
|
||||
free( data );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buffer_t* buffer = (buffer_t*) malloc( sizeof( buffer_t) );
|
||||
buffer->capacity = pow2size;
|
||||
buffer->size = size;
|
||||
buffer->position = 0;
|
||||
buffer->data = data;
|
||||
buffer->is_mapped = 0;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
buffer_t* buffer_map( void* data, size_t size ) {
|
||||
if( !data ) {
|
||||
return NULL;
|
||||
}
|
||||
buffer_t* buffer = (buffer_t*) malloc( sizeof( buffer_t) );
|
||||
buffer->capacity = size;
|
||||
buffer->size = size;
|
||||
buffer->position = 0;
|
||||
buffer->data = data;
|
||||
buffer->is_mapped = 1;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
void buffer_destroy( buffer_t* buffer ) {
|
||||
if( !buffer->is_mapped ) {
|
||||
free( buffer->data );
|
||||
}
|
||||
free( buffer );
|
||||
}
|
||||
|
||||
|
||||
bool buffer_save( buffer_t* buffer, char const* filename ) {
|
||||
FILE* fp = fopen( filename, "wb" );
|
||||
if( !fp ) {
|
||||
return false;
|
||||
}
|
||||
size_t written = fwrite( buffer->data, 1, buffer->size, fp );
|
||||
fclose( fp );
|
||||
return written == buffer->size;
|
||||
}
|
||||
|
||||
|
||||
void buffer_resize( buffer_t* buffer, size_t size ) {
|
||||
if( buffer->is_mapped ) {
|
||||
return;
|
||||
}
|
||||
if( size > buffer->capacity ) {
|
||||
while( size > buffer->capacity ) {
|
||||
buffer->capacity *= 2;
|
||||
}
|
||||
buffer->data = realloc( buffer->data, buffer->capacity );
|
||||
}
|
||||
buffer->size = size;
|
||||
if( buffer->position > size ) {
|
||||
buffer->position = size;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
size_t buffer_position( buffer_t* buffer ) {
|
||||
size_t result = buffer->position;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
size_t buffer_position_set( buffer_t* buffer, size_t position ) {
|
||||
buffer->position = position > buffer->size ? buffer->size : position;
|
||||
size_t result = buffer->position;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
size_t buffer_size( buffer_t* buffer ) {
|
||||
size_t result = buffer->size;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void* buffer_data( buffer_t* buffer ) {
|
||||
return buffer->data;
|
||||
}
|
||||
|
||||
#ifndef BUFFER_BIG_ENDIAN
|
||||
#define BUFFER_READ_IMPL \
|
||||
{ \
|
||||
int result = 0; \
|
||||
for( int i = 0; i < count; ++i ) { \
|
||||
if( buffer->position + sizeof( *value ) > buffer->size ) { \
|
||||
return result; \
|
||||
} \
|
||||
memcpy( &value[ i ], (void*)( ( (uintptr_t) buffer->data ) + buffer->position ), sizeof( *value ) ); \
|
||||
buffer->position += sizeof( *value ); \
|
||||
++result; \
|
||||
} \
|
||||
return result; \
|
||||
}
|
||||
#else
|
||||
#define BUFFER_READ_IMPL \
|
||||
{ \
|
||||
int result = 0; \
|
||||
for( int i = 0; i < count; ++i ) { \
|
||||
if( buffer->position + sizeof( *value ) > buffer->size ) { \
|
||||
return result; \
|
||||
} \
|
||||
void* x = &value[ i ]; \
|
||||
memcpy( x, (void*)( ( (uintptr_t) buffer->data ) + buffer->position ), sizeof( *value ) ); \
|
||||
if( sizeof(*value) == 2 ) \
|
||||
*(BUFFER_U16_T*)(x) = \
|
||||
((((*(BUFFER_U16_T*)(x)) & 0xFF00) >> 8) | \
|
||||
(((*(BUFFER_U16_T*)(x)) & 0x00FF) << 8)); \
|
||||
else if( sizeof(*value) == 4 ) \
|
||||
*(BUFFER_U32_T*)(x) = \
|
||||
((((*(BUFFER_U32_T*)(x)) & 0xFF000000) >> 24) | \
|
||||
(((*(BUFFER_U32_T*)(x)) & 0x00FF0000) >> 8) | \
|
||||
(((*(BUFFER_U32_T*)(x)) & 0x0000FF00) << 8) | \
|
||||
(((*(BUFFER_U32_T*)(x)) & 0x000000FF) << 24) ); \
|
||||
else if( sizeof(*value) == 8 ) \
|
||||
*(BUFFER_U64_T*)(x) = \
|
||||
((((*(BUFFER_U64_T*)(x)) & 0xFF00000000000000) >> 56 ) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x00FF000000000000) >> 40 ) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x0000FF0000000000) >> 24 ) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x000000FF00000000) >> 8 ) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x00000000FF000000) << 8) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x0000000000FF0000) << 24) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x000000000000FF00) << 40) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x00000000000000FF) << 56)); \
|
||||
buffer->position += sizeof( *value ); \
|
||||
++result; \
|
||||
} \
|
||||
return result; \
|
||||
}
|
||||
#endif
|
||||
|
||||
int buffer_read_char( buffer_t* buffer, char* value, int count ) BUFFER_READ_IMPL
|
||||
int buffer_read_i8( buffer_t* buffer, BUFFER_I8_T* value, int count ) BUFFER_READ_IMPL
|
||||
int buffer_read_i16( buffer_t* buffer, BUFFER_I16_T* value, int count ) BUFFER_READ_IMPL
|
||||
int buffer_read_i32( buffer_t* buffer, BUFFER_I32_T* value, int count ) BUFFER_READ_IMPL
|
||||
int buffer_read_i64( buffer_t* buffer, BUFFER_I64_T* value, int count ) BUFFER_READ_IMPL
|
||||
int buffer_read_u8( buffer_t* buffer, BUFFER_U8_T* value, int count ) BUFFER_READ_IMPL
|
||||
int buffer_read_u16( buffer_t* buffer, BUFFER_U16_T* value, int count ) BUFFER_READ_IMPL
|
||||
int buffer_read_u32( buffer_t* buffer, BUFFER_U32_T* value, int count ) BUFFER_READ_IMPL
|
||||
int buffer_read_u64( buffer_t* buffer, BUFFER_U64_T* value, int count ) BUFFER_READ_IMPL
|
||||
int buffer_read_float( buffer_t* buffer, float* value, int count ) BUFFER_READ_IMPL
|
||||
int buffer_read_double( buffer_t* buffer, double* value, int count ) BUFFER_READ_IMPL
|
||||
int buffer_read_bool( buffer_t* buffer, bool* value, int count ) BUFFER_READ_IMPL
|
||||
|
||||
#undef BUFFER_READ_IMPL
|
||||
|
||||
|
||||
#ifndef BUFFER_BIG_ENDIAN
|
||||
#define BUFFER_WRITE_IMPL \
|
||||
{ \
|
||||
int result = 0; \
|
||||
for( int i = 0; i < count; ++i ) { \
|
||||
if( buffer->position + sizeof( *value ) > buffer->size ) { \
|
||||
if( buffer->is_mapped ) break; \
|
||||
buffer->size = buffer->position + sizeof( *value ); \
|
||||
while( buffer->size > buffer->capacity ) { \
|
||||
buffer->capacity *= 2; \
|
||||
} \
|
||||
buffer->data = realloc( buffer->data, buffer->capacity ); \
|
||||
} \
|
||||
memcpy( (void*)( ( (uintptr_t) buffer->data ) + buffer->position ), &value[ i ], sizeof( *value ) ); \
|
||||
buffer->position += sizeof( *value ); \
|
||||
++result; \
|
||||
} \
|
||||
return result; \
|
||||
}
|
||||
#else
|
||||
#define BUFFER_WRITE_IMPL \
|
||||
{ \
|
||||
int result = 0; \
|
||||
for( int i = 0; i < count; ++i ) { \
|
||||
if( buffer->position + sizeof( *value ) > buffer->size ) { \
|
||||
if( buffer->is_mapped ) break; \
|
||||
buffer->size = buffer->position + sizeof( *value ); \
|
||||
while( buffer->size > buffer->capacity ) { \
|
||||
buffer->capacity *= 2; \
|
||||
} \
|
||||
buffer->data = realloc( buffer->data, buffer->capacity ); \
|
||||
} \
|
||||
void* x = (void*)( ( (uintptr_t) buffer->data ) + buffer->position ); \
|
||||
memcpy( x, &value[ i ], sizeof( *value ) ); \
|
||||
if( sizeof(*value) == 2 ) \
|
||||
*(BUFFER_U16_T*)(x) = \
|
||||
((((*(BUFFER_U16_T*)(x)) & 0xFF00) >> 8) | \
|
||||
(((*(BUFFER_U16_T*)(x)) & 0x00FF) << 8)); \
|
||||
else if( sizeof(*value) == 4 ) \
|
||||
*(BUFFER_U32_T*)(x) = \
|
||||
((((*(BUFFER_U32_T*)(x)) & 0xFF000000) >> 24) | \
|
||||
(((*(BUFFER_U32_T*)(x)) & 0x00FF0000) >> 8) | \
|
||||
(((*(BUFFER_U32_T*)(x)) & 0x0000FF00) << 8) | \
|
||||
(((*(BUFFER_U32_T*)(x)) & 0x000000FF) << 24) ); \
|
||||
else if( sizeof(*value) == 8 ) \
|
||||
*(BUFFER_U64_T*)(x) = \
|
||||
((((*(BUFFER_U64_T*)(x)) & 0xFF00000000000000) >> 56 ) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x00FF000000000000) >> 40 ) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x0000FF0000000000) >> 24 ) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x000000FF00000000) >> 8 ) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x00000000FF000000) << 8) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x0000000000FF0000) << 24) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x000000000000FF00) << 40) | \
|
||||
(((*(BUFFER_U64_T*)(x)) & 0x00000000000000FF) << 56)); \
|
||||
buffer->position += sizeof( *value ); \
|
||||
++result; \
|
||||
} \
|
||||
return result; \
|
||||
}
|
||||
#endif
|
||||
|
||||
int buffer_write_char( buffer_t* buffer, char const* value, int count ) BUFFER_WRITE_IMPL
|
||||
int buffer_write_i8( buffer_t* buffer, BUFFER_I8_T const* value, int count ) BUFFER_WRITE_IMPL
|
||||
int buffer_write_i16( buffer_t* buffer, BUFFER_I16_T const* value, int count ) BUFFER_WRITE_IMPL
|
||||
int buffer_write_i32( buffer_t* buffer, BUFFER_I32_T const* value, int count ) BUFFER_WRITE_IMPL
|
||||
int buffer_write_i64( buffer_t* buffer, BUFFER_I64_T const* value, int count ) BUFFER_WRITE_IMPL
|
||||
int buffer_write_u8( buffer_t* buffer, BUFFER_U8_T const* value, int count ) BUFFER_WRITE_IMPL
|
||||
int buffer_write_u16( buffer_t* buffer, BUFFER_U16_T const* value, int count ) BUFFER_WRITE_IMPL
|
||||
int buffer_write_u32( buffer_t* buffer, BUFFER_U32_T const* value, int count ) BUFFER_WRITE_IMPL
|
||||
int buffer_write_u64( buffer_t* buffer, BUFFER_U64_T const* value, int count ) BUFFER_WRITE_IMPL
|
||||
int buffer_write_float( buffer_t* buffer, float const* value, int count ) BUFFER_WRITE_IMPL
|
||||
int buffer_write_double( buffer_t* buffer, double const* value, int count ) BUFFER_WRITE_IMPL
|
||||
int buffer_write_bool( buffer_t* buffer, bool const* value, int count ) BUFFER_WRITE_IMPL
|
||||
|
||||
#undef BUFFER_WRITE_IMPL
|
||||
|
||||
#endif /* BUFFER_IMPLEMENTATION */
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This software is available under 2 licenses - you may choose the one you like.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE A - MIT License
|
||||
|
||||
Copyright (c) 2022 Mattias Gustavsson
|
||||
|
||||
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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||
software, either in source code form or as a compiled binary, for any purpose,
|
||||
commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||
software dedicate any and all copyright interest in the software to the public
|
||||
domain. We make this dedication for the benefit of the public at large and to
|
||||
the detriment of our heirs and successors. We intend this dedication to be an
|
||||
overt act of relinquishment in perpetuity of all present and future rights to
|
||||
this software under copyright law.
|
||||
|
||||
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 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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -0,0 +1,295 @@
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
Licensing information can be found at the end of the dir.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
dir.h - v0.1 - Directory listing functions for C/C++
|
||||
|
||||
Do this:
|
||||
#define DIR_IMPLEMENTATION
|
||||
before you include this file in *one* C/C++ dir to create the implementation.
|
||||
*/
|
||||
|
||||
#ifndef dir_h
|
||||
#define dir_h
|
||||
|
||||
typedef struct dir_t dir_t;
|
||||
typedef struct dir_entry_t dir_entry_t;
|
||||
|
||||
dir_t* dir_open( char const* path );
|
||||
void dir_close( dir_t* dir );
|
||||
char const* dir_path( dir_t* dir );
|
||||
dir_entry_t* dir_read( dir_t* dir );
|
||||
|
||||
char const* dir_name( dir_entry_t* entry );
|
||||
int dir_is_file( dir_entry_t* entry );
|
||||
int dir_is_folder( dir_entry_t* entry );
|
||||
|
||||
#endif /* dir_h */
|
||||
|
||||
/*
|
||||
----------------------
|
||||
IMPLEMENTATION
|
||||
----------------------
|
||||
*/
|
||||
|
||||
#ifdef DIR_IMPLEMENTATION
|
||||
#undef DIR_IMPLEMENTATION
|
||||
|
||||
#ifndef DIR_MALLOC
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdlib.h>
|
||||
#if defined(__cplusplus)
|
||||
#define DIR_MALLOC( size ) ( ::malloc( size ) )
|
||||
#define DIR_FREE( ptr ) ( ::free( ptr ) )
|
||||
#else
|
||||
#define DIR_MALLOC( size ) ( malloc( size ) )
|
||||
#define DIR_FREE( ptr ) ( free( ptr ) )
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// WINDOWS
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if defined( DIR_WINDOWS )
|
||||
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#if !defined( _WIN32_WINNT ) || _WIN32_WINNT < 0x0501
|
||||
#undef _WIN32_WINNT
|
||||
#define _WIN32_WINNT 0x0501 // requires Windows XP minimum
|
||||
#endif
|
||||
// 0x0400=Windows NT 4.0, 0x0500=Windows 2000, 0x0501=Windows XP, 0x0502=Windows Server 2003, 0x0600=Windows Vista,
|
||||
// 0x0601=Windows 7, 0x0602=Windows 8, 0x0603=Windows 8.1, 0x0A00=Windows 10,
|
||||
#define _WINSOCKAPI_
|
||||
#pragma warning( push )
|
||||
#pragma warning( disable: 4365 )
|
||||
#pragma warning( disable: 4619 )
|
||||
#pragma warning( disable: 4668 ) // 'symbol' is not defined as a preprocessor macro, replacing with '0' for 'directives'
|
||||
#pragma warning( disable: 4768 ) // __declspec attributes before linkage specification are ignored
|
||||
#pragma warning( disable: 4255 ) // 'function' : no function prototype given: converting '()' to '(void)'
|
||||
#define _NTDDSCM_H_
|
||||
#include <windows.h>
|
||||
#pragma warning( pop )
|
||||
|
||||
struct dir_entry_t
|
||||
{
|
||||
char name[ MAX_PATH ];
|
||||
BOOL is_folder;
|
||||
};
|
||||
|
||||
|
||||
struct dir_t
|
||||
{
|
||||
char path[ MAX_PATH ];
|
||||
HANDLE handle;
|
||||
WIN32_FIND_DATAA data;
|
||||
dir_entry_t entry;
|
||||
};
|
||||
|
||||
|
||||
dir_t* dir_open( char const* path )
|
||||
{
|
||||
if( !path ) return 0;
|
||||
|
||||
size_t path_len = strlen( path );
|
||||
BOOL trailing_path_separator = path[ path_len - 1 ] == '\\' || path[ path_len - 1 ] == '/';
|
||||
const char* string_to_append = "*.*";
|
||||
if( path_len + strlen( string_to_append ) + ( trailing_path_separator ? 0 : 1 ) >= MAX_PATH ) return NULL;
|
||||
char search_pattern[ MAX_PATH ];
|
||||
strcpy( search_pattern, path );
|
||||
if( !trailing_path_separator ) strcat( search_pattern, "\\" );
|
||||
strcat( search_pattern, string_to_append );
|
||||
|
||||
WIN32_FIND_DATAA data;
|
||||
HANDLE handle = FindFirstFileA( search_pattern, &data );
|
||||
if( handle == INVALID_HANDLE_VALUE ) return NULL;
|
||||
|
||||
dir_t* dir = (dir_t*) DIR_MALLOC( sizeof( dir_t ) );
|
||||
strcpy( dir->path, path );
|
||||
dir->handle = handle;
|
||||
dir->data = data;
|
||||
|
||||
return dir;
|
||||
}
|
||||
|
||||
|
||||
void dir_close( dir_t* dir )
|
||||
{
|
||||
if( !dir ) return;
|
||||
|
||||
if( dir->handle != INVALID_HANDLE_VALUE ) FindClose( dir->handle );
|
||||
DIR_FREE( dir );
|
||||
}
|
||||
|
||||
|
||||
char const* dir_path( dir_t* dir )
|
||||
{
|
||||
if( !dir ) return NULL;
|
||||
return dir->path;
|
||||
}
|
||||
|
||||
|
||||
dir_entry_t* dir_read( dir_t* dir )
|
||||
{
|
||||
if( !dir ) return NULL;
|
||||
if( dir->handle == INVALID_HANDLE_VALUE ) return NULL;
|
||||
|
||||
strcpy( dir->entry.name, dir->data.cFileName );
|
||||
dir->entry.is_folder = ( dir->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) != 0;
|
||||
|
||||
BOOL result = FindNextFileA( dir->handle, &dir->data );
|
||||
if( !result )
|
||||
{
|
||||
FindClose( dir->handle );
|
||||
dir->handle = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
|
||||
return &dir->entry;
|
||||
}
|
||||
|
||||
|
||||
char const* dir_name( dir_entry_t* entry )
|
||||
{
|
||||
if( !entry ) return NULL;
|
||||
return entry->name;
|
||||
}
|
||||
|
||||
|
||||
int dir_is_file( dir_entry_t* entry )
|
||||
{
|
||||
if( !entry ) return 0;
|
||||
return entry->is_folder == FALSE;
|
||||
}
|
||||
|
||||
|
||||
int dir_is_folder( dir_entry_t* entry )
|
||||
{
|
||||
if( !entry ) return 0;
|
||||
return entry->is_folder == TRUE;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// POSIX
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#elif defined( DIR_POSIX )
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
|
||||
dir_t* dir_open( char const* path )
|
||||
{
|
||||
DIR* dir = opendir( path );
|
||||
return (dir_t*) dir;
|
||||
}
|
||||
|
||||
|
||||
void dir_close( dir_t* dir )
|
||||
{
|
||||
if( !dir ) return;
|
||||
closedir( (DIR*) dir );
|
||||
}
|
||||
|
||||
|
||||
dir_entry_t* dir_read( dir_t* dir )
|
||||
{
|
||||
if( !dir ) return NULL;
|
||||
return (dir_entry_t*)readdir( (DIR*) dir );
|
||||
}
|
||||
|
||||
|
||||
char const* dir_name( dir_entry_t* entry )
|
||||
{
|
||||
if( !entry ) return NULL;
|
||||
return ( (struct dirent*)entry )->d_name;
|
||||
}
|
||||
|
||||
|
||||
int dir_is_file( dir_entry_t* entry )
|
||||
{
|
||||
if( !entry ) return 0;
|
||||
return ( (struct dirent*)entry )->d_type == DT_REG;
|
||||
}
|
||||
|
||||
|
||||
int dir_is_folder( dir_entry_t* entry )
|
||||
{
|
||||
if( !entry ) return 0;
|
||||
return ( (struct dirent*)entry )->d_type == DT_DIR;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PLATFORM ERROR
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#else
|
||||
|
||||
#error Undefined platform. Define DIR_WINDOWS or DIR_POSIX.
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* DIR_IMPLEMENTATION */
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This software is available under 2 licenses - you may choose the one you like.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE A - MIT License
|
||||
|
||||
Copyright (c) 2017 Mattias Gustavsson
|
||||
|
||||
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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||
software, either in source code form or as a compiled binary, for any purpose,
|
||||
commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||
software dedicate any and all copyright interest in the software to the public
|
||||
domain. We make this dedication for the benefit of the public at large and to
|
||||
the detriment of our heirs and successors. We intend this dedication to be an
|
||||
overt act of relinquishment in perpetuity of all present and future rights to
|
||||
this software under copyright law.
|
||||
|
||||
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 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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
*/
|
||||
+317
@@ -0,0 +1,317 @@
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
Licensing information can be found at the end of the file.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
frametimer.h - v0.1 - Framerate timer functionality.
|
||||
|
||||
Do this:
|
||||
#define FRAMETIMER_IMPLEMENTATION
|
||||
before you include this file in *one* C/C++ file to create the implementation.
|
||||
*/
|
||||
|
||||
#ifndef frametimer_h
|
||||
#define frametimer_h
|
||||
|
||||
typedef struct frametimer_t frametimer_t;
|
||||
|
||||
frametimer_t* frametimer_create( void* memxtx );
|
||||
|
||||
void frametimer_destroy( frametimer_t* frametimer );
|
||||
|
||||
void frametimer_lock_rate( frametimer_t* frametimer, int fps );
|
||||
|
||||
float frametimer_update( frametimer_t* frametimer );
|
||||
|
||||
float frametimer_delta_time( frametimer_t* frametimer );
|
||||
|
||||
int frametimer_frame_counter( frametimer_t* frametimer );
|
||||
|
||||
#endif /* frametimer_h */
|
||||
|
||||
/*
|
||||
----------------------
|
||||
IMPLEMENTATION
|
||||
----------------------
|
||||
*/
|
||||
|
||||
#ifdef FRAMETIMER_IMPLEMENTATION
|
||||
#undef FRAMETIMER_IMPLEMENTATION
|
||||
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#ifndef _CRT_SECURE_NO_WARNINGS
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
#ifdef _WIN32
|
||||
#if !defined( _WIN32_WINNT ) || _WIN32_WINNT < 0x0501
|
||||
#undef _WIN32_WINNT
|
||||
#define _WIN32_WINNT 0x0501 // requires Windows XP minimum
|
||||
#endif
|
||||
// 0x0400=Windows NT 4.0, 0x0500=Windows 2000, 0x0501=Windows XP, 0x0502=Windows Server 2003, 0x0600=Windows Vista,
|
||||
// 0x0601=Windows 7, 0x0602=Windows 8, 0x0603=Windows 8.1, 0x0A00=Windows 10,
|
||||
#define _WINSOCKAPI_
|
||||
#pragma warning( push )
|
||||
#pragma warning( disable: 4619 )
|
||||
#pragma warning( disable: 4668 ) // 'symbol' is not defined as a preprocessor macro, replacing with '0' for 'directives'
|
||||
#pragma warning( disable: 4768 ) // __declspec attributes before linkage specification are ignored
|
||||
#pragma warning( disable: 4255 ) // 'function' : no function prototype given: converting '()' to '(void)'
|
||||
#include <windows.h>
|
||||
#pragma warning( pop )
|
||||
#ifndef __TINYC__
|
||||
#pragma comment(lib, "winmm.lib")
|
||||
#else
|
||||
typedef struct timecaps_tag { UINT wPeriodMin; UINT wPeriodMax; } TIMECAPS, *PTIMECAPS, NEAR *NPTIMECAPS, FAR *LPTIMECAPS;
|
||||
typedef UINT MMRESULT;
|
||||
#define TIMERR_NOERROR (0)
|
||||
static MMRESULT (*timeGetDevCaps)( LPTIMECAPS ptc, UINT cbtc );
|
||||
static MMRESULT (*timeBeginPeriod)( UINT uPeriod );
|
||||
static MMRESULT (*timeEndPeriod)( UINT uPeriod );
|
||||
#endif
|
||||
#elif defined( __APPLE__ )
|
||||
#include <mach/mach_time.h>
|
||||
#elif defined( __wasm__ )
|
||||
#define WA_CORO_IMPLEMENT_NANOSLEEP
|
||||
#include <wajic_coro.h>
|
||||
#else
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
#ifndef FRAMETIMER_MALLOC
|
||||
#include <stdlib.h>
|
||||
#if defined(__cplusplus)
|
||||
#define FRAMETIMER_MALLOC( ctx, size ) ( ::malloc( size ) )
|
||||
#define FRAMETIMER_FREE( ctx, ptr ) ( ::free( ptr ) )
|
||||
#else
|
||||
#define FRAMETIMER_MALLOC( ctx, size ) ( malloc( size ) )
|
||||
#define FRAMETIMER_FREE( ctx, ptr ) ( free( ptr ) )
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef FRAMETIMER_U64
|
||||
#define FRAMETIMER_U64 unsigned long long
|
||||
#endif
|
||||
|
||||
struct frametimer_t
|
||||
{
|
||||
FRAMETIMER_U64 clock_freq;
|
||||
FRAMETIMER_U64 prev_clock;
|
||||
void* memctx;
|
||||
float delta_time;
|
||||
int initialized;
|
||||
int frame_counter;
|
||||
int frame_rate_lock;
|
||||
#ifdef _WIN32
|
||||
HANDLE waitable_timer;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
frametimer_t* frametimer_create( void* memctx )
|
||||
{
|
||||
frametimer_t* frametimer = (frametimer_t*) FRAMETIMER_MALLOC( memctx, sizeof( frametimer_t ) );
|
||||
#ifdef _WIN32
|
||||
#ifdef __TINYC__
|
||||
HMODULE winmm = LoadLibrary( "winmm" );
|
||||
timeGetDevCaps = GetProcAddress( winmm, "timeGetDevCaps");
|
||||
timeBeginPeriod = GetProcAddress( winmm, "timeBeginPeriod");
|
||||
timeEndPeriod = GetProcAddress( winmm, "timeEndPeriod");
|
||||
#endif
|
||||
TIMECAPS tc;
|
||||
if( timeGetDevCaps( &tc, sizeof( TIMECAPS ) ) == TIMERR_NOERROR )
|
||||
timeBeginPeriod( tc.wPeriodMin );
|
||||
frametimer->waitable_timer = CreateWaitableTimer(NULL, TRUE, NULL);
|
||||
#endif
|
||||
|
||||
frametimer->memctx = memctx;
|
||||
frametimer->initialized = 0;
|
||||
|
||||
#ifdef _WIN32
|
||||
LARGE_INTEGER f;
|
||||
QueryPerformanceFrequency( &f );
|
||||
frametimer->clock_freq = (FRAMETIMER_U64) f.QuadPart;
|
||||
#else
|
||||
frametimer->clock_freq = 1000000000ull;
|
||||
#endif
|
||||
|
||||
frametimer->prev_clock = 0;
|
||||
frametimer->delta_time = 0.0f;
|
||||
frametimer->frame_counter = 0;
|
||||
frametimer->frame_rate_lock = 0;
|
||||
return frametimer;
|
||||
}
|
||||
|
||||
|
||||
void frametimer_destroy( frametimer_t* frametimer )
|
||||
{
|
||||
#ifdef _WIN32
|
||||
CloseHandle( frametimer->waitable_timer );
|
||||
TIMECAPS tc;
|
||||
if( timeGetDevCaps( &tc, sizeof( TIMECAPS ) ) == TIMERR_NOERROR )
|
||||
timeEndPeriod( tc.wPeriodMin );
|
||||
#endif
|
||||
FRAMETIMER_FREE( frametimer->memctx, frametimer );
|
||||
}
|
||||
|
||||
|
||||
void frametimer_lock_rate( frametimer_t* frametimer, int fps )
|
||||
{
|
||||
frametimer->frame_rate_lock = ( fps > 0 && fps < 5 ) ? 5 : fps < 0 ? 0 : fps;
|
||||
}
|
||||
|
||||
|
||||
float frametimer_update( frametimer_t* frametimer )
|
||||
{
|
||||
if( !frametimer->initialized )
|
||||
{
|
||||
#ifdef _WIN32
|
||||
LARGE_INTEGER c;
|
||||
QueryPerformanceCounter( &c );
|
||||
frametimer->prev_clock = (FRAMETIMER_U64) c.QuadPart;
|
||||
#elif defined( __APPLE__ )
|
||||
frametimer->prev_clock = clock_gettime_nsec_np( CLOCK_UPTIME_RAW );
|
||||
#else
|
||||
struct timespec t;
|
||||
clock_gettime( CLOCK_MONOTONIC_RAW, &t );
|
||||
frametimer->prev_clock = (FRAMETIMER_U64)t.tv_sec;
|
||||
frametimer->prev_clock *= 1000000000ull;
|
||||
frametimer->prev_clock += (FRAMETIMER_U64)t.tv_nsec;
|
||||
#endif
|
||||
frametimer->initialized = 1;
|
||||
}
|
||||
|
||||
++frametimer->frame_counter;
|
||||
|
||||
|
||||
FRAMETIMER_U64 curr_clock = 0ULL;
|
||||
#ifdef _WIN32
|
||||
LARGE_INTEGER c;
|
||||
QueryPerformanceCounter( &c );
|
||||
curr_clock = (FRAMETIMER_U64) c.QuadPart;
|
||||
#elif defined( __APPLE__ )
|
||||
curr_clock = (FRAMETIMER_U64) clock_gettime_nsec_np( CLOCK_UPTIME_RAW );
|
||||
#else
|
||||
struct timespec t;
|
||||
clock_gettime( CLOCK_MONOTONIC_RAW, &t );
|
||||
curr_clock = (FRAMETIMER_U64)t.tv_sec;
|
||||
curr_clock *= 1000000000ull;
|
||||
curr_clock += (FRAMETIMER_U64)t.tv_nsec;
|
||||
#endif
|
||||
|
||||
if( frametimer->frame_rate_lock > 0 )
|
||||
{
|
||||
FRAMETIMER_U64 delta = 0ULL;
|
||||
if( curr_clock > frametimer->prev_clock )
|
||||
delta = curr_clock - frametimer->prev_clock - 1ULL;
|
||||
if( delta < frametimer->clock_freq / frametimer->frame_rate_lock )
|
||||
{
|
||||
FRAMETIMER_U64 wait = ( frametimer->clock_freq / frametimer->frame_rate_lock ) - delta;
|
||||
#ifdef _WIN32
|
||||
if( wait > 0 )
|
||||
{
|
||||
LARGE_INTEGER due_time;
|
||||
due_time.QuadPart = - (LONGLONG) ( ( 10000000ULL * wait ) / frametimer->clock_freq ) ;
|
||||
|
||||
SetWaitableTimer( frametimer->waitable_timer, &due_time, 0, 0, 0, FALSE );
|
||||
WaitForSingleObject( frametimer->waitable_timer, 200 ); // wait long enough for timer to trigger ( 200ms == 5fps )
|
||||
CancelWaitableTimer( frametimer->waitable_timer ); // in case we timed out
|
||||
}
|
||||
#else
|
||||
struct timespec t = { 0, 0 };
|
||||
t.tv_nsec = wait;
|
||||
while( t.tv_nsec > 0 )
|
||||
{
|
||||
struct timespec r = { 0, 0 };
|
||||
if( nanosleep( &t, &r ) >= 0 ) break;
|
||||
t = r;
|
||||
}
|
||||
#endif
|
||||
curr_clock += wait;
|
||||
}
|
||||
}
|
||||
|
||||
FRAMETIMER_U64 delta_clock = 0ULL;
|
||||
if( curr_clock > frametimer->prev_clock ) delta_clock = curr_clock - frametimer->prev_clock;
|
||||
|
||||
// Cap delta time if it is too high (running at less than 5 fps ) or things will jump
|
||||
// like crazy on occasional long stalls.
|
||||
if( delta_clock > frametimer->clock_freq / 5ULL ) delta_clock = frametimer->clock_freq / 5ULL; // Cap to 5 fps
|
||||
|
||||
float delta_time = (float) ( ( (double) delta_clock ) / ( (double) frametimer->clock_freq ) );
|
||||
|
||||
frametimer->delta_time = delta_time;
|
||||
frametimer->prev_clock = curr_clock;
|
||||
|
||||
return delta_time;
|
||||
}
|
||||
|
||||
|
||||
float frametimer_delta_time( frametimer_t* frametimer )
|
||||
{
|
||||
return frametimer->delta_time;
|
||||
}
|
||||
|
||||
|
||||
int frametimer_frame_counter( frametimer_t* frametimer )
|
||||
{
|
||||
return frametimer->frame_counter;
|
||||
}
|
||||
|
||||
|
||||
#endif /* FRAMETIMER_IMPLEMENTATION */
|
||||
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This software is available under 2 licenses - you may choose the one you like.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE A - MIT License
|
||||
|
||||
Copyright (c) 2015 Mattias Gustavsson
|
||||
|
||||
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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||
software, either in source code form or as a compiled binary, for any purpose,
|
||||
commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||
software dedicate any and all copyright interest in the software to the public
|
||||
domain. We make this dedication for the benefit of the public at large and to
|
||||
the detriment of our heirs and successors. We intend this dedication to be an
|
||||
overt act of relinquishment in perpetuity of all present and future rights to
|
||||
this software under copyright law.
|
||||
|
||||
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 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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -0,0 +1,797 @@
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
Licensing information can be found at the end of the file.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
id3tag.h - v0.1 - Read/write ID3 tags from/to mp3 files in C/C++.
|
||||
|
||||
Do this:
|
||||
#define ID3TAG_IMPLEMENTATION
|
||||
before you include this file in *one* C/C++ file to create the implementation.
|
||||
*/
|
||||
|
||||
#ifndef id3tag_h
|
||||
#define id3tag_h
|
||||
|
||||
#ifndef ID3TAG_U32
|
||||
#define ID3TAG_U32 unsigned int
|
||||
#endif
|
||||
|
||||
#define ID3TAG_PIC_TYPE_COVER_FRONT 3
|
||||
#define ID3TAG_PIC_TYPE_COVER_BACK 4
|
||||
#define ID3TAG_PIC_TYPE_LEAFLET_PAGE 5
|
||||
#define ID3TAG_PIC_TYPE_MEDIA 6
|
||||
#define ID3TAG_PIC_TYPE_ARTIST 8
|
||||
#define ID3TAG_PIC_TYPE_ARTIST_LOGO 13
|
||||
|
||||
|
||||
typedef struct id3tag_pic_t
|
||||
{
|
||||
int pic_type;
|
||||
char const* description;
|
||||
char const* mime_type;
|
||||
void const* data;
|
||||
size_t size;
|
||||
} id3tag_pic_t;
|
||||
|
||||
|
||||
typedef struct id3tag_t
|
||||
{
|
||||
char const* title;
|
||||
char const* artist;
|
||||
char const* album_artist;
|
||||
char const* album;
|
||||
char const* sort_title;
|
||||
char const* sort_artist;
|
||||
char const* sort_album_artist;
|
||||
char const* sort_album;
|
||||
char const* genre;
|
||||
char const* year;
|
||||
char const* track;
|
||||
char const* tracks;
|
||||
char const* disc;
|
||||
char const* discs;
|
||||
char const* compilation;
|
||||
int track_length;
|
||||
|
||||
int play_counter;
|
||||
|
||||
int pics_count;
|
||||
id3tag_pic_t const* pics;
|
||||
} id3tag_t;
|
||||
|
||||
#define ID3TAG_ALL_FIELDS ( 0xFFFFFFFF )
|
||||
#define ID3TAG_FIELD_TITLE ( 1U )
|
||||
#define ID3TAG_FIELD_ARTIST ( 1U << 1U )
|
||||
#define ID3TAG_FIELD_ALBUM_ARTIST ( 1U << 2U )
|
||||
#define ID3TAG_FIELD_ALBUM ( 1U << 3U )
|
||||
#define ID3TAG_FIELD_SORT_TITLE ( 1U << 4U )
|
||||
#define ID3TAG_FIELD_SORT_ARTIST ( 1U << 5U )
|
||||
#define ID3TAG_FIELD_SORT_ALBUM_ARTIST ( 1U << 6U )
|
||||
#define ID3TAG_FIELD_SORT_ALBUM ( 1U << 7U )
|
||||
#define ID3TAG_FIELD_GENRE ( 1U << 8U )
|
||||
#define ID3TAG_FIELD_YEAR ( 1U << 9U )
|
||||
#define ID3TAG_FIELD_TRACK ( 1U << 10U )
|
||||
#define ID3TAG_FIELD_TRACKS ( 1U << 11U )
|
||||
#define ID3TAG_FIELD_DISC ( 1U << 12U )
|
||||
#define ID3TAG_FIELD_DISCS ( 1U << 13U )
|
||||
#define ID3TAG_FIELD_COMPILATION ( 1U << 14U )
|
||||
#define ID3TAG_FIELD_PLAY_COUNTER ( 1U << 15U )
|
||||
#define ID3TAG_FIELD_PICS ( 1U << 16U )
|
||||
#define ID3TAG_FIELD_TRACK_LENGTH ( 1U << 17U )
|
||||
|
||||
size_t id3tag_size( void const* first_ten_bytes );
|
||||
|
||||
id3tag_t* id3tag_load( void const* data, size_t size, ID3TAG_U32 fields, void* memctx );
|
||||
id3tag_t* id3tag_load_id3v1( void const* data, size_t size, void* memctx );
|
||||
void id3tag_free( id3tag_t* id3tag );
|
||||
|
||||
size_t id3tag_save( id3tag_t const* id3tag, ID3TAG_U32 fields, void const* data, size_t capacity );
|
||||
|
||||
|
||||
#endif /* id3tag_h */
|
||||
|
||||
|
||||
/*
|
||||
----------------------
|
||||
IMPLEMENTATION
|
||||
----------------------
|
||||
*/
|
||||
|
||||
#ifdef ID3TAG_IMPLEMENTATION
|
||||
#undef ID3TAG_IMPLEMENTATION
|
||||
|
||||
#ifndef ID3TAG_U8
|
||||
#define ID3TAG_U8 unsigned char
|
||||
#endif
|
||||
|
||||
#ifndef ID3TAG_U16
|
||||
#define ID3TAG_U16 unsigned short
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef ID3TAG_MALLOC
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdlib.h>
|
||||
#define ID3TAG_MALLOC( ctx, size ) ( malloc( size ) )
|
||||
#define ID3TAG_FREE( ctx, ptr ) ( free( ptr ) )
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct id3tag_internal_t
|
||||
{
|
||||
id3tag_t tag;
|
||||
void* memctx;
|
||||
int pics_capacity;
|
||||
} id3tag_internal_t;
|
||||
|
||||
|
||||
int id3tag_internal_utf16_to_utf8( ID3TAG_U16* str, int len, char* out, int capacity, int endian_swap )
|
||||
{
|
||||
ID3TAG_U16* in_ptr = str;
|
||||
char* out_ptr = out;
|
||||
char* out_end = out + capacity;
|
||||
|
||||
for( int i = 0; i < len; ++i )
|
||||
{
|
||||
ID3TAG_U32 cp = (ID3TAG_U32) *in_ptr++;
|
||||
if( endian_swap ) cp = ( ( cp & 0xff00 ) >> 8 ) | ( ( cp & 0x00ff ) << 8 );
|
||||
|
||||
if( cp == 0x0000 ) break;
|
||||
|
||||
if( cp >= 0xd800u && cp <= 0xdbffu )
|
||||
cp = ( cp << 10 ) + (*in_ptr++) + ( 0x10000u - ( 0xd800u << 10 ) - 0xdc00u );
|
||||
|
||||
if( cp < 0x80 )
|
||||
{
|
||||
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) cp ); else ++out_ptr;
|
||||
}
|
||||
else if( cp < 0x800 )
|
||||
{
|
||||
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( cp >> 6) | 0xc0 ) ); else ++out_ptr;
|
||||
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( cp & 0x3f ) | 0x80 ) ); else ++out_ptr;
|
||||
}
|
||||
else if (cp < 0x10000)
|
||||
{
|
||||
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( cp >> 12 ) | 0xe0 ) ); else ++out_ptr;
|
||||
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( ( cp >> 6 ) & 0x3f ) | 0x80 ) ); else ++out_ptr;
|
||||
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( cp & 0x3f ) | 0x80 ) ); else ++out_ptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( cp >> 18 ) | 0xf0 ) ); else ++out_ptr;
|
||||
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( ( cp >> 12 ) & 0x3f ) | 0x80 ) ); else ++out_ptr;
|
||||
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( ( cp >> 6 ) & 0x3f ) | 0x80 ) ); else ++out_ptr;
|
||||
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( cp & 0x3f ) | 0x80 ) ); else ++out_ptr;
|
||||
}
|
||||
}
|
||||
if( out && out_ptr < out_end ) *out_ptr++ = '\0'; else ++out_ptr;
|
||||
|
||||
int out_len = out ? (int)( out_ptr - out ) : (int)(uintptr_t)( out_ptr );
|
||||
return out_len;
|
||||
}
|
||||
|
||||
|
||||
// TODO: Proper unicode support with a font that can handle it, instead of doing this
|
||||
char* utf8_to_latin1( char* s )
|
||||
{
|
||||
for( int readIndex = 0, writeIndex = 0 ; ; writeIndex++ )
|
||||
{
|
||||
if( s[ readIndex ] == 0 ) { s[ writeIndex ] = 0; break; }
|
||||
|
||||
int len;
|
||||
if ( (ID3TAG_U8) s[ readIndex ] < 0x80 ) len = 1;
|
||||
else if ( ( (ID3TAG_U8) s[ readIndex ] & 0x20 ) == 0 ) len = 2;
|
||||
else if ( ( (ID3TAG_U8) s[ readIndex ] & 0x10 ) == 0 ) len = 3;
|
||||
else if ( ( (ID3TAG_U8) s[ readIndex ] & 0x08 ) == 0 ) len = 4;
|
||||
else if ( ( (ID3TAG_U8) s[ readIndex ] & 0x04 ) == 0 ) len = 5;
|
||||
else len = 6;
|
||||
|
||||
char c = '\0';
|
||||
if( len == 1 )
|
||||
{
|
||||
s[ writeIndex ] = s[ readIndex++ ];
|
||||
}
|
||||
else
|
||||
{
|
||||
int v = ( s[ readIndex++ ] & ( 0xff >> ( len + 1 ) ) ) << ( ( len - 1 ) * 6 );
|
||||
for( len-- ; len > 0 ; len-- ) v |= ( (ID3TAG_U8)( s[ readIndex++ ] ) - 0x80 ) << ( ( len - 1 ) * 6 );
|
||||
if( v == 8216 ) v = '\'';
|
||||
if( v == 8217 ) v = '\'';
|
||||
if( v == 9733 ) v = '*';
|
||||
if( v == 8208 ) v = '-';
|
||||
if( v == 8211 ) v = '-';
|
||||
if( v == 8220 ) v = '"';
|
||||
if( v == 8221 ) v = 'r';
|
||||
if( v == 8482 )
|
||||
v = ' '; // TM
|
||||
if( v == 263 ) v = 'c';
|
||||
if( v == 65533 ) v = '?';
|
||||
if( v == 8230 ) { // '...'
|
||||
s[ writeIndex++ ] = '.';
|
||||
s[ writeIndex++ ] = '.';
|
||||
v = '.';
|
||||
}
|
||||
if( v == 339 ) {
|
||||
s[ writeIndex++ ] = 'o';
|
||||
v = 'e';
|
||||
}
|
||||
if( v > 0xff ) {
|
||||
v = ' ';
|
||||
}
|
||||
c = (char) v;
|
||||
s[ writeIndex ] = c;
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
size_t id3tag_size( void const* first_ten_bytes )
|
||||
{
|
||||
ID3TAG_U8* ptr = (ID3TAG_U8*) first_ten_bytes;
|
||||
|
||||
// verify the header to see if this is a valid ID3 tag
|
||||
if( *ptr++ != 0x49 || *ptr++ != 0x44 || *ptr++ != 0x33 || *ptr++ >= 0xff || *ptr++ >= 0xff ) return 0;
|
||||
|
||||
++ptr; // skip flags field
|
||||
|
||||
ID3TAG_U32 size = 0;
|
||||
for( int i = 0; i < 4; ++i )
|
||||
{
|
||||
ID3TAG_U8 b = *ptr++;
|
||||
if( b >= 0x80 ) return 0; // Not a valid ID3
|
||||
size |= ( (ID3TAG_U32) b ) << ( 7 * ( 3 - i ) );
|
||||
}
|
||||
|
||||
return (size_t) size + 10;
|
||||
}
|
||||
|
||||
|
||||
char* id3tag_internal_get_string( int encoding, ID3TAG_U8* ptr, int size, void* memctx, int* bytes_consumed )
|
||||
{
|
||||
(void) memctx;
|
||||
if( encoding == 0) // Latin1
|
||||
{
|
||||
char* str = (char*)ID3TAG_MALLOC( memctx, (size_t)( size + 1 ) );
|
||||
memcpy( str, ptr, (size_t) size );
|
||||
str[ size ] = '\0';
|
||||
if( bytes_consumed ) *bytes_consumed = (int) strlen( str ) + 1;
|
||||
return str;
|
||||
}
|
||||
else if( encoding == 1 ) // UTF16 with BOM
|
||||
{
|
||||
ID3TAG_U16 bom = *( (ID3TAG_U16*) ptr );
|
||||
if( bom == 0xfffe || bom == 0xfeff ) { ptr += 2; size -= 2; }
|
||||
int endian_swap = ( bom == 0xfffe );
|
||||
|
||||
int len = id3tag_internal_utf16_to_utf8( (ID3TAG_U16*) ptr, size / 2, 0, 0, endian_swap );
|
||||
char* str = (char*)ID3TAG_MALLOC( memctx, (size_t) len );
|
||||
id3tag_internal_utf16_to_utf8( (ID3TAG_U16*) ptr, size / 2, str, len, endian_swap );
|
||||
|
||||
if( bytes_consumed )
|
||||
{
|
||||
*bytes_consumed = ( ( bom == 0xfffe || bom == 0xfeff ) ? 2 : 0 );
|
||||
ID3TAG_U16* s = (ID3TAG_U16*) ptr;
|
||||
while( *s++ ) *bytes_consumed = ( *bytes_consumed ) + 2;
|
||||
*bytes_consumed = ( *bytes_consumed ) + 2;
|
||||
}
|
||||
|
||||
utf8_to_latin1( str );
|
||||
return str;
|
||||
}
|
||||
else if( encoding == 2 ) // UTF16BE without BOM
|
||||
{
|
||||
int endian_swap = 1;
|
||||
|
||||
int len = id3tag_internal_utf16_to_utf8( (ID3TAG_U16*) ptr, size / 2, 0, 0, endian_swap );
|
||||
char* str = (char*)ID3TAG_MALLOC( memctx, (size_t) len );
|
||||
id3tag_internal_utf16_to_utf8( (ID3TAG_U16*) ptr, size / 2, str, len, endian_swap );
|
||||
|
||||
if( bytes_consumed )
|
||||
{
|
||||
*bytes_consumed = 0;
|
||||
ID3TAG_U16* s = (ID3TAG_U16*) ptr;
|
||||
while( *s++ ) *bytes_consumed = ( *bytes_consumed ) + 2;
|
||||
*bytes_consumed = ( *bytes_consumed ) + 2;
|
||||
}
|
||||
|
||||
utf8_to_latin1( str );
|
||||
return str;
|
||||
}
|
||||
else if( encoding == 3 ) // UTF8
|
||||
{
|
||||
char* str = (char*)ID3TAG_MALLOC( memctx, (size_t) size + 1 );
|
||||
memcpy( str, ptr, (size_t) size );
|
||||
str[ size ] = '\0';
|
||||
if( bytes_consumed ) *bytes_consumed = (int) strlen( str ) + 1;
|
||||
|
||||
utf8_to_latin1( str );
|
||||
return str;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
id3tag_t* id3tag_load_id3v1( void const* data, size_t size, void* memctx )
|
||||
{
|
||||
if( size != 128 ) return NULL;
|
||||
char const* ptr = (char const*) data;
|
||||
if( strncmp( ptr, "TAG", 3 ) != 0 ) return NULL;
|
||||
ptr += 3;
|
||||
|
||||
id3tag_internal_t* tag = (id3tag_internal_t*) ID3TAG_MALLOC( memctx, sizeof( id3tag_internal_t ) + size );
|
||||
memset( tag, 0, sizeof( id3tag_internal_t ) );
|
||||
memcpy( tag + 1, data, size );
|
||||
tag->memctx = memctx;
|
||||
|
||||
tag->tag.title = (char*) ID3TAG_MALLOC( memctx, 31 );
|
||||
strncpy( (char*) tag->tag.title, ptr, 30 );
|
||||
( (char*) tag->tag.title )[ 30 ] = '\0';
|
||||
ptr += 30;
|
||||
|
||||
tag->tag.artist = (char*) ID3TAG_MALLOC( memctx, 31 );
|
||||
strncpy( (char*) tag->tag.artist, ptr, 30 );
|
||||
( (char*) tag->tag.artist )[ 30 ] = '\0';
|
||||
ptr += 30;
|
||||
|
||||
tag->tag.album = (char*) ID3TAG_MALLOC( memctx, 31 );
|
||||
strncpy( (char*) tag->tag.album, ptr, 30 );
|
||||
( (char*) tag->tag.album )[ 30 ] = '\0';
|
||||
ptr += 30;
|
||||
|
||||
tag->tag.year = (char*) ID3TAG_MALLOC( memctx, 5 );
|
||||
strncpy( (char*) tag->tag.year, ptr, 4 );
|
||||
( (char*) tag->tag.year)[ 4 ] = '\0';
|
||||
ptr += 4;
|
||||
|
||||
char const* comment = ptr;
|
||||
if( comment[ 28 ] == 0 && (int)(ID3TAG_U8)( ptr[ 29 ] ) > 0 )
|
||||
{
|
||||
tag->tag.track = (char*) ID3TAG_MALLOC( memctx, 30 );
|
||||
sprintf( (char*) tag->tag.track, "%d", (int)(ID3TAG_U8)( ptr[ 29 ] ) );
|
||||
}
|
||||
ptr += 30;
|
||||
|
||||
int genre = (int)(ID3TAG_U8)( *ptr );
|
||||
if( genre != 255 )
|
||||
{
|
||||
tag->tag.genre = (char*) ID3TAG_MALLOC( memctx, 30 );
|
||||
sprintf( (char*) tag->tag.genre, "(%d)", genre );
|
||||
}
|
||||
|
||||
return (id3tag_t*) tag;
|
||||
}
|
||||
|
||||
|
||||
id3tag_t* id3tag_load( void const* data, size_t size, ID3TAG_U32 fields, void* memctx )
|
||||
{
|
||||
enum fields_t
|
||||
{
|
||||
FIELD_TITLE,
|
||||
FIELD_ARTIST,
|
||||
FIELD_ALBUM_ARTIST,
|
||||
FIELD_ALBUM,
|
||||
FIELD_SORT_TITLE,
|
||||
FIELD_SORT_ARTIST,
|
||||
FIELD_SORT_ALBUM_ARTIST,
|
||||
FIELD_SORT_ALBUM,
|
||||
FIELD_GENRE,
|
||||
FIELD_YEAR,
|
||||
FIELD_TRACK,
|
||||
FIELD_TRACKS,
|
||||
FIELD_DISC,
|
||||
FIELD_DISCS,
|
||||
FIELD_COMPILATION,
|
||||
FIELD_PLAY_COUNTER,
|
||||
FIELD_TRACK_LENGTH,
|
||||
|
||||
FIELDCOUNT,
|
||||
};
|
||||
bool fields_found[ FIELDCOUNT ] = { false };
|
||||
|
||||
int fields_requested = 0;
|
||||
if( fields & ID3TAG_FIELD_TITLE ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_ARTIST ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_ALBUM_ARTIST ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_ALBUM ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_SORT_TITLE ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_SORT_ARTIST ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_SORT_ALBUM_ARTIST ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_SORT_ALBUM ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_GENRE ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_YEAR ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_TRACK ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_TRACKS ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_DISC ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_DISCS ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_COMPILATION ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_PLAY_COUNTER ) ++fields_requested;
|
||||
if( fields & ID3TAG_FIELD_TRACK_LENGTH ) ++fields_requested;
|
||||
|
||||
ID3TAG_U8* ptr = (ID3TAG_U8*) data;
|
||||
|
||||
// validate data
|
||||
if( size < 10 ) return NULL; // Not a valid ID3
|
||||
size_t tag_size = id3tag_size( data );
|
||||
if( tag_size == 0 ) return NULL; // Not a valid ID3
|
||||
if( size < tag_size ) return NULL; // Not enough data
|
||||
|
||||
id3tag_internal_t* tag = (id3tag_internal_t*) ID3TAG_MALLOC( memctx, sizeof( id3tag_internal_t ) + size );
|
||||
memset( tag, 0, sizeof( id3tag_internal_t ) );
|
||||
memcpy( tag + 1, data, size );
|
||||
tag->memctx = memctx;
|
||||
|
||||
ptr += 3; // skip past "ID3" marker
|
||||
|
||||
// read version and flags
|
||||
int version = *ptr++;
|
||||
int revision = *ptr++; (void) revision;
|
||||
ID3TAG_U8 header_flags = *ptr++;
|
||||
(void) header_flags; // TODO: inspect flags
|
||||
|
||||
ptr += 4; // skip past size field
|
||||
|
||||
ID3TAG_U8* end = ( (ID3TAG_U8*) data ) + tag_size;
|
||||
|
||||
while( ptr <= ( end - 10 ) )
|
||||
{
|
||||
if( ( fields & ID3TAG_FIELD_PICS ) == 0 )
|
||||
{
|
||||
int fields_found_count = 0;
|
||||
for( int i = 0; i < FIELDCOUNT; ++i ) if( fields_found[ i ] ) ++fields_found_count;
|
||||
if( fields_found_count == fields_requested ) break;
|
||||
}
|
||||
|
||||
if( version >= 3 && ptr[ 0 ] == 0 && ptr[ 1 ] == 0 && ptr[ 2 ] == 0 && ptr[ 3 ] == 0 ) break; // end of frames (in padding)
|
||||
if( version < 3 && ptr[ 0 ] == 0 && ptr[ 1 ] == 0 && ptr[ 2 ] == 0 ) break; // end of frames (in padding)
|
||||
|
||||
char frame_id[ 5 ]= { (char) *ptr++, (char) *ptr++, (char) *ptr++, '\0', '\0' };
|
||||
if( version >= 3 ) frame_id[ 3 ] = (char) *ptr++;
|
||||
int frame_size = 0;
|
||||
if( version >= 4 )
|
||||
for( int i = 3; i >= 0 ; --i ) frame_size |= ( (ID3TAG_U32) ( ( *ptr++ ) ) ) << ( 7 * i );
|
||||
else if( version >= 3 )
|
||||
for( int i = 3; i >= 0 ; --i ) frame_size |= ( (ID3TAG_U32) ( ( *ptr++ ) ) ) << ( 8 * i );
|
||||
else
|
||||
for( int i = 2; i >= 0 ; --i ) frame_size |= ( (ID3TAG_U32) ( ( *ptr++ ) ) ) << ( 8 * i );
|
||||
if( frame_size <= 0 ) break; // end of frames
|
||||
if( frame_size >= ( end - ptr ) ) break; // invalid frame
|
||||
ID3TAG_U16 flags = 0;
|
||||
if( version >= 3 ) flags = (ID3TAG_U16)( ( ( (ID3TAG_U16)( *ptr++ ) ) << 8 ) | ( *ptr++ ) );
|
||||
|
||||
|
||||
if( ( fields & ID3TAG_FIELD_TITLE ) && ( strcmp( frame_id , "TIT2" ) == 0 || strcmp( frame_id , "TT2" ) == 0 ) )
|
||||
{
|
||||
if( !tag->tag.title ) tag->tag.title = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
fields_found[ FIELD_TITLE ] = true;
|
||||
}
|
||||
|
||||
if( ( fields & ID3TAG_FIELD_ARTIST ) && ( strcmp( frame_id , "TPE1" ) == 0 || strcmp( frame_id , "TP1" ) == 0 ) )
|
||||
{
|
||||
if( !tag->tag.artist ) tag->tag.artist = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
fields_found[ FIELD_ARTIST ] = true;
|
||||
}
|
||||
|
||||
if( ( fields & ID3TAG_FIELD_ALBUM_ARTIST) && ( strcmp( frame_id , "TPE2" ) == 0 || strcmp( frame_id , "TP2" ) == 0 ) )
|
||||
{
|
||||
if( !tag->tag.album_artist ) tag->tag.album_artist = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
fields_found[ FIELD_ALBUM_ARTIST ] = true;
|
||||
}
|
||||
|
||||
if( ( fields & ID3TAG_FIELD_ALBUM ) && ( strcmp( frame_id , "TALB" ) == 0 || strcmp( frame_id , "TAL" ) == 0 ) )
|
||||
{
|
||||
if( !tag->tag.album ) tag->tag.album = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
fields_found[ FIELD_ALBUM ] = true;
|
||||
}
|
||||
|
||||
if( ( fields & ID3TAG_FIELD_GENRE ) && ( strcmp( frame_id , "TCON" ) == 0 || strcmp( frame_id , "TCO" ) == 0 ) )
|
||||
{
|
||||
if( !tag->tag.genre ) tag->tag.genre = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
fields_found[ FIELD_GENRE ] = true;
|
||||
}
|
||||
|
||||
if( ( fields & ID3TAG_FIELD_YEAR ) && ( strcmp( frame_id , "TYER" ) == 0 || strcmp( frame_id , "TYE" ) == 0 ) )
|
||||
{
|
||||
if( !tag->tag.year ) tag->tag.year = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
fields_found[ FIELD_YEAR ] = true;
|
||||
}
|
||||
|
||||
if( ( fields & ID3TAG_FIELD_COMPILATION ) && ( strcmp( frame_id , "TCMP" ) == 0 || strcmp( frame_id , "TCP" ) == 0 ) )
|
||||
{
|
||||
if( !tag->tag.compilation ) tag->tag.compilation = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
fields_found[ FIELD_COMPILATION ] = true;
|
||||
}
|
||||
|
||||
if( ( fields & ID3TAG_FIELD_SORT_TITLE ) && ( strcmp( frame_id , "TSOT" ) == 0 || strcmp( frame_id , "TST" ) == 0 ) )
|
||||
{
|
||||
if( !tag->tag.sort_title ) tag->tag.sort_title = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
fields_found[ FIELD_SORT_TITLE ] = true;
|
||||
}
|
||||
|
||||
if( ( fields & ID3TAG_FIELD_SORT_ARTIST ) && ( strcmp( frame_id , "TSOP" ) == 0 || strcmp( frame_id , "TSP" ) == 0 ) )
|
||||
{
|
||||
if( !tag->tag.sort_artist ) tag->tag.sort_artist = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
fields_found[ FIELD_SORT_ARTIST ] = true;
|
||||
}
|
||||
|
||||
if( ( fields & ID3TAG_FIELD_SORT_ALBUM ) && ( strcmp( frame_id , "TSOA" ) == 0 || strcmp( frame_id , "TSA" ) == 0 ) )
|
||||
{
|
||||
if( !tag->tag.sort_album ) tag->tag.sort_album = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
fields_found[ FIELD_SORT_ALBUM ] = true;
|
||||
}
|
||||
|
||||
if( ( fields & ID3TAG_FIELD_SORT_ALBUM_ARTIST ) && ( strcmp( frame_id , "TSO2" ) == 0 || strcmp( frame_id , "TS2" ) == 0 ) )
|
||||
{
|
||||
if( !tag->tag.sort_album_artist ) tag->tag.sort_album_artist = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
fields_found[ FIELD_SORT_ALBUM_ARTIST ] = true;
|
||||
}
|
||||
|
||||
if( ( fields & ID3TAG_FIELD_TRACK_LENGTH ) && ( strcmp( frame_id , "TLEN" ) == 0 || strcmp( frame_id , "TLE" ) == 0 ) )
|
||||
{
|
||||
char* str = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
if( tag->tag.track_length == 0 ) tag->tag.track_length = atoi( str );
|
||||
ID3TAG_FREE( memctx, str );
|
||||
if( tag->tag.track_length > 0 ) fields_found[ FIELD_TRACK_LENGTH ] = true;
|
||||
}
|
||||
|
||||
if( ( ( fields & ID3TAG_FIELD_TRACK ) || ( fields & ID3TAG_FIELD_TRACKS ) ) &&
|
||||
( strcmp( frame_id , "TRCK" ) == 0 || strcmp( frame_id , "TRK" ) == 0 ) )
|
||||
{
|
||||
char* first_str = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
char* separator = strchr( first_str, '/' );
|
||||
if( separator ) *separator = '\0';
|
||||
char* second_str = separator ? ( separator + 1 ) : NULL;
|
||||
|
||||
int first = atoi( first_str );
|
||||
int second = second_str ? atoi( second_str ) : 0;
|
||||
ID3TAG_FREE( memctx, first_str );
|
||||
|
||||
char buf[ 64 ];
|
||||
|
||||
if( first > 0 )
|
||||
{
|
||||
sprintf( buf, "%d", first );
|
||||
if( strlen( buf ) > 0 && !tag->tag.track )
|
||||
{
|
||||
tag->tag.track = (char*) ID3TAG_MALLOC( memctx, strlen( buf ) + 1 );
|
||||
strcpy( (char*) tag->tag.track, buf );
|
||||
fields_found[ FIELD_TRACK ] = true;
|
||||
}
|
||||
}
|
||||
|
||||
if( second > 0 )
|
||||
{
|
||||
sprintf( buf, "%d", second );
|
||||
if( strlen( buf ) > 0 && !tag->tag.tracks )
|
||||
{
|
||||
tag->tag.tracks = (char*) ID3TAG_MALLOC( memctx, strlen( buf ) + 1 );
|
||||
strcpy( (char*) tag->tag.tracks, buf );
|
||||
fields_found[ FIELD_TRACKS ] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( ( ( fields & ID3TAG_FIELD_DISC ) || ( fields & ID3TAG_FIELD_DISCS ) ) &&
|
||||
( strcmp( frame_id , "TPOS" ) == 0 || strcmp( frame_id , "TPA" ) == 0 ) )
|
||||
{
|
||||
char* first_str = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
|
||||
char* separator = strchr( first_str, '/' );
|
||||
if( separator ) *separator = '\0';
|
||||
char* second_str = separator ? ( separator + 1 ) : NULL;
|
||||
|
||||
int first = atoi( first_str );
|
||||
int second = second_str ? atoi( second_str ) : 0;
|
||||
ID3TAG_FREE( memctx, first_str );
|
||||
|
||||
char buf[ 64 ];
|
||||
|
||||
if( first > 0 )
|
||||
{
|
||||
sprintf( buf, "%d", first );
|
||||
if( strlen( buf ) > 0 && !tag->tag.disc )
|
||||
{
|
||||
tag->tag.disc = (char*) ID3TAG_MALLOC( memctx, strlen( buf ) + 1 );
|
||||
strcpy( (char*) tag->tag.disc, buf );
|
||||
fields_found[ FIELD_DISC ] = true;
|
||||
}
|
||||
}
|
||||
|
||||
if( second > 0 )
|
||||
{
|
||||
sprintf( buf, "%d", second );
|
||||
if( strlen( buf ) > 0 && !tag->tag.discs )
|
||||
{
|
||||
tag->tag.discs = (char*) ID3TAG_MALLOC( memctx, strlen( buf ) + 1 );
|
||||
strcpy( (char*) tag->tag.discs, buf );
|
||||
fields_found[ FIELD_DISCS ] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if( ( fields & ID3TAG_FIELD_PICS ) && ( strcmp( frame_id , "APIC" ) == 0 || strcmp( frame_id , "PIC" ) == 0 ) )
|
||||
{
|
||||
if( !tag->tag.pics )
|
||||
{
|
||||
tag->pics_capacity = 16;
|
||||
tag->tag.pics = (id3tag_pic_t*) ID3TAG_MALLOC( memctx, sizeof( id3tag_pic_t ) * tag->pics_capacity );
|
||||
tag->tag.pics_count = 0;
|
||||
}
|
||||
if( tag->tag.pics_count >= tag->pics_capacity )
|
||||
{
|
||||
tag->pics_capacity *= 2;
|
||||
id3tag_pic_t* new_pics = (id3tag_pic_t*) ID3TAG_MALLOC( memctx, sizeof( id3tag_pic_t ) * tag->pics_capacity );
|
||||
memcpy( new_pics, tag->tag.pics, sizeof( id3tag_pic_t ) * tag->tag.pics_count );
|
||||
ID3TAG_FREE( memctx, (id3tag_pic_t*)tag->tag.pics );
|
||||
tag->tag.pics = new_pics;
|
||||
}
|
||||
|
||||
int encoding = *ptr++;
|
||||
--frame_size;
|
||||
|
||||
id3tag_pic_t* pic = (id3tag_pic_t*) &tag->tag.pics[ tag->tag.pics_count++ ];
|
||||
memset( pic, 0, sizeof( *pic ) );
|
||||
|
||||
if( version < 3 )
|
||||
{
|
||||
char str[ 4 ] = { (char) *ptr++, (char) *ptr++, (char) *ptr++, '\0' };
|
||||
frame_size -= 3;
|
||||
|
||||
pic->mime_type = (char*) ID3TAG_MALLOC( memctx, 32 );
|
||||
if( strcmp( str, "JPG" ) == 0 )
|
||||
strcpy( (char*) pic->mime_type, "image/jpeg" );
|
||||
else if( strcmp( str, "PNG" ) == 0 )
|
||||
strcpy( (char*) pic->mime_type, "image/png" );
|
||||
else if( strcmp( str, "GIF" ) == 0 )
|
||||
strcpy( (char*) pic->mime_type, "image/gif" );
|
||||
else if( strcmp( str, "BMP" ) == 0 )
|
||||
strcpy( (char*) pic->mime_type, "image/bmp" );
|
||||
else
|
||||
strcpy( (char*) pic->mime_type, str );
|
||||
}
|
||||
else
|
||||
{
|
||||
int bytes = 0;
|
||||
pic->mime_type = id3tag_internal_get_string( encoding, ptr, frame_size, memctx, &bytes );
|
||||
ptr += bytes;
|
||||
frame_size -= bytes;
|
||||
}
|
||||
|
||||
pic->pic_type = *ptr++;
|
||||
--frame_size;
|
||||
|
||||
if( pic->pic_type != 0 )
|
||||
ptr = ptr;
|
||||
|
||||
int bytes = 0;
|
||||
pic->description = id3tag_internal_get_string( encoding, ptr, frame_size, memctx, &bytes );
|
||||
ptr += bytes;
|
||||
frame_size -= bytes;
|
||||
|
||||
if( frame_size )
|
||||
{
|
||||
pic->data = ID3TAG_MALLOC( memctx, (size_t) frame_size );
|
||||
memcpy( (void*) pic->data, ptr, (size_t) frame_size );
|
||||
pic->size = (size_t) frame_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( pic->mime_type ) ID3TAG_FREE( memctx, (char*) pic->mime_type );
|
||||
if( pic->description ) ID3TAG_FREE( memctx, (char*) pic->description );
|
||||
--tag->tag.pics_count;
|
||||
}
|
||||
}
|
||||
|
||||
ptr += frame_size;
|
||||
}
|
||||
|
||||
return (id3tag_t*) tag;
|
||||
}
|
||||
|
||||
|
||||
void id3tag_free( id3tag_t* id3tag )
|
||||
{
|
||||
id3tag_internal_t* internal = (id3tag_internal_t*) id3tag;
|
||||
|
||||
if( internal->tag.title) ID3TAG_FREE( internal->memctx, (char*) internal->tag.title );
|
||||
if( internal->tag.artist ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.artist );
|
||||
if( internal->tag.album_artist ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.album_artist );
|
||||
if( internal->tag.album ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.album );
|
||||
if( internal->tag.sort_title) ID3TAG_FREE( internal->memctx, (char*) internal->tag.sort_title );
|
||||
if( internal->tag.sort_artist ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.sort_artist );
|
||||
if( internal->tag.sort_album_artist ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.sort_album_artist );
|
||||
if( internal->tag.sort_album ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.sort_album );
|
||||
if( internal->tag.genre ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.genre );
|
||||
if( internal->tag.year ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.year );
|
||||
if( internal->tag.track ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.track );
|
||||
if( internal->tag.tracks ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.tracks );
|
||||
if( internal->tag.disc ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.disc );
|
||||
if( internal->tag.discs ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.discs );
|
||||
if( internal->tag.compilation ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.compilation );
|
||||
|
||||
if( internal->tag.pics )
|
||||
{
|
||||
for( int i = 0; i < internal->tag.pics_count; ++i )
|
||||
{
|
||||
if( internal->tag.pics[ i ].description ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.pics[ i ].description );
|
||||
if( internal->tag.pics[ i ].mime_type ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.pics[ i ].mime_type );
|
||||
if( internal->tag.pics[ i ].data ) ID3TAG_FREE( internal->memctx, (void*) internal->tag.pics[ i ].data );
|
||||
}
|
||||
|
||||
if( internal->tag.pics ) ID3TAG_FREE( internal->memctx, (id3tag_pic_t*) internal->tag.pics );
|
||||
}
|
||||
|
||||
ID3TAG_FREE( internal->memctx, id3tag );
|
||||
}
|
||||
|
||||
|
||||
size_t id3tag_save( id3tag_t const* id3tag, ID3TAG_U32 fields, void const* data, size_t capacity )
|
||||
{
|
||||
(void) id3tag, fields, data, capacity;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#endif /* ID3TAG_IMPLEMENTATION */
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This software is available under 2 licenses - you may choose the one you like.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE A - MIT License
|
||||
|
||||
Copyright (c) 2018 Mattias Gustavsson
|
||||
|
||||
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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||
software, either in source code form or as a compiled binary, for any purpose,
|
||||
commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||
software dedicate any and all copyright interest in the software to the public
|
||||
domain. We make this dedication for the benefit of the public at large and to
|
||||
the detriment of our heirs and successors. We intend this dedication to be an
|
||||
overt act of relinquishment in perpetuity of all present and future rights to
|
||||
this software under copyright law.
|
||||
|
||||
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 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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -0,0 +1,412 @@
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
Licensing information can be found at the end of the file.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
img.h - v0.1 - Image processing functions for C/C++.
|
||||
|
||||
Do this:
|
||||
#define IMG_IMPLEMENTATION
|
||||
before you include this file in *one* C/C++ file to create the implementation.
|
||||
*/
|
||||
|
||||
#ifndef img_h
|
||||
#define img_h
|
||||
|
||||
#ifndef IMG_U32
|
||||
#define IMG_U32 unsigned int
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct img_rgba_t {
|
||||
float r;
|
||||
float g;
|
||||
float b;
|
||||
float a;
|
||||
} img_rgba_t;
|
||||
|
||||
typedef struct img_t {
|
||||
int width;
|
||||
int height;
|
||||
img_rgba_t* pixels;
|
||||
} img_t;
|
||||
|
||||
typedef struct img_hsva_t {
|
||||
float h;
|
||||
float s;
|
||||
float v;
|
||||
float a;
|
||||
} img_hsva_t;
|
||||
|
||||
img_t img_create( int width, int height );
|
||||
img_t img_from_abgr32( IMG_U32* abgr, int width, int height );
|
||||
void img_free( img_t* img );
|
||||
|
||||
void img_to_argb32( img_t* img, IMG_U32* abgr );
|
||||
img_rgba_t img_sample_clamp( img_t* img, float u, float v );
|
||||
|
||||
img_rgba_t img_rgba_lerp( img_rgba_t a, img_rgba_t b, float s );
|
||||
img_hsva_t img_rgba_to_hsva( img_rgba_t rgb );
|
||||
img_rgba_t img_hsva_to_rgba( img_hsva_t hsv );
|
||||
|
||||
void img_adjust_brightness( img_t* img, float value );
|
||||
void img_adjust_contrast( img_t* img, float value );
|
||||
void img_adjust_saturation( img_t* img, float value );
|
||||
void img_sharpen( img_t* img, float radius, float blend );
|
||||
|
||||
|
||||
#endif /* img_h */
|
||||
|
||||
/*
|
||||
----------------------
|
||||
IMPLEMENTATION
|
||||
----------------------
|
||||
*/
|
||||
|
||||
#ifdef IMG_IMPLEMENTATION
|
||||
#undef IMG_IMPLEMENTATION
|
||||
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#if !defined( IMG_POW ) || !defined( IMG_FLOOR )
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#pragma warning( push )
|
||||
#pragma warning( disable: 4668 ) // 'symbol' is not defined as a preprocessor macro, replacing with '0' for 'directives'
|
||||
#include <math.h>
|
||||
#pragma warning( pop )
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef IMG_POW
|
||||
#ifdef __TINYC__
|
||||
#define IMG_POW( x, y ) ( (float) pow( (double) x, (double) y ) )
|
||||
#else
|
||||
#define IMG_POW( x, y ) powf( x, y )
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef IMG_FLOOR
|
||||
#ifdef __TINYC__
|
||||
#define IMG_FLOOR( x ) ( (float) floor( (double) x ) )
|
||||
#else
|
||||
#define IMG_FLOOR( x ) floorf( x )
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
img_t img_create( int width, int height ) {
|
||||
img_t img;
|
||||
img.width = width;
|
||||
img.height = height;
|
||||
img.pixels = (img_rgba_t*) malloc( sizeof( img_rgba_t ) * width * height );
|
||||
memset( img.pixels, 0, sizeof( img_rgba_t ) * width * height );
|
||||
return img;
|
||||
}
|
||||
|
||||
|
||||
img_t img_from_abgr32( IMG_U32* abgr, int width, int height ) {
|
||||
img_t img;
|
||||
img.width = width;
|
||||
img.height = height;
|
||||
img.pixels = (img_rgba_t*) malloc( sizeof( img_rgba_t ) * width * height );
|
||||
for( int i = 0; i < width * height; ++i ) {
|
||||
IMG_U32 p = abgr[ i ];
|
||||
IMG_U32 b = ( p ) & 0xff;
|
||||
IMG_U32 g = ( p >> 8 ) & 0xff;
|
||||
IMG_U32 r = ( p >> 16 ) & 0xff;
|
||||
IMG_U32 a = ( p >> 24 ) & 0xff;
|
||||
img_rgba_t* f = &img.pixels[ i ];
|
||||
f->r = ( (float) r ) / 255.0f;
|
||||
f->g = ( (float) g ) / 255.0f;
|
||||
f->b = ( (float) b ) / 255.0f;
|
||||
f->a = ( (float) a ) / 255.0f;
|
||||
}
|
||||
return img;
|
||||
}
|
||||
|
||||
|
||||
void img_free( img_t* img ) {
|
||||
free( img->pixels );
|
||||
img->pixels = NULL;
|
||||
}
|
||||
|
||||
|
||||
float img_clamp( float x, float low, float high ) {
|
||||
return x < low ? low : x > high ? high : x;
|
||||
}
|
||||
|
||||
|
||||
void img_to_argb32( img_t* img, IMG_U32* abgr ) {
|
||||
for( int i = 0; i < img->width * img->height; ++i ) {
|
||||
img_rgba_t p = img->pixels[ i ];
|
||||
p.r = img_clamp( p.r, 0.0f, 1.0f );
|
||||
p.g = img_clamp( p.g, 0.0f, 1.0f );
|
||||
p.b = img_clamp( p.b, 0.0f, 1.0f );
|
||||
p.a = img_clamp( p.a, 0.0f, 1.0f );
|
||||
IMG_U32 r = (IMG_U32)( p.r * 256.0f );
|
||||
IMG_U32 g = (IMG_U32)( p.g * 256.0f );
|
||||
IMG_U32 b = (IMG_U32)( p.b * 256.0f );
|
||||
IMG_U32 a = (IMG_U32)( p.a * 256.0f );
|
||||
r = r > 255 ? 255 : r;
|
||||
g = g > 255 ? 255 : g;
|
||||
b = b > 255 ? 255 : b;
|
||||
a = a > 255 ? 255 : a;
|
||||
abgr[ i ] = ( a << 24 ) | ( r << 16 ) | ( g << 8 ) | ( b );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
img_rgba_t img_rgba_lerp( img_rgba_t a, img_rgba_t b, float s ) {
|
||||
img_rgba_t c;
|
||||
c.r = a.r + ( b.r - a.r ) * s;
|
||||
c.g = a.g + ( b.g - a.g ) * s;
|
||||
c.b = a.b + ( b.b - a.b ) * s;
|
||||
c.a = a.a + ( b.a - a.a ) * s;
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
img_rgba_t img_sample_clamp( img_t* img, float u, float v ) {
|
||||
int maxw = ( img->width - 1 );
|
||||
int maxh = ( img->height - 1 );
|
||||
|
||||
float fu = IMG_FLOOR( u );
|
||||
float du = u - fu;
|
||||
float fv = IMG_FLOOR( v );
|
||||
float dv = v - fv;
|
||||
|
||||
int x1 = (int) ( fu );
|
||||
int y1 = (int) ( fv );
|
||||
int x2 = (int) ( fu + 1.0f );
|
||||
int y2 = (int) ( fv + 1.0f );
|
||||
|
||||
x1 = x1 < 0 ? 0 : x1 > maxw ? maxw : x1;
|
||||
x2 = x2 < 0 ? 0 : x2 > maxw ? maxw : x2;
|
||||
y1 = y1 < 0 ? 0 : y1 > maxh ? maxh : y1;
|
||||
y2 = y2 < 0 ? 0 : y2 > maxh ? maxh : y2;
|
||||
|
||||
img_rgba_t c1 = img->pixels[ x1 + y1 * img->width ];
|
||||
img_rgba_t c2 = img->pixels[ x2 + y1 * img->width ];
|
||||
img_rgba_t c3 = img->pixels[ x1 + y2 * img->width ];
|
||||
img_rgba_t c4 = img->pixels[ x2 + y2 * img->width ];
|
||||
|
||||
return img_rgba_lerp( img_rgba_lerp( c1, c2, du ), img_rgba_lerp( c3, c4, du ), dv );
|
||||
}
|
||||
|
||||
|
||||
void img_adjust_brightness( img_t* img, float value ) {
|
||||
if( value == 0.0f ) return;
|
||||
for( int i = 0; i < img->width * img->height; ++i ) {
|
||||
img_rgba_t* p = &img->pixels[ i ];
|
||||
p->r = p->r + value;
|
||||
p->g = p->g + value;
|
||||
p->b = p->b + value;
|
||||
}
|
||||
}
|
||||
|
||||
void img_adjust_contrast( img_t* img, float value ) {
|
||||
if( value == 0.0f ) return;
|
||||
for( int i = 0; i < img->width * img->height; ++i ) {
|
||||
img_rgba_t* p = &img->pixels[ i ];
|
||||
p->r = ( p->r - 0.5f ) * value + 0.5f;
|
||||
p->g = ( p->g - 0.5f ) * value + 0.5f;
|
||||
p->b = ( p->b - 0.5f ) * value + 0.5f;
|
||||
}
|
||||
}
|
||||
|
||||
#define img_min( x, y ) ( (x) < (y) ? (x) : (y) )
|
||||
#define img_max( x, y ) ( (x) > (y) ? (x) : (y) )
|
||||
|
||||
img_hsva_t img_rgba_to_hsva( img_rgba_t rgb ) {
|
||||
img_hsva_t hsv;
|
||||
hsv.a = rgb.a;
|
||||
|
||||
float cmin = img_min( rgb.r, img_min( rgb.g, rgb.b ) ); //Min. value of RGB
|
||||
float cmax = img_max( rgb.r, img_max( rgb.g, rgb.b ) ); //Max. value of RGB
|
||||
float cdel = cmax - cmin; //Delta RGB value
|
||||
|
||||
hsv.v = cmax;
|
||||
|
||||
if( cdel == 0 ) { //This is a gray, no chroma...
|
||||
hsv.h = 0; //HSV results from 0 to 1
|
||||
hsv.s = 0;
|
||||
} else { //Chromatic data...
|
||||
hsv.s = cdel / cmax;
|
||||
|
||||
float rdel = ( ( ( cmax - rgb.r ) / 6.0f ) + ( cdel / 2.0f ) ) / cdel;
|
||||
float gdel = ( ( ( cmax - rgb.g ) / 6.0f ) + ( cdel / 2.0f ) ) / cdel;
|
||||
float bdel = ( ( ( cmax - rgb.b ) / 6.0f ) + ( cdel / 2.0f ) ) / cdel;
|
||||
|
||||
if( rgb.r == cmax ) {
|
||||
hsv.h = bdel - gdel;
|
||||
} else if( rgb.g == cmax ) {
|
||||
hsv.h = ( 1.0f / 3.0f ) + rdel - bdel;
|
||||
} else {
|
||||
hsv.h = ( 2.0f / 3.0f ) + gdel - rdel;
|
||||
}
|
||||
|
||||
if ( hsv.h < 0.0f ) {
|
||||
hsv.h += 1.0f;
|
||||
}
|
||||
if ( hsv.h > 1.0f ) {
|
||||
hsv.h -= 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
hsv.h = hsv.h;
|
||||
hsv.s = hsv.s;
|
||||
hsv.v = hsv.v;
|
||||
return hsv;
|
||||
}
|
||||
|
||||
|
||||
img_rgba_t img_hsva_to_rgba( img_hsva_t hsv ) {
|
||||
img_rgba_t rgb;
|
||||
rgb.a = hsv.a;
|
||||
|
||||
if( hsv.s == 0.0f ) { // HSV from 0 to 1
|
||||
rgb.r = hsv.v;
|
||||
rgb.g = hsv.v;
|
||||
rgb.b = hsv.v;
|
||||
} else {
|
||||
float h = hsv.h * 6.0f;
|
||||
if( h == 6.0f ) {
|
||||
h = 0.0f; //H must be < 1
|
||||
}
|
||||
float i = IMG_FLOOR( h );
|
||||
float v1 = hsv.v * ( 1.0f - hsv.s );
|
||||
float v2 = hsv.v * ( 1.0f - hsv.s * ( h - i ) );
|
||||
float v3 = hsv.v * ( 1.0f - hsv.s * ( 1.0f - ( h - i ) ) );
|
||||
|
||||
switch( (int) i ) {
|
||||
case 0: { rgb.r = hsv.v; rgb.g = v3 ; rgb.b = v1 ; } break;
|
||||
case 1: { rgb.r = v2 ; rgb.g = hsv.v; rgb.b = v1 ; } break;
|
||||
case 2: { rgb.r = v1 ; rgb.g = hsv.v; rgb.b = v3 ; } break;
|
||||
case 3: { rgb.r = v1 ; rgb.g = v2 ; rgb.b = hsv.v; } break;
|
||||
case 4: { rgb.r = v3 ; rgb.g = v1 ; rgb.b = hsv.v; } break;
|
||||
default:{ rgb.r = hsv.v; rgb.g = v1 ; rgb.b = v2 ; } break;
|
||||
}
|
||||
}
|
||||
|
||||
rgb.r = rgb.r;
|
||||
rgb.g = rgb.g;
|
||||
rgb.b = rgb.b;
|
||||
return rgb;
|
||||
}
|
||||
|
||||
|
||||
void img_adjust_saturation( img_t* img, float value ) {
|
||||
if( value == 0.0f ) return;
|
||||
for( int i = 0; i < img->width * img->height; ++i ) {
|
||||
img_rgba_t* p = &img->pixels[ i ];
|
||||
img_hsva_t hsv = img_rgba_to_hsva( *p );
|
||||
hsv.s = img_clamp( hsv.s + value, 0.0f, 1.0f );
|
||||
*p = img_hsva_to_rgba( hsv );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void img_sharpen( img_t* img, float radius, float blend ) {
|
||||
float filter[ 9 ] = {
|
||||
-1.0f, -1.0f, -1.0f,
|
||||
-1.0f, 9.0f, -1.0f,
|
||||
-1.0f, -1.0f, -1.0f,
|
||||
};
|
||||
|
||||
img_rgba_t* result = (img_rgba_t*) malloc( img->width * img->height * sizeof( img_rgba_t ) );
|
||||
|
||||
img_rgba_t* src = img->pixels;
|
||||
img_rgba_t* dst = result;
|
||||
for( int y = 0; y < img->height; ++y ) {
|
||||
for( int x = 0; x < img->width; ++x ) {
|
||||
if( src->a > 0.0f ) {
|
||||
img_rgba_t acc = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||
for( int fx = -1; fx <= 1; ++fx ) {
|
||||
for( int fy = -1; fy <= 1; ++fy ) {
|
||||
float u = ( (float) x ) + ( (float) fx ) * radius;
|
||||
float v = ( (float) y ) + ( (float) fy ) * radius;
|
||||
img_rgba_t s = img_sample_clamp( img, u, v );
|
||||
if( s.a < 0.0001f ) {
|
||||
s = *src;
|
||||
}
|
||||
acc.r += s.r * ( filter[ ( 1 + fx ) + ( 1 + fy ) * 3 ] );
|
||||
acc.g += s.g * ( filter[ ( 1 + fx ) + ( 1 + fy ) * 3 ] );
|
||||
acc.b += s.b * ( filter[ ( 1 + fx ) + ( 1 + fy ) * 3 ] );
|
||||
}
|
||||
}
|
||||
*dst = img_rgba_lerp( *src, acc, blend );
|
||||
dst->a = src->a;
|
||||
} else {
|
||||
*dst = *src;
|
||||
}
|
||||
++src;
|
||||
++dst;
|
||||
}
|
||||
}
|
||||
|
||||
free( img->pixels );
|
||||
img->pixels = result;
|
||||
}
|
||||
|
||||
#endif /* IMG_IMPLEMENTATION */
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This software is available under 2 licenses - you may choose the one you like.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE A - MIT License
|
||||
|
||||
Copyright (c) 2019 Mattias Gustavsson
|
||||
|
||||
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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||
software, either in source code form or as a compiled binary, for any purpose,
|
||||
commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||
software dedicate any and all copyright interest in the software to the public
|
||||
domain. We make this dedication for the benefit of the public at large and to
|
||||
the detriment of our heirs and successors. We intend this dedication to be an
|
||||
overt act of relinquishment in perpetuity of all present and future rights to
|
||||
this software under copyright law.
|
||||
|
||||
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 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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -0,0 +1,339 @@
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
Licensing information can be found at the end of the file.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
mus.h - v0.1 - Parsing library for MUS music files (as used in DOS games).
|
||||
|
||||
Do this:
|
||||
#define MUS_IMPLEMENTATION
|
||||
before you include this file in *one* C/C++ file to create the implementation.
|
||||
*/
|
||||
|
||||
#ifndef mus_h
|
||||
#define mus_h
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct mus_t mus_t;
|
||||
|
||||
mus_t* mus_create( void const* data, size_t size, void* memctx );
|
||||
void mus_destroy( mus_t* mus );
|
||||
|
||||
typedef enum mus_cmd_t {
|
||||
MUS_CMD_RELEASE_NOTE,
|
||||
MUS_CMD_PLAY_NOTE,
|
||||
MUS_CMD_PITCH_BEND,
|
||||
MUS_CMD_SYSTEM_EVENT,
|
||||
MUS_CMD_CONTROLLER,
|
||||
MUS_CMD_END_OF_MEASURE,
|
||||
MUS_CMD_FINISH,
|
||||
MUS_CMD_RENDER_SAMPLES,
|
||||
} mus_cmd_t;
|
||||
|
||||
struct mus_cmd_release_note_t {
|
||||
int note;
|
||||
};
|
||||
|
||||
struct mus_cmd_play_note_t {
|
||||
int note;
|
||||
int volume;
|
||||
};
|
||||
|
||||
struct mus_cmd_pitch_bend_t {
|
||||
int bend_amount;
|
||||
};
|
||||
|
||||
enum mus_system_event_t {
|
||||
MUS_SYSTEM_EVENT_ALL_SOUNDS_OFF,
|
||||
MUS_SYSTEM_EVENT_ALL_NOTES_OFF,
|
||||
MUS_SYSTEM_EVENT_MONO,
|
||||
MUS_SYSTEM_EVENT_POLY,
|
||||
MUS_SYSTEM_EVENT_RESET_ALL_CONTROLLERS,
|
||||
};
|
||||
|
||||
struct mus_cmd_system_event_t {
|
||||
enum mus_system_event_t event;
|
||||
};
|
||||
|
||||
enum mus_controller_t {
|
||||
MUS_CONTROLLER_CHANGE_INSTRUMENT,
|
||||
MUS_CONTROLLER_BANK_SELECT,
|
||||
MUS_CONTROLLER_MODULATION,
|
||||
MUS_CONTROLLER_VOLUME,
|
||||
MUS_CONTROLLER_PAN,
|
||||
MUS_CONTROLLER_EXPRESSION,
|
||||
MUS_CONTROLLER_REVERB_DEPTH,
|
||||
MUS_CONTROLLER_CHORUS_DEPTH,
|
||||
MUS_CONTROLLER_SUSTAIN_PEDAL,
|
||||
MUS_CONTROLLER_SOFT_PEDAL,
|
||||
};
|
||||
|
||||
struct mus_cmd_controller_t {
|
||||
enum mus_controller_t controller;
|
||||
int value;
|
||||
};
|
||||
|
||||
struct mus_cmd_render_samples_t {
|
||||
int samples_count;
|
||||
};
|
||||
|
||||
union mus_cmd_data_t {
|
||||
struct mus_cmd_release_note_t release_note;
|
||||
struct mus_cmd_play_note_t play_note;
|
||||
struct mus_cmd_pitch_bend_t pitch_bend;
|
||||
struct mus_cmd_system_event_t system_event;
|
||||
struct mus_cmd_controller_t controller;
|
||||
struct mus_cmd_render_samples_t render_samples;
|
||||
};
|
||||
|
||||
typedef struct mus_event_t {
|
||||
int channel;
|
||||
enum mus_cmd_t cmd;
|
||||
union mus_cmd_data_t data;
|
||||
} mus_event_t;
|
||||
|
||||
|
||||
void mus_next_event( mus_t* mus, mus_event_t* event );
|
||||
|
||||
void mus_restart( mus_t* mus );
|
||||
|
||||
#endif /* mus_h */
|
||||
|
||||
|
||||
/*
|
||||
----------------------
|
||||
IMPLEMENTATION
|
||||
----------------------
|
||||
*/
|
||||
|
||||
#ifdef MUS_IMPLEMENTATION
|
||||
#undef MUS_IMPLEMENTATION
|
||||
|
||||
#if !defined( MUS_MALLOC ) || !defined( MUS_FREE )
|
||||
#include <stdlib.h>
|
||||
#define MUS_MALLOC malloc
|
||||
#define MUS_FREE free
|
||||
#endif
|
||||
|
||||
struct mus_t {
|
||||
int note_volume[ 16 ];
|
||||
int accumulated_delay;
|
||||
int pos;
|
||||
int length;
|
||||
uint8_t data[ 1 ]; // "open" array
|
||||
};
|
||||
|
||||
mus_t* mus_create( void const* data, size_t size, void* memctx ) {
|
||||
(void) memctx, (void) size;
|
||||
uintptr_t ptr = (uintptr_t) data;
|
||||
uint32_t sig = *(uint32_t*) ptr;
|
||||
ptr += 4;
|
||||
if( sig != 0x1a53554d ) { // Identifier "MUS" followed by 0x1A
|
||||
return NULL;
|
||||
}
|
||||
int length = *(uint16_t*) ptr;
|
||||
ptr += 2;
|
||||
|
||||
int offset = *(uint16_t*) ptr;
|
||||
ptr += 2;
|
||||
|
||||
if( (size_t)( length + offset ) > size ) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mus_t* mus = (mus_t*) MUS_MALLOC( sizeof( mus_t ) + length - 1 );
|
||||
mus->length = length;
|
||||
memcpy( mus->data, (void*)( ((uintptr_t)data) + offset ), length );
|
||||
mus_restart( mus );
|
||||
return mus;
|
||||
}
|
||||
|
||||
|
||||
void mus_destroy( mus_t* mus ) {
|
||||
MUS_FREE( mus );
|
||||
}
|
||||
|
||||
|
||||
void mus_next_event( mus_t* mus, mus_event_t* event ) {
|
||||
if( mus->accumulated_delay ) {
|
||||
int samples_count = ( mus->accumulated_delay * 44100 ) / 140;
|
||||
event->channel = 0;
|
||||
event->cmd = MUS_CMD_RENDER_SAMPLES;
|
||||
event->data.render_samples.samples_count = samples_count;
|
||||
mus->accumulated_delay = 0;
|
||||
return;
|
||||
}
|
||||
while( mus->pos < mus->length ) {
|
||||
uint8_t data = mus->data[ mus->pos++ ];
|
||||
int channel = data & 15;
|
||||
int type = ( data >> 4 ) & 7;
|
||||
int last = ( data >> 7 ) & 1;
|
||||
int event_valid = 0;
|
||||
switch( type ) {
|
||||
case 0: { // Release Note
|
||||
data = mus->data[ mus->pos++ ];
|
||||
int note = data & 127;
|
||||
event->channel = channel;
|
||||
event->cmd = MUS_CMD_RELEASE_NOTE;
|
||||
event->data.release_note.note = note;
|
||||
event_valid = 1;
|
||||
} break;
|
||||
|
||||
case 1: { // Play Note
|
||||
data = mus->data[ mus->pos++ ];
|
||||
int note = data & 127;
|
||||
int has_vol = ( data >> 7 );
|
||||
if( has_vol ) {
|
||||
data = mus->data[ mus->pos++ ];
|
||||
mus->note_volume[ channel ] = data;
|
||||
}
|
||||
event->channel = channel;
|
||||
event->cmd = MUS_CMD_PLAY_NOTE;
|
||||
event->data.play_note.note = note;
|
||||
event->data.play_note.volume = mus->note_volume[ channel ];
|
||||
event_valid = 1;
|
||||
} break;
|
||||
|
||||
case 2: { // Pitch Bend
|
||||
data = mus->data[ mus->pos++ ];
|
||||
int bend_amount = data;
|
||||
event->channel = channel;
|
||||
event->cmd = MUS_CMD_PITCH_BEND;
|
||||
event->data.pitch_bend.bend_amount = bend_amount;
|
||||
event_valid = 1;
|
||||
} break;
|
||||
|
||||
case 3: { // System Event
|
||||
data = mus->data[ mus->pos++ ];
|
||||
int sysevent = data & 127;
|
||||
event->channel = channel;
|
||||
event->cmd = MUS_CMD_SYSTEM_EVENT;
|
||||
event->data.system_event.event = (enum mus_system_event_t)sysevent;
|
||||
event_valid = 1;
|
||||
} break;
|
||||
|
||||
case 4: { // Controller
|
||||
data = mus->data[ mus->pos++ ];
|
||||
int controller = data & 127;
|
||||
data = mus->data[ mus->pos++ ];
|
||||
int value = data & 127;
|
||||
event->channel = channel;
|
||||
event->cmd = MUS_CMD_CONTROLLER;
|
||||
event->data.controller.controller = (enum mus_controller_t)controller;
|
||||
event->data.controller.value = value;
|
||||
event_valid = 1;
|
||||
} break;
|
||||
|
||||
case 5: { // End of Measure
|
||||
event->channel = channel;
|
||||
event->cmd = MUS_CMD_END_OF_MEASURE;
|
||||
event_valid = 1;
|
||||
} break;
|
||||
|
||||
case 6: { // Finish
|
||||
event->channel = channel;
|
||||
event->cmd = MUS_CMD_END_OF_MEASURE;
|
||||
event_valid = 1;
|
||||
} break;
|
||||
|
||||
case 7: { // Unused
|
||||
data = mus->data[ mus->pos++ ];
|
||||
} break;
|
||||
}
|
||||
|
||||
if( last ) {
|
||||
int delay = 0;
|
||||
for( ; ; ) {
|
||||
data = mus->data[ mus->pos++ ];
|
||||
delay = delay * 128 + ( data & 127 );
|
||||
if( ( data >> 7 ) == 0 ) {
|
||||
if( event_valid ) {
|
||||
mus->accumulated_delay = delay;
|
||||
} else {
|
||||
int samples_count = ( delay * 44100 ) / 140;
|
||||
event->channel = 0;
|
||||
event->cmd = MUS_CMD_RENDER_SAMPLES;
|
||||
event->data.render_samples.samples_count = samples_count;
|
||||
event_valid = 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( event_valid ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
event->channel = 0;
|
||||
event->cmd = MUS_CMD_FINISH;
|
||||
}
|
||||
|
||||
|
||||
void mus_restart( mus_t* mus ) {
|
||||
mus->accumulated_delay = 0;
|
||||
for( int i = 0; i < sizeof( mus->note_volume ) / sizeof( *mus->note_volume ); ++i ){
|
||||
mus->note_volume[ i ] = 127;
|
||||
}
|
||||
mus->pos = 0;
|
||||
}
|
||||
|
||||
|
||||
#endif /* MUS_IMPLEMENTATION */
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This software is available under 2 licenses - you may choose the one you like.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE A - MIT License
|
||||
|
||||
Copyright (c) 2021 Mattias Gustavsson
|
||||
|
||||
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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||
software, either in source code form or as a compiled binary, for any purpose,
|
||||
commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||
software dedicate any and all copyright interest in the software to the public
|
||||
domain. We make this dedication for the benefit of the public at large and to
|
||||
the detriment of our heirs and successors. We intend this dedication to be an
|
||||
overt act of relinquishment in perpetuity of all present and future rights to
|
||||
this software under copyright law.
|
||||
|
||||
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 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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
*/
|
||||
+682
@@ -0,0 +1,682 @@
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
Licensing information can be found at the end of the file.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
paldither.h - v0.1 - Convert true-color image to custom palette, with dither.
|
||||
|
||||
Do this:
|
||||
#define PALDITHER_IMPLEMENTATION
|
||||
before you include this file in *one* C/C++ file to create the implementation.
|
||||
*/
|
||||
|
||||
#ifndef paldither_h
|
||||
#define paldither_h
|
||||
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stddef.h>
|
||||
|
||||
#ifndef PALDITHER_U32
|
||||
#define PALDITHER_U32 unsigned int
|
||||
#endif
|
||||
|
||||
#ifndef PALDITHER_U8
|
||||
#define PALDITHER_U8 unsigned char
|
||||
#endif
|
||||
|
||||
|
||||
typedef enum paldither_type_t
|
||||
{
|
||||
PALDITHER_TYPE_DEFAULT,
|
||||
PALDITHER_TYPE_BAYER,
|
||||
PALDITHER_TYPE_NONE,
|
||||
} paldither_type_t;
|
||||
|
||||
|
||||
typedef struct paldither_palette_t
|
||||
{
|
||||
void* memctx;
|
||||
int color_count;
|
||||
PALDITHER_U32 colortable[ 256 ];
|
||||
} paldither_palette_t;
|
||||
|
||||
paldither_palette_t* paldither_palette_create( PALDITHER_U32 const* xbgr, int count, size_t* palette_size, void* memctx );
|
||||
paldither_palette_t* paldither_palette_create_from_data( void const* data, size_t size, void* memctx );
|
||||
|
||||
void paldither_palette_destroy( paldither_palette_t* palette );
|
||||
|
||||
void paldither_palettize( PALDITHER_U32* abgr, int width, int height, paldither_palette_t const* palette,
|
||||
paldither_type_t dither_type, PALDITHER_U8* output );
|
||||
|
||||
#endif /* paldither_h */
|
||||
|
||||
|
||||
/*
|
||||
----------------------
|
||||
IMPLEMENTATION
|
||||
----------------------
|
||||
*/
|
||||
|
||||
#ifdef PALDITHER_IMPLEMENTATION
|
||||
#undef PALDITHER_IMPLEMENTATION
|
||||
|
||||
#ifndef PALDITHER_MALLOC
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdlib.h>
|
||||
#if defined(__cplusplus)
|
||||
#define PALDITHER_MALLOC( ctx, size ) ( ::malloc( size ) )
|
||||
#define PALDITHER_FREE( ctx, ptr ) ( ::free( ptr ) )
|
||||
#else
|
||||
#define PALDITHER_MALLOC( ctx, size ) ( malloc( size ) )
|
||||
#define PALDITHER_FREE( ctx, ptr ) ( free( ptr ) )
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef PALDITHER_ASSERT
|
||||
#undef _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#undef _CRT_SECURE_NO_WARNINGS
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <assert.h>
|
||||
#define PALDITHER_ASSERT( expression, message ) assert( ( expression ) && ( message ) )
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
typedef struct paldither_mix_t
|
||||
{
|
||||
int first;
|
||||
int second;
|
||||
int d;
|
||||
unsigned char ratio;
|
||||
unsigned char r;
|
||||
unsigned char g;
|
||||
unsigned char b;
|
||||
unsigned char l;
|
||||
} paldither_mix_t;
|
||||
|
||||
|
||||
static void paldither_internal_mix( void* memctx, PALDITHER_U32 const* xbgr, int count, int mix_levels,
|
||||
paldither_mix_t** out_mix, int* out_mix_count )
|
||||
{
|
||||
(void) memctx;
|
||||
|
||||
int mix_count = ( ( count / 2 ) * ( count + 1 ) ) * mix_levels;
|
||||
paldither_mix_t* mix = (paldither_mix_t*) PALDITHER_MALLOC( memctx, mix_count * sizeof( paldither_mix_t ) );
|
||||
|
||||
int c = 0;
|
||||
for( int i = 0; i < count; ++i )
|
||||
{
|
||||
PALDITHER_U32 fcolor = xbgr[ i ] & 0x00ffffff;
|
||||
int fr = (int)( ( fcolor & 0x000000ff ) );
|
||||
int fg = (int)( ( fcolor & 0x0000ff00 ) >> 8 );
|
||||
int fb = (int)( ( fcolor & 0x00ff0000 ) >> 16 );
|
||||
int fl = (int)( ( 54 * fr + 183 * fg + 19 * fb + 127 ) >> 8 );
|
||||
PALDITHER_ASSERT( fl <= 0xff && fl >= 0, "Value out of range" );
|
||||
|
||||
for( int j = i + 1; j < count; ++j )
|
||||
{
|
||||
PALDITHER_U32 scolor = xbgr[ j ] & 0x00ffffff;
|
||||
int sr = (int)( ( scolor & 0x000000ff ) );
|
||||
int sg = (int)( ( scolor & 0x0000ff00 ) >> 8 );
|
||||
int sb = (int)( ( scolor & 0x00ff0000 ) >> 16 );
|
||||
int sl = (int)( ( 54 * sr + 183 * sg + 19 * sb + 127 ) >> 8 );
|
||||
PALDITHER_ASSERT( sl <= 0xff && sl >= 0, "Value out of range" );
|
||||
|
||||
int dr = fr - sr;
|
||||
int dg = fg - sg;
|
||||
int db = fb - sb;
|
||||
int dl = fl - sl;
|
||||
int d = ( ( ( ( dr*dr + dg*dg + db*db ) >> 1 ) + dl*dl ) + 127 ) >> 8;
|
||||
|
||||
for( int k = 0; k < mix_levels; ++k )
|
||||
{
|
||||
int r = fr;
|
||||
int g = fg;
|
||||
int b = fb;
|
||||
int l = fl;
|
||||
if( mix_levels > 1 )
|
||||
{
|
||||
int s = mix_levels - 1;
|
||||
int ik = s - k;
|
||||
r = ( r * ik + sr * k + ( mix_levels / 2 ) ) / s;
|
||||
g = ( g * ik + sg * k + ( mix_levels / 2 ) ) / s;
|
||||
b = ( b * ik + sb * k + ( mix_levels / 2 ) ) / s;
|
||||
l = ( 54 * r + 183 * g + 19 * b + 127 ) >> 8;
|
||||
PALDITHER_ASSERT( r <= 0xff && g <= 0xff && b <= 0xff && r >= 0 && g >= 0 && b >= 0 && l <= 0xff && l >= 0, "Value out of range" );
|
||||
}
|
||||
|
||||
mix[ c ].first = i;
|
||||
mix[ c ].second = j;
|
||||
mix[ c ].ratio = (unsigned char) k;
|
||||
mix[ c ].r = (unsigned char) r;
|
||||
mix[ c ].g = (unsigned char) g;
|
||||
mix[ c ].b = (unsigned char) b;
|
||||
mix[ c ].l = (unsigned char) l;
|
||||
mix[ c ].d = d;
|
||||
++c;
|
||||
PALDITHER_ASSERT( mix_count >= c, "Out of range" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*out_mix = mix;
|
||||
*out_mix_count = c;
|
||||
}
|
||||
|
||||
|
||||
static void paldither_internal_list( void* memctx, paldither_mix_t* mix, int mix_count,
|
||||
int out_map[ 16 * 16 * 16 ], int** out_list, int* out_list_count )
|
||||
{
|
||||
(void) memctx;
|
||||
int cube[ 17 * 17 * 17 ];
|
||||
for( int r = 0; r < 17; ++r )
|
||||
{
|
||||
for( int g = 0; g < 17; ++g )
|
||||
{
|
||||
for( int b = 0; b < 17; ++b )
|
||||
{
|
||||
int best_diff = 0x7FFFFFFF;
|
||||
int best_mix = 0;
|
||||
paldither_mix_t const* m = mix;
|
||||
int l = ( 54 * (r * 16) + 183 * (g * 16) + 19 * (b * 16) + 127 ) >> 8;
|
||||
for( int i = 0; i < mix_count; ++i, ++m )
|
||||
{
|
||||
int dr = r * 16 - m->r;
|
||||
int dg = g * 16 - m->g;
|
||||
int db = b * 16 - m->b;
|
||||
int dl = l - m->l;
|
||||
int d = ( ( ( dr*dr + dg*dg + db*db ) >> 1 ) + dl*dl) + ( m->d * 3 );
|
||||
if( i == 0 || d < best_diff ) { best_diff = d; best_mix = i; }
|
||||
}
|
||||
cube[ r * 17 * 17 + g * 17 + b ] = best_mix;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int map[ 16 * 16 * 16 ];
|
||||
int list_capacity = mix_count * 256;
|
||||
int* list = (int*) PALDITHER_MALLOC( memctx, list_capacity * sizeof( int ) );
|
||||
int list_count = 0;
|
||||
|
||||
int* mapptr = map;
|
||||
for( int r = 0; r < 16; ++r )
|
||||
{
|
||||
for( int g = 0; g < 16; ++g )
|
||||
{
|
||||
for( int b = 0; b < 16; ++b )
|
||||
{
|
||||
paldither_mix_t const* m = mix;
|
||||
*mapptr++ = list_count;
|
||||
if( list_count == list_capacity )
|
||||
{
|
||||
list_capacity *= 2;
|
||||
int* new_list = (int*) PALDITHER_MALLOC( memctx, list_capacity * sizeof( int ) );
|
||||
memcpy( new_list, list, list_count * sizeof( int ) );
|
||||
PALDITHER_FREE( memctx, list );
|
||||
list = new_list;
|
||||
}
|
||||
int count_index = list_count++;
|
||||
list[ count_index ] = 0;
|
||||
for( int i = 0; i < mix_count; ++i, ++m )
|
||||
{
|
||||
paldither_mix_t const* best_mix = &mix[ cube[ ( r ) * 17 * 17 + ( g ) * 17 + ( b ) ] ];
|
||||
paldither_mix_t const* best_mix2 = &mix[ cube[ ( r + 1 ) * 17 * 17 + ( g + 1 ) * 17 + ( b + 1 ) ] ];
|
||||
int pass =
|
||||
( m->r >= best_mix->r && m->r <= best_mix2->r )
|
||||
&& ( m->g >= best_mix->g && m->g <= best_mix2->g )
|
||||
&& ( m->b >= best_mix->b && m->b <= best_mix2->b );
|
||||
if( pass )
|
||||
{
|
||||
list[ count_index ]++;
|
||||
if( list_count == list_capacity )
|
||||
{
|
||||
list_capacity *= 2;
|
||||
int* new_list = (int*) PALDITHER_MALLOC( memctx, list_capacity * sizeof( int ) );
|
||||
memcpy( new_list, list, list_count * sizeof( int ) );
|
||||
PALDITHER_FREE( memctx, list );
|
||||
list = new_list;
|
||||
}
|
||||
list[ list_count++ ] = i;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
memcpy( out_map, map, sizeof( map ) );
|
||||
*out_list = list;
|
||||
*out_list_count = list_count;
|
||||
}
|
||||
|
||||
|
||||
paldither_palette_t* paldither_palette_create( PALDITHER_U32 const* xbgr, int count, size_t* palette_size, void* memctx )
|
||||
{
|
||||
(void) memctx;
|
||||
|
||||
int default_mix_count;
|
||||
paldither_mix_t* default_mix;
|
||||
paldither_internal_mix( memctx, xbgr, count, 11, &default_mix, &default_mix_count );
|
||||
|
||||
int default_map[ 16 * 16 * 16 ];
|
||||
int* default_list;
|
||||
int default_list_count = 0;
|
||||
paldither_internal_list( memctx, default_mix, default_mix_count, default_map, &default_list, &default_list_count );
|
||||
|
||||
int bayer_mix_count;
|
||||
paldither_mix_t* bayer_mix;
|
||||
paldither_internal_mix( memctx, xbgr, count, 17, &bayer_mix, &bayer_mix_count );
|
||||
|
||||
int bayer_map[ 16 * 16 * 16 ];
|
||||
int* bayer_list;
|
||||
int bayer_list_count = 0;
|
||||
paldither_internal_list( memctx, bayer_mix, bayer_mix_count, bayer_map, &bayer_list, &bayer_list_count );
|
||||
|
||||
int nodither_mix_count;
|
||||
paldither_mix_t* nodither_mix;
|
||||
paldither_internal_mix( memctx, xbgr, count, 1, &nodither_mix, &nodither_mix_count );
|
||||
|
||||
int nodither_map[ 16 * 16 * 16 ];
|
||||
int* nodither_list;
|
||||
int nodither_list_count = 0;
|
||||
paldither_internal_list( memctx, nodither_mix, nodither_mix_count, nodither_map, &nodither_list, &nodither_list_count );
|
||||
|
||||
size_t size = sizeof( paldither_palette_t ) +
|
||||
sizeof( int ) + default_list_count * sizeof( int ) +
|
||||
sizeof( int ) + default_mix_count * sizeof( paldither_mix_t ) +
|
||||
sizeof( default_map ) +
|
||||
sizeof( int ) + bayer_list_count * sizeof( int ) +
|
||||
sizeof( int ) + bayer_mix_count * sizeof( paldither_mix_t ) +
|
||||
sizeof( bayer_map ) +
|
||||
sizeof( int ) + nodither_list_count * sizeof( int ) +
|
||||
sizeof( int ) + nodither_mix_count * sizeof( paldither_mix_t ) +
|
||||
sizeof( nodither_map );
|
||||
paldither_palette_t* palette = (paldither_palette_t*) PALDITHER_MALLOC( memctx, size );
|
||||
palette->memctx = memctx;
|
||||
|
||||
palette->color_count = count;
|
||||
memcpy( palette->colortable, xbgr, sizeof( *xbgr ) * count );
|
||||
|
||||
uintptr_t dest = (uintptr_t)( palette + 1) ;
|
||||
|
||||
*(int*) dest = default_mix_count; dest += sizeof( int );
|
||||
memcpy( (void*)dest, default_mix, default_mix_count * sizeof( paldither_mix_t ) );
|
||||
dest += default_mix_count * sizeof( paldither_mix_t );
|
||||
|
||||
memcpy( (void*)dest, default_map, sizeof( default_map ) );
|
||||
dest += sizeof( default_map );
|
||||
|
||||
*(int*) dest = default_list_count; dest += sizeof( int );
|
||||
memcpy( (void*)dest, default_list, default_list_count * sizeof( int ) );
|
||||
dest += default_list_count * sizeof( int );
|
||||
|
||||
PALDITHER_FREE( memctx, default_list );
|
||||
PALDITHER_FREE( memctx, default_mix );
|
||||
|
||||
*(int*) dest = bayer_mix_count; dest += sizeof( int );
|
||||
memcpy( (void*)dest, bayer_mix, bayer_mix_count * sizeof( paldither_mix_t ) );
|
||||
dest += bayer_mix_count * sizeof( paldither_mix_t );
|
||||
|
||||
memcpy( (void*)dest, bayer_map, sizeof( bayer_map ) );
|
||||
dest += sizeof( bayer_map );
|
||||
|
||||
*(int*) dest = bayer_list_count; dest += sizeof( int );
|
||||
memcpy( (void*)dest, bayer_list, bayer_list_count * sizeof( int ) );
|
||||
dest += bayer_list_count * sizeof( int );
|
||||
|
||||
PALDITHER_FREE( memctx, bayer_list );
|
||||
PALDITHER_FREE( memctx, bayer_mix );
|
||||
|
||||
*(int*) dest = nodither_mix_count; dest += sizeof( int );
|
||||
memcpy( (void*)dest, nodither_mix, nodither_mix_count * sizeof( paldither_mix_t ) );
|
||||
dest += nodither_mix_count * sizeof( paldither_mix_t );
|
||||
|
||||
memcpy( (void*)dest, nodither_map, sizeof( nodither_map ) );
|
||||
dest += sizeof( nodither_map );
|
||||
|
||||
*(int*) dest = nodither_list_count; dest += sizeof( int );
|
||||
memcpy( (void*)dest, nodither_list, nodither_list_count * sizeof( int ) );
|
||||
dest += nodither_list_count * sizeof( int );
|
||||
|
||||
PALDITHER_FREE( memctx, nodither_list );
|
||||
PALDITHER_FREE( memctx, nodither_mix );
|
||||
|
||||
if( palette_size ) *palette_size = size;
|
||||
return palette;
|
||||
}
|
||||
|
||||
|
||||
paldither_palette_t* paldither_palette_create_from_data( void const* data, size_t size, void* memctx )
|
||||
{
|
||||
paldither_palette_t* palette = (paldither_palette_t*)PALDITHER_MALLOC( memctx, size );
|
||||
palette->memctx = memctx;
|
||||
memcpy( &palette->color_count, data, size );
|
||||
return palette;
|
||||
}
|
||||
|
||||
|
||||
void paldither_palette_destroy( paldither_palette_t* palette )
|
||||
{
|
||||
PALDITHER_FREE( palette->memctx, palette );
|
||||
}
|
||||
|
||||
|
||||
void paldither_palettize( PALDITHER_U32* abgr, int width, int height, paldither_palette_t const* palette,
|
||||
paldither_type_t dither_type, PALDITHER_U8* output )
|
||||
{
|
||||
unsigned char default_dither_pattern[ 4 * 4 * 11 ] =
|
||||
{
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
0,0,0,0,
|
||||
0,0,0,1,
|
||||
|
||||
1,0,1,0,
|
||||
0,1,0,0,
|
||||
1,0,1,0,
|
||||
0,0,0,1,
|
||||
|
||||
1,0,1,0,
|
||||
0,1,0,1,
|
||||
1,0,1,0,
|
||||
0,1,0,1,
|
||||
|
||||
1,0,1,0,
|
||||
1,1,0,1,
|
||||
1,0,1,0,
|
||||
0,1,1,1,
|
||||
|
||||
1,1,1,1,
|
||||
1,1,1,1,
|
||||
1,1,1,1,
|
||||
1,1,1,1,
|
||||
|
||||
1,1,1,1,
|
||||
1,1,1,1,
|
||||
1,1,1,1,
|
||||
1,1,1,1,
|
||||
|
||||
1,1,1,1,
|
||||
1,1,1,1,
|
||||
1,1,1,1,
|
||||
1,1,1,1,
|
||||
|
||||
1,1,1,1,
|
||||
1,1,1,1,
|
||||
1,1,1,1,
|
||||
1,1,1,1,
|
||||
|
||||
};
|
||||
|
||||
unsigned char bayer_dither_pattern[ 4 * 4 * 17 ] =
|
||||
{
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
|
||||
1, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
|
||||
1, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 0,
|
||||
|
||||
1, 0, 1, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 0,
|
||||
|
||||
1, 0, 1, 0,
|
||||
0, 0, 0, 0,
|
||||
1, 0, 1, 0,
|
||||
0, 0, 0, 0,
|
||||
|
||||
1, 0, 1, 0,
|
||||
0, 1, 0, 0,
|
||||
1, 0, 1, 0,
|
||||
0, 0, 0, 0,
|
||||
|
||||
1, 0, 1, 0,
|
||||
0, 1, 0, 0,
|
||||
1, 0, 1, 0,
|
||||
0, 0, 0, 1,
|
||||
|
||||
1, 0, 1, 0,
|
||||
0, 1, 0, 1,
|
||||
1, 0, 1, 0,
|
||||
0, 0, 0, 1,
|
||||
|
||||
1, 0, 1, 0,
|
||||
0, 1, 0, 1,
|
||||
1, 0, 1, 0,
|
||||
0, 1, 0, 1,
|
||||
|
||||
1, 1, 1, 0,
|
||||
0, 1, 0, 1,
|
||||
1, 0, 1, 0,
|
||||
0, 1, 0, 1,
|
||||
|
||||
1, 1, 1, 0,
|
||||
0, 1, 0, 1,
|
||||
1, 0, 1, 1,
|
||||
0, 1, 0, 1,
|
||||
|
||||
1, 1, 1, 1,
|
||||
0, 1, 0, 1,
|
||||
1, 0, 1, 1,
|
||||
0, 1, 0, 1,
|
||||
|
||||
1, 1, 1, 1,
|
||||
0, 1, 0, 1,
|
||||
1, 1, 1, 1,
|
||||
0, 1, 0, 1,
|
||||
|
||||
1, 1, 1, 1,
|
||||
1, 1, 0, 1,
|
||||
1, 1, 1, 1,
|
||||
0, 1, 0, 1,
|
||||
|
||||
1, 1, 1, 1,
|
||||
1, 1, 0, 1,
|
||||
1, 1, 1, 1,
|
||||
0, 1, 1, 1,
|
||||
|
||||
1, 1, 1, 1,
|
||||
1, 1, 1, 1,
|
||||
1, 1, 1, 1,
|
||||
0, 1, 1, 1,
|
||||
|
||||
1, 1, 1, 1,
|
||||
1, 1, 1, 1,
|
||||
1, 1, 1, 1,
|
||||
1, 1, 1, 1,
|
||||
};
|
||||
|
||||
unsigned char none_dither_pattern[ 4 * 4 * 2 ] =
|
||||
{
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
};
|
||||
|
||||
|
||||
uintptr_t ptr = (uintptr_t)( palette + 1 );
|
||||
|
||||
int default_pal_mix_count = *(int*)ptr; ptr += sizeof( int );
|
||||
paldither_mix_t const* default_pal_mix = (paldither_mix_t*) ptr; ptr += default_pal_mix_count * sizeof( paldither_mix_t );
|
||||
int const* default_pal_map = (int*) ptr; ptr += 16 * 16 * 16 * sizeof( int );
|
||||
int default_pal_list_count = *(int*)ptr; ptr += sizeof( int );
|
||||
int const* default_pal_list = (int*) ptr; ptr += default_pal_list_count * sizeof( int );
|
||||
|
||||
int bayer_pal_mix_count = *(int*)ptr; ptr += sizeof( int );
|
||||
paldither_mix_t const* bayer_pal_mix = (paldither_mix_t*) ptr; ptr += bayer_pal_mix_count * sizeof( paldither_mix_t );
|
||||
int const* bayer_pal_map = (int*) ptr; ptr += 16 * 16 * 16 * sizeof( int );
|
||||
int bayer_pal_list_count = *(int*)ptr; ptr += sizeof( int );
|
||||
int const* bayer_pal_list = (int*) ptr; ptr += bayer_pal_list_count * sizeof( int );
|
||||
|
||||
int nodither_pal_mix_count = *(int*)ptr; ptr += sizeof( int );
|
||||
paldither_mix_t const* nodither_pal_mix = (paldither_mix_t*) ptr; ptr += nodither_pal_mix_count * sizeof( paldither_mix_t );
|
||||
int const* nodither_pal_map = (int*) ptr; ptr += 16 * 16 * 16 * sizeof( int );
|
||||
int nodither_pal_list_count = *(int*)ptr; ptr += sizeof( int );
|
||||
int const* nodither_pal_list = (int*) ptr; ptr += nodither_pal_list_count * sizeof( int );
|
||||
|
||||
unsigned char* dither_pattern = default_dither_pattern;
|
||||
int pal_mix_count = default_pal_mix_count;
|
||||
paldither_mix_t const* pal_mix = default_pal_mix;
|
||||
int const* pal_map = default_pal_map;
|
||||
int const* pal_list = default_pal_list;
|
||||
|
||||
if( dither_type == PALDITHER_TYPE_BAYER )
|
||||
{
|
||||
dither_pattern = bayer_dither_pattern;
|
||||
pal_mix_count = bayer_pal_mix_count;
|
||||
pal_mix = bayer_pal_mix;
|
||||
pal_map = bayer_pal_map;
|
||||
pal_list = bayer_pal_list;
|
||||
}
|
||||
else if( dither_type == PALDITHER_TYPE_NONE )
|
||||
{
|
||||
dither_pattern = none_dither_pattern;
|
||||
pal_mix_count = nodither_pal_mix_count;
|
||||
pal_mix = nodither_pal_mix;
|
||||
pal_map = nodither_pal_map;
|
||||
pal_list = nodither_pal_list;
|
||||
}
|
||||
|
||||
for( int y = 0; y < height; ++y )
|
||||
{
|
||||
for( int x = 0; x < width; ++x )
|
||||
{
|
||||
PALDITHER_U32 color = abgr[ x + y * width ];
|
||||
int r = (int)( ( color & 0x000000ff ) );
|
||||
int g = (int)( ( color & 0x0000ff00 ) >> 8 );
|
||||
int b = (int)( ( color & 0x00ff0000 ) >> 16 );
|
||||
int l = (int)( ( 54 * r + 183 * g + 19 * b + 127 ) >> 8 );
|
||||
PALDITHER_ASSERT( l <= 0xff && l >= 0, "Value out of range" );
|
||||
|
||||
paldither_mix_t const* best_mix = 0;
|
||||
int pal_index = pal_map[ ( r >> 4 ) * 16 * 16 + ( g >> 4 ) * 16 + ( b >> 4 ) ];
|
||||
int count = pal_list[ pal_index++ ];
|
||||
if( count != 0 )
|
||||
{
|
||||
int best_diff = 0x7FFFFFFF;
|
||||
int const* index = &pal_list[ pal_index ];
|
||||
for( int i = 0; i < count; ++i, ++index )
|
||||
{
|
||||
paldither_mix_t const* m = &pal_mix[ *index ];
|
||||
int dr = r - m->r;
|
||||
int dg = g - m->g;
|
||||
int db = b - m->b;
|
||||
int dl = l - m->l;
|
||||
int d = ( ( ( dr*dr + dg*dg + db*db ) >> 1 ) + dl*dl) + ( m->d * 3 );
|
||||
if( i == 0 || d < best_diff ) { best_diff = d; best_mix = m; }
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int best_diff = 0x7FFFFFFF;
|
||||
paldither_mix_t const* m = pal_mix;
|
||||
for( int i = 0; i < pal_mix_count; ++i, ++m )
|
||||
{
|
||||
int dr = r - m->r;
|
||||
int dg = g - m->g;
|
||||
int db = b - m->b;
|
||||
int dl = l - m->l;
|
||||
int d = ( ( ( dr*dr + dg*dg + db*db ) >> 1 ) + dl*dl) + ( m->d * 3 );
|
||||
if( i == 0 || d < best_diff ) { best_diff = d; best_mix = m; }
|
||||
}
|
||||
}
|
||||
|
||||
int index = dither_pattern[ best_mix->ratio * 4 * 4 + ( x & 3 ) + ( y & 3 ) * 4 ] ?
|
||||
best_mix->second : best_mix->first;
|
||||
|
||||
if( output) output[ x + y * width ] = (PALDITHER_U8) index;
|
||||
abgr[ x + y * width ] = ( abgr[ x + y * width ] & 0xff000000 ) | ( palette->colortable[ index ] & 0x00ffffff );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif /* PALDITHER_IMPLEMENTATION */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This software is available under 2 licenses - you may choose the one you like.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE A - MIT License
|
||||
|
||||
Copyright (c) 2015 Mattias Gustavsson
|
||||
|
||||
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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||
software, either in source code form or as a compiled binary, for any purpose,
|
||||
commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||
software dedicate any and all copyright interest in the software to the public
|
||||
domain. We make this dedication for the benefit of the public at large and to
|
||||
the detriment of our heirs and successors. We intend this dedication to be an
|
||||
overt act of relinquishment in perpetuity of all present and future rights to
|
||||
this software under copyright law.
|
||||
|
||||
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 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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
*/
|
||||
+718
@@ -0,0 +1,718 @@
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
Licensing information can be found at the end of the file.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
palettize.h - v0.1 - Median-cut palette generation and remapping for C/C++.
|
||||
|
||||
Do this:
|
||||
#define PALETTIZE_IMPLEMENTATION
|
||||
before you include this file in *one* C/C++ file to create the implementation.
|
||||
*/
|
||||
|
||||
#ifndef palettize_h
|
||||
#define palettize_h
|
||||
|
||||
|
||||
#ifndef PALETTIZE_U32
|
||||
#define PALETTIZE_U32 unsigned int
|
||||
#endif
|
||||
|
||||
#ifndef PALETTIZE_U16
|
||||
#define PALETTIZE_U16 unsigned short
|
||||
#endif
|
||||
|
||||
#ifndef PALETTIZE_U8
|
||||
#define PALETTIZE_U8 unsigned char
|
||||
#endif
|
||||
|
||||
|
||||
int palettize_generate_palette_xbgr32( PALETTIZE_U32 const* xbgr, int width, int height,
|
||||
PALETTIZE_U32* palette, int palette_size, void* memctx );
|
||||
|
||||
void palettize_remap_xbgr32( PALETTIZE_U32 const* xbgr, int width, int height,
|
||||
PALETTIZE_U32 const* palette, int palette_size, PALETTIZE_U8* output );
|
||||
|
||||
|
||||
int palettize_generate_palette_rgb16( PALETTIZE_U16 const* rgb, int width, int height,
|
||||
PALETTIZE_U16* palette, int palette_size, void* memctx );
|
||||
|
||||
void palettize_remap_rgb16( PALETTIZE_U16 const* rgb, int width, int height,
|
||||
PALETTIZE_U16 const* palette, int palette_size, PALETTIZE_U8* output );
|
||||
|
||||
|
||||
int palettize_generate_rgb16_palette_xbgr32( PALETTIZE_U32 const* xbgr, int width, int height,
|
||||
PALETTIZE_U16* palette, int palette_size, void* memctx );
|
||||
|
||||
void palettize_remap_xbgr32_pal16( PALETTIZE_U32 const* xbgr, int width, int height,
|
||||
PALETTIZE_U16 const* palette, int palette_size, PALETTIZE_U8* output );
|
||||
|
||||
|
||||
#endif /* palettize_h */
|
||||
|
||||
/**
|
||||
|
||||
Example
|
||||
=======
|
||||
|
||||
An example which loads a bitmap, generate a palette from it, and remap the image to that palette:
|
||||
|
||||
#define PALETTIZE_IMPLEMENTATION
|
||||
#include "palettize.h
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include "stb_image_write.h"
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
(void) argc, argv;
|
||||
|
||||
int w, h, c;
|
||||
PALETTIZE_U32* img = (PALETTIZE_U32*) stbi_load( "test.png", &w, &h, &c, 4 );
|
||||
|
||||
PALETTIZE_U32 pal[ 256 ];
|
||||
int pal_count = palettize_generate_palette_xbgr32( img, w, h, pal, 256, 0 );
|
||||
|
||||
PALETTIZE_U8* palimg = (PALETTIZE_U8*) malloc( w * h );
|
||||
palettize_remap_xbgr32( img, w, h, pal, pal_count, palimg );
|
||||
|
||||
for( int i = 0; i < w * h; ++i ) img[ i ] = pal[ palimg[ i ] ];
|
||||
stbi_write_png( "palettized.png", w, h, 4, img, w * 4 );
|
||||
free( palimg );
|
||||
stbi_image_free( img );
|
||||
}
|
||||
|
||||
// pass-through so the program will build with either /SUBSYSTEM:WINDOWS or /SUBSYSTEN:CONSOLE
|
||||
extern "C" int __stdcall WinMain( struct HINSTANCE__*, struct HINSTANCE__*, char*, int ) { return main( __argc, __argv ); }
|
||||
|
||||
**/
|
||||
|
||||
|
||||
|
||||
/*
|
||||
----------------------
|
||||
IMPLEMENTATION
|
||||
----------------------
|
||||
*/
|
||||
|
||||
#ifdef PALETTIZE_IMPLEMENTATION
|
||||
#undef PALETTIZE_IMPLEMENTATION
|
||||
|
||||
|
||||
#ifndef PALETTIZE_MALLOC
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdlib.h>
|
||||
#if defined(__cplusplus)
|
||||
#define PALETTIZE_MALLOC( ctx, size ) ( ::malloc( size ) )
|
||||
#define PALETTIZE_FREE( ctx, ptr ) ( ::free( ptr ) )
|
||||
#else
|
||||
#define PALETTIZE_MALLOC( ctx, size ) ( malloc( size ) )
|
||||
#define PALETTIZE_FREE( ctx, ptr ) ( free( ptr ) )
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct palettize_internal_rgb_t { PALETTIZE_U8 rgb[ 3 ]; } palettize_internal_rgb_t;
|
||||
|
||||
|
||||
typedef struct palettize_internal_bucket_t
|
||||
{
|
||||
palettize_internal_rgb_t* points;
|
||||
int points_count;
|
||||
palettize_internal_rgb_t min_val;
|
||||
palettize_internal_rgb_t max_val;
|
||||
} palettize_internal_bucket_t;
|
||||
|
||||
|
||||
static void palettize_internal_bucket_init( palettize_internal_bucket_t* bucket, palettize_internal_rgb_t* points,
|
||||
int points_count )
|
||||
{
|
||||
bucket->points = points;
|
||||
bucket->points_count = points_count;
|
||||
|
||||
for( int j = 0; j < 3; ++j ) bucket->min_val.rgb[ j ] = bucket->max_val.rgb[ j ] = bucket->points[ 0 ].rgb[ j ];
|
||||
|
||||
for( int i = 1; i < bucket->points_count; ++i )
|
||||
{
|
||||
for( int j = 0; j < 3; ++j )
|
||||
{
|
||||
bucket->min_val.rgb[ j ] = bucket->min_val.rgb[ j ] < bucket->points[ i ].rgb[ j ] ?
|
||||
bucket->min_val.rgb[ j ] : bucket->points[ i ].rgb[ j ];
|
||||
bucket->max_val.rgb[ j ] = bucket->max_val.rgb[ j ] > bucket->points[ i ].rgb[ j ] ?
|
||||
bucket->max_val.rgb[ j ] : bucket->points[ i ].rgb[ j ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int palettize_internal_bucket_largest_range( palettize_internal_bucket_t const* bucket )
|
||||
{
|
||||
int m = bucket->max_val.rgb[ 0 ] - bucket->min_val.rgb[ 0 ];
|
||||
int max_index = 0;
|
||||
for( int i = 1; i < 3; ++i )
|
||||
{
|
||||
int diff = bucket->max_val.rgb[ i ] - bucket->min_val.rgb[ i ];
|
||||
if( diff > m ) { m = diff; max_index = i; }
|
||||
}
|
||||
return max_index;
|
||||
}
|
||||
|
||||
|
||||
static int palettize_internal_bucket_compare( palettize_internal_bucket_t const* a,
|
||||
palettize_internal_bucket_t const* b )
|
||||
{
|
||||
int i = palettize_internal_bucket_largest_range( a );
|
||||
int j = palettize_internal_bucket_largest_range( b );
|
||||
int ra = a->max_val.rgb[ i ] - a->min_val.rgb[ i ];
|
||||
int rb = b->max_val.rgb[ j ] - b->min_val.rgb[ j ];
|
||||
return ra < rb ? -1 : ra > rb ? 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
typedef struct palettize_internal_priority_queue_t
|
||||
{
|
||||
palettize_internal_bucket_t* elements;
|
||||
int capacity;
|
||||
int count;
|
||||
} palettize_internal_priority_queue_t;
|
||||
|
||||
|
||||
static void priority_queue_push( palettize_internal_priority_queue_t* queue, palettize_internal_bucket_t const* value )
|
||||
{
|
||||
PALDITHER_ASSERT( queue->count < queue->capacity, "Priority queue overflow" );
|
||||
if( queue->count >= queue->capacity ) return;
|
||||
queue->elements[ queue->count++ ] = *value;
|
||||
int index = queue->count;
|
||||
while( index > 1 &&
|
||||
palettize_internal_bucket_compare( &queue->elements[ index - 1 ], &queue->elements[ index / 2 - 1 ] ) > 0 )
|
||||
{
|
||||
palettize_internal_bucket_t temp = queue->elements[ index / 2 - 1 ];
|
||||
queue->elements[ index / 2 - 1 ] = queue->elements[ index - 1 ];
|
||||
queue->elements[ index - 1 ] = temp;
|
||||
index /=2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void priority_queue_pop( palettize_internal_priority_queue_t* queue )
|
||||
{
|
||||
if( queue->count == 0 ) return;
|
||||
queue->elements[ 0 ] = queue->elements[ --queue->count ];
|
||||
int v = 1;
|
||||
int u = 0;
|
||||
while( u != v)
|
||||
{
|
||||
u = v;
|
||||
if( 2 * u + 1 <= queue->count )
|
||||
{
|
||||
if( palettize_internal_bucket_compare( &queue->elements[ u - 1 ], &queue->elements[ 2 * u - 1 ] ) <= 0 )
|
||||
v = 2 * u;
|
||||
if( palettize_internal_bucket_compare( &queue->elements[ v - 1 ], &queue->elements[ 2 * u + 1 - 1 ] ) <= 0 )
|
||||
v = 2 * u + 1;
|
||||
}
|
||||
else if ( 2 * u <= queue->count )
|
||||
{
|
||||
if( palettize_internal_bucket_compare( &queue->elements[ u - 1 ], &queue->elements[ 2 * u - 1 ] ) <= 0 )
|
||||
v = 2 * u;
|
||||
}
|
||||
|
||||
if( u != v )
|
||||
{
|
||||
palettize_internal_bucket_t temp = queue->elements[ u - 1 ];
|
||||
queue->elements[ u - 1 ] = queue->elements[ v - 1 ];
|
||||
queue->elements[ v - 1 ] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void palettize_internal_nth_element(palettize_internal_rgb_t* first, palettize_internal_rgb_t* nth,
|
||||
palettize_internal_rgb_t* last, int index)
|
||||
{
|
||||
while( last - first > 3 )
|
||||
{
|
||||
palettize_internal_rgb_t const* a = first;
|
||||
palettize_internal_rgb_t const* b = first + ( last - first ) / 2;
|
||||
palettize_internal_rgb_t const* c = last - 1;
|
||||
palettize_internal_rgb_t const* pivot;
|
||||
if( a->rgb[ index ] < b->rgb[ index ] )
|
||||
pivot = b->rgb[ index ] < c->rgb[ index ] ? b : a->rgb[ index ] < c->rgb[ index ] ? c : a;
|
||||
else
|
||||
pivot = a->rgb[ index ] < c->rgb[ index ] ? a : b->rgb[ index ] < c->rgb[ index ] ? c : b;
|
||||
|
||||
palettize_internal_rgb_t* part_first = first;
|
||||
palettize_internal_rgb_t* part_last = last;
|
||||
palettize_internal_rgb_t* cut;
|
||||
for( ; ; )
|
||||
{
|
||||
while( part_first->rgb[ index ] < pivot->rgb[ index ] ) ++part_first;
|
||||
--part_last;
|
||||
while( pivot->rgb[ index ] < part_last->rgb[ index ] ) --part_last;
|
||||
if( part_first >= part_last ) { cut = part_first; break; }
|
||||
palettize_internal_rgb_t tmp = *part_first; *part_first = *part_last; *part_last = tmp;
|
||||
++part_first;
|
||||
}
|
||||
if( cut <= nth ) first = cut; else last = cut;
|
||||
}
|
||||
|
||||
if( first == last ) return;
|
||||
for( palettize_internal_rgb_t* i = first + 1; i != last; ++i )
|
||||
{
|
||||
if( i->rgb[ index ] < first->rgb[ index ] )
|
||||
{
|
||||
palettize_internal_rgb_t* copy_last = i;
|
||||
palettize_internal_rgb_t* copy_result = i + 1;
|
||||
while( first != copy_last ) *( --copy_result ) = *( --copy_last );
|
||||
*first = *i;
|
||||
}
|
||||
else
|
||||
{
|
||||
palettize_internal_rgb_t* ins_last = i;
|
||||
palettize_internal_rgb_t* next = ins_last;
|
||||
--next;
|
||||
while( i->rgb[ index ] < next->rgb[ index ] ) { *ins_last = *next; ins_last = next; --next; }
|
||||
*ins_last = *i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int palettize_internal_median_cut(palettize_internal_rgb_t* image, int points_count,
|
||||
palettize_internal_bucket_t* work_mem, int desired_size, palettize_internal_rgb_t* output )
|
||||
{
|
||||
palettize_internal_priority_queue_t bucket_queue;
|
||||
bucket_queue.count = 0;
|
||||
bucket_queue.capacity = desired_size;
|
||||
bucket_queue.elements = work_mem;
|
||||
|
||||
palettize_internal_bucket_t initial_bucket;
|
||||
palettize_internal_bucket_init( &initial_bucket, image, points_count );
|
||||
|
||||
priority_queue_push( &bucket_queue, &initial_bucket );
|
||||
while( bucket_queue.count > 0 && bucket_queue.elements[ 0 ].points_count > 1 )
|
||||
{
|
||||
if( bucket_queue.count >= desired_size ) break;
|
||||
palettize_internal_bucket_t largest_range_bucket = bucket_queue.elements[ 0 ];
|
||||
|
||||
priority_queue_pop( &bucket_queue );
|
||||
palettize_internal_rgb_t * begin = largest_range_bucket.points;
|
||||
palettize_internal_rgb_t * median = largest_range_bucket.points + ( largest_range_bucket.points_count + 1 ) / 2;
|
||||
palettize_internal_rgb_t * end = largest_range_bucket.points + largest_range_bucket.points_count;
|
||||
|
||||
switch( palettize_internal_bucket_largest_range( &largest_range_bucket ) )
|
||||
{
|
||||
case 0: palettize_internal_nth_element( begin, median, end, 0 ); break;
|
||||
case 1: palettize_internal_nth_element( begin, median, end, 1 ); break;
|
||||
case 2: palettize_internal_nth_element( begin, median, end, 2 ); break;
|
||||
}
|
||||
|
||||
palettize_internal_bucket_t bucket1;
|
||||
palettize_internal_bucket_init( &bucket1, begin, (int)( median-begin ) );
|
||||
priority_queue_push( &bucket_queue, &bucket1);
|
||||
|
||||
palettize_internal_bucket_t bucket2;
|
||||
palettize_internal_bucket_init( &bucket2, median, (int)( end-median ) );
|
||||
priority_queue_push( &bucket_queue, &bucket2);
|
||||
}
|
||||
|
||||
int result = 0;
|
||||
while( bucket_queue.count > 0 )
|
||||
{
|
||||
palettize_internal_bucket_t bucket = bucket_queue.elements[ 0 ];
|
||||
priority_queue_pop( &bucket_queue );
|
||||
|
||||
int sum[ 3 ] = { 0, 0, 0 };
|
||||
for( int i = 0; i < bucket.points_count; ++i )
|
||||
for( int j = 0; j < 3; ++j )
|
||||
sum[ j ] += bucket.points[ i ].rgb[ j ];
|
||||
|
||||
if( bucket.points_count )
|
||||
{
|
||||
palettize_internal_rgb_t average;
|
||||
for( int j = 0; j < 3; ++j ) average.rgb[ j ] = (PALETTIZE_U8) ( sum[ j ] / bucket.points_count );
|
||||
|
||||
output[ result++ ] = average;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
int palettize_generate_palette_xbgr32( PALETTIZE_U32 const* xbgr, int width, int height,
|
||||
PALETTIZE_U32* palette, int palette_size, void* memctx )
|
||||
{
|
||||
(void) memctx;
|
||||
int pixel_count = width * height;
|
||||
|
||||
int count = 0;
|
||||
for( int i = 0; i < pixel_count; ++i )
|
||||
{
|
||||
if( ( xbgr[ i ] >> 24 ) == 0 ) continue;
|
||||
int found = 0;
|
||||
for( int j = 0; j < count; ++j )
|
||||
{
|
||||
if( ( palette[ j ] & 0x00ffffff ) == ( xbgr[ i ] & 0x00ffffff ) )
|
||||
{
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( !found )
|
||||
{
|
||||
if( count >= palette_size ) goto skip;
|
||||
palette[ count++ ] = xbgr[ i ];
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
|
||||
skip: ;
|
||||
size_t malloc_size = sizeof( palettize_internal_rgb_t ) * ( pixel_count + palette_size );
|
||||
malloc_size += sizeof( palettize_internal_bucket_t ) * palette_size;
|
||||
palettize_internal_rgb_t* data = (palettize_internal_rgb_t*) PALETTIZE_MALLOC( memctx, malloc_size );
|
||||
|
||||
int data_size = 0;
|
||||
for( int p = 0; p < pixel_count; ++p )
|
||||
{
|
||||
PALETTIZE_U32 color = *xbgr++;
|
||||
PALETTIZE_U8 a = (PALETTIZE_U8 ) ( ( color & 0xff000000 ) >> 24 );
|
||||
if( a > 0 )
|
||||
{
|
||||
PALETTIZE_U8 b = (PALETTIZE_U8) ( ( color & 0x00ff0000 ) >> 16 );
|
||||
PALETTIZE_U8 g = (PALETTIZE_U8) ( ( color & 0x0000ff00 ) >> 8 );
|
||||
PALETTIZE_U8 r = (PALETTIZE_U8) ( ( color & 0x000000ff ) );
|
||||
data[ data_size ].rgb[ 0 ] = r;
|
||||
data[ data_size ].rgb[ 1 ] = g;
|
||||
data[ data_size ].rgb[ 2 ] = b;
|
||||
++data_size;
|
||||
}
|
||||
}
|
||||
palettize_internal_rgb_t* result = data + pixel_count;
|
||||
palettize_internal_bucket_t* work_mem = (palettize_internal_bucket_t*) ( data + pixel_count + palette_size );
|
||||
int result_count = palettize_internal_median_cut( data, data_size, work_mem, palette_size, result );
|
||||
|
||||
count = 0;
|
||||
for( int i = 0; i < result_count; ++i )
|
||||
{
|
||||
palettize_internal_rgb_t p = result[ i ];
|
||||
PALETTIZE_U32 c = 0xff000000 | ( p.rgb[ 2 ] << 16 ) | ( p.rgb[ 1 ] << 8 ) | p.rgb[ 0 ];
|
||||
int found = 0;
|
||||
for( int j = 0; j < count; ++j ) if( palette[ j ] == c ) { found = 1; break; }
|
||||
if( !found ) palette[ count++ ] = c;
|
||||
if( count >= palette_size ) break;
|
||||
}
|
||||
|
||||
PALETTIZE_FREE( memctx, data );
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
void palettize_remap_xbgr32( PALETTIZE_U32 const* xbgr, int width, int height,
|
||||
PALETTIZE_U32 const* palette, int palette_size, PALETTIZE_U8* output )
|
||||
{
|
||||
for( int y = 0; y < height; ++y )
|
||||
{
|
||||
for( int x = 0; x < width; ++x )
|
||||
{
|
||||
PALETTIZE_U32 color = *xbgr++;
|
||||
int best_index = 0;
|
||||
int mindist_sq = 255 * 255 + 255 * 255 + 255 * 255 + 1;
|
||||
for( int i = 0; i < palette_size; ++i )
|
||||
{
|
||||
PALETTIZE_U8 cr = ( (PALETTIZE_U8) ( ( color & 0x00ff0000 ) >> 16 ) );
|
||||
PALETTIZE_U8 cg = ( (PALETTIZE_U8) ( ( color & 0x0000ff00 ) >> 8 ) );
|
||||
PALETTIZE_U8 cb = ( (PALETTIZE_U8) ( ( color & 0x000000ff ) ) );
|
||||
PALETTIZE_U8 pr = ( (PALETTIZE_U8) ( ( palette[ i ] & 0x00ff0000 ) >> 16 ) );
|
||||
PALETTIZE_U8 pg = ( (PALETTIZE_U8) ( ( palette[ i ] & 0x0000ff00 ) >> 8 ) );
|
||||
PALETTIZE_U8 pb = ( (PALETTIZE_U8) ( ( palette[ i ] & 0x000000ff ) ) );
|
||||
int dr = ( (int) cr ) - ( (int) pr );
|
||||
int dg = ( (int) cg ) - ( (int) pg );
|
||||
int db = ( (int) cb ) - ( (int) pb );
|
||||
int dist_sq = dr * dr + dg * dg + db * db;
|
||||
if( dist_sq < mindist_sq ) { mindist_sq = dist_sq; best_index = i; }
|
||||
}
|
||||
*output++ = (PALETTIZE_U8) best_index;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static PALETTIZE_U16 palettize_internal_rgb32_to_rgb16( PALETTIZE_U32 color )
|
||||
{
|
||||
return ((PALETTIZE_U16) ( ((color & 0x00f80000)>>8) | ((color & 0x0000fc00)>>5) | ((color & 0x000000f8)>>3) ));
|
||||
}
|
||||
|
||||
|
||||
static PALETTIZE_U32 palettize_internal_rgb16_to_rgb32( PALETTIZE_U16 color )
|
||||
{
|
||||
return (((PALETTIZE_U32)color & 0xf800)<<8) | (((PALETTIZE_U32)color & 0x07e0)<<5) |
|
||||
(((PALETTIZE_U32)color & 0x001f)<<3) | (0xff000000);
|
||||
}
|
||||
|
||||
|
||||
int palettize_generate_palette_rgb16( PALETTIZE_U16 const* rgb, int width, int height,
|
||||
PALETTIZE_U16* palette, int palette_size, void* memctx )
|
||||
{
|
||||
(void) memctx;
|
||||
int pixel_count = width * height;
|
||||
|
||||
int count = 0;
|
||||
for( int i = 0; i < pixel_count; ++i )
|
||||
{
|
||||
int found = 0;
|
||||
for( int j = 0; j < count; ++j )
|
||||
{
|
||||
if( ( palette[ j ] ) == ( rgb[ i ] & 0x00ffffff ) )
|
||||
{
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( !found )
|
||||
{
|
||||
if( count >= palette_size ) goto skip;
|
||||
palette[ count++ ] = rgb[ i ];
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
|
||||
skip: ;
|
||||
size_t malloc_size = sizeof( palettize_internal_rgb_t ) * ( pixel_count + palette_size );
|
||||
malloc_size += sizeof( palettize_internal_bucket_t ) * palette_size;
|
||||
palettize_internal_rgb_t* data = (palettize_internal_rgb_t*) PALETTIZE_MALLOC( memctx, malloc_size );
|
||||
|
||||
int data_size = 0;
|
||||
for( int p = 0; p < pixel_count; ++p )
|
||||
{
|
||||
PALETTIZE_U32 color = palettize_internal_rgb16_to_rgb32( *rgb++ );
|
||||
PALETTIZE_U8 a = (PALETTIZE_U8) ( ( color & 0xff000000 ) >> 24 );
|
||||
if( a > 0 )
|
||||
{
|
||||
PALETTIZE_U8 b = (PALETTIZE_U8) ( ( color & 0x00ff0000 ) >> 16 );
|
||||
PALETTIZE_U8 g = (PALETTIZE_U8) ( ( color & 0x0000ff00 ) >> 8 );
|
||||
PALETTIZE_U8 r = (PALETTIZE_U8) ( ( color & 0x000000ff ) );
|
||||
data[ data_size ].rgb[ 0 ] = r;
|
||||
data[ data_size ].rgb[ 1 ] = g;
|
||||
data[ data_size ].rgb[ 2 ] = b;
|
||||
++data_size;
|
||||
}
|
||||
}
|
||||
|
||||
palettize_internal_rgb_t* result = data + pixel_count;
|
||||
palettize_internal_bucket_t* work_mem = (palettize_internal_bucket_t*) ( data + pixel_count + palette_size );
|
||||
int result_count = palettize_internal_median_cut( data, data_size, work_mem, palette_size, result );
|
||||
|
||||
count = 0;
|
||||
for( int i = 0; i < result_count; ++i )
|
||||
{
|
||||
palettize_internal_rgb_t p = result[ i ];
|
||||
PALETTIZE_U16 c = palettize_internal_rgb32_to_rgb16(
|
||||
(PALETTIZE_U32)( ( p.rgb[ 2 ] << 16 ) | ( p.rgb[ 1 ] << 8 ) | p.rgb[ 0 ] ) );
|
||||
int found = 0;
|
||||
for( int j = 0; j < count; ++j ) if( palette[ j ] == c ) { found = 1; break; }
|
||||
if( !found ) palette[ count++ ] = c;
|
||||
if( count >= palette_size ) break;
|
||||
}
|
||||
|
||||
PALETTIZE_FREE( memctx, data );
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
void palettize_remap_rgb16( PALETTIZE_U16 const* rgb, int width, int height,
|
||||
PALETTIZE_U16 const* palette, int palette_size, PALETTIZE_U8* output )
|
||||
{
|
||||
for( int y = 0; y < height; ++y )
|
||||
{
|
||||
for( int x = 0; x < width; ++x )
|
||||
{
|
||||
PALETTIZE_U32 color = palettize_internal_rgb16_to_rgb32( *rgb++ );
|
||||
int best_index = 0;
|
||||
int mindist_sq = 255 * 255 + 255 * 255 + 255 * 255 + 1;
|
||||
for( int i = 0; i < palette_size; ++i )
|
||||
{
|
||||
PALETTIZE_U8 cr = ( (PALETTIZE_U8) ( ( color & 0x00ff0000 ) >> 16 ) );
|
||||
PALETTIZE_U8 cg = ( (PALETTIZE_U8) ( ( color & 0x0000ff00 ) >> 8 ) );
|
||||
PALETTIZE_U8 cb = ( (PALETTIZE_U8) ( ( color & 0x000000ff ) ) );
|
||||
PALETTIZE_U32 pal = palettize_internal_rgb16_to_rgb32( palette[ i ] );
|
||||
PALETTIZE_U8 pr = ( (PALETTIZE_U8) ( ( pal & 0x00ff0000 ) >> 16 ) );
|
||||
PALETTIZE_U8 pg = ( (PALETTIZE_U8) ( ( pal & 0x0000ff00 ) >> 8 ) );
|
||||
PALETTIZE_U8 pb = ( (PALETTIZE_U8) ( ( pal & 0x000000ff ) ) );
|
||||
int dr = ( (int) cr) - ( (int) pr );
|
||||
int dg = ( (int) cg) - ( (int) pg );
|
||||
int db = ( (int) cb) - ( (int) pb );
|
||||
int dist_sq = dr * dr + dg * dg + db * db;
|
||||
if( dist_sq < mindist_sq ) { mindist_sq = dist_sq; best_index = i; }
|
||||
}
|
||||
*output++ = (PALETTIZE_U8) best_index;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int palettize_generate_rgb16_palette_xbgr32( PALETTIZE_U32 const* xbgr, int width, int height,
|
||||
PALETTIZE_U16* palette, int palette_size, void* memctx )
|
||||
{
|
||||
(void) memctx;
|
||||
int pixel_count = width * height;
|
||||
|
||||
int count = 0;
|
||||
for( int i = 0; i < pixel_count; ++i )
|
||||
{
|
||||
if( ( xbgr[ i ] >> 24 ) == 0 ) continue;
|
||||
PALETTIZE_U16 c = palettize_internal_rgb32_to_rgb16( xbgr[ i ] );
|
||||
int found = 0;
|
||||
for( int j = 0; j < count; ++j )
|
||||
{
|
||||
if( palette[ j ] == c )
|
||||
{
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( !found )
|
||||
{
|
||||
if( count >= palette_size ) goto skip;
|
||||
palette[ count++ ] = c;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
|
||||
skip: ;
|
||||
size_t malloc_size = sizeof( palettize_internal_rgb_t ) * ( pixel_count + palette_size );
|
||||
malloc_size += sizeof( palettize_internal_bucket_t ) * palette_size;
|
||||
palettize_internal_rgb_t* data = (palettize_internal_rgb_t*) PALETTIZE_MALLOC( memctx, malloc_size );
|
||||
|
||||
int data_size = 0;
|
||||
for( int p = 0; p < pixel_count; ++p )
|
||||
{
|
||||
PALETTIZE_U32 color = *xbgr++;
|
||||
PALETTIZE_U8 a = (PALETTIZE_U8) ( ( color & 0xff000000 ) >>24 );
|
||||
if( a > 0 )
|
||||
{
|
||||
color = palettize_internal_rgb16_to_rgb32( palettize_internal_rgb32_to_rgb16( color ) );
|
||||
PALETTIZE_U8 b = (PALETTIZE_U8) ( ( color & 0x00ff0000 ) >> 16 );
|
||||
PALETTIZE_U8 g = (PALETTIZE_U8) ( ( color & 0x0000ff00 ) >> 8 );
|
||||
PALETTIZE_U8 r = (PALETTIZE_U8) ( ( color & 0x000000ff ) );
|
||||
data[ data_size ].rgb[ 0 ] = r;
|
||||
data[ data_size ].rgb[ 1 ] = g;
|
||||
data[ data_size ].rgb[ 2 ] = b;
|
||||
++data_size;
|
||||
}
|
||||
}
|
||||
palettize_internal_rgb_t* result = data + pixel_count;
|
||||
palettize_internal_bucket_t* work_mem = (palettize_internal_bucket_t*) ( data + pixel_count + palette_size );
|
||||
int result_count = palettize_internal_median_cut( data, data_size, work_mem, palette_size, result );
|
||||
|
||||
count = 0;
|
||||
for( int i = 0; i < result_count; ++i )
|
||||
{
|
||||
palettize_internal_rgb_t p = result[ i ];
|
||||
PALETTIZE_U16 c = palettize_internal_rgb32_to_rgb16(
|
||||
(PALETTIZE_U32)( ( p.rgb[ 2 ] << 16 ) | ( p.rgb[ 1 ] << 8 ) | p.rgb[ 0 ] ) );
|
||||
int found = 0;
|
||||
for( int j= 0; j < count; ++j ) if( palette[ j ] == c ) { found = 1; break; }
|
||||
if( !found ) palette[ count++ ] = c;
|
||||
if( count >= palette_size ) break;
|
||||
}
|
||||
|
||||
PALETTIZE_FREE( memctx, data );
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
void palettize_remap_xbgr32_pal16( PALETTIZE_U32 const* xbgr, int width, int height,
|
||||
PALETTIZE_U16 const* palette, int palette_size, PALETTIZE_U8* output )
|
||||
{
|
||||
for( int y = 0; y < height; ++y )
|
||||
{
|
||||
for( int x = 0; x < width; ++x )
|
||||
{
|
||||
PALETTIZE_U32 color = *xbgr++;
|
||||
int best_index = 0;
|
||||
int mindist_sq = 255 * 255 + 255 * 255 + 255 * 255 + 1;
|
||||
for( int i = 0; i < palette_size; ++i )
|
||||
{
|
||||
PALETTIZE_U8 cr = ( (PALETTIZE_U8) ( ( color & 0x00ff0000 ) >> 16 ) );
|
||||
PALETTIZE_U8 cg = ( (PALETTIZE_U8) ( ( color & 0x0000ff00 ) >> 8 ) );
|
||||
PALETTIZE_U8 cb = ( (PALETTIZE_U8) ( ( color & 0x000000ff ) ) );
|
||||
PALETTIZE_U32 pal = palettize_internal_rgb16_to_rgb32( palette[ i ] );
|
||||
PALETTIZE_U8 pr = ( (PALETTIZE_U8) ( ( pal & 0x00ff0000 ) >> 16) );
|
||||
PALETTIZE_U8 pg = ( (PALETTIZE_U8) ( ( pal & 0x0000ff00 ) >> 8 ) );
|
||||
PALETTIZE_U8 pb = ( (PALETTIZE_U8) ( ( pal & 0x000000ff ) ) );
|
||||
int dr = ( (int) cr) - ( (int) pr );
|
||||
int db = ( (int) cb) - ( (int) pb );
|
||||
int dg = ( (int) cg) - ( (int) pg );
|
||||
int dist_sq = dr * dr + dg * dg + db * db;
|
||||
if( dist_sq < mindist_sq ) { mindist_sq = dist_sq; best_index = i; }
|
||||
}
|
||||
*output++ = (PALETTIZE_U8) best_index;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* PALETTIZE_IMPLEMENTATION */
|
||||
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This software is available under 2 licenses - you may choose the one you like.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE A - MIT License
|
||||
|
||||
Copyright (c) 2017 Mattias Gustavsson
|
||||
|
||||
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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||
software, either in source code form or as a compiled binary, for any purpose,
|
||||
commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||
software dedicate any and all copyright interest in the software to the public
|
||||
domain. We make this dedication for the benefit of the public at large and to
|
||||
the detriment of our heirs and successors. We intend this dedication to be an
|
||||
overt act of relinquishment in perpetuity of all present and future rights to
|
||||
this software under copyright law.
|
||||
|
||||
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 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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -0,0 +1,444 @@
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
Licensing information can be found at the end of the file.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
palrle.h - v0.1 - Run-length encoding of palettized bitmaps, for C/C++.
|
||||
|
||||
Do this:
|
||||
#define PALRLE_IMPLEMENTATION
|
||||
before you include this file in *one* C/C++ file to create the implementation.
|
||||
*/
|
||||
|
||||
#ifndef palrle_h
|
||||
#define palrle_h
|
||||
|
||||
#ifndef PALRLE_U8
|
||||
#define PALRLE_U8 unsigned char
|
||||
#endif
|
||||
#ifndef PALRLE_U16
|
||||
#define PALRLE_U16 unsigned short
|
||||
#endif
|
||||
#ifndef PALRLE_U32
|
||||
#define PALRLE_U32 unsigned int
|
||||
#endif
|
||||
|
||||
typedef struct palrle_data_t {
|
||||
PALRLE_U32 size;
|
||||
PALRLE_U16 width;
|
||||
PALRLE_U16 height;
|
||||
PALRLE_U16 xoffset;
|
||||
PALRLE_U16 yoffset;
|
||||
PALRLE_U16 hpitch;
|
||||
PALRLE_U16 vpitch;
|
||||
PALRLE_U16 palette_count;
|
||||
PALRLE_U8 data[ 1 ]; // "open" array
|
||||
} palrle_data_t;
|
||||
|
||||
palrle_data_t* palrle_encode( PALRLE_U8* pixels, int width, int height, PALRLE_U32* palette, int palette_count, void* memctx );
|
||||
palrle_data_t* palrle_encode_mask( PALRLE_U8* pixels, PALRLE_U8* mask, int width, int height, PALRLE_U32* palette, int palette_count, void* memctx );
|
||||
//palrle_data_t* palrle_encode_transparency_index( PALRLE_U8* pixels, PALRLE_U8 transparency_index, int width, int height, PALRLE_U32* palette, int palette_count, void* memctx );
|
||||
|
||||
void palrle_decode( palrle_data_t* rle_data, PALRLE_U8* pixels, PALRLE_U8* mask );
|
||||
|
||||
void palrle_blit( palrle_data_t* rle_data, int x, int y, PALRLE_U8* pixels, int width, int height );
|
||||
|
||||
void palrle_free( palrle_data_t* rle_data, void* memctx );
|
||||
|
||||
|
||||
#endif /* palrle_h */
|
||||
|
||||
/*
|
||||
----------------------
|
||||
IMPLEMENTATION
|
||||
----------------------
|
||||
*/
|
||||
|
||||
#ifdef PALRLE_IMPLEMENTATION
|
||||
#undef PALRLE_IMPLEMENTATION
|
||||
|
||||
#ifndef PALRLE_MALLOC
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdlib.h>
|
||||
#define PALRLE_MALLOC( ctx, size ) ( malloc( size ) )
|
||||
#define PALRLE_FREE( ctx, ptr ) ( free( ptr ) )
|
||||
#endif
|
||||
|
||||
|
||||
palrle_data_t* palrle_encode( PALRLE_U8* pixels, int width, int height, PALRLE_U32* palette, int palette_count, void* memctx ) {
|
||||
(void) memctx;
|
||||
|
||||
int xmin = 0;
|
||||
int xmax = width - 1;
|
||||
int ymin = 0;
|
||||
int ymax = height - 1;
|
||||
int hpitch = xmax - xmin + 1;
|
||||
int vpitch = ymax - ymin + 1;
|
||||
|
||||
// Bitmap is completely empty, so just store the palette and dimensions
|
||||
if( hpitch <= 0 || vpitch <= 0 ) {
|
||||
size_t size = sizeof( palrle_data_t ) - sizeof( PALRLE_U8 ) + sizeof( PALRLE_U32 ) * palette_count;
|
||||
palrle_data_t* data = (palrle_data_t*) PALRLE_MALLOC( memctx, size );
|
||||
data->size = (PALRLE_U32) size;
|
||||
data->width = (PALRLE_U16) width;
|
||||
data->height = (PALRLE_U16) height;
|
||||
data->xoffset = 0;
|
||||
data->yoffset = 0;
|
||||
data->hpitch = 0;
|
||||
data->vpitch = 0;
|
||||
data->palette_count = (PALRLE_U16) palette_count;
|
||||
if( palette ) memcpy( data->data, palette, sizeof( PALRLE_U32 ) * palette_count );
|
||||
return data;
|
||||
}
|
||||
|
||||
palrle_data_t* data = (palrle_data_t*) PALRLE_MALLOC( memctx,
|
||||
sizeof( palrle_data_t ) + // size for the struct itself
|
||||
sizeof( PALRLE_U32 ) * palette_count + // size for storing palette entries
|
||||
sizeof( PALRLE_U32 ) * vpitch + // size for storing the offset for each row
|
||||
hpitch * vpitch * 2 ); // assume worst case - we should never need more than twice the number of pixels
|
||||
memset( data, 0, sizeof( palrle_data_t ) + sizeof( PALRLE_U32 ) * palette_count + sizeof( PALRLE_U32 ) * vpitch +
|
||||
hpitch * vpitch * 2 );
|
||||
|
||||
|
||||
data->size = (PALRLE_U32) ( sizeof( palrle_data_t ) - sizeof( PALRLE_U8 ) );
|
||||
data->width = (PALRLE_U16) width;
|
||||
data->height = (PALRLE_U16) height;
|
||||
data->xoffset = (PALRLE_U16) xmin;
|
||||
data->yoffset = (PALRLE_U16) ymin;
|
||||
data->hpitch = (PALRLE_U16) hpitch;
|
||||
data->vpitch = (PALRLE_U16) vpitch;
|
||||
data->palette_count = (PALRLE_U16) palette_count;
|
||||
if( palette ) memcpy( data->data, palette, sizeof( PALRLE_U32 ) * palette_count );
|
||||
|
||||
int row_offset = (int)( sizeof( PALRLE_U32 ) * palette_count );
|
||||
int rle_offset = (int)( row_offset + sizeof( PALRLE_U32 ) * vpitch );
|
||||
for( int y = 0; y < vpitch; ++y ) {
|
||||
*( (PALRLE_U32*)&data->data[ row_offset ] ) = (PALRLE_U32) rle_offset;
|
||||
row_offset += sizeof( PALRLE_U32 );
|
||||
int x = 0;
|
||||
while( x < hpitch ) {
|
||||
// add empty pixel count
|
||||
data->data[ rle_offset++ ] = (PALRLE_U8) 0;
|
||||
|
||||
// add non-empty pixels
|
||||
int color = pixels[ x + xmin + ( y + ymin ) * width ];
|
||||
int count = 0;
|
||||
int tx = x;
|
||||
while( tx < hpitch ) {
|
||||
if( pixels[ tx + xmin + ( y + ymin ) * width ] != color ) break;
|
||||
if( count >= 127 ) break;
|
||||
++count;
|
||||
++tx;
|
||||
}
|
||||
|
||||
if( count == 0 ) {
|
||||
data->data[ rle_offset++ ] = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if( count > 2 ) { // store as a run
|
||||
data->data[ rle_offset++ ] = (PALRLE_U8) count; // number of pixels
|
||||
data->data[ rle_offset++ ] = (PALRLE_U8) color; // of palette index `color`
|
||||
x = tx;
|
||||
} else { // store as unique values
|
||||
PALRLE_U8* uniques_out = &data->data[ rle_offset++ ];
|
||||
int uniques = 1;
|
||||
data->data[ rle_offset++ ] = (PALRLE_U8) color;
|
||||
++x;
|
||||
|
||||
// add pixexls until we encounter a run
|
||||
while( x < hpitch ) {
|
||||
// check the length of the next run
|
||||
color = pixels[ x + xmin + ( y + ymin ) * width ];
|
||||
count = 0;
|
||||
tx = x;
|
||||
while( tx < hpitch ) {
|
||||
if( pixels[ tx + xmin + ( y + ymin ) * width ] != color ) break;
|
||||
if( count > 2 ) break; // No need to keep counting, we know this is a run
|
||||
++count;
|
||||
++tx;
|
||||
}
|
||||
if( count > 2 || count == 0 ) {
|
||||
break;
|
||||
} else {
|
||||
if( uniques >= 128 ) break;
|
||||
++uniques;
|
||||
data->data[ rle_offset++ ] = (PALRLE_U8) color;
|
||||
++x;
|
||||
}
|
||||
}
|
||||
*uniques_out = (PALRLE_U8)( (char) -uniques );
|
||||
}
|
||||
}
|
||||
}
|
||||
data->size += rle_offset;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
palrle_data_t* palrle_encode_mask( PALRLE_U8* pixels, PALRLE_U8* mask, int width, int height, PALRLE_U32* palette, int palette_count, void* memctx ) {
|
||||
(void) memctx;
|
||||
// Crop to smallest non-empty region
|
||||
int xmin = width;
|
||||
int xmax = 0;
|
||||
int ymin = height;
|
||||
int ymax = 0;
|
||||
for( int y = 0; y < height; ++y ) {
|
||||
for( int x = 0; x < width; ++x ) {
|
||||
if( mask[ x + y * width ] ) {
|
||||
if( x < xmin ) xmin = x;
|
||||
if( y < ymin ) ymin = y;
|
||||
if( x > xmax ) xmax = x;
|
||||
if( y > ymax ) ymax = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
int hpitch = xmax - xmin + 1;
|
||||
int vpitch = ymax - ymin + 1;
|
||||
|
||||
// Bitmap is completely empty, so just store the palette and dimensions
|
||||
if( hpitch <= 0 || vpitch <= 0 ) {
|
||||
size_t size = sizeof( palrle_data_t ) - sizeof( PALRLE_U8 ) + sizeof( PALRLE_U32 ) * palette_count;
|
||||
palrle_data_t* data = (palrle_data_t*) PALRLE_MALLOC( memctx, size );
|
||||
data->size = (PALRLE_U32) size;
|
||||
data->width = (PALRLE_U16) width;
|
||||
data->height = (PALRLE_U16) height;
|
||||
data->xoffset = 0;
|
||||
data->yoffset = 0;
|
||||
data->hpitch = 0;
|
||||
data->vpitch = 0;
|
||||
data->palette_count = (PALRLE_U16) palette_count;
|
||||
if( palette ) memcpy( data->data, palette, sizeof( PALRLE_U32 ) * palette_count );
|
||||
return data;
|
||||
}
|
||||
|
||||
palrle_data_t* data = (palrle_data_t*) PALRLE_MALLOC( memctx,
|
||||
sizeof( palrle_data_t ) + // size for the struct itself
|
||||
sizeof( PALRLE_U32 ) * palette_count + // size for storing palette entries
|
||||
sizeof( PALRLE_U32 ) * vpitch + // size for storing the offset for each row
|
||||
hpitch * vpitch * 2 ); // assume worst case - we should never need more than twice the number of pixels
|
||||
memset( data, 0, sizeof( palrle_data_t ) + sizeof( PALRLE_U32 ) * palette_count + sizeof( PALRLE_U32 ) * vpitch +
|
||||
hpitch * vpitch * 2 );
|
||||
|
||||
|
||||
data->size = (PALRLE_U32) ( sizeof( palrle_data_t ) - sizeof( PALRLE_U8 ) );
|
||||
data->width = (PALRLE_U16) width;
|
||||
data->height = (PALRLE_U16) height;
|
||||
data->xoffset = (PALRLE_U16) xmin;
|
||||
data->yoffset = (PALRLE_U16) ymin;
|
||||
data->hpitch = (PALRLE_U16) hpitch;
|
||||
data->vpitch = (PALRLE_U16) vpitch;
|
||||
data->palette_count = (PALRLE_U16) palette_count;
|
||||
if( palette ) memcpy( data->data, palette, sizeof( PALRLE_U32 ) * palette_count );
|
||||
|
||||
int row_offset = (int)( sizeof( PALRLE_U32 ) * palette_count );
|
||||
int rle_offset = (int)( row_offset + sizeof( PALRLE_U32 ) * vpitch );
|
||||
for( int y = 0; y < vpitch; ++y ) {
|
||||
*( (PALRLE_U32*)&data->data[ row_offset ] ) = (PALRLE_U32) rle_offset;
|
||||
row_offset += sizeof( PALRLE_U32 );
|
||||
int x = 0;
|
||||
while( x < hpitch ) {
|
||||
// add empty pixel count
|
||||
int empty = 0;
|
||||
while( x < hpitch ) {
|
||||
if( mask[ x + xmin + ( y + ymin ) * width ] != 0 ) break;
|
||||
if( empty >= 255 ) {
|
||||
data->data[ rle_offset++ ] = 255; // 255 empty pixels
|
||||
data->data[ rle_offset++ ] = 0; // 0 non-empty pixels
|
||||
empty = 0;
|
||||
}
|
||||
++empty;
|
||||
++x;
|
||||
}
|
||||
data->data[ rle_offset++ ] = (PALRLE_U8) empty;
|
||||
|
||||
// add non-empty pixels
|
||||
int color = pixels[ x + xmin + ( y + ymin ) * width ];
|
||||
int count = 0;
|
||||
int tx = x;
|
||||
while( tx < hpitch ) {
|
||||
if( mask[ tx + xmin + ( y + ymin ) * width ] == 0 ) break;
|
||||
if( pixels[ tx + xmin + ( y + ymin ) * width ] != color ) break;
|
||||
if( count >= 127 ) break;
|
||||
++count;
|
||||
++tx;
|
||||
}
|
||||
|
||||
if( count == 0 ) {
|
||||
data->data[ rle_offset++ ] = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if( count > 2 ) { // store as a run
|
||||
data->data[ rle_offset++ ] = (PALRLE_U8) count; // number of pixels
|
||||
data->data[ rle_offset++ ] = (PALRLE_U8) color; // of palette index `color`
|
||||
x = tx;
|
||||
} else { // store as unique values
|
||||
PALRLE_U8* uniques_out = &data->data[ rle_offset++ ];
|
||||
int uniques = 1;
|
||||
data->data[ rle_offset++ ] = (PALRLE_U8) color;
|
||||
++x;
|
||||
|
||||
// add pixexls until we encounter a run
|
||||
while( x < hpitch ) {
|
||||
// check the length of the next run
|
||||
color = pixels[ x + xmin + ( y + ymin ) * width ];
|
||||
count = 0;
|
||||
tx = x;
|
||||
while( tx < hpitch ) {
|
||||
if( mask[ tx + xmin + ( y + ymin ) * width ] == 0 ) break;
|
||||
if( pixels[ tx + xmin + ( y + ymin ) * width ] != color ) break;
|
||||
if( count > 2 ) break; // No need to keep counting, we know this is a run
|
||||
++count;
|
||||
++tx;
|
||||
}
|
||||
if( count > 2 || count == 0 ) {
|
||||
break;
|
||||
} else {
|
||||
if( uniques >= 128 ) break;
|
||||
++uniques;
|
||||
data->data[ rle_offset++ ] = (PALRLE_U8) color;
|
||||
++x;
|
||||
}
|
||||
}
|
||||
*uniques_out = (PALRLE_U8)( (char) -uniques );
|
||||
}
|
||||
}
|
||||
}
|
||||
data->size += rle_offset;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
palrle_data_t* palrle_encode_transparency_index( PALRLE_U8* pixels, PALRLE_U8 transparency_index, int width, int height, PALRLE_U32* palette, int palette_count, void* memctx ) {
|
||||
(void) pixels, transparency_index, width, height, memctx;
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
void palrle_decode( palrle_data_t* rle_data, PALRLE_U8* pixels, PALRLE_U8* mask ) {
|
||||
memset( pixels, 0, rle_data->width * rle_data->height * sizeof( PALRLE_U8 ) );
|
||||
if( mask ) memset( mask, 0, rle_data->width * rle_data->height * sizeof( PALRLE_U8 ) );
|
||||
PALRLE_U8* data = &rle_data->data[ sizeof( PALRLE_U32 ) * ( rle_data->palette_count + rle_data->vpitch ) ];
|
||||
for( int y = 0; y < rle_data->vpitch; ++y ) {
|
||||
int x = 0;
|
||||
while( x < rle_data->hpitch ) {
|
||||
x += *data++;
|
||||
char count = (char)( *data++ );
|
||||
if( count > 0 ) {
|
||||
PALRLE_U8 color = *data++;
|
||||
for( int i = 0; i < count; ++i ) {
|
||||
pixels[ rle_data->xoffset + x + ( rle_data->yoffset + y ) * rle_data->width ] = color;
|
||||
if( mask ) mask[ rle_data->xoffset + x + ( rle_data->yoffset + y ) * rle_data->width ] = 255;
|
||||
++x;
|
||||
}
|
||||
} else {
|
||||
for( int i = 0; i < -count; ++i ) {
|
||||
pixels[ rle_data->xoffset + x + ( rle_data->yoffset + y ) * rle_data->width ] = *data++;
|
||||
if( mask ) mask[ rle_data->xoffset + x + ( rle_data->yoffset + y ) * rle_data->width ] = 255;
|
||||
++x;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void palrle_blit( palrle_data_t* rle_data, int x, int y, PALRLE_U8* pixels, int width, int height ) {
|
||||
PALRLE_U8* data = &rle_data->data[ sizeof( PALRLE_U32 ) * ( rle_data->palette_count + rle_data->vpitch ) ];
|
||||
for( int iy = 0; iy < rle_data->vpitch; ++iy ) {
|
||||
int ix = 0;
|
||||
while( ix < rle_data->hpitch ) {
|
||||
ix += *data++;
|
||||
char count = (char)( *data++ );
|
||||
if( count > 0 ) {
|
||||
PALRLE_U8 color = *data++;
|
||||
for( int i = 0; i < count; ++i ) {
|
||||
int xp = rle_data->xoffset + ix + x;
|
||||
int yp = rle_data->yoffset + iy + y;
|
||||
if( xp > 0 && yp > 0 && xp < width && yp < height ) pixels[ xp + yp * width ] = color;
|
||||
++ix;
|
||||
}
|
||||
} else {
|
||||
for( int i = 0; i < -count; ++i ) {
|
||||
PALRLE_U8 color = *data++;
|
||||
int xp = rle_data->xoffset + ix + x;
|
||||
int yp = rle_data->yoffset + iy + y;
|
||||
if( xp > 0 && yp > 0 && xp < width && yp < height ) pixels[ xp + yp * width ] = color;
|
||||
++ix;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void palrle_free( palrle_data_t* rle_data, void* memctx ) {
|
||||
(void) rle_data, (void) memctx;
|
||||
PALRLE_FREE( memctx, rle_data );
|
||||
}
|
||||
|
||||
#endif /* PALRLE_IMPLEMENTATION */
|
||||
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This software is available under 2 licenses - you may choose the one you like.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE A - MIT License
|
||||
|
||||
Copyright (c) 2019 Mattias Gustavsson
|
||||
|
||||
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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||
software, either in source code form or as a compiled binary, for any purpose,
|
||||
commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||
software dedicate any and all copyright interest in the software to the public
|
||||
domain. We make this dedication for the benefit of the public at large and to
|
||||
the detriment of our heirs and successors. We intend this dedication to be an
|
||||
overt act of relinquishment in perpetuity of all present and future rights to
|
||||
this software under copyright law.
|
||||
|
||||
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 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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
*/
|
||||
+523
@@ -0,0 +1,523 @@
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
Licensing information can be found at the end of the file.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
pixelfont.h - v0.1 - Custom pixel font format builder and renderer.
|
||||
|
||||
Do this:
|
||||
#define PIXELFONT_IMPLEMENTATION
|
||||
before you include this file in *one* C/C++ file to create the implementation.
|
||||
|
||||
If you want to generate pixelfonts through the pixelfont_builder api, do this:
|
||||
#define PIXELFONT_BUILDER_IMPLEMENTATION
|
||||
before include the file to create the implementation.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef pixelfont_h
|
||||
#define pixelfont_h
|
||||
|
||||
#ifndef PIXELFONT_I8
|
||||
#define PIXELFONT_I8 signed char
|
||||
#endif
|
||||
|
||||
#ifndef PIXELFONT_U8
|
||||
#define PIXELFONT_U8 unsigned char
|
||||
#endif
|
||||
|
||||
#ifndef PIXELFONT_U32
|
||||
#define PIXELFONT_U32 unsigned int
|
||||
#endif
|
||||
|
||||
typedef struct pixelfont_t
|
||||
{
|
||||
PIXELFONT_U32 size_in_bytes;
|
||||
PIXELFONT_U8 height;
|
||||
PIXELFONT_U8 line_spacing;
|
||||
PIXELFONT_U8 baseline;
|
||||
PIXELFONT_U32 offsets[ 256 ];
|
||||
PIXELFONT_U8 glyphs[ 1 ]; // "open" array - ok to access out of bounds (use size_in_bytes to determine the end)
|
||||
} pixelfont_t;
|
||||
|
||||
|
||||
typedef enum pixelfont_align_t
|
||||
{
|
||||
PIXELFONT_ALIGN_LEFT,
|
||||
PIXELFONT_ALIGN_RIGHT,
|
||||
PIXELFONT_ALIGN_CENTER,
|
||||
} pixelfont_align_t;
|
||||
|
||||
|
||||
typedef enum pixelfont_bold_t
|
||||
{
|
||||
PIXELFONT_BOLD_OFF,
|
||||
PIXELFONT_BOLD_ON,
|
||||
} pixelfont_bold_t;
|
||||
|
||||
|
||||
typedef enum pixelfont_italic_t
|
||||
{
|
||||
PIXELFONT_ITALIC_OFF,
|
||||
PIXELFONT_ITALIC_ON,
|
||||
} pixelfont_italic_t;
|
||||
|
||||
|
||||
typedef enum pixelfont_underline_t
|
||||
{
|
||||
PIXELFONT_UNDERLINE_OFF,
|
||||
PIXELFONT_UNDERLINE_ON,
|
||||
} pixelfont_underline_t;
|
||||
|
||||
|
||||
typedef struct pixelfont_bounds_t
|
||||
{
|
||||
int width;
|
||||
int height;
|
||||
} pixelfont_bounds_t;
|
||||
|
||||
|
||||
|
||||
typedef struct pixelfont_builder_t pixelfont_builder_t;
|
||||
|
||||
pixelfont_builder_t* pixelfont_builder_create( int height, int baseline, int line_spacing, void* memctx );
|
||||
void pixelfont_builder_destroy( pixelfont_builder_t* builder );
|
||||
|
||||
void pixelfont_builder_glyph( pixelfont_builder_t* builder, int glyph, int pixels_stride, PIXELFONT_U8* pixels, int width, int lead, int trail );
|
||||
void pixelfont_builder_kerning( pixelfont_builder_t* builder, int glyph, int follower, int adjust );
|
||||
|
||||
pixelfont_t* pixelfont_builder_font( pixelfont_builder_t* builder );
|
||||
|
||||
#endif /* pixelfont_h */
|
||||
|
||||
|
||||
// The following will be defined each time the file is included. By defining PIXELFONT_COLOR and PIXELFONT_FUNC_NAME
|
||||
// it is possible to create declarations for different pixel formats. By defining the same ones, and the additional
|
||||
// PIXELFONT_PIXEL_FUNC before including the implementation part, the corresponding definition can be created.
|
||||
|
||||
#ifndef PIXELFONT_COLOR
|
||||
#define PIXELFONT_COLOR PIXELFONT_U8
|
||||
#endif
|
||||
|
||||
#ifndef PIXELFONT_FUNC_NAME
|
||||
#define PIXELFONT_FUNC_NAME pixelfont_blit
|
||||
#endif
|
||||
|
||||
void PIXELFONT_FUNC_NAME( pixelfont_t const* font, int x, int y, char const* text, PIXELFONT_COLOR color,
|
||||
PIXELFONT_COLOR* target, int width, int height, pixelfont_align_t align, int wrap_width, int hspacing,
|
||||
int vspacing, int limit, pixelfont_bold_t bold, pixelfont_italic_t italic, pixelfont_underline_t underline,
|
||||
pixelfont_bounds_t* bounds );
|
||||
|
||||
|
||||
/*
|
||||
----------------------
|
||||
IMPLEMENTATION
|
||||
----------------------
|
||||
*/
|
||||
|
||||
#ifdef PIXELFONT_IMPLEMENTATION
|
||||
#undef PIXELFONT_IMPLEMENTATION
|
||||
|
||||
#ifndef PIXELFONT_PIXEL_FUNC
|
||||
#define PIXELFONT_PIXEL_FUNC( dst, fnt, col ) *(dst) = (col);
|
||||
#endif
|
||||
|
||||
void PIXELFONT_FUNC_NAME( pixelfont_t const* font, int x, int y, char const* text, PIXELFONT_COLOR color,
|
||||
PIXELFONT_COLOR* target, int width, int height, pixelfont_align_t align, int wrap_width, int hspacing,
|
||||
int vspacing, int limit, pixelfont_bold_t bold, pixelfont_italic_t italic, pixelfont_underline_t underline,
|
||||
pixelfont_bounds_t* bounds )
|
||||
{
|
||||
int xp = x;
|
||||
int yp = y;
|
||||
int max_x = x;
|
||||
int last_x_on_line = xp;
|
||||
int count = 0;
|
||||
char const* str = text;
|
||||
while( *str )
|
||||
{
|
||||
int line_char_count = 0;
|
||||
int line_width = 0;
|
||||
int last_space_char_count = 0;
|
||||
int last_space_width = 0;
|
||||
char const* tstr = str;
|
||||
while( *tstr != '\n' && *tstr != '\0' && ( wrap_width <= 0 || line_width <= wrap_width ) )
|
||||
{
|
||||
if( *tstr <= ' ' )
|
||||
{
|
||||
last_space_char_count = line_char_count;
|
||||
last_space_width = line_width;
|
||||
}
|
||||
PIXELFONT_U8 const* g = font->glyphs + font->offsets[ (int) *tstr ];
|
||||
line_width += (PIXELFONT_I8) *g++;
|
||||
int w = *g++;
|
||||
g += font->height * w;
|
||||
line_width += (PIXELFONT_I8) *g++;
|
||||
line_width += hspacing + ( bold ? 1 : 0 );
|
||||
++tstr;
|
||||
int kern = *g++;
|
||||
for( int k = 0; k < kern; ++k )
|
||||
if( *g++ == *tstr ) { line_width += (PIXELFONT_I8) *g++; break; } else ++g;
|
||||
++line_char_count;
|
||||
}
|
||||
|
||||
int skip_space = 0;
|
||||
if( wrap_width > 0 && line_width > wrap_width )
|
||||
{
|
||||
if( last_space_char_count > 0 ) line_char_count = last_space_char_count;
|
||||
line_width = last_space_width;
|
||||
skip_space = 1;
|
||||
}
|
||||
|
||||
if( wrap_width > 0 )
|
||||
{
|
||||
if( align == PIXELFONT_ALIGN_RIGHT ) x += wrap_width - line_width;
|
||||
if( align == PIXELFONT_ALIGN_CENTER ) x += ( wrap_width - line_width ) / 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( align == PIXELFONT_ALIGN_RIGHT ) x -= line_width;
|
||||
if( align == PIXELFONT_ALIGN_CENTER ) x -= line_width / 2;
|
||||
}
|
||||
|
||||
for( int c = 0; c < line_char_count; ++c )
|
||||
{
|
||||
PIXELFONT_U8 const* g = font->glyphs + font->offsets[ (int) *str ];
|
||||
x += (PIXELFONT_I8) *g++;
|
||||
int w = *g++;
|
||||
int h = font->height;
|
||||
for( int iy = y; iy < y + h; ++iy )
|
||||
{
|
||||
int xs = x + ( italic ? ( h - ( iy - y ) ) / 2 - 1 : 0 );
|
||||
for( int ix = xs; ix < xs + w; ++ix )
|
||||
{
|
||||
int p = *g++;
|
||||
if( p && target )
|
||||
if( limit < 0 || count < limit )
|
||||
if( ix >= 0 && iy >= 0 && ix < width && iy < height )
|
||||
{
|
||||
last_x_on_line = ix >= last_x_on_line ? ix + ( bold ? 1 : 0 ) : last_x_on_line;
|
||||
PIXELFONT_PIXEL_FUNC( ( &target[ ix + iy * width ] ), (PIXELFONT_U8)p, color );
|
||||
if( bold && ix + 1 < width )
|
||||
PIXELFONT_PIXEL_FUNC( ( &target[ ix + 1 + iy * width ] ), (PIXELFONT_U8)p, color );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
x += (PIXELFONT_I8) *g++;
|
||||
x += hspacing + ( bold ? 1 : 0 );
|
||||
++str;
|
||||
++count;
|
||||
|
||||
int kern = *g++;
|
||||
for( int k = 0; k < kern; ++k )
|
||||
if( *g++ == *str ) { x += (PIXELFONT_I8) *g++; break; } else ++g;
|
||||
|
||||
}
|
||||
|
||||
if( underline && target && y + font->baseline + 1 >= 0 && y + font->baseline + 1 < height && last_x_on_line > xp )
|
||||
for( int ix = xp; ix <= last_x_on_line; ++ix )
|
||||
if( ix >= 0 && ix < width )
|
||||
PIXELFONT_PIXEL_FUNC( ( &target[ ix + ( y + font->baseline + 1 ) * width ] ), (PIXELFONT_U8)255, color );
|
||||
last_x_on_line = xp;
|
||||
max_x = x > max_x ? x : max_x;
|
||||
x = xp;
|
||||
y += font->line_spacing + vspacing;
|
||||
if( *str == '\n' ) ++str;
|
||||
if( *str && skip_space && *str <= ' ' ) ++str;
|
||||
}
|
||||
|
||||
if( bounds )
|
||||
{
|
||||
bounds->width = wrap_width > 0 ? wrap_width : ( max_x - xp );
|
||||
bounds->height = y - yp;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#undef PIXELFONT_COLOR
|
||||
#undef PIXELFONT_FUNC_NAME
|
||||
|
||||
#endif /* PIXELFONT_IMPLEMENTATION */
|
||||
|
||||
|
||||
|
||||
#ifdef PIXELFONT_BUILDER_IMPLEMENTATION
|
||||
#undef PIXELFONT_BUILDER_IMPLEMENTATION
|
||||
|
||||
|
||||
#ifndef PIXELFONT_MALLOC
|
||||
#undef _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#undef _CRT_SECURE_NO_WARNINGS
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdlib.h>
|
||||
#define PIXELFONT_MALLOC( ctx, size ) ( malloc( size ) )
|
||||
#define PIXELFONT_FREE( ctx, ptr ) ( free( ptr ) )
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef PIXELFONT_MEMCPY
|
||||
#undef _CRT_NONSTDC_NO_DEPRECATE
|
||||
#define _CRT_NONSTDC_NO_DEPRECATE
|
||||
#undef _CRT_SECURE_NO_WARNINGS
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <string.h>
|
||||
#define PIXELFONT_MEMCPY( dst, src, cnt ) ( memcpy( dst, src, cnt ) )
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct pixelfont_builder_glyph_t
|
||||
{
|
||||
int lead;
|
||||
int trail;
|
||||
int width;
|
||||
PIXELFONT_U8* pixels;
|
||||
} pixelfont_builder_glyph_t;
|
||||
|
||||
|
||||
typedef struct pixelfont_builder_kerning_t
|
||||
{
|
||||
int glyph;
|
||||
int follower;
|
||||
int adjust;
|
||||
} pixelfont_builder_kerning_t;
|
||||
|
||||
|
||||
struct pixelfont_builder_t
|
||||
{
|
||||
void* memctx;
|
||||
pixelfont_t* font;
|
||||
int height;
|
||||
int baseline;
|
||||
int line_spacing;
|
||||
|
||||
pixelfont_builder_glyph_t glyphs[ 256 ];
|
||||
|
||||
int kernings_capacity;
|
||||
int kernings_count;
|
||||
pixelfont_builder_kerning_t* kernings;
|
||||
};
|
||||
|
||||
|
||||
pixelfont_builder_t* pixelfont_builder_create( int height, int baseline, int line_spacing, void* memctx )
|
||||
{
|
||||
pixelfont_builder_t* builder = (pixelfont_builder_t*) PIXELFONT_MALLOC( memctx, sizeof( pixelfont_builder_t ) ) ;
|
||||
memset( builder, 0, sizeof( *builder ) );
|
||||
builder->memctx = memctx;
|
||||
builder->font = 0;
|
||||
builder->height = height < 0 ? 0 : height > 255 ? 255 : height;
|
||||
builder->baseline = baseline < 0 ? 0 : baseline > 255 ? 255 : baseline;
|
||||
builder->line_spacing = line_spacing < 0 ? 0 : line_spacing > 255 ? 255 : line_spacing;
|
||||
builder->kernings_capacity = 0;
|
||||
builder->kernings_count = 0;
|
||||
builder->kernings = 0;
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
||||
void pixelfont_builder_destroy( pixelfont_builder_t* builder )
|
||||
{
|
||||
for( int i = 0; i < sizeof( builder->glyphs ) / sizeof( *builder->glyphs ); ++i )
|
||||
if( builder->glyphs[ i ].pixels ) PIXELFONT_FREE( builder->memctx, builder->glyphs[ i ].pixels );
|
||||
|
||||
if( builder->kernings ) PIXELFONT_FREE( builder->memctx, builder->kernings );
|
||||
if( builder->font ) PIXELFONT_FREE( builder->memctx, builder->font );
|
||||
PIXELFONT_FREE( builder->memctx, builder );
|
||||
}
|
||||
|
||||
|
||||
void pixelfont_builder_glyph( pixelfont_builder_t* builder, int glyph, int pixels_stride, PIXELFONT_U8* pixels, int width, int lead, int trail )
|
||||
{
|
||||
if( glyph < 0 || glyph > 255 ) return;
|
||||
|
||||
if( builder->glyphs[ glyph ].pixels ) PIXELFONT_FREE( builder->memctx, builder->glyphs[ glyph ].pixels );
|
||||
builder->glyphs[ glyph ].pixels = 0;
|
||||
builder->glyphs[ glyph ].lead = 0;
|
||||
builder->glyphs[ glyph ].trail = 0;
|
||||
builder->glyphs[ glyph ].width = 0;
|
||||
|
||||
if( pixels && width > 0 )
|
||||
{
|
||||
builder->glyphs[ glyph ].pixels = (PIXELFONT_U8*) PIXELFONT_MALLOC( builder->memctx, width * builder->height * sizeof( PIXELFONT_U8 ) );
|
||||
for( int y = 0; y < builder->height; ++y )
|
||||
for( int x = 0; x < width; ++x )
|
||||
builder->glyphs[ glyph ].pixels[ x + y * width ] = pixels[ x + y * pixels_stride ];
|
||||
builder->glyphs[ glyph ].lead = lead < -127 ? -127 : lead > 127 ? 127 : lead;
|
||||
builder->glyphs[ glyph ].trail = trail < -127 ? -127 : trail > 127 ? 127 : trail;
|
||||
builder->glyphs[ glyph ].width = width < 0 ? 0 : width > 255 ? 255 : width;
|
||||
}
|
||||
else if( lead || trail )
|
||||
{
|
||||
builder->glyphs[ glyph ].pixels = (PIXELFONT_U8*) PIXELFONT_MALLOC( builder->memctx, 1 );
|
||||
builder->glyphs[ glyph ].lead = lead < -127 ? -127 : lead > 127 ? 127 : lead;
|
||||
builder->glyphs[ glyph ].trail = trail < -127 ? -127 : trail > 127 ? 127 : trail;
|
||||
builder->glyphs[ glyph ].width = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void pixelfont_builder_kerning( pixelfont_builder_t* builder, int glyph, int follower, int adjust )
|
||||
{
|
||||
adjust = adjust < -127 ? -127 : adjust > 127 ? 127 : adjust;
|
||||
|
||||
for( int i = 0; i < builder->kernings_count; ++i )
|
||||
{
|
||||
if( builder->kernings[ i ].glyph == glyph && builder->kernings[ i ].follower == follower )
|
||||
{
|
||||
if( adjust )
|
||||
builder->kernings[ i ].adjust = adjust;
|
||||
else
|
||||
builder->kernings[ i ] = builder->kernings[ --builder->kernings_count ];
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if( adjust )
|
||||
{
|
||||
if( !builder->kernings || builder->kernings_count >= builder->kernings_capacity )
|
||||
{
|
||||
builder->kernings_capacity = builder->kernings_capacity ? builder->kernings_capacity * 2 : 256;
|
||||
pixelfont_builder_kerning_t* kernings = (pixelfont_builder_kerning_t*) PIXELFONT_MALLOC( builder->memctx,
|
||||
sizeof( pixelfont_builder_kerning_t ) * builder->kernings_capacity );
|
||||
if( builder->kernings )
|
||||
{
|
||||
PIXELFONT_MEMCPY( kernings, builder->kernings, sizeof( pixelfont_builder_kerning_t ) * builder->kernings_count );
|
||||
PIXELFONT_FREE( builder->memctx, builder->kernings );
|
||||
}
|
||||
builder->kernings = kernings;
|
||||
}
|
||||
|
||||
builder->kernings[ builder->kernings_count ].glyph = glyph;
|
||||
builder->kernings[ builder->kernings_count ].follower = follower;
|
||||
builder->kernings[ builder->kernings_count ].adjust = adjust;
|
||||
++builder->kernings_count;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pixelfont_t* pixelfont_builder_font( pixelfont_builder_t* builder )
|
||||
{
|
||||
if( builder->font ) PIXELFONT_FREE( builder->memctx, builder->font );
|
||||
|
||||
PIXELFONT_U8 kerning_counts[ 256 ];
|
||||
memset( kerning_counts, 0, sizeof( kerning_counts ) );
|
||||
for( int i = 0; i < builder->kernings_count; ++i )
|
||||
++kerning_counts[ builder->kernings[ i ].glyph ];
|
||||
|
||||
PIXELFONT_U32 offsets[ 256 ];
|
||||
memset( offsets, 0, sizeof( offsets ) );
|
||||
PIXELFONT_U32 current_offset = 0;
|
||||
int total_width = 0;
|
||||
int glyph_count = 0;
|
||||
for( int i = 0; i < sizeof( builder->glyphs ) / sizeof( *builder->glyphs ); ++i )
|
||||
{
|
||||
if( builder->glyphs[ i ].pixels )
|
||||
{
|
||||
++glyph_count;
|
||||
total_width += builder->glyphs[ i ].width;
|
||||
offsets[ i ] = current_offset;
|
||||
current_offset += 1 + kerning_counts[ i ] + 3 + builder->glyphs[ i ].width * builder->height;
|
||||
}
|
||||
}
|
||||
|
||||
size_t size_in_bytes = sizeof( pixelfont_t ) - sizeof( PIXELFONT_U8 ); // base size excluding final placeholder byte
|
||||
size_in_bytes += glyph_count + 2 * builder->kernings_count; // kerning pair count for each glyph + all kerning data
|
||||
size_in_bytes += 3 * glyph_count; // lead, trail and width for each glyph
|
||||
size_in_bytes += total_width * builder->height; // pixel data for all glyphs
|
||||
|
||||
pixelfont_t* font = (pixelfont_t*) PIXELFONT_MALLOC( builder->memctx, size_in_bytes );
|
||||
memset( font, 0, sizeof( *font ) );
|
||||
font->size_in_bytes = (PIXELFONT_U32) size_in_bytes;
|
||||
font->height = (PIXELFONT_U8) builder->height;
|
||||
font->line_spacing = (PIXELFONT_U8) builder->line_spacing;
|
||||
font->baseline = (PIXELFONT_U8) builder->baseline;
|
||||
memcpy( font->offsets, offsets, sizeof( font->offsets ) );
|
||||
|
||||
for( int i = 0; i < sizeof( builder->glyphs ) / sizeof( *builder->glyphs ); ++i )
|
||||
{
|
||||
if( builder->glyphs[ i ].pixels )
|
||||
{
|
||||
PIXELFONT_U8* src = builder->glyphs[ i ].pixels;
|
||||
PIXELFONT_U8* out = font->glyphs + offsets[ i ];
|
||||
*out++ = (PIXELFONT_U8) builder->glyphs[ i ].lead;
|
||||
*out++ = (PIXELFONT_U8) builder->glyphs[ i ].width;
|
||||
for( int y = 0; y < builder->height; ++y )
|
||||
for( int x = 0; x < builder->glyphs[ i ].width; ++x )
|
||||
*out++ = *src++;
|
||||
*out++ = (PIXELFONT_U8) builder->glyphs[ i ].trail;
|
||||
|
||||
*out++ = kerning_counts[ i ];
|
||||
for( int j = 0; j < builder->kernings_count; ++j )
|
||||
{
|
||||
if( builder->kernings[ j ].glyph == i )
|
||||
{
|
||||
*out++ = (PIXELFONT_U8) builder->kernings[ j ].follower;
|
||||
*out++ = (PIXELFONT_U8) builder->kernings[ j ].adjust;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
builder->font = font;
|
||||
return builder->font;
|
||||
}
|
||||
|
||||
|
||||
#endif /* PIXELFONT_BUILDER_IMPLEMENTATION */
|
||||
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This software is available under 2 licenses - you may choose the one you like.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE A - MIT License
|
||||
|
||||
Copyright (c) 2017 Mattias Gustavsson
|
||||
|
||||
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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||
software, either in source code form or as a compiled binary, for any purpose,
|
||||
commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||
software dedicate any and all copyright interest in the software to the public
|
||||
domain. We make this dedication for the benefit of the public at large and to
|
||||
the detriment of our heirs and successors. We intend this dedication to be an
|
||||
overt act of relinquishment in perpetuity of all present and future rights to
|
||||
this software under copyright law.
|
||||
|
||||
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 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.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
*/
|
||||
Reference in New Issue
Block a user