|
pink26 0.1.1
Image processing and digital topology for teaching
|
Reading and writing the Pink image formats. More...
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). | |
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
| 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.
| buf | Bytes of the file. |
| len | Number of bytes in buf. |
| out | Set to NULL on entry; receives the image on success. The caller owns it and releases it with p26_image_free(). |
| 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().
| path | Path of the file. |
| out | Set to NULL on entry; receives the image on success (released by the caller with p26_image_free()). |
| 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.
| img | Image to encode. |
| buf | Receives a malloc'ed buffer holding the whole file; release it with p26_buffer_free(). NULL on error. |
| len | Receives the number of bytes in *buf; 0 on error. |
| void p26_buffer_free | ( | uint8_t * | buf | ) |
Releases a buffer returned by p26_write_pink_mem().
| buf | The buffer; NULL is accepted and does nothing. |
| 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.
| path | Path of the file. |
| img | Image to write. |