Byakuren 1.0.1
A theme color extracting library implemented by C.
Loading...
Searching...
No Matches
bkr_common.h File Reference
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "third-party/xmempool/xmempool.h"

Go to the source code of this file.

Data Structures

struct  bkr_rgb
 RGB color structure. More...
 
struct  bkr_color_stats
 Color statistics structure. More...
 
struct  bkr_octree_node
 Octree node structure for color quantization. More...
 
struct  bkr_octree_reducible_list_node
 Reducible list node structure for octree color quantization. More...
 
struct  bkr_palette_array
 Color palette array structure. More...
 
struct  bkr_mindiff_parameter
 Parameters for the minimum difference color quantization method. More...
 

Macros

#define BKR_VERSION   "1.0.1"
 Byakuren - A theme color extracting library implemented by C.
 
#define NULL   (0)
 
#define SAFE_DESTROY_POOL(pool)
 Safely destroy a memory pool.
 
#define SAFE_FREE(pointer)
 Safely free allocated memory.
 
#define MAX_INT   (2147483647)
 Maximum value for a 32-bit signed integer.
 
#define BKR_RGB_TO_INT32(r, g, b)
 Convert RGB values to a 32-bit integer representation.
 
#define DIFF_A_B_GTE_OFFSET(item, a, b, offset)
 Check if the difference between two color components is within a given offset.
 
#define BKR_IS_GRAY_INNER(item, offset)
 
#define BKR_IS_GRAY(item, offset)
 Check if a color is considered grayscale based on a given offset.
 

Typedefs

typedef struct bkr_rgb bkr_rgb
 RGB color structure.
 
typedef struct bkr_color_stats bkr_color_stats
 Color statistics structure.
 
typedef struct bkr_octree_node bkr_octree_node
 Octree node structure for color quantization.
 
typedef struct bkr_octree_reducible_list_node bkr_octree_reducible_list_node
 Reducible list node structure for octree color quantization.
 
typedef struct bkr_palette_array bkr_palette_array
 Color palette array structure.
 
typedef struct bkr_mindiff_parameter bkr_mindiff_parameter
 Parameters for the minimum difference color quantization method.
 

Functions

int bkr_init ()
 Initialize the Byakuren library.
 
void bkr_destroy ()
 Clean up and release resources used by the Byakuren library.
 
int _stats_cmp (const void *a, const void *b)
 Comparison function for sorting color statistics.
 

Variables

xmem_pool_handle bkr_rgb_pool
 Memory pool handle for RGB color structures.
 

Macro Definition Documentation

◆ BKR_IS_GRAY

#define BKR_IS_GRAY ( item,
offset )
Value:
(BKR_IS_GRAY_INNER(item, offset))
#define BKR_IS_GRAY_INNER(item, offset)
Definition bkr_common.h:70

Check if a color is considered grayscale based on a given offset.

This macro checks if the differences between red, green, and blue components are all within the specified offset.

Definition at line 81 of file bkr_common.h.

◆ BKR_IS_GRAY_INNER

#define BKR_IS_GRAY_INNER ( item,
offset )
Value:
DIFF_A_B_GTE_OFFSET(item, red, green, offset) && \
DIFF_A_B_GTE_OFFSET(item, red, blue, offset) && \
DIFF_A_B_GTE_OFFSET(item, green, blue, offset)
#define DIFF_A_B_GTE_OFFSET(item, a, b, offset)
Check if the difference between two color components is within a given offset.
Definition bkr_common.h:67

Definition at line 70 of file bkr_common.h.

70#define BKR_IS_GRAY_INNER(item, offset) \
71 DIFF_A_B_GTE_OFFSET(item, red, green, offset) && \
72 DIFF_A_B_GTE_OFFSET(item, red, blue, offset) && \
73 DIFF_A_B_GTE_OFFSET(item, green, blue, offset)

◆ BKR_RGB_TO_INT32

#define BKR_RGB_TO_INT32 ( r,
g,
b )
Value:
(((r) << 16) + ((g) << 8) + (b))

Convert RGB values to a 32-bit integer representation.

Definition at line 61 of file bkr_common.h.

◆ BKR_VERSION

#define BKR_VERSION   "1.0.1"

Byakuren - A theme color extracting library implemented by C.

Copyright (c) 2024 XadillaX i@233.nosp@m.3.mo.nosp@m.e

MIT License https://github.com/XadillaX/byakuren/blob/master/LICENSE

Version string of the Byakuren library.

Definition at line 23 of file bkr_common.h.

◆ DIFF_A_B_GTE_OFFSET

