Simple Graphics Library 1.0.0.0
Runtime-independent image processing primitives
Loading...
Searching...
No Matches
Image Operations

Common Buffer Rules

Resize, crop, and rotate operate on packed pixels with a caller-supplied positive byte count per pixel. Their source size is width * height * bpp. Conversion uses the FourCC-specific formulas in Pixel Formats and Buffer Sizes. All image buffers are caller-owned and source/destination ranges must not overlap.

Resize

Create one LUT when the same source and destination dimensions are used for multiple frames. The LUT does not depend on pixel channel order or bpp.

sgl_result_t result;
dst_width, dst_height, src_width, src_height);
if (lut == SGL_NULL) {
}
else {
pool, lut,
dst, dst_width, dst_height,
src, src_width, src_height,
}
void sgl_generic_destroy_bilinear_lut(sgl_bilinear_lookup_t *lut)
Destroys a bilinear LUT.
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.
#define SGL_NULL
Null pointer constant for public SGL interfaces.
Definition sgl-type.h:138
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
@ SGL_ERROR_MEMORY_ALLOCATION
The configured SGL memory pool could not satisfy an allocation.
Definition sgl-core.h:71
#define SGL_BPP32
Four bytes per pixel.
Definition sgl-core.h:52

Pass SGL_NULL as the LUT for one-shot work. SGL then allocates and destroys a temporary table. Nearest-neighbor is fastest and preserves exact source pixel values; bilinear smooths a 2x2 neighborhood; bicubic uses a 4x4 neighborhood.

Crop

Crop coordinates are zero-based and the complete rectangle must fit inside the source image. The destination has no source stride or padding:

pool,
src, src_width, src_height,
dst,
crop_x, crop_y, crop_width, crop_height,
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.

Allocate at least crop_width * crop_height * bpp destination bytes.

Rotate

Query dimensions before allocating the caller-owned destination. Positive angles rotate clockwise. Output pixels outside the source footprint are zero.

sgl_int32_t dst_width;
sgl_int32_t dst_height;
sgl_int32_t dst_bytes;
src_width, src_height, angle,
&dst_width, &dst_height);
src_width, src_height, SGL_BPP32, angle);
if ((result == SGL_SUCCESS) && (dst_bytes > 0)) {
pool, rotate_lut,
src, src_width, src_height,
dst, SGL_BPP32, angle);
}
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.
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_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.
signed int sgl_int32_t
Signed integer with exactly 32 value bits.
Definition sgl-type.h:32
@ SGL_SUCCESS
Operation completed successfully.
Definition sgl-core.h:68

A rotation LUT can be shared by generic and SIMD calls and reused across frames only when source dimensions and normalized angle are identical.

Convert

Choose the entry point from the source and destination format categories:

Source Destination Generic API SIMD API
RGB RGB sgl_generic_rgb2rgb() sgl_simd_rgb2rgb()
RGB YUV sgl_generic_rgb2yuv() sgl_simd_rgb2yuv()
YUV RGB sgl_generic_yuv2rgb() sgl_simd_yuv2rgb()
YUV YUV sgl_generic_yuv2yuv() sgl_simd_yuv2yuv()
pool,
width, height);
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.
#define SGL_FOURCC_RGBA
V4L2 RGBA32 / DRM ABGR8888 code; packed bytes R, G, B, A.
Definition sgl-fourcc.h:44
#define SGL_FOURCC_NV12
DRM/V4L2 NV12; full Y plane followed by interleaved Cb, Cr 4:2:0.
Definition sgl-fourcc.h:88

Every supported source format can be converted to every supported destination format in the selected category. When adding alpha to RGB, SGL writes 255; when removing alpha or converting to YUV, it discards alpha.

Generic and SIMD Backends

Generic functions are always declared. SIMD functions are declared only when SGL_CFG_HAS_SIMD is enabled in the generated sgl-config.h. Keep portable fallback selection at compile time:

#if defined(SGL_CFG_HAS_SIMD)
result = sgl_simd_crop(pool, src, width, height, dst, x, y, cw, ch, bpp);
#else
result = sgl_generic_crop(pool, src, width, height, dst, x, y, cw, ch, bpp);
#endif
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.