Skip to content

Commit be395b6

Browse files
Improve documentation (#95)
1 parent ba63194 commit be395b6

File tree

16 files changed

+178
-47
lines changed

16 files changed

+178
-47
lines changed

src/color/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ impl Precision {
6767
/// A color format with a specific number of channels and precision.
6868
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6969
pub struct ColorFormat {
70+
/// The number and semantics of the channels.
7071
pub channels: Channels,
72+
/// The precision/bit depth of the values in all channels.
7173
pub precision: Precision,
7274
}
7375
impl ColorFormat {
76+
/// Creates a new `ColorFormat` with the given channels and precision.
7477
pub const fn new(channels: Channels, precision: Precision) -> Self {
7578
Self {
7679
channels,

src/decode/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub(crate) const fn get_decoders(format: Format) -> DecoderSet {
5959
Format::Y410 => Y410,
6060
Format::Y416 => Y416,
6161

62-
// sub-sampled formats
62+
// subsampled formats
6363
Format::R1_UNORM => R1_UNORM,
6464
Format::R8G8_B8G8_UNORM => R8G8_B8G8_UNORM,
6565
Format::G8R8_G8B8_UNORM => G8R8_G8B8_UNORM,
@@ -223,6 +223,13 @@ pub fn decode_rect<R: Read + Seek>(
223223
}
224224
}
225225

226+
/// Options for decoding images.
227+
///
228+
/// ## See also
229+
///
230+
/// - [`decode`]
231+
/// - [`decode_rect`]
232+
/// - [`Decoder::options`](crate::Decoder::options)
226233
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
227234
#[non_exhaustive]
228235
pub struct DecodeOptions {

src/decode/read_write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ pub(crate) fn process_bi_planar_helper<
11241124
pub(crate) struct BiPlaneInfo {
11251125
pub plane1_element_size: u8,
11261126
pub plane2_element_size: u8,
1127-
/// The sub-sampling of plane2.
1127+
/// The subsampling of plane2.
11281128
pub sub_sampling: (u8, u8),
11291129
}
11301130
pub(crate) fn for_each_bi_planar(

src/decoder.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ pub struct Decoder<R> {
1919
layout: DataLayout,
2020

2121
iter: SurfaceIterator,
22+
/// The options used for decoding.
23+
///
24+
/// Default: [`DecodeOptions::default()`]
2225
pub options: DecodeOptions,
2326
}
2427
impl<R> Decoder<R> {

src/detect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(crate) const fn dxgi_format_to_supported(dxgi_format: DxgiFormat) -> Option<
6969
DxgiFormat::Y410 => Some(Format::Y410),
7070
DxgiFormat::Y416 => Some(Format::Y416),
7171

72-
// sub-sampled formats
72+
// subsampled formats
7373
DxgiFormat::R8G8_B8G8_UNORM => Some(Format::R8G8_B8G8_UNORM),
7474
DxgiFormat::G8R8_G8B8_UNORM => Some(Format::G8R8_G8B8_UNORM),
7575
DxgiFormat::YUY2 => Some(Format::YUY2),

src/encode/mod.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub(crate) const fn get_encoders(format: Format) -> Option<EncoderSet> {
6161
Format::Y410 => Y410,
6262
Format::Y416 => Y416,
6363

64-
// sub-sampled formats
64+
// subsampled formats
6565
Format::R1_UNORM => R1_UNORM,
6666
Format::R8G8_B8G8_UNORM => R8G8_B8G8_UNORM,
6767
Format::G8R8_G8B8_UNORM => G8R8_G8B8_UNORM,
@@ -222,6 +222,12 @@ fn encode_parallel(
222222
progress.checked_report(1.0)
223223
}
224224

225+
/// Options for encoding images.
226+
///
227+
/// ## See also
228+
///
229+
/// - [`encode`]
230+
/// - [`Encoder::encoding`](crate::Encoder::encoding)
225231
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
226232
#[non_exhaustive]
227233
pub struct EncodeOptions {
@@ -240,6 +246,9 @@ pub struct EncodeOptions {
240246
pub dithering: Dithering,
241247
/// The error metric for block compression formats.
242248
///
249+
/// This is currently only supported for BC1-3. All other formats will
250+
/// ignore this option and assume [`ErrorMetric::Uniform`].
251+
///
243252
/// Default: [`ErrorMetric::Uniform`]
244253
pub error_metric: ErrorMetric,
245254
/// The compression quality.
@@ -276,6 +285,8 @@ impl Default for EncodeOptions {
276285
}
277286
}
278287

288+
/// Specifies whether dithering is enabled/supported for color and/or alpha
289+
/// channels.
279290
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
280291
pub enum Dithering {
281292
/// Dithering is disabled for all channels.
@@ -289,6 +300,7 @@ pub enum Dithering {
289300
Alpha = 0b10,
290301
}
291302
impl Dithering {
303+
/// Creates a new `Dithering` given the channels it is enabled for.
292304
pub const fn new(color: bool, alpha: bool) -> Self {
293305
match (color, alpha) {
294306
(true, true) => Dithering::ColorAndAlpha,
@@ -298,9 +310,11 @@ impl Dithering {
298310
}
299311
}
300312

313+
/// Whether dithering is enabled for color channels.
301314
pub const fn color(self) -> bool {
302315
matches!(self, Dithering::ColorAndAlpha | Dithering::Color)
303316
}
317+
/// Whether dithering is enabled for the alpha channel.
304318
pub const fn alpha(self) -> bool {
305319
matches!(self, Dithering::ColorAndAlpha | Dithering::Alpha)
306320
}
@@ -318,10 +332,20 @@ impl Dithering {
318332
}
319333
}
320334

335+
/// The error metric encoders use to optimize compressed formats.
336+
///
337+
/// Not all formats support all error metrics. See
338+
/// [`EncodeOptions::error_metric`] for more details.
321339
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
322340
pub enum ErrorMetric {
341+
/// The error of all channels is weighted equally.
323342
#[default]
324343
Uniform,
344+
/// Color is assumed to be sRGB, and the error is calculated in a
345+
/// perceptually uniform color space.
346+
///
347+
/// [Oklab](https://en.wikipedia.org/wiki/Oklab_color_space) is currently
348+
/// used as the perceptually uniform color space.
325349
Perceptual,
326350
}
327351

src/encoder.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ pub struct Encoder<W> {
2121

2222
/// The encoding options used to encode surfaces.
2323
///
24-
/// Defaults: `EncodeOptions::default()`
24+
/// Default: `EncodeOptions::default()`
2525
pub encoding: EncodeOptions,
2626
/// Options regarding automatic mipmap generation.
2727
///
2828
/// Set `self.mipmaps.generate = true` to enable automatic mipmap generation.
2929
///
30-
/// Defaults: `MipmapOptions::default()`
30+
/// Default: `MipmapOptions::default()`
3131
pub mipmaps: MipmapOptions,
3232

3333
// internal cache for resizing
@@ -283,16 +283,30 @@ impl<W> Encoder<W> {
283283
}
284284
}
285285

286+
/// The filter to use when resizing images for mipmap generation.
287+
///
288+
/// ## See also
289+
///
290+
/// - [`MipmapOptions::resize_filter`]
286291
#[derive(Debug, Clone, Copy, Default)]
287292
pub enum ResizeFilter {
293+
/// Nearest neighbor interpolation (=point filtering).
288294
Nearest,
295+
/// Box (also called area or binning).
296+
///
297+
/// This is the default filter, because it produces mipmaps that are
298+
/// generally free of artifacts and sharp (without being over sharpened).
289299
#[default]
290300
Box,
301+
/// Triangle filtering (=linear interpolation).
291302
Triangle,
303+
/// Mitchell interpolation.
292304
Mitchell,
305+
/// Lanczos interpolation with a radius of 3.
293306
Lanczos3,
294307
}
295308

309+
/// Options for automatic mipmap generation in [`Encoder`].
296310
#[derive(Debug, Clone, Copy)]
297311
pub struct MipmapOptions {
298312
/// Whether to generate mipmaps for the texture.
@@ -301,23 +315,25 @@ pub struct MipmapOptions {
301315
/// generate all mipmaps until the next level 0 object or EOF.
302316
///
303317
/// Note: Generating mipmaps for volume depth slices is not supported. This
304-
/// will **NOT** result in an error and instead the encoder will silently
305-
/// ignore the option.
318+
/// will **NOT** result in an error. Instead, the encoder will silently
319+
/// ignore the option and assume mipmap generation is disabled.
306320
///
307321
/// Default: `false`
308322
pub generate: bool,
309-
/// Whether the alpha channel (if any) is straight alpha.
323+
/// Whether the alpha channel (if any) is straight alpha transparency.
310324
///
311325
/// This is important when generating mipmaps. Resizing RGBA with straight
312326
/// alpha requires that the alpha channel is premultiplied into the color
313-
/// channels before resizing and then unpremultiplied after resizing. This
327+
/// channels before resizing and then un-premultiplied after resizing. This
314328
/// is necessary to avoid color bleeding.
315329
///
316-
/// If the alpha channel is premultiplied alpha or custom (e.g. like in
317-
/// channel-packed textures), this option should be set to `false`.
330+
/// If the alpha channel is premultiplied alpha transparency or custom
331+
/// (e.g. like in channel-packed textures), this option should be set to
332+
/// `false`.
318333
///
319334
/// If this option is set to `false`, all channels will be resized
320-
/// independently of each other.
335+
/// independently of each other. If set to `true`, the alpha channel will
336+
/// be interpreted as straight alpha transparency and handled accordingly.
321337
///
322338
/// Default: `true`
323339
pub resize_straight_alpha: bool,

src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ pub enum EncodingError {
272272
/// Returned by [`Encoder`](crate::Encoder) and [`encode()`](crate::encode()) when the format
273273
/// does not support encoding.
274274
UnsupportedFormat(Format),
275+
/// Returned when the format requires the image size to be a multiple of
276+
/// certain dimensions, but the provided image does not satisfy this.
277+
///
278+
/// The two `NonZeroU32` values specify the required width and height
279+
/// multiples respectively.
275280
InvalidSize(NonZeroU32, NonZeroU32),
276281

277282
/// Returned by [`Encoder`](crate::Encoder) when the user tries to write a surface

src/format.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub enum Format {
4949
Y410,
5050
Y416,
5151

52-
// sub-sampled formats
52+
// subsampled formats
5353
R1_UNORM,
5454
R8G8_B8G8_UNORM,
5555
G8R8_G8B8_UNORM,
@@ -147,13 +147,13 @@ impl Format {
147147
}
148148
/// Returns the format of a surface from a DXGI format.
149149
///
150-
/// `None` if the DXGI format is not supported for decoding.
150+
/// `None` if the DXGI format is not supported for decoding/encoding.
151151
pub const fn from_dxgi(dxgi: DxgiFormat) -> Option<Format> {
152152
detect::dxgi_format_to_supported(dxgi)
153153
}
154154
/// Returns the format of a surface from a FourCC code.
155155
///
156-
/// `None` if the FourCC code is not supported for decoding.
156+
/// `None` if the FourCC code is not supported for decoding/encoding.
157157
pub const fn from_four_cc(four_cc: FourCC) -> Option<Format> {
158158
detect::four_cc_to_supported(four_cc)
159159
}
@@ -240,7 +240,7 @@ impl TryFrom<Format> for DxgiFormat {
240240
Format::Y410 => DxgiFormat::Y410,
241241
Format::Y416 => DxgiFormat::Y416,
242242

243-
// sub-sampled
243+
// subsampled
244244
Format::R1_UNORM => DxgiFormat::R1_UNORM,
245245
Format::R8G8_B8G8_UNORM => DxgiFormat::R8G8_B8G8_UNORM,
246246
Format::G8R8_G8B8_UNORM => DxgiFormat::G8R8_G8B8_UNORM,

src/header.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//!
99
//! [`RawHeader`] is a low-level representation of an unparsed DDS header. It is
1010
//! bit-for-bit what is on disk. You rarely need to interact with this type, but
11-
//! it can useful for manually detecting and parsing non-standard DDS files.
11+
//! it can be useful for manually detecting and parsing non-standard DDS files.
1212
//!
1313
//! # Creating a header
1414
//!
@@ -420,7 +420,7 @@ pub struct ParseOptions {
420420
/// DDS files typically start with the magic bytes `"DDS "`. By default, the
421421
/// decoder will check for these bytes and error if they are not present.
422422
///
423-
/// If this is set to `true`, the decoder assume that the magic bytes are
423+
/// If this is set to `true`, the decoder assumes that the magic bytes are
424424
/// not present and immediately start reading the header. This can be used
425425
/// to read DDS files without magic bytes.
426426
///
@@ -1871,7 +1871,7 @@ impl TryFrom<u32> for DxgiFormat {
18711871
type Error = u32;
18721872

18731873
fn try_from(value: u32) -> Result<Self, Self::Error> {
1874-
// NOTE: This implementation is NOT generated by the marco for
1874+
// NOTE: This implementation is NOT generated by the macro for
18751875
// performance and code size reasons. On virtually any optimization
18761876
// level, the below code translates to around 6 instructions, while a
18771877
// generated match arm (0 | 1 | 2 | ... | 115 | 130 | 131 | 132 => ...)

0 commit comments

Comments
 (0)