stb 0.3.2

Safe Rust API for stb libraries
Documentation
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Rust API for image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC.
//! See https://github.com/nothings/stb/blob/master/stb_image.h
//!
//! Primarily of interest to game developers and other people who can avoid problematic images and
//! only need the trivial interface.
//!
//! - JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib)
//! - PNG 1/2/4/8/16-bit-per-channel
//! - TGA (not sure what subset, if a subset)
//! - BMP non-1bpp, non-RLE
//! - PSD (composited view only, no extra channels, 8/16 bit-per-channel)
//! - GIF (*comp always reports as 4-channel)
//! - HDR (radiance rgbE format)
//! - PIC (Softimage PIC)
//! - PNM (PPM and PGM binary only)
//!
//! Current limitations:
//! - No 12-bit-per-channel JPEG
//! - No JPEGs with arithmetic coding
//! - GIF always returns *comp=4
//!
//! Rust implementation notes:
//!
//! -  The crate wraps `stbi_io_callbacks` with a generic reader (anything that implements `io::Read` and `io::Seek`).
//! So look for `stbi_xyz_from_reader` APIs instead of `stbi_xyz_from_callbacks`.
//! -  There is no `Stdio` version of the API since it is convenient enough to use `stbi_xyz_from_reader`
//! API from Rust and there is no need to pay C string conversion overhead.
//! - You can use `stbi_no_FORMAT` feature toggles to disable not needed image formats.

use stb_sys as sys;
use std::cmp::Ordering;
use std::ffi;
use std::io;
use std::os::raw;
use std::slice;

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
pub enum Channels {
    Default = 0,
    Grey = 1,
    GreyAlpha = 2,
    Rgb = 3,
    RgbAlpha = 4,
}

#[derive(Debug, Default, Copy, Clone)]
pub struct Info {
    /// Image width in pixels
    pub width: i32,
    /// Image height in pixels
    pub height: i32,
    /// Number of image components in image file
    pub components: i32,
}

/// Holds image memory allocated by stb and responsible for calling `stbi_image_free` once dropped.
pub struct Data<T> {
    data: *mut T,
    size: usize,
}

impl<T> Data<T> {
    fn new(data: *mut T, desired_channels: Channels, info: Info) -> Self {
        let components = if desired_channels == Channels::Default {
            info.components
        } else {
            desired_channels as i32
        };

        let size = (info.width * info.height * components) as usize;

        Data { data, size }
    }

    /// Returns image memory as a slice
    pub fn as_slice(&self) -> &[T] {
        let size = self.size();
        unsafe { slice::from_raw_parts(self.data, size) }
    }

    /// Returns the number of elements (which is width x height x desired_channels)
    pub fn size(&self) -> usize {
        self.size
    }
}

impl<T: Clone> Data<T> {
    /// Consumes this object into Rust owned vector
    pub fn into_vec(self) -> Vec<T> {
        self.as_slice().to_vec()
    }
}

impl<T> Drop for Data<T> {
    fn drop(&mut self) {
        unsafe { sys::stbi_image_free(self.data as *mut ffi::c_void) };
    }
}

/// IO wrapper for stb
struct Wrapper<'a, R> {
    reader: &'a mut R,
    err: bool,
}

