Simple Graphics Library 1.0.0.0
Runtime-independent image processing primitives
Loading...
Searching...
No Matches
sgl-core.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2025 Dylan Hong
5 *
6 * This code is released under the MIT License.
7 * For conditions of distribution and use, see the LICENSE file.
8 */
9#ifndef SGL_CORE_H_
10#define SGL_CORE_H_
11
22#if defined(__cplusplus)
23extern "C" {
24#endif
25/*
26 *******************************************************************
27 * INCLUDES
28 *******************************************************************
29 */
30#include <sgl-compiler.h>
31#include <sgl-type.h>
32#include <sgl-fourcc.h>
33#include <sgl-config.h>
34
40#define SGL_UNUSED_PARAM(p) SGL_UNUSED(p)
42#define SGL_DIV_ROUNDUP(n, d) (((n) + (d) - 1) / (d))
44#define SGL_SAFE_FREE(p) if ((p) != SGL_NULL) { sgl_free((p)); (p) = SGL_NULL; }
46#define SGL_THREADPOOL_DEFAULT_MAX_ROUTINE_LISTS (4U)
48#define SGL_GENERIC_BULK_SIZE (4)
50#define SGL_SIMD_BULK_SIZE (8)
52#define SGL_BPP32 (4)
54#define SGL_BPP24 (3)
56#define SGL_BPP16 (2)
58#define SGL_BPP8 (1)
79
81typedef struct sgl_nearest_neighbor_lookup_table sgl_nearest_neighbor_lookup_t;
83typedef struct sgl_bilinear_lookup_table sgl_bilinear_lookup_t;
85typedef struct sgl_bicubic_lookup_table sgl_bicubic_lookup_t;
87typedef struct sgl_rotate_lookup_table sgl_rotate_lookup_t;
89typedef struct sgl_queue sgl_queue_t;
91typedef struct sgl_threadpool sgl_threadpool_t;
101 void *SGL_RESTRICT current,
102 void *SGL_RESTRICT cookie);
118void *sgl_memcpy(void *SGL_RESTRICT destination,
119 const void *SGL_RESTRICT source,
120 sgl_size_t size);
128void *sgl_memset(void *destination, sgl_int32_t value, sgl_size_t size);
178void sgl_free(void *memory);
200 sgl_int32_t d_width,
201 sgl_int32_t d_height,
202 sgl_int32_t s_width,
203 sgl_int32_t s_height);
206 sgl_int32_t d_width,
207 sgl_int32_t d_height,
208 sgl_int32_t s_width,
209 sgl_int32_t s_height);
212 sgl_int32_t d_width,
213 sgl_int32_t d_height,
214 sgl_int32_t s_width,
215 sgl_int32_t s_height);
216
223
247 sgl_int32_t d_width,
248 sgl_int32_t d_height,
250 sgl_int32_t s_width,
251 sgl_int32_t s_height,
252 sgl_int32_t bpp);
253
259 sgl_int32_t d_width,
260 sgl_int32_t d_height,
262 sgl_int32_t s_width,
263 sgl_int32_t s_height,
264 sgl_int32_t bpp);
265
271 sgl_int32_t d_width,
272 sgl_int32_t d_height,
274 sgl_int32_t s_width,
275 sgl_int32_t s_height,
276 sgl_int32_t bpp);
277
278#if defined(SGL_CFG_HAS_SIMD)
284 sgl_int32_t d_width,
285 sgl_int32_t d_height,
287 sgl_int32_t s_width,
288 sgl_int32_t s_height,
289 sgl_int32_t bpp);
290
296 sgl_int32_t d_width,
297 sgl_int32_t d_height,
299 sgl_int32_t s_width,
300 sgl_int32_t s_height,
301 sgl_int32_t bpp);
307 sgl_int32_t d_width,
308 sgl_int32_t d_height,
310 sgl_int32_t s_width,
311 sgl_int32_t s_height,
312 sgl_int32_t bpp);
313#endif /* SGL_CFG_HAS_SIMD */
340 const sgl_uint8_t *SGL_RESTRICT src,
341 sgl_int32_t src_width,
342 sgl_int32_t src_height,
344 sgl_int32_t x,
345 sgl_int32_t y,
346 sgl_int32_t crop_width,
347 sgl_int32_t crop_height,
348 sgl_int32_t bpp);
349#if defined(SGL_CFG_HAS_SIMD)
353 const sgl_uint8_t *SGL_RESTRICT src,
354 sgl_int32_t src_width,
355 sgl_int32_t src_height,
357 sgl_int32_t x,
358 sgl_int32_t y,
359 sgl_int32_t crop_width,
360 sgl_int32_t crop_height,
361 sgl_int32_t bpp);
362#endif /* SGL_CFG_HAS_SIMD */
384 sgl_int32_t src_width,
385 sgl_int32_t src_height,
386 sgl_float64_t angle,
387 sgl_int32_t *dst_width,
388 sgl_int32_t *dst_height);
398 sgl_int32_t src_width,
399 sgl_int32_t src_height,
400 sgl_int32_t bpp,
401 sgl_float64_t angle);
412 sgl_int32_t src_width,
413 sgl_int32_t src_height,
414 sgl_float64_t angle);
417
437 const sgl_uint8_t *SGL_RESTRICT src,
438 sgl_int32_t src_width,
439 sgl_int32_t src_height,
441 sgl_int32_t bpp,
442 sgl_float64_t angle);
443#if defined(SGL_CFG_HAS_SIMD)
448 const sgl_uint8_t *SGL_RESTRICT src,
449 sgl_int32_t src_width,
450 sgl_int32_t src_height,
452 sgl_int32_t bpp,
453 sgl_float64_t angle);
454#endif /* SGL_CFG_HAS_SIMD */
485 const sgl_uint8_t *SGL_RESTRICT src,
486 sgl_fourcc_t src_format,
488 sgl_fourcc_t dst_format,
489 sgl_int32_t width,
490 sgl_int32_t height);
508 const sgl_uint8_t *SGL_RESTRICT src,
509 sgl_fourcc_t src_format,
511 sgl_fourcc_t dst_format,
512 sgl_int32_t width,
513 sgl_int32_t height);
531 const sgl_uint8_t *SGL_RESTRICT src,
532 sgl_fourcc_t src_format,
534 sgl_fourcc_t dst_format,
535 sgl_int32_t width,
536 sgl_int32_t height);
554 const sgl_uint8_t *SGL_RESTRICT src,
555 sgl_fourcc_t src_format,
557 sgl_fourcc_t dst_format,
558 sgl_int32_t width,
559 sgl_int32_t height);
560#if defined(SGL_CFG_HAS_SIMD)
564 const sgl_uint8_t *SGL_RESTRICT src,
565 sgl_fourcc_t src_format,
567 sgl_fourcc_t dst_format,
568 sgl_int32_t width,
569 sgl_int32_t height);
573 const sgl_uint8_t *SGL_RESTRICT src,
574 sgl_fourcc_t src_format,
576 sgl_fourcc_t dst_format,
577 sgl_int32_t width,
578 sgl_int32_t height);
582 const sgl_uint8_t *SGL_RESTRICT src,
583 sgl_fourcc_t src_format,
585 sgl_fourcc_t dst_format,
586 sgl_int32_t width,
587 sgl_int32_t height);
591 const sgl_uint8_t *SGL_RESTRICT src,
592 sgl_fourcc_t src_format,
594 sgl_fourcc_t dst_format,
595 sgl_int32_t width,
596 sgl_int32_t height);
597#endif /* SGL_CFG_HAS_SIMD */
689#if defined(SGL_CFG_HAS_THREAD)
700 sgl_size_t num_threads,
701 sgl_size_t max_routine_lists,
702 const char *base_name);
733 sgl_queue_t *SGL_RESTRICT operations,
734 void *SGL_RESTRICT cookie);
750 sgl_queue_t *SGL_RESTRICT operations,
751 void *SGL_RESTRICT cookie);
752#endif /* SGL_CFG_HAS_THREAD */
755#if defined(__cplusplus)
756}
757#endif
758
759#endif /* !SGL_CORE_H_ */
760
761/* End Of File */
#define SGL_RESTRICT
Declares a non-aliasing pointer contract.
sgl_result_t sgl_simd_rgb2rgb(sgl_threadpool_t *pool, const sgl_uint8_t *src, sgl_fourcc_t src_format, sgl_uint8_t *dst, sgl_fourcc_t dst_format, sgl_int32_t width, sgl_int32_t height)
SIMD RGB-to-RGB conversion.
sgl_result_t sgl_generic_rgb2yuv(sgl_threadpool_t *pool, const sgl_uint8_t *src, sgl_fourcc_t src_format, sgl_uint8_t *dst, sgl_fourcc_t dst_format, sgl_int32_t width, sgl_int32_t height)
Converts any supported RGB layout to any supported YUV layout.
sgl_result_t sgl_generic_yuv2yuv(sgl_threadpool_t *pool, const sgl_uint8_t *src, sgl_fourcc_t src_format, sgl_uint8_t *dst, sgl_fourcc_t dst_format, sgl_int32_t width, sgl_int32_t height)
Converts between any two supported YUV layouts.
sgl_result_t sgl_generic_yuv2rgb(sgl_threadpool_t *pool, const sgl_uint8_t *src, sgl_fourcc_t src_format, sgl_uint8_t *dst, sgl_fourcc_t dst_format, sgl_int32_t width, sgl_int32_t height)
Converts any supported YUV layout to any supported RGB layout.
sgl_result_t sgl_simd_yuv2yuv(sgl_threadpool_t *pool, const sgl_uint8_t *src, sgl_fourcc_t src_format, sgl_uint8_t *dst, sgl_fourcc_t dst_format, sgl_int32_t width, sgl_int32_t height)
SIMD YUV-to-YUV conversion.
sgl_result_t sgl_generic_rgb2rgb(sgl_threadpool_t *pool, const sgl_uint8_t *src, sgl_fourcc_t src_format, sgl_uint8_t *dst, sgl_fourcc_t dst_format, sgl_int32_t width, sgl_int32_t height)
Converts between any two supported RGB layouts.
sgl_result_t sgl_simd_yuv2rgb(sgl_threadpool_t *pool, const sgl_uint8_t *src, sgl_fourcc_t src_format, sgl_uint8_t *dst, sgl_fourcc_t dst_format, sgl_int32_t width, sgl_int32_t height)
SIMD YUV-to-RGB conversion.
sgl_result_t sgl_simd_rgb2yuv(sgl_threadpool_t *pool, const sgl_uint8_t *src, sgl_fourcc_t src_format, sgl_uint8_t *dst, sgl_fourcc_t dst_format, sgl_int32_t width, sgl_int32_t height)
SIMD RGB-to-YUV conversion.
sgl_result_t sgl_generic_crop(sgl_threadpool_t *pool, const sgl_uint8_t *src, sgl_int32_t src_width, sgl_int32_t src_height, sgl_uint8_t *dst, sgl_int32_t x, sgl_int32_t y, sgl_int32_t crop_width, sgl_int32_t crop_height, sgl_int32_t bpp)
Copies a rectangular source region to a compact destination image.
sgl_result_t sgl_simd_crop(sgl_threadpool_t *pool, const sgl_uint8_t *src, sgl_int32_t src_width, sgl_int32_t src_height, sgl_uint8_t *dst, sgl_int32_t x, sgl_int32_t y, sgl_int32_t crop_width, sgl_int32_t crop_height, sgl_int32_t bpp)
SIMD crop implementation.
void * sgl_memcpy(void *destination, const void *source, sgl_size_t size)
Copies bytes between non-overlapping memory ranges.
void * sgl_memset(void *destination, sgl_int32_t value, sgl_size_t size)
Fills a memory range with one byte value.
sgl_result_t sgl_memory_pool_initialize(void *memory, sgl_size_t size)
Registers the process-wide SGL memory pool.
sgl_result_t sgl_memory_pool_deinitialize(void)
Detaches the caller-owned memory pool.
void sgl_free(void *memory)
Releases a block allocated by sgl_malloc() or sgl_calloc().
void * sgl_malloc(sgl_size_t size)
Allocates an aligned block from the SGL memory pool.
void * sgl_calloc(sgl_size_t count, sgl_size_t size)
Allocates and zero-initializes an array from the SGL memory pool.
sgl_result_t sgl_queue_copy(sgl_queue_t *dst, const sgl_queue_t *src)
Copies queue state and entries without transferring object ownership.
void sgl_queue_destroy(sgl_queue_t **queue)
Destroys a queue and clears the caller's handle.
sgl_result_t sgl_queue_unsafe_enqueue(sgl_queue_t *queue, const void *data)
Enqueues an object pointer without locking the queue.
void * sgl_queue_dequeue(sgl_queue_t *queue)
Removes and returns the oldest queue entry.
sgl_result_t sgl_queue_is_full(const sgl_queue_t *queue)
Tests whether a valid queue is full.
sgl_queue_t * sgl_queue_create(sgl_size_t capacity)
Creates an empty queue.
sgl_result_t sgl_queue_is_empty(const sgl_queue_t *queue)
Tests whether a valid queue is empty.
sgl_size_t sgl_queue_get_capacity(const sgl_queue_t *queue)
Returns queue capacity.
sgl_result_t sgl_queue_enqueue(sgl_queue_t *queue, const void *data)
Enqueues an object pointer while holding the queue lock.
sgl_size_t sgl_queue_get_count(const sgl_queue_t *queue)
Returns the current entry count.
void * sgl_queue_peek(sgl_queue_t *queue)
Returns the oldest queue entry without removing it.
void sgl_generic_destroy_bilinear_lut(sgl_bilinear_lookup_t *lut)
Destroys a bilinear LUT.
sgl_nearest_neighbor_lookup_t * sgl_generic_create_nearest_neighbor_lut(sgl_int32_t d_width, sgl_int32_t d_height, sgl_int32_t s_width, sgl_int32_t s_height)
Creates a nearest-neighbor lookup table for one resize geometry.
void sgl_generic_destroy_nearest_neighbor_lut(sgl_nearest_neighbor_lookup_t *lut)
Destroys a nearest-neighbor LUT.
sgl_result_t sgl_simd_resize_bicubic(sgl_threadpool_t *pool, sgl_bicubic_lookup_t *ext_lut, sgl_uint8_t *dst, sgl_int32_t d_width, sgl_int32_t d_height, sgl_uint8_t *src, sgl_int32_t s_width, sgl_int32_t s_height, sgl_int32_t bpp)
SIMD bicubic resize.
sgl_result_t sgl_generic_resize_nearest(sgl_threadpool_t *pool, sgl_nearest_neighbor_lookup_t *ext_lut, sgl_uint8_t *dst, sgl_int32_t d_width, sgl_int32_t d_height, sgl_uint8_t *src, sgl_int32_t s_width, sgl_int32_t s_height, sgl_int32_t bpp)
Resizes a packed image using nearest-neighbor sampling.
sgl_result_t sgl_simd_resize_bilinear(sgl_threadpool_t *pool, sgl_bilinear_lookup_t *ext_lut, sgl_uint8_t *dst, sgl_int32_t d_width, sgl_int32_t d_height, sgl_uint8_t *src, sgl_int32_t s_width, sgl_int32_t s_height, sgl_int32_t bpp)
SIMD bilinear resize.
sgl_bicubic_lookup_t * sgl_generic_create_bicubic_lut(sgl_int32_t d_width, sgl_int32_t d_height, sgl_int32_t s_width, sgl_int32_t s_height)
Creates a bicubic lookup table.
sgl_result_t sgl_generic_resize_bicubic(sgl_threadpool_t *pool, sgl_bicubic_lookup_t *ext_lut, sgl_uint8_t *dst, sgl_int32_t d_width, sgl_int32_t d_height, sgl_uint8_t *src, sgl_int32_t s_width, sgl_int32_t s_height, sgl_int32_t bpp)
Resizes a packed image using bicubic interpolation.
sgl_bilinear_lookup_t * sgl_generic_create_bilinear_lut(sgl_int32_t d_width, sgl_int32_t d_height, sgl_int32_t s_width, sgl_int32_t s_height)
Creates a bilinear lookup table.
sgl_result_t sgl_generic_resize_bilinear(sgl_threadpool_t *pool, sgl_bilinear_lookup_t *ext_lut, sgl_uint8_t *dst, sgl_int32_t d_width, sgl_int32_t d_height, sgl_uint8_t *src, sgl_int32_t s_width, sgl_int32_t s_height, sgl_int32_t bpp)
Resizes a packed image using bilinear interpolation.
void sgl_generic_destroy_bicubic_lut(sgl_bicubic_lookup_t *lut)
Destroys a bicubic LUT.
sgl_result_t sgl_simd_resize_nearest(sgl_threadpool_t *pool, sgl_nearest_neighbor_lookup_t *ext_lut, sgl_uint8_t *dst, sgl_int32_t d_width, sgl_int32_t d_height, sgl_uint8_t *src, sgl_int32_t s_width, sgl_int32_t s_height, sgl_int32_t bpp)
SIMD nearest-neighbor resize.
sgl_int32_t sgl_get_rotated_size(sgl_int32_t src_width, sgl_int32_t src_height, sgl_int32_t bpp, sgl_float64_t angle)
Calculates the required destination buffer size for a rotation.
void sgl_generic_destroy_rotate_lut(sgl_rotate_lookup_t *lut)
Destroys a rotation LUT.
sgl_result_t sgl_simd_rotate(sgl_threadpool_t *pool, sgl_rotate_lookup_t *ext_lut, const sgl_uint8_t *src, sgl_int32_t src_width, sgl_int32_t src_height, sgl_uint8_t *dst, sgl_int32_t bpp, sgl_float64_t angle)
SIMD rotation implementation.
sgl_result_t sgl_get_rotated_dimensions(sgl_int32_t src_width, sgl_int32_t src_height, sgl_float64_t angle, sgl_int32_t *dst_width, sgl_int32_t *dst_height)
Calculates the destination bounding-box dimensions for a rotation.
sgl_rotate_lookup_t * sgl_generic_create_rotate_lut(sgl_int32_t src_width, sgl_int32_t src_height, sgl_float64_t angle)
Creates a reusable rotation mapping table.
sgl_result_t sgl_generic_rotate(sgl_threadpool_t *pool, sgl_rotate_lookup_t *ext_lut, const sgl_uint8_t *src, sgl_int32_t src_width, sgl_int32_t src_height, sgl_uint8_t *dst, sgl_int32_t bpp, sgl_float64_t angle)
Rotates a tightly packed image using nearest-neighbor sampling.
double sgl_float64_t
Double-precision floating-point value.
Definition sgl-type.h:113
signed int sgl_int32_t
Signed integer with exactly 32 value bits.
Definition sgl-type.h:32
unsigned long sgl_size_t
Unsigned type used for object sizes and byte counts.
Definition sgl-type.h:44
unsigned char sgl_uint8_t
Unsigned integer with exactly 8 value bits.
Definition sgl-type.h:26
sgl_result_t sgl_threadpool_destroy(sgl_threadpool_t *pool)
Stops workers and destroys a thread pool.
sgl_size_t sgl_threadpool_get_num_threads(const sgl_threadpool_t *pool)
Returns the number of background workers.
sgl_threadpool_t * sgl_threadpool_create(sgl_size_t num_threads, sgl_size_t max_routine_lists, const char *base_name)
Creates a worker-thread pool.
sgl_result_t sgl_threadpool_attach_routine(sgl_threadpool_t *pool, sgl_threadpool_routine_t routine, sgl_queue_t *operations, void *cookie)
Processes all queued operations and restores their queue entries.
sgl_result_t sgl_threadpool_attach_routine_consuming(sgl_threadpool_t *pool, sgl_threadpool_routine_t routine, sgl_queue_t *operations, void *cookie)
Processes and removes every operation entry from the queue.
struct sgl_rotate_lookup_table sgl_rotate_lookup_t
Opaque nearest-neighbor rotation lookup table.
Definition sgl-core.h:87
struct sgl_bicubic_lookup_table sgl_bicubic_lookup_t
Opaque bicubic resize lookup table.
Definition sgl-core.h:85
struct sgl_threadpool sgl_threadpool_t
Opaque synchronous worker-thread pool.
Definition sgl-core.h:91
struct sgl_queue sgl_queue_t
Opaque fixed-capacity queue.
Definition sgl-core.h:89
struct sgl_bilinear_lookup_table sgl_bilinear_lookup_t
Opaque bilinear resize lookup table.
Definition sgl-core.h:83
sgl_result_t
Result codes returned by SGL operations.
Definition sgl-core.h:67
void(* sgl_threadpool_routine_t)(void *current, void *cookie)
Processes one queue entry submitted to a thread pool.
Definition sgl-core.h:100
struct sgl_nearest_neighbor_lookup_table sgl_nearest_neighbor_lookup_t
Opaque nearest-neighbor resize lookup table.
Definition sgl-core.h:81
@ SGL_ERROR_INVALID_ARGUMENTS
One or more arguments violate the API contract.
Definition sgl-core.h:70
@ SGL_QUEUE_IS_NOT_EMPTY
Queue contains at least one entry.
Definition sgl-core.h:75
@ SGL_ERROR_MISSMATCHED_CAPACITY
Destination queue capacity is smaller than the source capacity.
Definition sgl-core.h:72
@ SGL_QUEUE_IS_FULL
Queue has reached its fixed capacity.
Definition sgl-core.h:76
@ SGL_FAILURE
Operation failed without a more specific code.
Definition sgl-core.h:69
@ SGL_SUCCESS
Operation completed successfully.
Definition sgl-core.h:68
@ SGL_QUEUE_IS_EMPTY
Queue contains no entries.
Definition sgl-core.h:74
@ SGL_QUEUE_IS_NOT_FULL
Queue can accept another entry.
Definition sgl-core.h:77
@ SGL_ERROR_NOT_SUPPORTED
Requested feature is unavailable in this build.
Definition sgl-core.h:73
@ SGL_ERROR_MEMORY_ALLOCATION
The configured SGL memory pool could not satisfy an allocation.
Definition sgl-core.h:71
Portable compiler attributes, branch hints, and prefetch helpers.
Standard and SGL-specific pixel-format identifiers.
sgl_uint32_t sgl_fourcc_t
32-bit pixel-format identifier created by SGL_FOURCC.
Definition sgl-fourcc.h:20
Runtime-independent scalar, pointer, size, and Boolean types.