pink26 0.1.1
Image processing and digital topology for teaching
Loading...
Searching...
No Matches
image.h File Reference

The image descriptor shared by every pink26 operator. More...

#include <stddef.h>
#include <pink26/status.h>

Go to the source code of this file.

Data Structures

struct  p26_image
 A 2D or 3D image. More...

Enumerations

enum  { P26_MAX_NDIM = 3 }
 Largest number of axes of an image.
enum  p26_dtype { P26_U8 = 1 , P26_I32 = 4 , P26_F32 = 5 , P26_F64 = 6 }
 Pixel type. More...

Functions

size_t p26_dtype_size (p26_dtype dtype)
 Size in bytes of one pixel of the given type.
p26_status p26_checked_count (int ndim, const p26_index *shape, p26_dtype dtype, p26_index *pixel_count, size_t *byte_count)
 Validates an image geometry and computes its pixel and byte counts.
p26_status p26_image_alloc (int ndim, const p26_index *shape, p26_dtype dtype, p26_image **out)
 Allocates an image whose pixels are all zero.
p26_status p26_image_wrap (int ndim, const p26_index *shape, p26_dtype dtype, void *data, p26_image **out)
 Creates an image descriptor over pixels owned by the caller (no copy).
void p26_image_free (p26_image *img)
 Releases an image descriptor, and its pixels if it owns them.
p26_status p26_image_validate (const p26_image *img)
 Checks that a caller-supplied image descriptor can be used safely.
p26_index p26_rs (const p26_image *img)
 Row size: number of pixels along x (Pink's rowsize).
p26_index p26_cs (const p26_image *img)
 Column size: number of pixels along y (Pink's colsize).
p26_index p26_ds (const p26_image *img)
 Depth size: number of pixels along z (Pink's depth); 1 for a 2D image.

Detailed Description

The image descriptor shared by every pink26 operator.

A p26_image is a 2D or 3D array of pixels stored contiguously, x varying fastest, with the shape given in NumPy order (slowest axis first) so that the Python bindings can share the memory without copying.

Names inherited from Pink: rs (row size, the width along x), cs (column size, the height along y) and ds (depth size, along z) mirror Pink's rowsize(), colsize() and depth(). Pixel (x, y, z) has index i = z * rs * cs + y * rs + x, as in Pink.

Enumeration Type Documentation

◆ p26_dtype

enum p26_dtype

Pixel type.

The values are Pink's VFF_TYP_* codes, so they convert as is.

Enumerator
P26_U8 

uint8_t (VFF_TYP_1_BYTE)

P26_I32 

int32_t (VFF_TYP_4_BYTE)

P26_F32 

float (VFF_TYP_FLOAT)

P26_F64 

double (VFF_TYP_DOUBLE)

Function Documentation

◆ p26_dtype_size()

size_t p26_dtype_size ( p26_dtype dtype)

Size in bytes of one pixel of the given type.

Parameters
dtypePixel type (any value).
Returns
1, 4, 4 or 8; 0 for a value that is not a p26_dtype.

◆ p26_checked_count()

p26_status p26_checked_count ( int ndim,
const p26_index * shape,
p26_dtype dtype,
p26_index * pixel_count,
size_t * byte_count )

Validates an image geometry and computes its pixel and byte counts.

The products are checked: no intermediate result may exceed PTRDIFF_MAX.

Parameters
ndim2 or 3.
shapendim positive values, NumPy order (see p26_image).
dtypePixel type.
pixel_countReceives the number of pixels; may be NULL.
byte_countReceives the size of the pixel data in bytes; may be NULL.
Returns
P26_OK; P26_E_INVALID_ARG if ndim is not 2 or 3, shape is NULL or a value of shape is not positive; P26_E_TYPE if dtype is unknown; P26_E_OVERFLOW if the pixel or byte count exceeds PTRDIFF_MAX. On error, *pixel_count and *byte_count are left untouched.

◆ p26_image_alloc()

p26_status p26_image_alloc ( int ndim,
const p26_index * shape,
p26_dtype dtype,
p26_image ** out )

Allocates an image whose pixels are all zero.

Parameters
ndim2 or 3.
shapendim positive values, NumPy order. For ndim == 2, image->shape[2] is set to 1.
dtypePixel type.
outSet to NULL on entry; receives the new image (owns_data = 1, voxel sizes 1.0). The caller releases it with p26_image_free().
Returns
P26_OK; P26_E_INVALID_ARG if out is NULL or the geometry is invalid; P26_E_TYPE, P26_E_OVERFLOW as p26_checked_count(); P26_E_NOMEM if an allocation fails. On error, *out stays NULL.

◆ p26_image_wrap()

p26_status p26_image_wrap ( int ndim,
const p26_index * shape,
p26_dtype dtype,
void * data,
p26_image ** out )

Creates an image descriptor over pixels owned by the caller (no copy).

Parameters
ndim2 or 3.
shapendim positive values, NumPy order.
dtypePixel type.
dataPixels, C-contiguous; must hold the whole image and outlive the descriptor. p26_image_free() does not release it.
outSet to NULL on entry; receives the new descriptor (owns_data = 0, voxel sizes 1.0).
Returns
P26_OK; P26_E_INVALID_ARG if data or out is NULL or the geometry is invalid; P26_E_TYPE, P26_E_OVERFLOW as p26_checked_count(); P26_E_NOMEM if the descriptor cannot be allocated. On error, *out stays NULL.

◆ p26_image_free()

void p26_image_free ( p26_image * img)

Releases an image descriptor, and its pixels if it owns them.

Parameters
imgImage to release; NULL is accepted and does nothing.

◆ p26_image_validate()

p26_status p26_image_validate ( const p26_image * img)

Checks that a caller-supplied image descriptor can be used safely.

Images made by p26_image_alloc() or p26_image_wrap() are always valid. The fields of a p26_image are public, though, so a function that receives one from its caller calls this first, before it reads the shape (p26_rs() of an image with ndim 0 would read outside img->shape) or the pixels.

Parameters
imgImage to check.
Returns
P26_OK; P26_E_INVALID_ARG if img or img->data is NULL, img->ndim is not 2 or 3, or a value of img->shape is not positive; P26_E_TYPE if img->dtype is unknown; P26_E_OVERFLOW if the pixel or byte count exceeds PTRDIFF_MAX.

◆ p26_rs()

p26_index p26_rs ( const p26_image * img)

Row size: number of pixels along x (Pink's rowsize).

Parameters
imgA valid image (not NULL, ndim 2 or 3); not checked.
Returns
The number of pixels of a row, shape[ndim - 1].

◆ p26_cs()

p26_index p26_cs ( const p26_image * img)

Column size: number of pixels along y (Pink's colsize).

Parameters
imgA valid image (not NULL, ndim 2 or 3); not checked.
Returns
The number of rows, shape[ndim - 2].

◆ p26_ds()

p26_index p26_ds ( const p26_image * img)

Depth size: number of pixels along z (Pink's depth); 1 for a 2D image.

Parameters
imgA valid image (not NULL, ndim 2 or 3); not checked.
Returns
The number of planes, shape[0] for a 3D image; 1 for a 2D image.