impl<'a, R> Wrapper<'a, R>
where
    R: io::Read + io::Seek,
{
    fn new(reader: &'a mut R) -> (Wrapper<'a, R>, sys::stbi_io_callbacks) {
        let reader = Wrapper { reader, err: false };
        let callbacks = sys::stbi_io_callbacks {
            read: Some(Self::io_read),
            skip: Some(Self::io_skip),
            eof: Some(Self::io_eof),
        };

        (reader, callbacks)
    }

    fn from_user_data(user: *mut raw::c_void) -> &'a mut Wrapper<'a, R> {
        unsafe { &mut *(user as *mut Wrapper<R>) }
    }

    fn read(&mut self, data: *mut raw::c_char, size: raw::c_int) -> raw::c_int {
        if self.err {
            return 0;
        }

        let dest = unsafe {
            let data = data as *mut u8;
            slice::from_raw_parts_mut(data, size as _)
        };

        if let Ok(n) = self.reader.read(dest) {
            return n as _;
        }

        self.err = true;
        0
    }

    fn skip(&mut self, n: raw::c_int) {
        match n.cmp(&0) {
            Ordering::Greater => {
                if self.reader.seek(io::SeekFrom::Current(n as _)).is_err() {
                    self.err = true
                }
            }
            Ordering::Less => {
                // stb allows negative seeks while Rust API considers this as an error
                if self
                    .reader
                    .seek(io::SeekFrom::Current(0)) // Find current position
                    .and_then(|pos| self.reader.seek(io::SeekFrom::Start(pos - n as u64))) // Seek from start
                    .is_err()
                {
                    self.err = true
                }
            }
            _ => {
                // Do nothing if zero
            }
        }
    }

    fn eof(&self) -> raw::c_int {
        if self.err {
            1
        } else {
            0
        }
    }

    /// Fill `data` with `size` bytes.
    /// Return number of bytes actually read
    extern "C" fn io_read(
        user: *mut raw::c_void,
        data: *mut raw::c_char,
        size: raw::c_int,
    ) -> raw::c_int {
        Wrapper::<R>::from_user_data(user).read(data, size)
    }

    /// Skip the next `n` bytes, or 'unget' the last `-n` bytes if negative
    extern "C" fn io_skip(user: *mut raw::c_void, n: raw::c_int) {
        Wrapper::<R>::from_user_data(user).skip(n);
    }

    /// Returns nonzero if we are at end of file/data
    extern "C" fn io_eof(user: *mut raw::c_void) -> raw::c_int {
        Wrapper::<R>::from_user_data(user).eof()
    }
}

/// By default we convert iphone-formatted PNGs back to RGB, even though they are internally
/// encoded differently. You can disable this conversion by calling
/// `stbi_convert_iphone_png_to_rgb(false)`, in which case you will always just get the
/// native iphone "format" through (which is BGR stored in RGB).
pub fn stbi_convert_iphone_png_to_rgb(true_if_should_convert: bool) {
    unsafe { sys::stbi_convert_iphone_png_to_rgb(if true_if_should_convert { 1 } else { 0 }) }
}

/// Call `stbi_set_unpremultiply_on_load(true)` to force a divide per pixel to remove any
/// premultiplied alpha *only* if the image file explicitly says there's premultiplied
/// data (currently only happens in iPhone images, and only if iPhone convert-to-rgb processing is on).
pub fn stbi_set_unpremultiply_on_load(true_if_should_unpremultiply: bool) {
    unsafe { sys::stbi_set_unpremultiply_on_load(if true_if_should_unpremultiply { 1 } else { 0 }) }
}

/// Flip the image vertically, so the first pixel in the output array is the bottom left
pub fn stbi_set_flip_vertically_on_load(true_if_should_flip: bool) {
    unsafe { sys::stbi_set_flip_vertically_on_load(if true_if_should_flip { 1 } else { 0 }) }
}

/// Get image dimensions & components from a slice without fully decoding
pub fn stbi_info_from_memory(buffer: &[u8]) -> Option<Info> {
    let mut info = Info::default();
    let ret = unsafe {
        sys::stbi_info_from_memory(
            buffer.as_ptr(),
            buffer.len() as i32,
            &mut info.width,
            &mut info.height,
            &mut info.components,
        )
    };
    if ret == 0 {
        None
    } else {
        Some(info)
    }
}

/// Get image dimensions & components from reader without fully decoding
pub fn stbi_info_from_reader<R>(reader: &mut R) -> Option<Info>
where
    R: io::Read + io::Seek,
{
    let (mut reader, callbacks) = Wrapper::new(reader);
    let mut info = Info::default();

    let ret = unsafe {
        sys::stbi_info_from_callbacks(
            &callbacks,
            &mut reader as *mut _ as *mut ffi::c_void,
            &mut info.width,
            &mut info.height,
            &mut info.components,
        )
    };

    if ret == 0 {
        None
    } else {
        Some(info)
    }
}

pub fn stbi_is_16_bit_from_memory(buffer: &[u8]) -> bool {
    let ret = unsafe { sys::stbi_is_16_bit_from_memory(buffer.as_ptr(), buffer.len() as i32) };
    ret == 1
}

