kornia-io 0.2.0

Image and Video IO library in Rust for computer vision
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use crate::{
    error::IoError,
    limits::{alloc_image, check_image_dimensions},
};
use kornia_image::{Image, ImageError, ImageSize};
use std::{path::Path, sync::Mutex};
use turbojpeg;

/// Re-exported so callers don't need a direct ``turbojpeg`` dep just to
/// call [`JpegTurboEncoder::set_subsamp`].
pub use turbojpeg::Subsamp;

/// Error types for the JPEG module.
#[derive(thiserror::Error, Debug)]
pub enum JpegTurboError {
    /// Error when the JPEG compressor cannot be created.
    #[error("Something went wrong with the JPEG compressor")]
    TurboJpegError(#[from] turbojpeg::Error),

    /// Error when the image data is not contiguous.
    #[error("Image data is not contiguous")]
    ImageDataNotContiguous,

    /// Error to create the image.
    #[error("Failed to create image")]
    ImageCreationError(#[from] ImageError),

    /// Error when mutex is poisoned.
    #[error("Mutex is poisoned")]
    MutexPoisoned,

    /// The pixel buffer length does not match `width * height * channels`.
    #[error("pixel buffer length {got} != expected {expected}")]
    InvalidBufferLength {
        /// Actual slice length.
        got: usize,
        /// Length implied by the given `ImageSize` and channel count.
        expected: usize,
    },

    /// The JPEG header declares more pixels than [`crate::limits::MAX_IMAGE_PIXELS`].
    #[error("JPEG of {width}x{height} exceeds the maximum of {max_pixels} pixels")]
    ImageTooLarge {
        /// Declared width in pixels.
        width: usize,
        /// Declared height in pixels.
        height: usize,
        /// Maximum number of pixels allowed.
        max_pixels: usize,
    },

    /// Failed to allocate the output buffer.
    #[error("Failed to allocate {0} bytes for the decoded image")]
    AllocationFailed(usize),

    /// I/O error, e.g. when reading a JPEG file from disk.
    #[error(transparent)]
    IoError(#[from] std::io::Error),
}

impl JpegTurboError {
    // Converts the errors of the crate-wide decode limits (`crate::limits`) without
    // losing information. `IoError` cannot be embedded directly since it already
    // wraps `JpegTurboError`.
    fn from_limits(err: IoError) -> Self {
        match err {
            IoError::ImageTooLarge {
                width,
                height,
                max_pixels,
            } => Self::ImageTooLarge {
                width,
                height,
                max_pixels,
            },
            IoError::AllocationFailed(bytes) => Self::AllocationFailed(bytes),
            IoError::ImageCreationError(e) => Self::ImageCreationError(e),
            IoError::JpegTurboError(e) => e,
            other => Self::IoError(std::io::Error::other(other)),
        }
    }
}

/// A JPEG decoder using the turbojpeg library.
/// **Note on migration:** The `Default` trait implementation was removed. Please use [`JpegTurboDecoder::new()`] instead.
pub struct JpegTurboDecoder(Mutex<turbojpeg::Decompressor>);

/// A JPEG encoder using the turbojpeg library.
/// **Note on migration:** The `Default` trait implementation was removed. Please use [`JpegTurboEncoder::new()`] instead.
pub struct JpegTurboEncoder(Mutex<turbojpeg::Compressor>);

// Implementations for ImageDecoder and ImageEncoder

/// Implementation of the ImageEncoder struct.
impl JpegTurboEncoder {
    /// Creates a new `ImageEncoder`.
    ///
    /// # Returns
    ///
    /// A new `ImageEncoder` instance.
    ///
    /// # Panics
    ///
    /// Panics if the compressor cannot be created.
    pub fn new() -> Result<Self, JpegTurboError> {
        let compressor = turbojpeg::Compressor::new()?;
        Ok(Self(Mutex::new(compressor)))
    }

    /// Encodes the given RGB8 image into a JPEG image.
    ///
    /// # Arguments
    ///
    /// * `image` - The image to encode.
    ///
    /// # Returns
    ///
    /// The encoded data as `Vec<u8>`.
    pub fn encode_rgb8(&self, image: &Image<u8, 3>) -> Result<Vec<u8>, JpegTurboError> {
        self.encode_rgb8_slice(image.as_slice(), image.size())
    }

    /// Encodes a borrowed RGB8 pixel slice (`width * height * 3`, row-major, no padding) into a
    /// JPEG image.
    ///
    /// Zero-copy input: turbojpeg reads the slice directly, so no owning [`Image`] has to be
    /// built first. [`encode_rgb8`](Self::encode_rgb8) delegates here.
    ///
    /// # Arguments
    ///
    /// * `pixels` - The RGB8 pixels, row-major `R,G,B` with no row padding.
    /// * `size` - The image dimensions; `pixels.len()` must equal `width * height * 3`.
    pub fn encode_rgb8_slice(
        &self,
        pixels: &[u8],
        size: ImageSize,
    ) -> Result<Vec<u8>, JpegTurboError> {
        let expected = size.checked_len(3)?;
        if pixels.len() != expected {
            return Err(JpegTurboError::InvalidBufferLength {
                got: pixels.len(),
                expected,
            });
        }

        let buf = turbojpeg::Image {
            pixels,
            width: size.width,
            pitch: 3 * size.width,
            height: size.height,
            format: turbojpeg::PixelFormat::RGB,
        };

        Ok(self
            .0
            .lock()
            .map_err(|_| JpegTurboError::MutexPoisoned)?
            .compress_to_vec(buf)?)
    }

    /// Sets the quality of the encoder.
    ///
    /// # Arguments
    ///
    /// * `quality` - The quality to set.
    pub fn set_quality(&self, quality: i32) -> Result<(), JpegTurboError> {
        Ok(self
            .0
            .lock()
            .map_err(|_| JpegTurboError::MutexPoisoned)?
            .set_quality(quality)?)
    }

    /// Sets chroma subsampling: ``Subsamp::None`` is 4:4:4 (no subsampling,
    /// largest files, highest quality), ``Sub2x1`` is 4:2:2, ``Sub2x2`` is
    /// 4:2:0 (half the chroma data, fastest, default for cv2/PIL at q ≤ 95).
    pub fn set_subsamp(&self, subsamp: turbojpeg::Subsamp) -> Result<(), JpegTurboError> {
        self.0
            .lock()
            .map_err(|_| JpegTurboError::MutexPoisoned)?
            .set_subsamp(subsamp)?;
        Ok(())
    }
}

/// Implementation of the ImageDecoder struct.
impl JpegTurboDecoder {
    /// Creates a new `ImageDecoder`.
    ///
    /// # Returns
    ///
    /// A new `ImageDecoder` instance.
    pub fn new() -> Result<Self, JpegTurboError> {
        let decompressor = turbojpeg::Decompressor::new()?;
        Ok(JpegTurboDecoder(Mutex::new(decompressor)))
    }

    /// Reads the header of a JPEG image.
    ///
    /// # Arguments
    ///
    /// * `jpeg_data` - The JPEG data to read the header from.
    ///
    /// # Returns
    ///
    /// The image size.
    ///
    /// # Errors
    ///
    /// Returns an error if the header cannot be read, or
    /// [`JpegTurboError::ImageTooLarge`] if it declares more pixels than
    /// [`crate::limits::MAX_IMAGE_PIXELS`].
    pub fn read_header(&self, jpeg_data: &[u8]) -> Result<ImageSize, JpegTurboError> {
        // read the JPEG header with image size
        let header = self
            .0
            .lock()
            .map_err(|_| JpegTurboError::MutexPoisoned)?
            .read_header(jpeg_data)?;

        // Reject headers declaring more pixels than the decode limit before any allocation.
        check_image_dimensions(header.width, header.height).map_err(JpegTurboError::from_limits)?;

        Ok(ImageSize {
            width: header.width,
            height: header.height,
        })
    }

    /// Decodes the given JPEG data as RGB8 image.
    pub fn decode_rgb8(&self, jpeg_data: &[u8]) -> Result<Image<u8, 3>, JpegTurboError> {
        let mut dst =
            alloc_image(self.read_header(jpeg_data)?).map_err(JpegTurboError::from_limits)?;
        self.decode_rgb8_into(jpeg_data, &mut dst)?;
        Ok(dst)
    }

    /// Decodes the given JPEG data as Gray/Mono8 image.
    pub fn decode_gray8(&self, jpeg_data: &[u8]) -> Result<Image<u8, 1>, JpegTurboError> {
        let mut dst =
            alloc_image(self.read_header(jpeg_data)?).map_err(JpegTurboError::from_limits)?;
        self.decode_gray8_into(jpeg_data, &mut dst)?;
        Ok(dst)
    }

    /// Decodes JPEG bytes as RGB8 into a pre-allocated buffer.
    pub fn decode_rgb8_into(
        &self,
        jpeg_data: &[u8],
        dst: &mut Image<u8, 3>,
    ) -> Result<(), JpegTurboError> {
        let size = dst.size();
        self.decode_into(
            jpeg_data,
            dst.as_slice_mut(),
            size,
            turbojpeg::PixelFormat::RGB,
        )
    }

    /// Decodes JPEG bytes as Gray/Mono8 into a pre-allocated buffer.
    pub fn decode_gray8_into(
        &self,
        jpeg_data: &[u8],
        dst: &mut Image<u8, 1>,
    ) -> Result<(), JpegTurboError> {
        let size = dst.size();
        self.decode_into(
            jpeg_data,
            dst.as_slice_mut(),
            size,
            turbojpeg::PixelFormat::GRAY,
        )
    }

    fn decode_into(
        &self,
        jpeg_data: &[u8],
        pixels: &mut [u8],
        image_size: ImageSize,
        format: turbojpeg::PixelFormat,
    ) -> Result<(), JpegTurboError> {
        let header_size = self.read_header(jpeg_data)?;
        if header_size != image_size {
            return Err(JpegTurboError::ImageCreationError(
                ImageError::InvalidImageSize(
                    header_size.width,
                    header_size.height,
                    image_size.width,
                    image_size.height,
                ),
            ));
        }

        let pitch = format.size() * image_size.width;
        let buf = turbojpeg::Image {
            pixels,
            width: image_size.width,
            pitch,
            height: image_size.height,
            format,
        };

        self.0
            .lock()
            .map_err(|_| JpegTurboError::MutexPoisoned)?
            .decompress(jpeg_data, buf)?;
        Ok(())
    }
}

/// Reads a JPEG image in `RGB8` format from the given file path.
///
/// The method reads the JPEG image data directly from a file leveraging the libjpeg-turbo library.
///
/// # Arguments
///
/// * `image_path` - The path to the JPEG image.
///
/// # Returns
///
/// An in image containing the JPEG image data.
///
/// # Example
///
/// ```
/// use kornia_image::Image;
/// use kornia_io::jpegturbo as F;
///
/// let image: Image<u8, 3> = F::read_image_jpegturbo_rgb8("../../tests/data/dog.jpeg").unwrap();
///
/// assert_eq!(image.cols(), 258);
/// assert_eq!(image.rows(), 195);
/// assert_eq!(image.num_channels(), 3);
/// ```
pub fn read_image_jpegturbo_rgb8(file_path: impl AsRef<Path>) -> Result<Image<u8, 3>, IoError> {
    let file_path = file_path.as_ref().to_owned();
    // verify the file exists and is a JPEG
    if !file_path.exists() {
        return Err(IoError::FileDoesNotExist(file_path.to_path_buf()));
    }

    if file_path
        .extension()
        .is_none_or(|ext| !ext.eq_ignore_ascii_case("jpg") && !ext.eq_ignore_ascii_case("jpeg"))
    {
        return Err(IoError::InvalidFileExtension(file_path.to_path_buf()));
    }

    // open the file and map it to memory
    let jpeg_data = std::fs::read(file_path)?;

    // decode the data directly from memory
    let image = {
        let decoder = JpegTurboDecoder::new()?;
        decoder.decode_rgb8(&jpeg_data)?
    };

    Ok(image)
}

/// Writes the given JPEG data to the given file path.
///
/// # Arguments
///
/// * `file_path` - The path to the JPEG image.
/// * `image` - The tensor containing the JPEG image data.
/// * `quality` - The quality of the JPEG encoding, range from 0 (lowest) to 100 (highest)
pub fn write_image_jpegturbo_rgb8(
    file_path: impl AsRef<Path>,
    image: &Image<u8, 3>,
    quality: u8,
) -> Result<(), IoError> {
    let file_path = file_path.as_ref().to_owned();

    // compress the image
    let encoder = JpegTurboEncoder::new()?;
    encoder.set_quality(quality as i32)?;

    let jpeg_data = encoder.encode_rgb8(image)?;

    // write the data directly to a file
    std::fs::write(file_path, jpeg_data)?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::IoError;

    #[test]
    fn encode_rgb8_slice_matches_owning_and_validates_len() -> Result<(), JpegTurboError> {
        let size = ImageSize {
            width: 64,
            height: 48,
        };
        let pixels: Vec<u8> = (0..size.width * size.height * 3)
            .map(|i| (i % 251) as u8)
            .collect();
        let enc = JpegTurboEncoder::new()?;
        // The zero-copy slice path and the owning path must produce identical bytes.
        let img = Image::<u8, 3>::from_size_slice(size, &pixels)?;
        assert_eq!(
            enc.encode_rgb8_slice(&pixels, size)?,
            enc.encode_rgb8(&img)?
        );
        // A wrong-length slice is rejected, not read out of bounds.
        assert!(matches!(
            enc.encode_rgb8_slice(&pixels[..pixels.len() - 1], size),
            Err(JpegTurboError::InvalidBufferLength { .. })
        ));
        Ok(())
    }

    #[test]
    fn image_decoder() -> Result<(), JpegTurboError> {
        let jpeg_data = std::fs::read("../../tests/data/dog.jpeg")?;
        // read the header
        let image_size = JpegTurboDecoder::new()?.read_header(&jpeg_data)?;
        assert_eq!(image_size.width, 258);
        assert_eq!(image_size.height, 195);
        // load the image as file and decode it
        let image = JpegTurboDecoder::new()?.decode_rgb8(&jpeg_data)?;
        assert_eq!(image.cols(), 258);
        assert_eq!(image.rows(), 195);
        assert_eq!(image.num_channels(), 3);
        Ok(())
    }

    #[test]
    fn image_decoder_gray8() -> Result<(), JpegTurboError> {
        let jpeg_data = std::fs::read("../../tests/data/dog.jpeg")?;
        let image = JpegTurboDecoder::new()?.decode_gray8(&jpeg_data)?;
        assert_eq!(image.cols(), 258);
        assert_eq!(image.rows(), 195);
        assert_eq!(image.num_channels(), 1);
        assert_eq!(image.as_slice().len(), 258 * 195);
        Ok(())
    }

    #[test]
    fn image_encoder() -> Result<(), Box<dyn std::error::Error>> {
        let jpeg_data_fs = std::fs::read("../../tests/data/dog.jpeg")?;
        let image = JpegTurboDecoder::new()?.decode_rgb8(&jpeg_data_fs)?;
        let jpeg_data = JpegTurboEncoder::new()?.encode_rgb8(&image)?;
        let image_back = JpegTurboDecoder::new()?.decode_rgb8(&jpeg_data)?;
        assert_eq!(image_back.cols(), 258);
        assert_eq!(image_back.rows(), 195);
        assert_eq!(image_back.num_channels(), 3);
        Ok(())
    }

    #[test]
    fn read_jpeg() -> Result<(), IoError> {
        let image = read_image_jpegturbo_rgb8("../../tests/data/dog.jpeg")?;
        assert_eq!(image.cols(), 258);
        assert_eq!(image.rows(), 195);
        Ok(())
    }

    #[test]
    fn read_write_jpeg() -> Result<(), IoError> {
        let tmp_dir = tempfile::tempdir()?;
        std::fs::create_dir_all(tmp_dir.path())?;

        let file_path = tmp_dir.path().join("dog.jpeg");
        let image_data = read_image_jpegturbo_rgb8("../../tests/data/dog.jpeg")?;
        write_image_jpegturbo_rgb8(&file_path, &image_data, 100)?;

        let image_data_back = read_image_jpegturbo_rgb8(&file_path)?;
        assert!(file_path.exists(), "File does not exist: {file_path:?}");

        assert_eq!(image_data_back.cols(), 258);
        assert_eq!(image_data_back.rows(), 195);
        assert_eq!(image_data_back.num_channels(), 3);

        Ok(())
    }
}