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
/*
This file is part of jpegxl-rs.

jpegxl-rs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

jpegxl-rs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with jpegxl-rs.  If not, see <https://www.gnu.org/licenses/>.
*/

#![deny(missing_docs)]
//! A safe JPEGXL Decoder wrapper.

mod error;
#[cfg(features = "with-image")]
mod image_support;
mod parallel;

use error::*;
use jpegxl_sys::*;
use std::ffi::c_void;
use std::ptr::null;

pub use error::JpegxlError;
pub use jpegxl_sys::JpegxlBasicInfo;

#[cfg(features = "with-image")]
pub use image_support::*;
pub use parallel::*;

/// JPEG XL Decoder
pub struct JpegxlDecoder {
    /// Opaque pointer to underlying JpegxlDecoder
    decoder: *mut jpegxl_sys::JpegxlDecoder,
    /// Basic info about the image. `None` if it have not read the head.
    pub basic_info: Option<JpegxlBasicInfo>,
}

impl JpegxlDecoder {
    /// Create a decoder.<br/>
    /// Memory manager and Parallel runner API are WIP.
    // TODO: Add memory manager API
    pub fn new(
        memory_manager: Option<JpegxlMemoryManager>,
        parallel_runner: Option<ParallelRunner>,
    ) -> Option<Self> {
        unsafe {
            let manager_ptr = match memory_manager {
                Some(manager) => &manager as *const JpegxlMemoryManager,
                None => null(),
            };
            let decoder_ptr = JpegxlDecoderCreate(manager_ptr);
            if decoder_ptr.is_null() {
                return None;
            }

            if let Some(mut runner) = parallel_runner {
                let status = JpegxlDecoderSetParallelRunner(
                    decoder_ptr,
                    Some(ParallelRunner::runner_func),
                    &mut runner as *mut ParallelRunner as *mut c_void,
                );
                get_error(status).ok()?;
            }

            Some(JpegxlDecoder {
                decoder: decoder_ptr,
                basic_info: None,
            })
        }
    }

    /// Create a decoder with default settings, e.g. with default memory allocator and single-threaded.
    pub fn new_with_default() -> Option<Self> {
        Self::new(None, None)
    }

    // TODO: Handle more data types when the underlying library implemented them
    fn get_pixel_format(&self) -> JpegxlPixelFormat {
        JpegxlPixelFormat {
            data_type: JpegxlDataType_JPEGXL_TYPE_UINT8,
            num_channels: if self.basic_info.unwrap().alpha_bits == 0 {
                3
            } else {
                4
            },
        }
    }

    /// Decode a JPEG XL image.<br />
    /// Currently only support RGB(A)8 encoded static image. Color info and transformation info are discarded.
    /// # Example
    /// ```
    /// # use jpegxl_rs::*;
    /// # || -> Result<(), Box<dyn std::error::Error>> {
    /// let sample = std::fs::read("test/sample.jxl")?;
    /// let mut decoder = JpegxlDecoder::new_with_default().ok_or(JpegxlError::CannotCreateDecoder)?;
    /// let buffer = decoder.decode(&sample)?;
    /// # Ok(())
    /// # }();
    /// ```
    pub fn decode<T: AsRef<[u8]>>(&mut self, data: &T) -> Result<Vec<u8>, JpegxlError> {
        let mut status;
        status = unsafe {
            JpegxlDecoderSubscribeEvents(
                self.decoder,
                (JpegxlDecoderStatus_JPEGXL_DEC_BASIC_INFO
                    | JpegxlDecoderStatus_JPEGXL_DEC_FULL_IMAGE) as i32,
            )
        };
        get_error(status)?;

        let next_in = &mut data.as_ref().as_ptr();
        let mut avail_in = data.as_ref().len() as size_t;
        status = unsafe { JpegxlDecoderProcessInput(self.decoder, next_in, &mut avail_in) };
        get_error(status)?;

        let mut basic_info = JpegxlBasicInfo::new_uninit();
        unsafe {
            status = JpegxlDecoderGetBasicInfo(self.decoder, basic_info.as_mut_ptr());
            get_error(status)?;
            let basic_info = basic_info.assume_init();
            self.basic_info = Some(basic_info);
        }

        // Get the buffer size
        let mut size: size_t = 0;
        let pixel_format = self.get_pixel_format();
        status = unsafe { JpegxlDecoderImageOutBufferSize(self.decoder, &pixel_format, &mut size) };
        get_error(status)?;

        // Create a buffer to hold decoded image
        let mut buffer: Vec<u8> = Vec::with_capacity(size as usize);
        unsafe {
            buffer.set_len(size as usize);
            status = JpegxlDecoderSetImageOutBuffer(
                self.decoder,
                &pixel_format,
                buffer.as_mut_ptr() as *mut c_void,
                size,
            );
            get_error(status)?;
        }

        // Read what left of the image
        status = unsafe { JpegxlDecoderProcessInput(self.decoder, next_in, &mut avail_in) };
        get_error(status)?;

        Ok(buffer)
    }
}

impl Drop for JpegxlDecoder {
    fn drop(&mut self) {
        unsafe {
            JpegxlDecoderDestroy(self.decoder);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_decode() -> Result<(), image::ImageError> {
        let sample = std::fs::read("test/sample.jxl")?;
        let mut decoder =
            JpegxlDecoder::new_with_default().ok_or(JpegxlError::CannotCreateDecoder)?;
        let buffer = decoder.decode(&sample)?;
        let basic_info = decoder.basic_info.unwrap();

        use image::{ImageBuffer, RgbImage};
        let image: RgbImage =
            ImageBuffer::from_raw(basic_info.xsize, basic_info.ysize, buffer).unwrap();

        image.save("sample.png")?;
        std::fs::remove_file("sample.png")?;

        Ok(())
    }

    #[test]
    fn test_parallel_decode() -> Result<(), image::ImageError> {
        let sample = std::fs::read("test/sample.jxl")?;
        let parallel_runner = ParallelRunner::new();

        let mut decoder = JpegxlDecoder::new(None, Some(parallel_runner))
            .ok_or(JpegxlError::CannotCreateDecoder)?;
        let parallel_buffer = decoder.decode(&sample)?;

        decoder = JpegxlDecoder::new_with_default().ok_or(JpegxlError::CannotCreateDecoder)?;
        let single_buffer = decoder.decode(&sample)?;

        assert!(
            parallel_buffer == single_buffer,
            "Multithread runner should be the same as singlethread one"
        );

        Ok(())
    }
}