pub fn stbi_is_16_bit_from_reader<R>(reader: &mut R) -> bool
where
    R: io::Read + io::Seek,
{
    let (mut reader, callbacks) = Wrapper::new(reader);
    let ret = unsafe {
        sys::stbi_is_16_bit_from_callbacks(&callbacks, &mut reader as *mut _ as *mut ffi::c_void)
    };
    ret == 1
}

pub fn stbi_load_from_memory(
    buffer: &[u8],
    desired_channels: Channels,
) -> Option<(Info, Data<u8>)> {
    let mut info = Info::default();

    let data = unsafe {
        sys::stbi_load_from_memory(
            buffer.as_ptr(),
            buffer.len() as i32,
            &mut info.width,
            &mut info.height,
            &mut info.components,
            desired_channels as i32,
        )
    };

    if data.is_null() {
        None
    } else {
        Some((info, Data::new(data, desired_channels, info)))
    }
}

/// 8-bits-per-channel interface, load image from reader
pub fn stbi_load_from_reader<R>(
    reader: &mut R,
    desired_channels: Channels,
) -> Option<(Info, Data<u8>)>
where
    R: io::Read + io::Seek,
{
    let (mut reader, callbacks) = Wrapper::new(reader);
    let mut info = Info::default();

    let data = unsafe {
        sys::stbi_load_from_callbacks(
            &callbacks,
            &mut reader as *mut _ as *mut ffi::c_void,
            &mut info.width,
            &mut info.height,
            &mut info.components,
            desired_channels as i32,
        )
    };

    if data.is_null() {
        None
    } else {
        Some((info, Data::new(data, desired_channels, info)))
    }
}

/// 16-bits-per-channel interface, load image from memory
pub fn stbi_load_16_from_memory(
    buffer: &[u8],
    desired_channels: Channels,
) -> Option<(Info, Data<u16>)> {
    let mut info = Info::default();

    let data = unsafe {
        sys::stbi_load_16_from_memory(
            buffer.as_ptr(),
            buffer.len() as i32,
            &mut info.width,
            &mut info.height,
            &mut info.components,
            desired_channels as i32,
        )
    };

    if data.is_null() {
        None
    } else {
        Some((info, Data::new(data, desired_channels, info)))
    }
}

pub fn stbi_load_16_from_reader<R>(
    reader: &mut R,
    desired_channels: Channels,
) -> Option<(Info, Data<u16>)>
where
    R: io::Read + io::Seek,
{
    let (mut reader, callbacks) = Wrapper::new(reader);
    let mut info = Info::default();

    let data = unsafe {
        sys::stbi_load_16_from_callbacks(
            &callbacks,
            &mut reader as *mut _ as *mut ffi::c_void,
            &mut info.width,
            &mut info.height,
            &mut info.components,
            desired_channels as i32,
        )
    };

    if data.is_null() {
        None
    } else {
        Some((info, Data::new(data, desired_channels, info)))
    }
}

#[cfg(not(feature = "stbi_no_linear"))]
pub fn stbi_loadf_from_memory(
    buffer: &[u8],
    desired_channels: Channels,
) -> Option<(Info, Data<f32>)> {
    let mut info = Info::default();

    let data = unsafe {
        sys::stbi_loadf_from_memory(
            buffer.as_ptr(),
            buffer.len() as i32,
            &mut info.width,
            &mut info.height,
            &mut info.components,
            desired_channels as i32,
        )
    };

    if data.is_null() {
        None
    } else {
        Some((info, Data::new(data, desired_channels, info)))
    }
}

#[cfg(not(feature = "stbi_no_linear"))]
pub fn stbi_loadf_from_reader<R>(
    reader: &mut R,
    desired_channels: Channels,
) -> Option<(Info, Data<f32>)>
where
    R: io::Read + io::Seek,
{
    let (mut reader, callbacks) = Wrapper::new(reader);
    let mut info = Info::default();

    let data = unsafe {
        sys::stbi_loadf_from_callbacks(
            &callbacks,
            &mut reader as *mut _ as *mut ffi::c_void,
            &mut info.width,
            &mut info.height,
            &mut info.components,
            desired_channels as i32,
        )
    };

    if data.is_null() {
        None
    } else {
        Some((info, Data::new(data, desired_channels, info)))
    }
}

