@@ -117,13 +117,51 @@ let jpeg = Encoder::new()
117117 . encode_rgb (& pixels , width , height )? ;
118118```
119119
120+ ### Type-Safe Encoding with imgref (default feature)
121+
122+ The ` imgref ` feature (enabled by default) provides type-safe encoding with automatic stride handling:
123+
124+ ``` rust
125+ use mozjpeg_rs :: Encoder ;
126+ use imgref :: ImgVec ;
127+ use rgb :: RGB8 ;
128+
129+ // Type-safe: dimensions baked in, can't mix up width/height
130+ let pixels : Vec <RGB8 > = vec! [RGB8 :: new (128 , 64 , 32 ); 640 * 480 ];
131+ let img = ImgVec :: new (pixels , 640 , 480 );
132+ let jpeg = Encoder :: new (). quality (85 ). encode_imgref (img . as_ref ())? ;
133+
134+ // Subimages work automatically (stride handled internally)
135+ let crop = img . sub_image (100 , 100 , 200 , 200 );
136+ let jpeg = encoder . encode_imgref (crop )? ;
137+ ```
138+
139+ Supported pixel types: ` RGB<u8> ` , ` RGBA<u8> ` (alpha discarded), ` Gray<u8> ` , ` [u8; 3] ` , ` [u8; 4] ` , ` u8 ` .
140+
141+ ### Strided Encoding
142+
143+ For memory-aligned buffers or cropping without copy:
144+
145+ ``` rust
146+ // Memory-aligned buffer (rows padded to 256 bytes)
147+ let stride = 256 ;
148+ let buffer : Vec <u8 > = vec! [128 ; stride * height ];
149+ let jpeg = encoder . encode_rgb_strided (& buffer , width , height , stride )? ;
150+
151+ // Crop without copy - point into larger buffer
152+ let crop_data = & full_image [crop_offset .. ];
153+ let jpeg = encoder . encode_rgb_strided (crop_data , crop_w , crop_h , full_stride )? ;
154+ ```
155+
120156## Features
121157
122158- ** Trellis quantization** - Rate-distortion optimized coefficient selection (AC + DC)
123159- ** Progressive JPEG** - Multi-scan encoding with spectral selection
124160- ** Huffman optimization** - 2-pass encoding for optimal entropy coding
125161- ** Overshoot deringing** - Reduces ringing artifacts at sharp edges
126162- ** Chroma subsampling** - 4:4:4, 4:2:2, 4:2:0 modes
163+ - ** Type-safe imgref integration** - Encode ` ImgRef<RGB8> ` directly with automatic stride handling
164+ - ** Strided encoding** - Memory-aligned buffers, crop without copy
127165- ** Safe Rust** - ` #![deny(unsafe_code)] ` with exceptions only for SIMD intrinsics
128166
129167### Encoder Settings Matrix
@@ -206,7 +244,7 @@ mozjpeg-rs uses `multiversion` for automatic vectorization by default. Optional
206244
207245``` toml
208246[dependencies ]
209- mozjpeg-rs = { version = " 0.2 " , features = [" simd-intrinsics" ] }
247+ mozjpeg-rs = { version = " 0.6 " , features = [" simd-intrinsics" ] }
210248```
211249
212250In benchmarks, the difference is minimal (~ 2%) as ` multiversion ` autovectorization works well for DCT and color conversion.
0 commit comments