### Full Resize Configuration Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md A comprehensive Rust example demonstrating image container creation, resize options configuration (algorithm, cropping, alpha usage), CPU extension selection, and the resizing process. ```rust use fast_image_resize::*; use fast_image_resize::pixels::PixelType; // 1. Create image containers let src = Image::new(1920, 1080, PixelType::U8x3); let mut dst = Image::new(800, 600, PixelType::U8x3); // 2. Configure resize options let options = ResizeOptions::new() .resize_alg(ResizeAlg::Convolution(FilterType::Lanczos3)) .crop(100.0, 100.0, 800.0, 800.0) .use_alpha(false); // 3. Create resizer with CPU extensions let mut resizer = Resizer::new(); #[cfg(target_arch = "x86_64")] unsafe { if CpuExtensions::Avx2.is_supported() { resizer.set_cpu_extensions(CpuExtensions::Avx2); } } // 4. Resize resizer.resize(&src, &mut dst, &options)?; // 5. Optional cleanup resizer.reset_internal_buffers(); Ok::<(), ResizeError>(()) ``` -------------------------------- ### Install Arm64 target Source: https://github.com/cykooz/fast_image_resize/blob/main/dev.md Installs the Arm64 target for Rust. ```shell rustup target add aarch64-unknown-linux-gnu ``` -------------------------------- ### image feature integration example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md Example of using the image feature to integrate with the image crate. ```rust use image::ImageReader; use fast_image_resize::{Resizer, Image}; let img = ImageReader::open("input.png")?.decode()?; let mut resized = Image::new(800, 600, img.pixel_type()?); let mut resizer = Resizer::new(); resizer.resize(&img, &mut resized, None)?; ``` -------------------------------- ### bytemuck feature example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md Example of using the bytemuck feature with pixel types. ```rust use fast_image_resize::pixels::U8x4; use bytemuck::Pod; // Can now use bytemuck functions with pixel types let data: &[U8x4] = bytemuck::cast_slice(&u32_data); ``` -------------------------------- ### Memory Estimation Examples Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Examples demonstrating how to calculate buffer size based on image dimensions and pixel type. ```text Buffer Size = width × height × pixel_type.size() Examples: - 1920×1080 RGB (U8x3) = 1920 × 1080 × 3 = 6,220,800 bytes ≈ 5.9 MB - 800×600 RGBA (U8x4) = 800 × 600 × 4 = 1,920,000 bytes ≈ 1.8 MB ``` -------------------------------- ### rayon feature usage example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md Example of configuring and using the rayon thread pool for resizing. ```rust use rayon::ThreadPoolBuilder; // Configure thread count let pool = ThreadPoolBuilder::new() .num_threads(4) .build() .unwrap(); pool.install(|| { // Resize operations use configured thread pool resizer.resize(&src, &mut dst, None).unwrap(); }); ``` -------------------------------- ### Resizer::size_of_internal_buffers Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/resizer.md Demonstrates how to get the size of the Resizer's internal buffers. ```rust let resizer = Resizer::new(); let buffer_size = resizer.size_of_internal_buffers(); println!("Internal buffers: {} bytes", buffer_size); ``` -------------------------------- ### ResizeOptions algorithm configuration Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md Examples of configuring different resize algorithms using ResizeOptions. ```rust let options = ResizeOptions::new() .resize_alg(ResizeAlg::Nearest); let options = ResizeOptions::new() .resize_alg(ResizeAlg::Convolution(FilterType::Bilinear)); let options = ResizeOptions::new() .resize_alg(ResizeAlg::SuperSampling(FilterType::Lanczos3, 2)); ``` -------------------------------- ### ResizeOptions::resize_alg Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/resizer.md Example of setting the resize algorithm using the builder pattern. ```rust let options = ResizeOptions::new() .resize_alg(ResizeAlg::Convolution(FilterType::Bilinear)); ``` -------------------------------- ### create_gamma_22_mapper Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/pixel-component-mapper.md Example of creating and using the convenience function for Gamma 2.2 mapper. ```rust use fast_image_resize::create_gamma_22_mapper; let mapper = create_gamma_22_mapper(); mapper.forward_map(&src, &mut dst)?; // Apply gamma correction ``` -------------------------------- ### ResizeOptions::new Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/resizer.md Example of creating new ResizeOptions with default values. ```rust use fast_image_resize::ResizeOptions; let options = ResizeOptions::new(); ``` -------------------------------- ### PixelComponentMapper::new Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/pixel-component-mapper.md Example demonstrating the creation and usage of a PixelComponentMapper for gamma correction. ```rust use fast_image_resize::PixelComponentMapper; fn gamma_to_linear(x: f32) -> f32 { x.powf(2.2) } fn linear_to_gamma(x: f32) -> f32 { x.powf(1.0 / 2.2) } let mapper = PixelComponentMapper::new(gamma_to_linear, linear_to_gamma); ``` -------------------------------- ### Resizer::new Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/resizer.md Demonstrates creating a new Resizer instance with default settings. ```rust use fast_image_resize::Resizer; let mut resizer = Resizer::new(); ``` -------------------------------- ### MulDiv::new Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/mul-div.md Creates a new MulDiv instance with default CPU extensions. ```rust use fast_image_resize::MulDiv; let mul_div = MulDiv::new(); ``` -------------------------------- ### Install Wasm32 target Source: https://github.com/cykooz/fast_image_resize/blob/main/dev.md Installs the Wasm32 target for Rust and suggests installing Wasmtime. ```shell rustup target add wasm32-wasip2 ``` -------------------------------- ### sRGB Conversion Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/pixel-component-mapper.md Example demonstrating how to use the sRGB mapper for forward and backward mapping during image resizing. ```rust use fast_image_resize::create_srgb_mapper; let srgb_mapper = create_srgb_mapper(); // Prepare: Convert from sRGB to linear srgb_mapper.forward_map(&srgb_image, &mut linear_image)?; // Resize linear image resizer.resize(&linear_image, &mut resized_linear, None)?; // Finalize: Convert back to sRGB srgb_mapper.backward_map(&resized_linear, &mut result_srgb)?; ``` -------------------------------- ### Resizer::cpu_extensions Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/resizer.md Demonstrates retrieving the currently used CPU SIMD extensions. ```rust let resizer = Resizer::new(); let ext = resizer.cpu_extensions(); println!("Using extension: {:?}", ext); ``` -------------------------------- ### Image::buffer_mut Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Gets a mutable reference to the pixel buffer. ```rust let data = image.buffer_mut(); data.fill(0); // Clear image to black ``` -------------------------------- ### Crop Options Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md Examples of configuring explicit cropping and auto-cropping to aspect ratio using ResizeOptions. ```rust let options = ResizeOptions::new() .crop(100.0, 100.0, 800.0, 600.0); let options = ResizeOptions::new() .fit_into_destination(None); let options = ResizeOptions::new() .fit_into_destination(Some((0.0, 0.0))); ``` -------------------------------- ### CPU Extensions - Verification Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md Example of checking if a specific CPU extension (Avx2) is supported and then setting it. ```rust if CpuExtensions::Avx2.is_supported() { unsafe { resizer.set_cpu_extensions(CpuExtensions::Avx2); } } ``` -------------------------------- ### PixelComponentMapper::forward_map_inplace Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/pixel-component-mapper.md Example of using forward_map_inplace to transform an image's pixels directly. ```rust let mut image = Image::new(800, 600, PixelType::U8x3); // ... fill with sRGB data ... mapper.forward_map_inplace(&mut image)?; // Image now contains linear RGB ``` -------------------------------- ### Image::buffer Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Gets a read-only reference to the pixel buffer. ```rust let data = image.buffer(); println!("Image data size: {} bytes", data.len()); ``` -------------------------------- ### Resizer::resize Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/resizer.md Shows how to resize a source image into a destination buffer using Resizer::resize, with both default and custom options. ```rust use fast_image_resize::{Image, Resizer, ResizeOptions, FilterType, ResizeAlg}; use fast_image_resize::pixels::PixelType; let mut src = Image::new(1920, 1080, PixelType::U8x3); let mut dst = Image::new(800, 600, PixelType::U8x3); let mut resizer = Resizer::new(); // Basic resize with defaults (Lanczos3) resizer.resize(&src, &mut dst, None)?; // With custom options let options = ResizeOptions::new() .resize_alg(ResizeAlg::Convolution(FilterType::Bilinear)); resizer.resize(&src, &mut dst, &options)? ``` -------------------------------- ### PixelComponentMapper::forward_map Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/pixel-component-mapper.md Example of using forward_map to convert an sRGB image to a linear RGB image. ```rust use fast_image_resize::{PixelComponentMapper, Image}; use fast_image_resize::pixels::PixelType; let mapper = PixelComponentMapper::new( |x| x.powf(2.2), // sRGB to linear |x| x.powf(1.0/2.2) ); let src = Image::new(800, 600, PixelType::U8x3); // sRGB let mut dst = Image::new(800, 600, PixelType::U8x3); // Linear mapper.forward_map(&src, &mut dst)?; ``` -------------------------------- ### Alpha Premultiplication Options Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md Examples of enabling or disabling alpha premultiplication for transparency handling during resize. ```rust let options = ResizeOptions::new() .use_alpha(true); let options = ResizeOptions::new() .use_alpha(false); ``` -------------------------------- ### Image::copy Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Creates a deep copy of the image. ```rust let original = Image::new(100, 100, PixelType::U8x3); let clone = original.copy(); ``` -------------------------------- ### PixelComponentMapper::backward_map Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/pixel-component-mapper.md Example of using backward_map to convert a linear RGB image back to sRGB. ```rust // Convert linear RGB back to sRGB mapper.backward_map(&linear_image, &mut srgb_image)?; ``` -------------------------------- ### CreateFilterError Trigger Examples Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/errors.md Examples demonstrating how CreateFilterError can be triggered due to invalid support values. ```rust Filter::new("Custom", |x| x, 0.0)? // Error: support must be > 0 Filter::new("Custom", |x| x, f64::NAN)? // Error: support must be finite ``` -------------------------------- ### Cargo.toml Dependencies Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md Example of how to add fast_image_resize to your Cargo.toml with 'image' and 'rayon' features enabled. ```toml [dependencies] fast_image_resize = { version = "6.0", features = ["image", "rayon"] } ``` -------------------------------- ### Custom Mapping Function Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/pixel-component-mapper.md Example of creating a custom PixelComponentMapper using custom forward and backward mapping functions, specifically a filmlike tone mapping approximation. ```rust use fast_image_resize::PixelComponentMapper; // Custom tone mapping function fn tone_map_forward(x: f32) -> f32 { // Filmic tone mapping approximation (x * (6.2 * x + 0.5)) / (x * (6.2 * x + 1.7) + 0.06) } fn tone_map_backward(x: f32) -> f32 { // Inverse tone mapping (approximate) -0.08 / (x * 6.2 - 6.2) } let tone_mapper = PixelComponentMapper::new( tone_map_forward, tone_map_backward ); tone_mapper.forward_map(&hdr, &mut tone_mapped)?; ``` -------------------------------- ### Resizer::reset_internal_buffers Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/resizer.md Shows how to deallocate internal buffers to free memory using Resizer::reset_internal_buffers. ```rust let mut resizer = Resizer::new(); // ... perform resizing ... resizer.reset_internal_buffers(); // Free memory ``` -------------------------------- ### Image::new Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Creates a new image with an owned buffer initialized to zeros. ```rust use fast_image_resize::{Image, pixels::PixelType}; // Create 1920x1080 RGB image let mut image = Image::new(1920, 1080, PixelType::U8x3); ``` -------------------------------- ### ResizeOptions::crop Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/resizer.md Example of setting an explicit crop region for the source image. ```rust let options = ResizeOptions::new() .crop(100.0, 100.0, 800.0, 600.0); // Crop 800x600 from (100, 100) ``` -------------------------------- ### CPU Extensions - Manual Override Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md Example of manually forcing a specific CPU extension (SSE4.1) for resizing on x86_64 architectures. ```rust #[cfg(target_arch = "x86_64")] unsafe { let mut resizer = Resizer::new(); resizer.set_cpu_extensions(CpuExtensions::Sse4_1); } ``` -------------------------------- ### Resizer::resize_typed Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/resizer.md Illustrates using Resizer::resize_typed for resizing with compile-time pixel type checking. ```rust use fast_image_resize::{Image, Resizer, pixels::U8x4, ImageView, ImageViewMut}; let src = Image::new(1920, 1080, PixelType::U8x4); let mut dst = Image::new(800, 600, PixelType::U8x4); let mut resizer = Resizer::new(); // Get typed views if let (Some(src_view), Some(mut dst_view)) = ( src.typed_image::(), dst.typed_image_mut::() ) { resizer.resize_typed(&src_view, &mut dst_view, None)?; } ``` -------------------------------- ### Resizer::set_cpu_extensions Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/resizer.md Example of how to set specific CPU extensions for resizing operations, with a check for x86_64 architecture and SSE4.1 support. ```rust use fast_image_resize::{Resizer, CpuExtensions}; let mut resizer = Resizer::new(); #[cfg(target_arch = "x86_64")] unsafe { if CpuExtensions::Sse4_1.is_supported() { resizer.set_cpu_extensions(CpuExtensions::Sse4_1); } } ``` -------------------------------- ### Internal Buffer Management Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md Examples of checking the size of internal buffers, and resetting them to free memory. ```rust let mut resizer = Resizer::new(); let size = resizer.size_of_internal_buffers(); println!("Using {} bytes", size); resizer.reset_internal_buffers(); ``` -------------------------------- ### Error Debug Display Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/errors.md Demonstrates how to use the `Debug` and `Display` traits for error output. ```rust println!("{:?}", error); // Detailed debug output println!("{}", error); // User-friendly message via Display ``` -------------------------------- ### ResizeOptions::fit_into_destination Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/resizer.md Examples of enabling auto-crop to fit the destination aspect ratio, with and without specifying centering. ```rust // Center crop let options = ResizeOptions::new() .fit_into_destination(None); // Defaults to (0.5, 0.5) // Top-left crop let options = ResizeOptions::new() .fit_into_destination(Some((0.0, 0.0))); ``` -------------------------------- ### CPU Extensions - Automatic Detection Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md Example of creating a Resizer that automatically detects and uses the best available CPU extensions (SIMD). ```rust let resizer = Resizer::new(); ``` -------------------------------- ### Image::from_slice_u8 Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Creates an image by borrowing a mutable slice of pixel data. ```rust let mut buffer = vec![0u8; 1920 * 1080 * 3]; let mut image = Image::from_slice_u8( 1920, 1080, &mut buffer, PixelType::U8x3 )?; ``` -------------------------------- ### Usage Pattern 3: Read-Only Reference Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Example of resizing using a read-only reference as the source. ```rust let src_data: &[u8] = get_compressed_image(); let src = ImageRef::new( width, height, src_data, PixelType::U8x4 )?; resizer.resize(&src, &mut dst, None)?; ``` -------------------------------- ### Image::into_vec Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Consumes the image and extracts the buffer as a Vec. ```rust let image = Image::new(100, 100, PixelType::U8x4); let buffer: Vec = image.into_vec(); ``` -------------------------------- ### ResizeOptions::use_alpha Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/resizer.md Example of disabling alpha channel consideration for potentially faster resizing. ```rust // Ignore alpha channel (faster for non-RGBA images) let options = ResizeOptions::new() .use_alpha(false); ``` -------------------------------- ### Usage Pattern 2: Borrowed Buffer Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Example of resizing using a borrowed buffer for the destination. ```rust let mut buffer = vec![0u8; 640 * 480 * 3]; let mut image = Image::from_slice_u8( 640, 480, &mut buffer, PixelType::U8x3 )?; resizer.resize(&src, &mut image, None)?; ``` -------------------------------- ### Image::from_vec_u8 Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Creates an image from a vector buffer containing pixel data. ```rust let buffer = vec![0u8; 1920 * 1080 * 3]; // RGB buffer let image = Image::from_vec_u8(1920, 1080, buffer, PixelType::U8x3)?; ``` -------------------------------- ### Usage Pattern 1: Owned Buffer Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Example of resizing using an owned buffer for both source and destination. ```rust use fast_image_resize::{Resizer, Image, ResizeOptions}; use fast_image_resize::pixels::PixelType; let mut src = Image::new(1920, 1080, PixelType::U8x3); let mut dst = Image::new(800, 600, PixelType::U8x3); let mut resizer = Resizer::new(); resizer.resize(&src, &mut dst, None)?; ``` -------------------------------- ### Resize RGBA8 image Source: https://github.com/cykooz/fast_image_resize/blob/main/README.md Example of resizing an RGBA8 image using the fast_image_resize crate. It demonstrates reading a source image, creating a destination image buffer, performing the resize operation, and saving the result as a PNG file. ```rust use std::io::BufWriter; use image::codecs::png::PngEncoder; use image::{ExtendedColorType, ImageEncoder, ImageReader}; use fast_image_resize::{IntoImageView, Resizer}; use fast_image_resize::images::Image; fn main() { // Read source image from file let src_image = ImageReader::open("./data/nasa-4928x3279.png") .unwrap() .decode() .unwrap(); // Create container for data of destination image let dst_width = 1024; let dst_height = 768; let mut dst_image = Image::new( dst_width, dst_height, src_image.pixel_type().unwrap(), ); // Create Resizer instance and resize source image // into buffer of destination image let mut resizer = Resizer::new(); resizer.resize(&src_image, &mut dst_image, None).unwrap(); // Write destination image as PNG-file let mut result_buf = BufWriter::new(Vec::new()); PngEncoder::new(&mut result_buf) .write_image( dst_image.buffer(), dst_width, dst_height, src_image.color().into(), ) .unwrap(); } ``` -------------------------------- ### MulDiv::multiply_alpha_inplace Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/mul-div.md Multiplies RGB/Luma channels by alpha in-place, modifying the image buffer directly for memory efficiency. ```rust let mut image = Image::new(800, 600, PixelType::U8x4); // ... fill with image data ... mul_div.multiply_alpha_inplace(&mut image)?; ``` -------------------------------- ### Error Handling Strategy Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/INDEX.md Provides an example of robust error handling, including input validation and handling specific resize errors. ```rust // Immediately validate inputs if src.width() == 0 || dst.width() == 0 { return Err(ResizeError::...); } // Comprehensive error types with context match resizer.resize(&src, &mut dst, None) { Ok(()) => { /* success */ } Err(ResizeError::PixelTypesAreDifferent) => { /* handle */ } Err(ResizeError::SrcCroppingError(crop_err)) => { /* handle */ } Err(ResizeError::ImageError(img_err)) => { /* handle */ } } ``` -------------------------------- ### MulDiv::multiply_alpha Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/mul-div.md Multiplies RGB/Luma channels by alpha channel, storing the result in the destination. Supports various pixel types and returns an error for unsupported types, mismatched dimensions, or different pixel types. ```rust use fast_image_resize::{MulDiv, Image}; use fast_image_resize::pixels::PixelType; let src = Image::new(800, 600, PixelType::U8x4); let mut dst = Image::new(800, 600, PixelType::U8x4); let mul_div = MulDiv::new(); mul_div.multiply_alpha(&src, &mut dst)?; ``` -------------------------------- ### Typical Workflow: Correct Alpha Handling Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/mul-div.md Demonstrates the standard workflow for correctly resizing semi-transparent images using premultiplied alpha. ```rust use fast_image_resize::{Resizer, MulDiv, Image, ResizeOptions}; use fast_image_resize::pixels::PixelType; let mut src = Image::new(1920, 1080, PixelType::U8x4); let mut dst = Image::new(800, 600, PixelType::U8x4); let mut temp = Image::new(1920, 1080, PixelType::U8x4); let mul_div = MulDiv::new(); let mut resizer = Resizer::new(); // Step 1: Multiply RGB by alpha mul_div.multiply_alpha(&src, &mut temp)?; // Step 2: Resize the premultiplied image resizer.resize(&temp, &mut dst, None)?; // Step 3: Divide back to unmultiply mul_div.divide_alpha(&dst, &mut dst)?; // Result is correct semi-transparent image ``` -------------------------------- ### Choose Resize Algorithm Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/INDEX.md Demonstrates how to select different resizing algorithms based on quality and performance requirements. ```rust use fast_image_resize::{ResizeOptions, ResizeAlg, FilterType}; // High quality (default) let opts = ResizeOptions::new() .resize_alg(ResizeAlg::Convolution(FilterType::Lanczos3)); // Fast and good let opts = ResizeOptions::new() .resize_alg(ResizeAlg::Convolution(FilterType::Bilinear)); // Fastest let opts = ResizeOptions::new() .resize_alg(ResizeAlg::Nearest); // Extreme quality for downscaling let opts = ResizeOptions::new() .resize_alg(ResizeAlg::SuperSampling(FilterType::Lanczos3, 2)); ``` -------------------------------- ### ResizeError::SrcCroppingError Recovery Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/errors.md Example of recovering from a SrcCroppingError. ```rust match result { Err(ResizeError::SrcCroppingError(CropBoxError::PositionIsOutOfImageBoundaries)) => { // Clamp crop position to image bounds } // ... handle other crop errors } ``` -------------------------------- ### ImageBufferError::InvalidBufferSize Trigger Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/errors.md Example triggering ImageBufferError::InvalidBufferSize. ```rust // Buffer size < width × height × pixel_type.size() let buf = vec![0u8; 100]; // Only 100 bytes let img = Image::from_vec_u8( 640, 480, buf, PixelType::U8x3 // Needs 640×480×3 = 921,600 bytes )?; ``` -------------------------------- ### CropBoxError::WidthOrHeightLessThanZero Trigger Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/errors.md Example triggering CropBoxError::WidthOrHeightLessThanZero. ```rust options.crop(100.0, 100.0, -50.0, 100.0) // Negative width ``` -------------------------------- ### CropBoxError::SizeIsOutOfImageBoundaries Trigger Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/errors.md Example triggering CropBoxError::SizeIsOutOfImageBoundaries. ```rust // Trigger: (left + width) > img_width OR (top + height) > img_height options.crop(1900.0, 1050.0, 100.0, 100.0) // Extends past 1920x1080 ``` -------------------------------- ### CropBoxError::PositionIsOutOfImageBoundaries Trigger Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/errors.md Example triggering CropBoxError::PositionIsOutOfImageBoundaries. ```rust // Trigger: left or top >= image dimensions options.crop(2000.0, 1000.0, 100.0, 100.0) // On 1920x1080 image ``` -------------------------------- ### ImageError Variant Recovery Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/errors.md Example of how to recover from an ImageError::UnsupportedPixelType. ```rust if let Err(ResizeError::ImageError(ImageError::UnsupportedPixelType)) = result { // Convert image to supported format before resizing } ``` -------------------------------- ### SIMD Targeting for Maximum Performance Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md Command to build the project with target-specific SIMD features enabled, like AVX2 for x86_64. ```bash RUSTFLAGS="-C target-feature=+avx2" cargo build --release ``` -------------------------------- ### Image::pixel_type Example Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Retrieves the pixel format of the image. ```rust let pt = image.pixel_type(); assert_eq!(pt, PixelType::U8x3); ``` -------------------------------- ### Run specific benchmark in quick mode Source: https://github.com/cykooz/fast_image_resize/blob/main/dev.md Runs a specific benchmark in 'quick' mode with color always enabled. ```shell cargo bench --bench bench_resize -- --color=always --quick ``` -------------------------------- ### PixelTypesAreDifferent Variant Recovery Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/errors.md Example of ensuring source and destination images have the same pixel type. ```rust // Ensure both images have same pixel type let dst = Image::new(width, height, src.pixel_type().unwrap())?; resizer.resize(&src, &mut dst, None)?; ``` -------------------------------- ### Release Profile Optimization Settings Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md Recommended Cargo.toml settings for the release profile to optimize build performance and size. ```toml [profile.release] opt-level = 3 lto = true codegen-units = 1 ``` -------------------------------- ### Image::typed_image_mut Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Get a typed mutable view of the image. ```rust pub fn typed_image_mut(&mut self) -> Option> ``` ```rust use fast_image_resize::pixels::U8x4; if let Some(mut typed_view) = image.typed_image_mut::() { // Can write to U8x4 pixels } ``` -------------------------------- ### no_std + Custom Allocator Configuration Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md TOML configuration for a minimal build using no_std and a custom allocator. ```toml [dependencies] fast_image_resize = { version = "6.0", default-features = false, features = ["no_std"] } [features] default = ["custom-alloc"] custom-alloc = [] ``` -------------------------------- ### Run benchmarks Source: https://github.com/cykooz/fast_image_resize/blob/main/dev.md Runs benchmarks to compare with other crates and writes results to report files. ```shell WRITE_COMPARE_RESULT=1 cargo bench -- Compare ``` -------------------------------- ### Image::typed_image Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Get a typed immutable view of the image. ```rust pub fn typed_image(&self) -> Option> ``` ```rust use fast_image_resize::pixels::U8x3; if let Some(typed_view) = image.typed_image::() { // Can now work with U8x3 pixels } ``` -------------------------------- ### Resize with cropping Source: https://github.com/cykooz/fast_image_resize/blob/main/README.md Demonstrates how to resize an image while cropping a specific region of the source image. ```rust use image::codecs::png::PngEncoder; use image::{ColorType, ImageReader, GenericImageView}; use fast_image_resize::{IntoImageView, Resizer, ResizeOptions}; use fast_image_resize::images::Image; fn main() { let img = ImageReader::open("./data/nasa-4928x3279.png") .unwrap() .decode() .unwrap(); // Create container for data of destination image let mut dst_image = Image::new( 1024, 768, img.pixel_type().unwrap(), ); // Create Resizer instance and resize cropped source image // into buffer of destination image let mut resizer = Resizer::new(); resizer.resize( &img, &mut dst_image, &ResizeOptions::new().crop( 10.0, // left 10.0, // top 2000.0, // width 2000.0, // height ), ).unwrap(); } ``` -------------------------------- ### ImageBufferError::InvalidBufferSize Recovery Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/errors.md Example of recovering from ImageBufferError::InvalidBufferSize by allocating the correct buffer size. ```rust let needed_size = width as usize * height as usize * pixel_type.size(); let mut buffer = vec![0u8; needed_size]; let image = Image::from_vec_u8(width, height, buffer, pixel_type)?; ``` -------------------------------- ### Use in a no_std environment Source: https://github.com/cykooz/fast_image_resize/blob/main/README.md To use the crate in a `no_std` environment you must disable default features and enabled the `no_std` feature. ```toml [dependencies] fast_image_resize = { version = "6.0", default-features = false, features = ["no_std"] } ``` -------------------------------- ### set_cpu_extensions Method Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/mul-div.md Allows overriding the CPU extensions for alpha operations. Requires careful usage to ensure compatibility. ```rust pub unsafe fn set_cpu_extensions(&mut self, extensions: CpuExtensions) ``` -------------------------------- ### MappingError: DifferentDimensions Trigger Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/errors.md Example triggering DifferentDimensions error when source and destination images have different sizes. ```rust let src = Image::new(640, 480, PixelType::U8x3); let mut dst = Image::new(800, 600, PixelType::U8x3); // Different size mapper.forward_map(&src, &mut dst)?; // Error: DifferentDimensions ``` -------------------------------- ### cpu_extensions Method Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/mul-div.md Retrieves the currently active CPU extensions used for SIMD acceleration. ```rust pub fn cpu_extensions(&self) -> CpuExtensions ``` -------------------------------- ### MappingError: UnsupportedCombinationOfImageTypes Trigger Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/errors.md Example triggering UnsupportedCombinationOfImageTypes error due to incompatible source and destination pixel types. ```rust let mapper = create_srgb_mapper(); let src = Image::new(100, 100, PixelType::U8x3); let mut dst = Image::new(100, 100, PixelType::U8x4); // Different component count mapper.forward_map(&src, &mut dst)?; // Error: UnsupportedCombinationOfImageTypes ``` -------------------------------- ### Resize with Cropping Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/INDEX.md This code snippet shows how to configure resizing with cropping using `ResizeOptions`. ```rust use fast_image_resize::{ResizeOptions}; let options = ResizeOptions::new() .crop(100.0, 100.0, 800.0, 600.0); resizer.resize(&src, &mut dst, &options)?; ``` -------------------------------- ### InvalidBufferAlignment Trigger Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/errors.md Example demonstrating how InvalidBufferAlignment error is triggered due to misaligned buffer and pixel type requirements. ```rust let unaligned_buf: &[u8] = &[0, 1, 2]; // Misaligned ImageRef::new(10, 10, unaligned_buf, PixelType::U8x4) // Error: InvalidBufferAlignment ``` -------------------------------- ### ImageRef::new Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/api-reference/image.md Create an image reference from a byte buffer slice. ```rust pub fn new( width: u32, height: u32, buffer: &'a [u8], pixel_type: PixelType, ) -> Result ``` ```rust let data: &[u8] = &[/* pixel data */]; let image = ImageRef::new(640, 480, data, PixelType::U8x3)?; ``` -------------------------------- ### WebAssembly (WASM) Configuration Source: https://github.com/cykooz/fast_image_resize/blob/main/_autodocs/configuration.md TOML configuration for building the library for WebAssembly, enabling Simd128 acceleration. ```toml [dependencies] fast_image_resize = { version = "6.0" } ```