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

Reading and writing the Pink image formats. More...

#include <stddef.h>
#include <stdint.h>
#include <pink26/image.h>

Go to the source code of this file.

Functions

p26_status p26_read_pink_mem (const uint8_t *buf, size_t len, p26_image **out)
 Reads a Pink image held in memory.
p26_status p26_read_pink_file (const char *path, p26_image **out)
 Reads a Pink image file (binary mode), then decodes it as p26_read_pink_mem().
p26_status p26_write_pink_mem (const p26_image *img, uint8_t **buf, size_t *len)
 Encodes an image in the binary Pink format.
void p26_buffer_free (uint8_t *buf)
 Releases a buffer returned by p26_write_pink_mem().
p26_status p26_write_pink_file (const char *path, const p26_image *img)
 Writes an image to a file in the binary Pink format (see p26_write_pink_mem() for the encoding).

Detailed Description

Reading and writing the Pink image formats.

Pink extends the netpbm PGM format. A file is a text header followed by the pixels:

P5            magic number: the pixel type and the encoding
#xdim 0.5     optional comments; #xdim/#ydim/#zdim give voxel sizes
rs cs [ds]    dimensions: 2 values in 2D, 3 in 3D
255           maximum value
<pixels>      raw (little-endian) or ASCII, x fastest

Magic numbers understood by pink26:

type   raw   ASCII
u8     P5    P2     (P7: obsolete raw 3D u8, read only)
i32    P8    PB
f32    P9    PA
f64    PC    PD

Function Documentation

◆ p26_read_pink_mem()

p26_status p26_read_pink_mem ( const uint8_t * buf,
size_t len,
p26_image ** out )

Reads a Pink image held in memory.

The reader validates the header and the announced size before allocating: the size is checked against the bytes actually present, so a small hostile file cannot trigger a huge allocation. Header lines may end with "\n" or "\r\n". Voxel-size comments with an invalid or non-positive value are ignored. Raw multi-byte data is little-endian. Bytes after the expected data are ignored. Real numbers are read with '.' whatever the process locale.

Parameters
bufBytes of the file.
lenNumber of bytes in buf.
outSet to NULL on entry; receives the image on success. The caller owns it and releases it with p26_image_free().
Returns
P26_OK; P26_E_INVALID_ARG if out is NULL, or buf is NULL with len > 0; P26_E_FORMAT if the content is malformed or truncated (including len == 0); P26_E_UNSUPPORTED for 16-bit data (maximum value > 255) or more than 3 dimensions (several bands); P26_E_OVERFLOW if the announced size does not fit in memory; P26_E_NOMEM if an allocation fails. On error, *out stays NULL.

◆ p26_read_pink_file()

p26_status p26_read_pink_file ( const char * path,
p26_image ** out )

Reads a Pink image file (binary mode), then decodes it as p26_read_pink_mem().

Parameters
pathPath of the file.
outSet to NULL on entry; receives the image on success (released by the caller with p26_image_free()).
Returns
The statuses of p26_read_pink_mem(), and: P26_E_INVALID_ARG if path or out is NULL; P26_E_IO if the file cannot be opened, measured or fully read; P26_E_NOMEM if the file contents cannot be buffered. On error, *out stays NULL.

◆ p26_write_pink_mem()

p26_status p26_write_pink_mem ( const p26_image * img,
uint8_t ** buf,
size_t * len )

Encodes an image in the binary Pink format.

Format by type: U8 -> P5 (maximum value 255), I32 -> P8 (4294967295, Pink's convention), F32 -> P9 and F64 -> PC (0). Dimensions are written "rs cs" for a 2D image and "rs cs ds" for a 3D image, even when ds == 1. A #xdim v (then #ydim, and #zdim for a 3D image only) comment line is written for each voxel size that differs from 1.0, with "%.17g" in the C locale so that the value reads back exactly. voxel[2] of a 2D image is never written: a 2D image always reads back with voxel[2] = 1.0. Raw data is little-endian.

Parameters
imgImage to encode.
bufReceives a malloc'ed buffer holding the whole file; release it with p26_buffer_free(). NULL on error.
lenReceives the number of bytes in *buf; 0 on error.
Returns
P26_OK; P26_E_INVALID_ARG if img, buf or len is NULL, or img is invalid (NULL data, ndim or shape: see p26_image_validate()); P26_E_TYPE if img->dtype is unknown; P26_E_OVERFLOW if the header or the total size does not fit; P26_E_NOMEM if an allocation fails.

◆ p26_buffer_free()

void p26_buffer_free ( uint8_t * buf)

Releases a buffer returned by p26_write_pink_mem().

Parameters
bufThe buffer; NULL is accepted and does nothing.

◆ p26_write_pink_file()

p26_status p26_write_pink_file ( const char * path,
const p26_image * img )

Writes an image to a file in the binary Pink format (see p26_write_pink_mem() for the encoding).

The file is created or truncated.

Parameters
pathPath of the file.
imgImage to write.
Returns
The statuses of p26_write_pink_mem() (nothing is written then), and: P26_E_INVALID_ARG if path or img is NULL; P26_E_IO if the file cannot be opened, fully written or closed (the file may then exist with partial content).