### Quick Start
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
A simple example demonstrating how to decode, transform, and encode an image using the fluent API.
```APIDOC
## Quick Start
```kotlin
import io.clroot.slimg.*
// Decode → transform → encode in a single chain
val result = SlimgImage.decodeFile("photo.jpg")
.resize(width = 800)
.crop(aspectRatio = 16 to 9)
.encode(Format.WEB_P, quality = 85)
File("photo.webp").writeBytes(result.data)
```
```
--------------------------------
### Installation
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Instructions on how to add the slimg-kotlin library to your project using Gradle or Maven.
```APIDOC
## Installation
### Gradle (Kotlin DSL)
```kotlin
dependencies {
implementation("io.clroot.slimg:slimg-kotlin:$slimgVersion")
}
```
### Gradle (Groovy)
```groovy
dependencies {
implementation 'io.clroot.slimg:slimg-kotlin:$slimgVersion'
}
```
### Maven
```xml
io.clroot.slimg
slimg-kotlin
${slimgVersion}
```
```
--------------------------------
### Install slimg via Package Managers
Source: https://github.com/clroot/slimg/blob/main/README.md
Instructions for installing the slimg CLI tool using common package managers like Cargo or Homebrew.
```bash
cargo install slimg
```
```bash
brew install clroot/tap/slimg
```
--------------------------------
### Quick Start: Decode, Transform, and Encode Image with SlimgImage
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
A concise example demonstrating the fluent API of SlimgImage to decode an image from a file, resize it, crop it to a specific aspect ratio, and then encode it into WebP format with a specified quality. The resulting image data is then written to a new file.
```kotlin
import io.clroot.slimg.*
// Decode → transform → encode in a single chain
val result = SlimgImage.decodeFile("photo.jpg")
.resize(width = 800)
.crop(aspectRatio = 16 to 9)
.encode(Format.WEB_P, quality = 85)
File("photo.webp").writeBytes(result.data)
```
--------------------------------
### Install slimg Python Library
Source: https://github.com/clroot/slimg/blob/main/bindings/python/README.md
Installs the slimg Python package using pip. This command fetches and installs the library and its dependencies, enabling image optimization functionalities.
```bash
pip install slimg
```
--------------------------------
### Programmatic Image Processing with slimg-core
Source: https://github.com/clroot/slimg/blob/main/docs/usage.md
Example of using the slimg-core Rust library to decode an image, apply an aspect ratio extension, and save the result as a WebP file.
```rust
use slimg_core::*;
let (image, format) = decode_file(Path::new("photo.jpg"))?;
let result = convert(&image, &PipelineOptions {
format: Format::WebP,
quality: 80,
resize: None,
crop: None,
extend: Some(ExtendMode::AspectRatio { width: 1, height: 1 }),
fill_color: Some(FillColor::Solid([255, 255, 255, 255])),
})?;
result.save(Path::new("photo.webp"))?;
```
--------------------------------
### SlimgImage Fluent API - Extending
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Examples of extending (adding padding) to images using the SlimgImage fluent API.
```APIDOC
### Extending (add padding)
```kotlin
img.extend(1920, 1080) // to exact size
img.extend(aspectRatio = 1 to 1) // to aspect ratio
img.extend(1920, 1080, fill = Slimg.solidColor(255, 0, 0)) // with color
```
```
--------------------------------
### Perform Image Operations via CLI
Source: https://github.com/clroot/slimg/blob/main/README.md
Examples of using the slimg CLI to convert, optimize, resize, crop, and extend images. Supports batch processing and recursive directory operations.
```bash
# Convert format
slimg convert photo.jpg --format webp
# Optimize (re-encode in same format)
slimg optimize photo.jpg --quality 70
# Resize
slimg resize photo.jpg --width 800
# Crop by coordinates
slimg crop photo.jpg --region 100,50,800,600
# Crop to aspect ratio (center-anchored)
slimg crop photo.jpg --aspect 16:9
# Extend to square with padding
slimg extend photo.jpg --aspect 1:1
# Extend with transparent background
slimg extend photo.png --aspect 1:1 --transparent
# Batch processing with format conversion
slimg convert ./images --format webp --output ./output --recursive --jobs 4
```
--------------------------------
### Image Resizing with Slimg (Python)
Source: https://context7.com/clroot/slimg/llms.txt
Shows various methods for resizing images using the slimg library, including resizing by width, height, exact dimensions, fitting within bounds, and scaling by a factor. Assumes slimg is installed.
```python
import slimg
image = slimg.open("photo.jpg")
resized = slimg.resize(image, width=800)
resized = slimg.resize(image, height=600)
resized = slimg.resize(image, exact=(800, 600))
resized = slimg.resize(image, fit=(1200, 1200))
resized = slimg.resize(image, scale=0.5)
result = slimg.convert(resized, format="png")
result.save("resized.png")
```
--------------------------------
### Low-level Functions - Direct FFI Bindings
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Provides examples of using the direct FFI bindings for low-level image operations.
```APIDOC
### Low-level Functions
Direct FFI bindings are also available as top-level functions:
```kotlin
val decoded = decode(byteArray)
val result = convert(image, PipelineOptions(Format.WEB_P, 80u.toUByte(), null, null, null, null))
val optimized = optimize(byteArray, 80u.toUByte())
```
```
--------------------------------
### SlimgImage Fluent API - Resizing
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Examples of image resizing operations using the SlimgImage fluent API.
```APIDOC
### Resizing
```kotlin
img.resize(width = 800) // by width (preserve aspect ratio)
img.resize(height = 600) // by height
img.resize(width = 800, height = 600) // exact dimensions
img.resize(fit = 1200 to 1200) // fit within bounds
img.resize(scale = 0.5) // scale factor
```
```
--------------------------------
### SlimgImage Fluent API - Cropping
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Examples of image cropping operations using the SlimgImage fluent API.
```APIDOC
### Cropping
```kotlin
img.crop(aspectRatio = 16 to 9) // center-anchored
img.crop(x = 100, y = 50, width = 800, height = 600) // region
```
```
--------------------------------
### SlimgImage Fluent API: Image Encoding Options
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Examples of encoding an image using the SlimgImage fluent API. Allows encoding to a specific format (like WebP) with quality settings, or optimizing and re-encoding in the source format.
```kotlin
// Encode
img.encode(Format.WEB_P, quality = 85) // to specific format
img.optimize(quality = 70) // re-encode in source format
```
--------------------------------
### Image Conversion and Manipulation with Slimg (Python)
Source: https://context7.com/clroot/slimg/llms.txt
Demonstrates basic image conversion to WebP and AVIF formats, resizing, and a full pipeline including cropping and extending. Requires the slimg library.
```python
import slimg
result = slimg.convert(image, format="webp", quality=80)
result.save("photo.webp")
result = slimg.convert(image, format="avif", quality=60)
result.save("photo.avif")
resized_img = slimg.resize(image, width=800)
result = slimg.convert(resized_img, format="png")
result.save("thumbnail.png")
cropped = slimg.crop(image, aspect_ratio=(16, 9))
extended = slimg.extend(cropped, aspect_ratio=(1, 1), fill=(255, 255, 255))
result = slimg.convert(extended, format="webp", quality=85)
result.save("processed.webp")
```
--------------------------------
### Manage Supported Image Formats
Source: https://context7.com/clroot/slimg/llms.txt
Demonstrates how to list, detect, and utilize supported image formats across Rust, Python, and Kotlin.
```rust
use slimg_core::Format;
let formats = [Format::Jpeg, Format::Png, Format::WebP, Format::Avif, Format::Jxl, Format::Qoi];
for fmt in formats {
println!("{:?}: extension={}, can_encode={}", fmt, fmt.extension(), fmt.can_encode());
}
let format = Format::from_magic_bytes(&bytes);
let format = Format::from_extension(Path::new("photo.webp"));
```
```python
import slimg
formats = [slimg.Format.JPEG, slimg.Format.PNG, slimg.Format.WEBP, slimg.Format.AVIF, slimg.Format.JXL, slimg.Format.QOI]
result = slimg.convert(image, format="webp")
result = slimg.convert(image, format=slimg.Format.WEBP)
```
```kotlin
import io.clroot.slimg.Format
val formats = listOf(Format.JPEG, Format.PNG, Format.WEB_P, Format.AVIF, Format.JXL, Format.QOI)
val result = img.encode(Format.WEB_P, quality = 85)
```
--------------------------------
### Rust: Image Conversion and Pipeline
Source: https://context7.com/clroot/slimg/llms.txt
Demonstrates how to convert images to different formats and apply transformations like resizing, cropping, and extending in a single pipeline using the Rust slimg library.
```APIDOC
## convert
Convert images to different formats with optional resize, crop, and extend operations in a single pipeline.
### Description
This function allows for comprehensive image manipulation, including format conversion, resizing, cropping, and extending, all within a single operation.
### Method
N/A (Rust function)
### Endpoint
N/A (Rust function)
### Parameters
N/A (Rust function parameters are defined in `PipelineOptions`)
### Request Example
```rust
use slimg_core::{convert, decode_file, PipelineOptions, Format, ResizeMode, CropMode, ExtendMode, FillColor};
use std::path::Path;
// Simple format conversion
let (image, _) = decode_file(Path::new("photo.jpg"))?;
let result = convert(&image, &PipelineOptions {
format: Format::WebP,
quality: 80,
resize: None,
crop: None,
extend: None,
fill_color: None,
})?;
result.save(Path::new("photo.webp"))?;
// Full pipeline: crop to 16:9, extend to square, resize to 800px width
let (image, _) = decode_file(Path::new("photo.jpg"))?;
let result = convert(&image, &PipelineOptions {
format: Format::Png,
quality: 90,
crop: Some(CropMode::AspectRatio { width: 16, height: 9 }),
extend: Some(ExtendMode::AspectRatio { width: 1, height: 1 }),
fill_color: Some(FillColor::Solid([255, 255, 255, 255])), // White padding
resize: Some(ResizeMode::Width(800)),
})?;
println!("Output: {}x{} ({} bytes)", result.width, result.height, result.data.len());
result.save(Path::new("output.png"))?;
```
### Response
N/A (Rust function returns a result object)
### Response Example
N/A
```
--------------------------------
### Resize Images with Slimg (Kotlin)
Source: https://context7.com/clroot/slimg/llms.txt
Demonstrates various resizing techniques in Kotlin, including resizing by width, height, exact dimensions, fitting within bounds, and scaling by a factor.
```kotlin
import io.clroot.slimg.*
val img = SlimgImage.decodeFile("photo.jpg")
// Resize by width (preserves aspect ratio)
val resized = img.resize(width = 800)
// Resize by height (preserves aspect ratio)
val resized = img.resize(height = 600)
// Resize to exact dimensions
val resized = img.resize(width = 800, height = 600)
// Fit within bounds (preserves aspect ratio)
val resized = img.resize(fit = 1200 to 1200)
// Scale by factor
val resized = img.resize(scale = 0.5)
println("Resized to: ${resized.width}x${resized.height}")
```
--------------------------------
### SlimgImage Fluent API - Encoding
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Examples of encoding images to different formats using the SlimgImage fluent API.
```APIDOC
### Encoding
```kotlin
img.encode(Format.WEB_P, quality = 85) // to specific format
img.optimize(quality = 70) // re-encode in source format
```
```
--------------------------------
### Image Optimization with Slimg (Python)
Source: https://context7.com/clroot/slimg/llms.txt
Explains how to optimize images using slimg.optimize_file for file paths and slimg.optimize for byte data, reducing file size while maintaining the original format. Requires the slimg library.
```python
import slimg
result = slimg.optimize_file("photo.jpg", quality=75)
result.save("optimized.jpg")
with open("photo.png", "rb") as f:
data = f.read()
result = slimg.optimize(data, quality=80)
print(f"Original: {len(data)} bytes")
print(f"Optimized: {len(result.data)} bytes")
print(f"Format: {result.format}")
result.save("optimized.png")
```
--------------------------------
### SlimgImage Fluent API: Image Resizing Options
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Illustrates different ways to resize an image using the SlimgImage fluent API. Options include resizing by width, height, exact dimensions, fitting within bounds, and scaling by a factor.
```kotlin
// Resize
img.resize(width = 800) // by width (preserve aspect ratio)
img.resize(height = 600) // by height
img.resize(width = 800, height = 600) // exact dimensions
img.resize(fit = 1200 to 1200) // fit within bounds
img.resize(scale = 0.5) // scale factor
```
--------------------------------
### Add Dependencies for QOI and JXL
Source: https://github.com/clroot/slimg/blob/main/docs/plans/2026-02-17-slimg-implementation.md
This snippet shows the TOML configuration to add the 'rapid-qoi' and 'jxl-oxide' crates as project dependencies. These are necessary for QOI encoding/decoding and JXL decoding, respectively.
```toml
rapid-qoi = "0.6"
jxl-oxide = "0.12"
```
--------------------------------
### Image Processing with slimg-core
Source: https://github.com/clroot/slimg/blob/main/crates/slimg-core/README.md
Demonstrates how to decode an image from a file, convert it to a different format with quality settings, perform resizing, and optimize image data in-place.
```rust
use slimg_core::*;
use std::path::Path;
// Decode from file
let (image, format) = decode_file(Path::new("photo.jpg"))?;
// Convert to WebP at quality 80
let result = convert(&image, &PipelineOptions {
format: Format::WebP,
quality: 80,
resize: None,
})?;
result.save(Path::new("photo.webp"))?;
// Convert and resize in one step
let result = convert(&image, &PipelineOptions {
format: Format::Avif,
quality: 60,
resize: Some(ResizeMode::Width(800)),
})?;
// Optimize in-place (re-encode same format)
let data = std::fs::read("photo.jpg")?;
let optimized = optimize(&data, 75)?;
optimized.save(Path::new("photo.jpg"))?;
```
--------------------------------
### Install slimg-kotlin using Gradle and Maven
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Instructions for adding the slimg-kotlin library as a dependency in Gradle (Kotlin DSL and Groovy) and Maven projects. Ensure you replace '$slimgVersion' with the actual version number.
```kotlin
dependencies {
implementation("io.clroot.slimg:slimg-kotlin:$slimgVersion")
}
```
```groovy
dependencies {
implementation 'io.clroot.slimg:slimg-kotlin:$slimgVersion'
}
```
```xml
io.clroot.slimg
slimg-kotlin
${slimgVersion}
```
--------------------------------
### Slimg Object: Image Manipulation Functions
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Provides examples of using the Slimg object for individual image manipulation tasks such as resizing, cropping, and extending (adding padding), using specific mode objects for parameters.
```kotlin
// Image operations
val resized = Slimg.resize(image, ResizeMode.Width(800u))
val cropped = Slimg.crop(image, CropMode.AspectRatio(16u, 9u))
val extended = Slimg.extend(image, ExtendMode.Size(1920u, 1080u))
```
--------------------------------
### Python: Image Opening and Decoding
Source: https://context7.com/clroot/slimg/llms.txt
Demonstrates how to open image files and decode image data from bytes using the `slimg` Python library, including accessing image properties.
```APIDOC
## slimg.open and slimg.decode
Open images from files or decode from bytes with automatic format detection.
### Description
This section covers the `slimg.open` and `slimg.decode` functions in the Python library, which provide convenient ways to load image data from files or byte streams, automatically detecting the image format.
### Method
N/A (Python functions)
### Endpoint
N/A (Python functions)
### Parameters
N/A (Python function parameters are `filepath` for `open` and `data` for `decode`)
### Request Example
```python
import slimg
# Open an image file
image = slimg.open("photo.jpg")
print(f"{image.width}x{image.height} {image.format}")
# Output: 1920x1080 Format.JPEG
# Decode from bytes
with open("photo.png", "rb") as f:
data = f.read()
image = slimg.decode(data)
print(f"Format: {image.format}") # Format.PNG
# Access image properties
print(f"Dimensions: {image.width}x{image.height}")
print(f"Data size: {len(image.data)} bytes") # Raw RGBA pixels
```
### Response
N/A (Python functions return an image object)
### Response Example
N/A
```
--------------------------------
### Slimg Object: One-off Image Conversion and Optimization
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Demonstrates using the Slimg object for single image operations like decoding, converting with simplified parameters (using Int for quality and specific Resize/Crop modes), and optimizing byte arrays.
```kotlin
// Decode
val decoded = Slimg.decode(byteArray) // or InputStream, ByteBuffer
val decoded = Slimg.decodeFile("photo.jpg")
// Convert with simplified parameters
val result = Slimg.convert(
decoded.image,
Format.WEB_P,
quality = 85, // Int, not UByte
resize = ResizeMode.Width(800u),
crop = CropMode.AspectRatio(16u, 9u),
)
// Optimize
val optimized = Slimg.optimize(fileBytes, quality = 70) // Int quality
```
--------------------------------
### Rust: Resize Modes
Source: https://context7.com/clroot/slimg/llms.txt
Explains different modes for resizing images using the Rust slimg library, including resizing by width, height, exact dimensions, fitting within bounds, and scaling.
```APIDOC
## ResizeMode
Control how images are resized with various modes for different use cases.
### Description
This section details the `ResizeMode` enum in the slimg Rust library, which provides flexible options for resizing images based on width, height, aspect ratio, or scale factor.
### Method
N/A (Rust function)
### Endpoint
N/A (Rust function)
### Parameters
N/A (Rust function parameters are `image` and `mode`)
### Request Example
```rust
use slimg_core::{resize::resize, decode_file, ResizeMode};
use std::path::Path;
let (image, _) = decode_file(Path::new("photo.jpg"))?;
// Resize by width (preserves aspect ratio)
let resized = resize(&image, &ResizeMode::Width(800))?;
// Resize by height (preserves aspect ratio)
let resized = resize(&image, &ResizeMode::Height(600))?;
// Exact dimensions (may distort image)
let resized = resize(&image, &ResizeMode::Exact(800, 600))?;
// Fit within bounds (preserves aspect ratio, never exceeds bounds)
let resized = resize(&image, &ResizeMode::Fit(1200, 1200))?;
// Scale by factor (0.5 = half size, 2.0 = double size)
let resized = resize(&image, &ResizeMode::Scale(0.5))?;
println!("Resized to {}x{}", resized.width, resized.height);
```
### Response
N/A (Rust function returns a resized image object)
### Response Example
N/A
```
--------------------------------
### Rust: Extend Modes and Fill Colors
Source: https://context7.com/clroot/slimg/llms.txt
Explains how to extend images by adding padding using `ExtendMode` and specifying `FillColor` options like solid colors or transparency in Rust.
```APIDOC
## ExtendMode and FillColor
Extend images by adding padding with configurable fill colors or transparency.
### Description
This section details the `ExtendMode` and `FillColor` types in the Rust slimg library, enabling users to add padding to images to meet specific dimensions or aspect ratios, with options for solid color or transparent backgrounds.
### Method
N/A (Rust function)
### Endpoint
N/A (Rust function)
### Parameters
N/A (Rust function parameters are `image`, `extend_mode`, and `fill_color`)
### Request Example
```rust
use slimg_core::{extend::extend, decode_file, ExtendMode, FillColor};
use std::path::Path;
let (image, _) = decode_file(Path::new("photo.jpg"))?;
// Extend to aspect ratio with white background
let extended = extend(
&image,
&ExtendMode::AspectRatio { width: 1, height: 1 },
&FillColor::Solid([255, 255, 255, 255]), // RGBA white
)?;
// Extend to exact size with transparent background
let extended = extend(
&image,
&ExtendMode::Size { width: 1920, height: 1080 },
&FillColor::Transparent,
)?;
// Extend to 16:9 with black padding
let extended = extend(
&image,
&ExtendMode::AspectRatio { width: 16, height: 9 },
&FillColor::Solid([0, 0, 0, 255]), // RGBA black
)?;
println!("Extended to {}x{}", extended.width, extended.height);
```
### Response
N/A (Rust function returns an extended image object)
### Response Example
N/A
```
--------------------------------
### Batch Process Images with Parallelism
Source: https://github.com/clroot/slimg/blob/main/docs/usage.md
Shows how to process entire directories recursively using multiple CPU cores for improved performance.
```bash
slimg convert ./images --format webp --recursive --jobs 4
```
--------------------------------
### Use slimg-core as a Rust Library
Source: https://github.com/clroot/slimg/blob/main/README.md
Demonstrates how to use the slimg-core crate to programmatically decode, convert, and save images within a Rust application.
```rust
use slimg_core::*;
// Decode an image file
let (image, format) = decode_file(Path::new("photo.jpg"))?;
// Convert to WebP
let result = convert(&image, &PipelineOptions {
format: Format::WebP,
quality: 80,
resize: None,
crop: None,
extend: None,
fill_color: None,
})?;
// Save the result
result.save(Path::new("photo.webp"))?;
```
--------------------------------
### Run AVIF Codec Tests (Bash)
Source: https://github.com/clroot/slimg/blob/main/docs/plans/2026-02-17-slimg-implementation.md
Command to execute the tests for the slimg-core crate, specifically verifying the functionality of the AVIF codec. Note that AVIF encoding can be slow, potentially increasing test execution time.
```bash
cargo test -p slimg-core
```
--------------------------------
### Image Extending with Slimg (Python)
Source: https://context7.com/clroot/slimg/llms.txt
Demonstrates extending images by adding padding with customizable fill colors using the slimg library. Supports extending to specific aspect ratios or exact dimensions.
```python
import slimg
image = slimg.open("photo.jpg")
extended = slimg.extend(image, aspect_ratio=(1, 1), fill=(255, 255, 255))
extended = slimg.extend(image, aspect_ratio=(16, 9), fill=(0, 0, 0))
extended = slimg.extend(image, aspect_ratio=(1, 1))
extended = slimg.extend(image, size=(1920, 1080), fill=(128, 128, 128))
result = slimg.convert(extended, format="png")
result.save("extended.png")
```
--------------------------------
### Basic Image Operations with slimg
Source: https://github.com/clroot/slimg/blob/main/bindings/python/README.md
Demonstrates fundamental image operations using the slimg Python library. This includes opening an image, retrieving its properties, converting it to different formats (like WebP), optimizing it, resizing, cropping, and extending the canvas.
```python
import slimg
# Open an image file
image = slimg.open("photo.jpg")
print(f"{image.width}x{image.height} {image.format}")
# Convert to WebP
result = slimg.convert(image, format="webp", quality=80)
result.save("photo.webp")
# Optimize in the same format
result = slimg.optimize_file("photo.jpg", quality=75)
result.save("optimized.jpg")
# Resize by width (preserves aspect ratio)
resized = slimg.resize(image, width=800)
result = slimg.convert(resized, format="png")
result.save("thumbnail.png")
# Crop to aspect ratio (centre-anchored)
cropped = slimg.crop(image, aspect_ratio=(16, 9))
# Crop by pixel region
cropped = slimg.crop(image, region=(100, 50, 800, 600))
# Extend (pad) to aspect ratio with a fill colour
extended = slimg.extend(image, aspect_ratio=(1, 1), fill=(255, 255, 255))
# Extend with transparent padding (default)
extended = slimg.extend(image, aspect_ratio=(1, 1))
```
--------------------------------
### Image Cropping with Slimg (Python)
Source: https://context7.com/clroot/slimg/llms.txt
Illustrates how to crop images using the slimg library, either by specifying an aspect ratio or a pixel region. The cropped image can then be converted and saved.
```python
import slimg
image = slimg.open("photo.jpg")
cropped = slimg.crop(image, aspect_ratio=(16, 9))
cropped = slimg.crop(image, aspect_ratio=(1, 1))
cropped = slimg.crop(image, region=(100, 50, 800, 600))
result = slimg.convert(cropped, format="webp", quality=85)
result.save("cropped.webp")
```
--------------------------------
### Testing the Resize Module
Source: https://github.com/clroot/slimg/blob/main/docs/plans/2026-02-17-slimg-implementation.md
Illustrates the tests written to verify the functionality of the resize module.
```APIDOC
## Resize Module Tests
### Description
This section outlines the unit tests for the `resize` module, ensuring that various resizing modes function correctly and preserve aspect ratios as expected.
### Method
N/A (Unit tests)
### Endpoint
N/A
### Parameters
N/A
### Request Example
N/A
### Response
N/A
### Test Cases
- `resize_by_width_preserves_ratio`
- `resize_by_height_preserves_ratio`
- `resize_fit_within_bounds`
- `resize_by_scale`
- `resize_exact_ignores_ratio`
### Running Tests
```bash
# Navigate to the project root
cd /path/to/slimg
# Run tests for the slimg-core crate
cargo test -p slimg-core
```
```
--------------------------------
### SlimgImage Fluent API: Image Extension (Padding) Options
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Demonstrates how to extend an image by adding padding using the SlimgImage fluent API. Supports extending to an exact size or to a specified aspect ratio, with an option to fill with a solid color.
```kotlin
// Extend (add padding)
img.extend(1920, 1080) // to exact size
img.extend(aspectRatio = 1 to 1) // to aspect ratio
img.extend(1920, 1080, fill = Slimg.solidColor(255, 0, 0)) // with color
```
--------------------------------
### Run slimg Benchmarks with Criterion
Source: https://github.com/clroot/slimg/blob/main/docs/benchmarks.md
Commands to execute the benchmark suite for the slimg-core Rust crate using the Criterion benchmarking framework. These commands allow running all benchmarks or specific groups like encode, decode, convert, optimize, and resize.
```bash
cargo bench -p slimg-core
cargo bench -p slimg-core -- encode
cargo bench -p slimg-core -- decode
cargo bench -p slimg-core -- convert
cargo bench -p slimg-core -- optimize
cargo bench -p slimg-core -- resize
```
--------------------------------
### SlimgImage Fluent API: Chaining Multiple Operations
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Illustrates the power of chaining multiple image manipulation operations (resize, crop, extend, encode) in a single sequence using the SlimgImage fluent API. The final result is saved to a file.
```kotlin
val result = SlimgImage.decodeFile("photo.jpg")
.resize(fit = 1200 to 1200)
.crop(aspectRatio = 1 to 1)
.extend(aspectRatio = 16 to 9, fill = Slimg.solidColor(0, 0, 0))
.encode(Format.WEB_P, quality = 80)
File("output.webp").writeBytes(result.data)
```
--------------------------------
### SlimgImage Fluent API (Kotlin/JVM)
Source: https://context7.com/clroot/slimg/llms.txt
Demonstrates the fluent API in Kotlin for chaining image decoding, transformation (resize, crop, extend), and encoding operations. This API is immutable and efficient for complex pipelines.
```kotlin
import io.clroot.slimg.*
import java.io.File
val result = SlimgImage.decodeFile("photo.jpg")
.resize(width = 800)
.crop(aspectRatio = 16 to 9)
.encode(Format.WEB_P, quality = 85)
File("photo.webp").writeBytes(result.data)
val result = SlimgImage.decodeFile("photo.jpg")
.resize(fit = 1200 to 1200)
.crop(aspectRatio = 1 to 1)
.extend(aspectRatio = 16 to 9, fill = Slimg.solidColor(0, 0, 0))
.encode(Format.WEB_P, quality = 80)
File("output.webp").writeBytes(result.data)
val img = SlimgImage.decodeFile("photo.jpg")
println("Original: ${img.width}x${img.height}")
val resized = img.resize(width = 800)
println("Resized: ${resized.width}x${resized.height}")
```
--------------------------------
### Crop and Extend Images (Kotlin)
Source: https://context7.com/clroot/slimg/llms.txt
Shows how to crop images to specific aspect ratios or regions, and how to extend images with padding or background colors using the Slimg Kotlin API.
```kotlin
import io.clroot.slimg.*
import java.io.File
val img = SlimgImage.decodeFile("photo.jpg")
// Crop to aspect ratio (center-anchored)
val cropped = img.crop(aspectRatio = 16 to 9)
// Crop by region (x, y, width, height)
val cropped = img.crop(x = 100, y = 50, width = 800, height = 600)
// Extend to aspect ratio with fill color
val extended = img.extend(aspectRatio = 1 to 1, fill = Slimg.solidColor(255, 255, 255))
// Extend to exact size with transparent background
val extended = img.extend(1920, 1080, fill = FillColor.Transparent)
// Chained operations
val result = img
.crop(aspectRatio = 16 to 9)
.extend(aspectRatio = 1 to 1, fill = Slimg.solidColor(0, 0, 0))
.encode(Format.PNG, quality = 90)
File("output.png").writeBytes(result.data)
```
--------------------------------
### SlimgImage Fluent API: Image Cropping Options
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Shows how to crop an image using the SlimgImage fluent API. Supports cropping to a specific aspect ratio (center-anchored) or to a defined rectangular region.
```kotlin
// Crop
img.crop(aspectRatio = 16 to 9) // center-anchored
img.crop(x = 100, y = 50, width = 800, height = 600) // region
```
--------------------------------
### Extend Image Canvas via CLI
Source: https://github.com/clroot/slimg/blob/main/docs/usage.md
Demonstrates how to use the slimg CLI to extend an image's canvas to a specific aspect ratio or fixed size. Options include setting background colors, transparency, and output formats.
```bash
slimg extend photo.jpg --aspect 1:1
slimg extend photo.jpg --aspect 16:9 --color '#000000'
slimg extend photo.png --size 1920x1080 --transparent
slimg extend photo.jpg --aspect 1:1 --transparent --format png
slimg extend ./images --aspect 1:1 --output ./squared --recursive
```
--------------------------------
### Rust: Image Optimization
Source: https://context7.com/clroot/slimg/llms.txt
Shows how to re-encode images in their original format with a specified quality to reduce file size using the Rust slimg library.
```APIDOC
## optimize
Re-encode images in their original format at a specified quality level to reduce file size.
### Description
This function optimizes an image by re-encoding it with a target quality setting, aiming to minimize file size while preserving the original format.
### Method
N/A (Rust function)
### Endpoint
N/A (Rust function)
### Parameters
N/A (Rust function parameters are `bytes` and `quality`)
### Request Example
```rust
use slimg_core::optimize;
use std::fs;
// Read image bytes and optimize
let bytes = fs::read("photo.jpg")?;
let result = optimize(&bytes, 75)?; // Quality 0-100
// Save optimized image
result.save(std::path::Path::new("optimized.jpg"))?;
// Check compression ratio
let original_size = bytes.len();
let optimized_size = result.data.len();
println!("Reduced from {} to {} bytes ({:.1}%)",
original_size,
optimized_size,
(1.0 - optimized_size as f64 / original_size as f64) * 100.0
);
```
### Response
N/A (Rust function returns a result object)
### Response Example
N/A
```
--------------------------------
### Low-level FFI Bindings for Image Operations
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Demonstrates the direct FFI (Foreign Function Interface) bindings available as top-level functions for decoding, converting, and optimizing images. These functions offer lower-level access to the slimg library's capabilities.
```kotlin
val decoded = decode(byteArray)
val result = convert(image, PipelineOptions(Format.WEB_P, 80u.toUByte(), null, null, null, null))
val optimized = optimize(byteArray, 80u.toUByte())
```
--------------------------------
### SlimgImage Fluent API - InputStream and ByteBuffer
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Shows how to use the SlimgImage fluent API with InputStream and ByteBuffer sources.
```APIDOC
#### With InputStream / ByteBuffer
Works with any byte source — file streams, network responses, WebFlux `DataBuffer`, etc.
```kotlin
// From InputStream
FileInputStream("photo.jpg").use { stream ->
val result = SlimgImage.decode(stream)
.resize(width = 800)
.encode(Format.WEB_P)
File("photo.webp").writeBytes(result.data)
}
// From ByteBuffer (e.g. WebFlux DataBuffer)
val result = SlimgImage.decode(dataBuffer.asByteBuffer())
.resize(fit = 1200 to 1200)
.encode(Format.WEB_P, quality = 85)
```
```
--------------------------------
### SlimgImage Fluent API - Chaining Operations
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Demonstrates chaining multiple image operations together for complex transformations.
```APIDOC
#### Chaining
Each operation returns a new `SlimgImage`, so you can chain freely:
```kotlin
val result = SlimgImage.decodeFile("photo.jpg")
.resize(fit = 1200 to 1200)
.crop(aspectRatio = 1 to 1)
.extend(aspectRatio = 16 to 9, fill = Slimg.solidColor(0, 0, 0))
.encode(Format.WEB_P, quality = 80)
File("output.webp").writeBytes(result.data)
```
```
--------------------------------
### Decode and Resize Image using Slimg
Source: https://github.com/clroot/slimg/blob/main/bindings/kotlin/README.md
Demonstrates how to load an image from a file path and perform a resize operation. The library supports various input types and resize modes like fit or exact dimensions.
```java
import com.slimg.SlimgImage;
import com.slimg.Format;
// Decode an image from a file path
SlimgImage image = SlimgImage.decodeFile("input.jpg");
// Resize the image to fit within 800x600 bounds
image.resize(800, 600);
// Encode the result to PNG format
byte[] output = image.encode(Format.PNG, 90);
```
--------------------------------
### CLI Command: extend
Source: https://github.com/clroot/slimg/blob/main/docs/usage.md
Extends an image by adding padding to match a target aspect ratio or specific canvas size.
```APIDOC
## COMMAND extend
### Description
Extend an image by adding padding to match a target aspect ratio or size. The original image is centered on the new canvas.
### Method
CLI
### Endpoint
slimg extend [input_path]
### Parameters
#### Options
- **--aspect** (string) - Optional - Target aspect ratio (e.g., '1:1', '16:9'). Mutually exclusive with --size.
- **--size** (string) - Optional - Target canvas size (e.g., '1920x1080'). Mutually exclusive with --aspect.
- **--color** (string) - Optional - Fill color as hex (e.g., '#FF0000'). Default: white. Mutually exclusive with --transparent.
- **--transparent** (flag) - Optional - Use transparent background. Mutually exclusive with --color.
- **--format, -f** (string) - Optional - Convert to a different format.
- **--quality, -q** (integer) - Optional - Encoding quality 0-100 (default: 80).
- **--output, -o** (string) - Optional - Output path (file or directory).
- **--recursive** (flag) - Optional - Process subdirectories.
- **--jobs, -j** (integer) - Optional - Number of parallel jobs (default: all cores).
- **--overwrite** (flag) - Optional - Overwrite existing files.
### Request Example
slimg extend photo.jpg --aspect 1:1 --color '#000000'
### Response
#### Success Response (0)
- **Exit Code** (integer) - Returns 0 on success.
#### Response Example
Successfully processed photo.jpg
```