Expand description
Crate to handle image data backed either by a contiguous slice or a vector.
The image data is stored in a row-major order and can be of different pixel
types - u8, u16, and f32. The image data supports arbitrary color spaces
and number of channels, but the number of channels must be consistent with the
length of the backing storage.
The image size is limited to 65535 x 65535 pixels. In case the image is a
Bayer mosaic image, the crate supports debayering of the image data.
The crate additionally supports serialization and deserialization of the image
data using the serde framework.
The crate provides a concrete type ImageRef to store image data and a type-erased
version DynamicImageRef to store image data with different pixel types.
Additionally, the crate provides a GenericImageRef type to store a DynamicImageRef
with additional metadata, such as the image creation timestamp, and many more. The
metadata keys must be 80 characters or less. Uniqueness of the keys is not enforced,
but is strongly recommended; the keys are case-insensitive.
The crate, with the optional image feature, provides can convert between
DynamicImageRef and DynamicImage from the image crate.
With the optional fitsio feature, the crate can write a GenericImageRef, with
all associated metadata, to a FITS file.
§Usage
use refimage::{ImageRef, ColorSpace, DynamicImageRef, GenericImageRef, GenericImageOwned};
use refimage::chrono::DateTime;
use std::time::Duration;
let mut data = vec![1u8, 2, 3, 4, 5, 6, 0, 0]; // 3x2 grayscale image, with extra padding that will be ignored
let img = ImageRef::new(&mut data, 3, 2, ColorSpace::Gray).unwrap(); // Create ImageRef
let img = DynamicImageRef::from(img); // Convert to DynamicImageRef
let now = DateTime::from_timestamp(1_700_000_000, 0).unwrap(); // in an app: chrono::Utc::now()
let mut img = GenericImageRef::new(now, Duration::from_millis(20), img); // timestamp + exposure are mandatory
img.insert_key("CAMERANAME", "Canon EOS 5D Mark IV".to_string()).unwrap(); // Insert metadata
let serialized = bincode::serialize(&img).unwrap(); // Serialize the image
let deserialized: GenericImageOwned = bincode::deserialize(&serialized).unwrap(); // Deserialize the image§Processing pipelines
All pixel conversions — debayer, luminance, pixel-type conversion, affine pixel
scaling, crop, ROI, flips, 90° rotations, aspect-preserving resize — are
Ops on a declarative, reusable
Pipeline. apply
runs it once and returns an owned image; given a GenericImageRef it returns a
GenericImageOwned with the metadata carried across unchanged.
compile-ing against a concrete
ImageSpec pre-allocates every buffer, yielding a
Runner that processes successive frames with zero
per-frame allocation (serial-tiled strategy).
§FITS
GenericImageRef / GenericImageOwned can be written to the Flexible Image Transport
System via the FitsWrite trait.
It supports uncompressed output and the
tile-compression convention with GZIP_1 and RICE_1.
§Optional Features
Features are available to extend the functionalities of the core refimage data types:
rayon: Parallelizes the luminance / demosaic / cast kernels inside thepipeline, and enables its parallelStrategyvariants (enabled by default).grow: Lets a compiledRunnerreallocate its buffers when handed a frame whose shape differs from the one it was compiled for (enabled by default).image: EnablesTryFromconversions betweenDynamicImageandDynamicImageRef,DynamicImageOwned(disabled by default).
Re-exports§
pub use chrono;
Modules§
- pipeline
- Reusable, allocation-aware processing pipelines for pixel conversion in this crate (debayer, luminance, pixel-type conversion, affine pixel scaling, crop, ROI, flips, 90° rotations, aspect-preserving resize).
Structs§
- Auto
Tile - Type-state marker: the tile shape has not been chosen yet, so both
tile_rowsandtile_dimsare available. - Fits
Compression - An opaque, resolved compression choice, built from
Gzip/Rice/Hcompress(orFitsCompression::NONE) and handed to the write methods. - Fits
Writer - A multi-HDU FITS file open for writing, over any
Writesink (a file by default, or an in-memory buffer — handy onwasm32, which has no filesystem). - Fixed
Tile - Type-state marker: the tile shape is fixed, so neither tile selector is in scope any
more (you cannot combine
tile_rowsandtile_dims). - Generic
Image Owned - A serializable, generic image with metadata, backed by
DynamicImageOwned. - Generic
Image Ref - A serializable, generic image with metadata, backed by
DynamicImageRef. - Generic
Line Item - A metadata item.
- Gzip
GZIP_1tile compression — lossless for every pixel type.- Hcompress
HCOMPRESS_1tile compression (H-transform + quadtree coder). Lossless atscale = 0;f32is quantized first. Each tile must be at least 4×4; the default tiling is one whole channel plane per tile.- Image
Owned - A structure that holds image data backed by a vector.
- Image
Ref - A structure that holds image data backed by a slice or a vector.
- Image
View - A read-only, single-type view over an image’s samples: a borrowed slice plus
the shape and (optional) sub-container bit-depth tag. The immutable analogue
of
ImageRef. - Metadata
- Image metadata: a mandatory typed core with an ordered map of extra items.
- Optimum
Exposure - Configuration used to find the optimum exposure.
- Optimum
Exposure Builder - Builder for the
OptimumExposurecalculator. - Optimum
Exposure Result - Outcome of
OptimumExposure::calculate/CalcOptExp::calc_opt_exp. - Quantize
- Lossy quantization of
f32images, shared byRiceandHcompress. Integer images ignore it. - Rice
RICE_1tile compression — lossless foru8/u16;f32is quantized first (seequantize).- U10
- A 10-bit sample stored right-aligned in a
u16(seePixelType::U10). - U12
- A 12-bit sample stored right-aligned in a
u16(seePixelType::U12); seeU10for the rationale. The only difference is the saturating range,0..=4095. - U14
- A 14-bit sample stored right-aligned in a
u16(seePixelType::U14); seeU10for the rationale. The only difference is the saturating range,0..=16383.
Enums§
- Bayer
Error - Error codes for the Bayer demosaicing.
- Bayer
Pattern - Enum to describe the Bayer pattern of the image.
- Color
Space - Description of the color space of the image.
- Demosaic
Method - The demosaicing algorithm to use to fill in the missing color channels.
- Dither
Seed - Choice of subtractive-dither seed (
ZDITHER0). - Dynamic
Image image - A Dynamic Image
- Dynamic
Image Owned - Image data with a dynamic pixel type, backed by owned data.
- Dynamic
Image Ref - Image data with a dynamic pixel type, backed by a mutable slice of data.
- Dynamic
Image View - A type-erased, read-only view over an image’s samples — the immutable
analogue of
DynamicImageRef, borrowed from either that or aDynamicImageOwned. - Exposure
Error - Errors from configuring or running the optimum-exposure calculator.
- Fits
Compression Kind - The algorithm behind a
FitsCompression, without its settings — handy for branching on what a value selects. - Fits
Error - Errors produced while writing a FITS file.
- Generic
Image - A serializable, generic image with metadata, backed by either
a
GenericImageRefor aGenericImageOwned. - Generic
Value - A type-erased enum to hold a metadata value.
- Image
Error - Errors from constructing an image or reinterpreting its backing bytes.
- Interop
Error image - Errors from converting between
image::DynamicImageand this crate’s types. - Metadata
Error - Errors from inserting, replacing, removing, or reading metadata.
- Pixel
Type - The primitive element type of an image’s samples.
- Serde
Error - Errors from (de)serializing a
DynamicImageOwned/DynamicImageRefto or from the crate’s internal wire format.
Constants§
- CAMERANAME_
KEY - Key for the camera name metadata.
- EXPOSURE_
KEY - Name of the exposure field, reserved because
Metadatastores it as a typed field rather than a map entry. - FRAMEID_
KEY - Name of the frame-ID field, reserved because
Metadatastores it as a typed field rather than a map entry. - PROGRAMNAME_
KEY - Key for the name of the program that generated this object.
- TIMESTAMP_
KEY - Name of the timestamp field, reserved because
Metadatastores it as a typed field rather than a map entry.
Traits§
- Bayer
Shift - A trait for shifting Bayer patterns.
- Calc
OptExp - Trait to calculate the optimum exposure time and binning.
- Deserialize
- A data structure that can be deserialized from any data format supported by Serde.
- Enlargeable
- An
Enlargable::Largervalue should be enough to calculate the sum (average) of a few hundred or thousand Enlargeable values. - Fits
Write - Write an image, with its metadata, to a FITS file.
- Image
Props - A trait for accessing the properties of an image.
- Insert
Value - Trait to insert a metadata value into a
MetaCollection. - Pixel
Data - Type-erased access to an image’s raw sample buffer.
- Pixel
Stor - The type of each channel in a pixel. For example, this can be
u8,u16,f32. - Serialize
- A data structure that can be serialized into any data format supported by Serde.
Functions§
- create_
fits - Create a FITS file on disk with an empty primary HDU, ready for
FitsWrite::append_fits.compressis the compression each appended image uses. - create_
fits_ to - Like
create_fits, but writes to anyWritesink instead of a path — e.g. aVec<u8>for an in-memory multi-HDU FITS file. The empty primary HDU is written immediately.
Type Aliases§
- Exposure
Result Resultalias forExposureError.- Fits
Result Resultalias forFitsError.- Image
Result Resultalias forImageError.- Interop
Result image Resultalias forInteropError.- Meta
Collection - A collection of metadata items, keyed by uppercase name and kept in insertion order.
- Metadata
Result Resultalias forMetadataError.- Serde
Result Resultalias forSerdeError.