1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// #![deny(exported_private_dependencies)]
//! 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](https://fits.gsfc.nasa.gov/fits_primer.html) 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
//! [`Op`](pipeline::Op)s on a declarative, reusable
//! [`Pipeline`](pipeline::Pipeline). [`apply`](pipeline::Pipeline::apply)
//! runs it once and returns an owned image; given a [`GenericImageRef`] it returns a
//! [`GenericImageOwned`] with the metadata carried across unchanged.
//! [`compile`](pipeline::Pipeline::compile)-ing against a concrete
//! [`ImageSpec`](pipeline::ImageSpec) pre-allocates every buffer, yielding a
//! [`Runner`](pipeline::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](https://fits.gsfc.nasa.gov/fits_standard.html) 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 the [`pipeline`], and enables its parallel [`Strategy`](pipeline::Strategy) variants (<b>enabled</b> by default).
//! - `grow`: Lets a compiled [`Runner`](pipeline::Runner) reallocate its buffers when handed a frame whose shape differs from the one it was compiled for (<b>enabled</b> by default).
//! - `image`: Enables [`TryFrom`] conversions between [`DynamicImage`] and [`DynamicImageRef`], [`DynamicImageOwned`] (<b>disabled</b> by default).
//!
/// Re-export of the [`chrono`](https://docs.rs/chrono) crate. Image timestamps are
/// [`chrono::DateTime<Utc>`](chrono::DateTime); the caller supplies them (typically
/// `refimage::chrono::Utc::now()` — which needs `chrono`'s `clock` feature, so add
/// `chrono` as a direct dependency of your binary if you rely on `now()`).
pub use chrono;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use GenericImage;
pub use GenericImageOwned;
pub use GenericImageRef;
pub use DynamicImage; // Used for image interop
pub use ImageOwned;
pub use ImageRef;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
use ;
/// Image data with a dynamic pixel type, backed by a mutable slice of data.
///
/// This represents a _matrix_ of _pixels_ whose element type is one of `u8`,
/// `u16`, or `f32` (a `u16` buffer may additionally be tagged 10-/12-/14-bit —
/// see [`ImageRef::with_bit_depth`]). The matrix is stored in _row-major_ order
/// in a single contiguous buffer, backed by a mutable slice, and aims to enable
/// reuse of allocated memory without re-allocation.
///
/// Raw sample access is via the [`PixelData`] trait; a shared read-only borrow
/// is a [`DynamicImageView`] ([`view`](DynamicImageRef::view)).
///
/// # Note
/// - Does not support alpha channel natively.
/// - Internally [`DynamicImageRef`] and [`DynamicImageOwned`] serialize to the same
/// representation, and [`DynamicImageRef`] can be deserialized into [`DynamicImageOwned`] only.
///
/// # Usage
///
/// ```
/// use refimage::{ImageRef, ColorSpace, DynamicImageRef};
///
/// let mut data = vec![1u8, 2, 3, 4, 5, 6];
/// let img = ImageRef::new(&mut data, 3, 2, ColorSpace::Gray).unwrap();
/// let img = DynamicImageRef::from(img);
///
/// ```
///
/// This type acts as a type-erased version of `ImageRef` and can be used to store
/// image data with different pixel types. The pixel type is determined at runtime.
/// Image data with a dynamic pixel type, backed by owned data.
///
/// This represents a _matrix_ of _pixels_ whose element type is one of `u8`,
/// `u16`, or `f32` (a `u16` buffer may additionally be tagged 10-/12-/14-bit —
/// see [`ImageOwned::with_bit_depth`]). The matrix is stored in _row-major_
/// order in a single contiguous buffer, backed by a vector.
///
/// Raw sample access is via the [`PixelData`] trait; a shared read-only borrow
/// is a [`DynamicImageView`] ([`view`](DynamicImageOwned::view)).
///
/// # Note
/// - Does not support alpha channel natively.
/// - [`DynamicImageRef`] implements [`Serialize`] and [`Deserialize`] traits, and can be
/// deserialized from a [`DynamicImageRef`].
///
/// # Usage
///
/// ```
/// use refimage::{ImageOwned, ColorSpace, DynamicImageOwned};
///
/// let data = vec![1u8, 2, 3, 4, 5, 6];
/// let img = ImageOwned::from_owned(data, 3, 2, ColorSpace::Gray).unwrap();
/// let img = DynamicImageOwned::from(img);
///
/// ```
///
/// This type acts as a type-erased version of `ImageRef` and can be used to store
/// image data with different pixel types. The pixel type is determined at runtime.
/// Description of the color space of the image.
///
/// The colorspace information is used to enable debayering of the image data, and
/// for interpretation of single or multi-channel images.
/// Enum to describe the Bayer pattern of the image.
///
/// The Bayer pattern is used to interpret the raw image data from a Bayer mosaic image.
/// The primitive element type of an image's samples.
///
/// Only the six variants here can be stored in a [`DynamicImageRef`] /
/// [`DynamicImageOwned`] or processed by a [`pipeline`]: the storage widths
/// [`U8`](Self::U8), [`U16`](Self::U16) and [`F32`](Self::F32), plus the three
/// sub-container machine-vision depths [`U10`](Self::U10) / [`U12`](Self::U12) /
/// [`U14`](Self::U14) that live right-aligned in a `u16` (see
/// [`ImageRef::with_bit_depth`]). The `#[repr(i8)]` discriminants follow the FITS
/// `BITPIX` convention (`U8` = 8, `U16` = 16, `F32` = -32); `U10` / `U12` / `U14`
/// are a `refimage` extension that serializes as its 16-bit [`storage`](Self::storage).
// Can't use the macro-call itself within the `doc` attribute. So force it to eval it as part of
// the macro invocation.
//
// The inspiration for the macro and implementation is from
// <https://github.com/GuillaumeGomez/doc-comment>
//
// MIT License
//
// Copyright (c) 2018 Guillaume Gomez
=>
}
// Provides the README.md as doc, to ensure the example works!
insert_as_doc!;