#[cfg(not(feature = "stbi_no_hdr"))]
pub fn stbi_hdr_to_ldr_gamma(gamma: f32) {
    unsafe { sys::stbi_hdr_to_ldr_gamma(gamma) }
}

#[cfg(not(feature = "stbi_no_hdr"))]
pub fn stbi_hdr_to_ldr_scale(scale: f32) {
    unsafe { sys::stbi_hdr_to_ldr_scale(scale) }
}

#[cfg(not(feature = "stbi_no_linear"))]
pub fn stbi_ldr_to_hdr_gamma(gamma: f32) {
    unsafe { sys::stbi_ldr_to_hdr_gamma(gamma) }
}

#[cfg(not(feature = "stbi_no_linear"))]
pub fn stbi_ldr_to_hdr_scale(scale: f32) {
    unsafe { sys::stbi_ldr_to_hdr_scale(scale) }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::PathBuf;

    fn fixture_path(file: &str) -> PathBuf {
        let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let mut path = PathBuf::from(root.parent().unwrap());

        path.push("tests/fixtures");
        path.push(file);

        path
    }

    #[test]
    fn info_from_memory() {
        let data = fs::read(fixture_path("white.png")).expect("Failed to read test file");

        let info = stbi_info_from_memory(&data).expect("Failed to get image info from memory");
        assert_eq!(info.width, 20);
        assert_eq!(info.height, 30);
        assert_eq!(info.components, 1);
    }

    #[test]
    fn info_from_reader() {
        let mut f = fs::File::open(fixture_path("white.png")).expect("Failed to open file reader");
        let info = stbi_info_from_reader(&mut f).expect("Failed to get image info from callbacks");

        assert_eq!(info.width, 20);
        assert_eq!(info.height, 30);
        assert_eq!(info.components, 1);
    }

    #[test]
    fn load_8bit_from_memory() {
        let data = fs::read(fixture_path("white.png")).expect("Failed to read test file");
        let (info, image) =
            stbi_load_from_memory(&data, Channels::Grey).expect("Failed to load image");

        assert_eq!(info.width, 20);
        assert_eq!(info.height, 30);
        assert_eq!(info.components, 1);

        let data = image.as_slice();
        assert_eq!(data.len(), 600);

        for c in data.iter().cloned() {
            assert_eq!(c, 255);
        }
    }

    #[test]
    fn load_8bit_from_reader() {
        let mut f = fs::File::open(fixture_path("white.png")).expect("Failed to open file reader");
        let (info, image) = stbi_load_from_reader(&mut f, Channels::Grey)
            .expect("Failed to load image from reader");

        assert_eq!(info.width, 20);
        assert_eq!(info.height, 30);
        assert_eq!(info.components, 1);

        let data = image.as_slice();
        assert_eq!(data.len(), 600);

        for c in data.iter().cloned() {
            assert_eq!(c, u8::MAX);
        }
    }

    #[test]
    fn load_16bit_from_memory() {
        let data = fs::read(fixture_path("white.png")).expect("Failed to read test file");
        let (info, image) =
            stbi_load_16_from_memory(&data, Channels::Default).expect("Failed to load image");

        assert_eq!(info.width, 20);
        assert_eq!(info.height, 30);
        assert_eq!(info.components, 1);

        let data = image.as_slice();
        assert_eq!(data.len(), 600);

        for c in data.iter().cloned() {
            assert_eq!(c, u16::MAX);
        }
    }

    #[test]
    fn load_16bit_from_memory_remap_channels() {
        let data = fs::read(fixture_path("white.png")).expect("Failed to read test file");
        let (info, image) =
            stbi_load_16_from_memory(&data, Channels::GreyAlpha).expect("Failed to load image");

        assert_eq!(info.width, 20);
        assert_eq!(info.height, 30);
        assert_eq!(info.components, 1);

        let data = image.as_slice();
        assert_eq!(data.len(), 1200);

        for c in data.iter().cloned() {
            assert_eq!(c, u16::MAX);
        }
    }

    #[test]
    fn into_vec() {
        let data = fs::read(fixture_path("white.png")).expect("Failed to read test file");
        let (_, image) =
            stbi_load_from_memory(&data, Channels::Grey).expect("Failed to load image");

        let v = image.into_vec();

        assert_eq!(v.len(), 600);
        for c in v {
            assert_eq!(c, 255);
        }
    }
}