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
use TokenStream;
use ;
/// Derive macro for `PlainPixel` trait.
///
/// # Requirements
/// - Struct must be `#[repr(C)]`
/// - Struct must have named fields
/// - All fields must be `PlainPixel` types themselves
///
/// # Example
/// ```ignore
/// #[derive(Clone, Copy, PlainPixel)]
/// #[repr(C)]
/// pub struct Rgb8 {
/// pub r: Saturating<u8>,
/// pub g: Saturating<u8>,
/// pub b: Saturating<u8>,
/// }
/// ```
///
/// # Generated Implementation
/// ```ignore
/// unsafe impl PlainPixel for Rgb8 {
/// const CHANNELS: &'static [usize] = &[1, 1, 1];
/// }
/// ```
/// Derive macro for `HomogeneousPixel` trait.
///
/// # Requirements
/// - Struct must be `#[repr(C)]` or `#[repr(transparent)]`
/// - For `#[repr(C)]`: all fields must have the **same type** (the channel type)
/// - For `#[repr(transparent)]`: exactly one field, treated as a single-channel pixel
/// - The struct must also implement `PlainPixel` (typically via `#[derive(PlainPixel)]`)
///
/// # Example
/// ```ignore
/// #[derive(Clone, Copy, PlainPixel, HomogeneousPixel)]
/// #[repr(C)]
/// pub struct Rgb8 {
/// pub r: Saturating<u8>,
/// pub g: Saturating<u8>,
/// pub b: Saturating<u8>,
/// }
/// ```
///
/// # Generated Implementation
/// ```ignore
/// unsafe impl HomogeneousPixel for Rgb8 {
/// type Channel = Saturating<u8>;
/// type Channels = [Saturating<u8>; 3];
/// }
/// ```
/// Derive macro for `ZeroablePixel` trait.
///
/// Generates `zero()` by calling `ZeroablePixel::zero()` on each field's type.
///
/// # Requirements
/// - Must be a struct (not enum or union)
/// - Must have at least one field
/// - By default, all field types must implement `ZeroablePixel`
///
/// # Per-Field Control with `#[zero(...)]`
///
/// You can override the zero-initialization strategy for individual fields:
///
/// - **No attribute** (default): uses `<T as ZeroablePixel>::zero()`
/// - `#[zero(default)]`: uses `<T as Default>::default()`
/// - `#[zero(<expr>)]`: uses the given expression verbatim
///
/// # Example
/// ```ignore
/// #[derive(Clone, Copy, PlainPixel, ZeroablePixel)]
/// #[repr(C)]
/// pub struct Rgb8 {
/// pub r: Saturating<u8>,
/// pub g: Saturating<u8>,
/// pub b: Saturating<u8>,
/// }
/// ```
///
/// # Example with `#[zero(...)]` attributes
/// ```ignore
/// #[derive(Clone, Copy, ZeroablePixel)]
/// #[repr(C)]
/// pub struct PixelWithMeta {
/// pub r: u8,
/// pub g: u8,
/// pub b: u8,
/// #[zero(default)]
/// pub metadata: SomeDefaultType,
/// #[zero(MyTag::new(42))]
/// pub tag: MyTag,
/// }
/// ```
///
/// # Generated Implementation
/// ```ignore
/// impl ZeroablePixel for Rgb8 {
/// fn zero() -> Self {
/// Rgb8 {
/// r: <Saturating<u8> as ZeroablePixel>::zero(),
/// g: <Saturating<u8> as ZeroablePixel>::zero(),
/// b: <Saturating<u8> as ZeroablePixel>::zero(),
/// }
/// }
/// }
/// ```
/// Derive macro for `LinearPixel` trait.
///
/// Generates channel-wise `Add`, `LinearPixel`, and optionally `FromLinear` impls,
/// plus the `LinearSpace` marker trait.
///
/// # Requirements
/// - Must be a struct with at least one field (named or tuple)
/// - Must have `#[linear(accumulator = AccType)]` attribute
///
/// # Attribute
/// - `accumulator = Type`: the intermediate-precision accumulator type.
/// - For storage pixels (e.g. `Rgb8`), use the float pixel (e.g. `RgbF32`).
/// - For float/accumulator pixels (e.g. `RgbF32`), use `Self`.
/// - When `Self` is used, no `FromLinear` impl is generated (blanket identity covers it).
///
/// # Example (storage pixel → float accumulator)
/// ```ignore
/// #[derive(Clone, Copy, LinearPixel)]
/// #[repr(C)]
/// #[linear(accumulator = RgbF32)]
/// pub struct Rgb8 {
/// pub r: Saturating<u8>,
/// pub g: Saturating<u8>,
/// pub b: Saturating<u8>,
/// }
/// ```
///
/// # Example (self-accumulating float pixel)
/// ```ignore
/// #[derive(Clone, Copy, LinearPixel)]
/// #[repr(C)]
/// #[linear(accumulator = Self)]
/// pub struct RgbF32 {
/// pub r: f32,
/// pub g: f32,
/// pub b: f32,
/// }
/// ```
///
/// # Generated impls
/// - `impl Add for T` — channel-wise addition
/// - `impl LinearPixel for T` — delegates `scale()` to each field's `LinearPixel::scale`
/// - `impl FromLinear<Acc> for T` — delegates per-field `FromLinear` conversion (only when accumulator ≠ `Self`)
/// - `impl LinearSpace for T` — marker trait asserting values are in a linear space
/// Derive macro for `WhiteChannel` trait.
///
/// Emits an impl delegating to the channel type's `BoundedChannel::MAX`.
/// This is the correct answer for every homogeneous pixel in the library
/// whose channel type is `BoundedChannel`.
///
/// **Reduced-range pixels (`Mono<BITS>`) must not use this derive.**
/// They implement `WhiteChannel` manually, returning the pixel's own
/// invariant-respecting maximum (e.g. `Saturating(1023)` for `Mono<10>`)
/// rather than the channel type's storage maximum
/// (`<Saturating<u16> as BoundedChannel>::MAX == Saturating(65535)`).
///
/// # Requirements
/// - Must be a struct (named or tuple).
/// - The struct must also implement `HomogeneousPixel` (typically via
/// `#[derive(HomogeneousPixel)]`).
/// - The struct's channel type (`<Self as HomogeneousPixel>::Channel`)
/// must implement `BoundedChannel`.
///
/// # Example
/// ```ignore
/// #[derive(Clone, Copy, PlainPixel, HomogeneousPixel, WhiteChannel)]
/// #[repr(C)]
/// pub struct Rgb8 {
/// pub r: Saturating<u8>,
/// pub g: Saturating<u8>,
/// pub b: Saturating<u8>,
/// }
/// ```