#define DIFF_A_B_GTE_OFFSET ( item,
a,
b,
offset )
Value:
(abs((item)->a - (item)->b) <= offset)

Check if the difference between two color components is within a given offset.

Definition at line 67 of file bkr_common.h.

67#define DIFF_A_B_GTE_OFFSET(item, a, b, offset) \
68 (abs((item)->a - (item)->b) <= offset)

◆ MAX_INT

#define MAX_INT   (2147483647)

Maximum value for a 32-bit signed integer.

Definition at line 56 of file bkr_common.h.

◆ NULL

#define NULL   (0)

Definition at line 26 of file bkr_common.h.

◆ SAFE_DESTROY_POOL

#define SAFE_DESTROY_POOL ( pool)
Value:
if (pool) { \
xmem_destroy_pool(pool); \
pool = NULL; \
}
#define NULL
Definition bkr_common.h:26

Safely destroy a memory pool.

This macro checks if the pool exists before destroying it and sets the pointer to NULL afterwards.

Definition at line 35 of file bkr_common.h.

35#define SAFE_DESTROY_POOL(pool) \
36 if (pool) { \
37 xmem_destroy_pool(pool); \
38 pool = NULL; \
39 }

◆ SAFE_FREE

#define SAFE_FREE ( pointer)
Value:
if (pointer) { \
free(pointer); \
pointer = NULL; \
}

Safely free allocated memory.

This macro checks if the pointer is not NULL before freeing it and sets the pointer to NULL afterwards.

Definition at line 47 of file bkr_common.h.

47#define SAFE_FREE(pointer) \
48 if (pointer) { \
49 free(pointer); \
50 pointer = NULL; \
51 }

Typedef Documentation

◆ bkr_color_stats

typedef struct bkr_color_stats bkr_color_stats

Color statistics structure.

This structure holds information about a specific color, including its RGB values, a 32-bit integer representation, and its frequency count. It is used to store results of color analysis operations.

◆ bkr_mindiff_parameter

typedef struct bkr_mindiff_parameter bkr_mindiff_parameter

Parameters for the minimum difference color quantization method.

This structure holds parameters used in the minimum difference method for color quantization, including a custom palette and grayscale offset.

◆ bkr_octree_node

typedef struct bkr_octree_node bkr_octree_node

Octree node structure for color quantization.

This structure represents a node in the octree used for color quantization. It stores color component sums, leaf status, pixel count, and pointers to child nodes.

◆ bkr_octree_reducible_list_node

typedef struct bkr_octree_reducible_list_node bkr_octree_reducible_list_node

Reducible list node structure for octree color quantization.

This structure is used to maintain a list of reducible nodes in the octree. It is part of the mechanism for merging nodes during color reduction.

◆ bkr_palette_array

typedef struct bkr_palette_array bkr_palette_array

Color palette array structure.

This structure represents a color palette, containing a count of colors and a pointer to an array of RGB color structures.

◆ bkr_rgb

typedef struct bkr_rgb bkr_rgb

RGB color structure.

This structure represents a color in the RGB color space. Each component (red, green, blue) is an 8-bit unsigned integer, allowing for 256 levels of intensity for each color channel.

Function Documentation

◆ _stats_cmp()

int _stats_cmp ( const void * a,
const void * b )
extern

Comparison function for sorting color statistics.

This function is used as a comparator for qsort to sort bkr_color_stats structures.

It first compares the count of colors, sorting in descending order. If the counts are equal, it then compares the color values, also in descending order.

Parameters
aPointer to the first color statistic to compare.
bPointer to the second color statistic to compare.
Returns
Negative if a should come before b, positive if b should come before a, and zero if they are equivalent.

◆ bkr_destroy()

void bkr_destroy ( )
extern

Clean up and release resources used by the Byakuren library.

This function destroys the memory pools and frees any allocated resources.

It should be called when the Byakuren library is no longer needed to prevent memory leaks and ensure proper cleanup of resources.

◆ bkr_init()

int bkr_init ( )
extern

Initialize the Byakuren library.

This function initializes the necessary resources for the Byakuren library.

It creates a memory pool for RGB color structures and initializes the octree environment. This function should be called before using any other Byakuren library functions.

Returns
1 if initialization is successful, 0 otherwise.

Variable Documentation

◆ bkr_rgb_pool

xmem_pool_handle bkr_rgb_pool
extern

Memory pool handle for RGB color structures.

This global variable holds the handle to a memory pool used for allocating bkr_rgb structures. It improves memory allocation efficiency for frequently used RGB color objects.

Note
This library does not support multi-threading, primarily due to limitations of the memory pool implementation.