|
| 1 | +extern crate sixel_sys as sixel; |
| 2 | +#[macro_use] |
| 3 | +extern crate lazy_static; |
| 4 | + |
| 5 | +#[allow(unused_imports)] |
| 6 | +use sixel::*; |
| 7 | + |
| 8 | +use std::path::Path; |
| 9 | +use std::ffi::CString; |
| 10 | +use std::sync::Mutex; |
| 11 | + |
| 12 | +lazy_static!{ |
| 13 | + pub static ref lock: Mutex<()> = Mutex::new(()); |
| 14 | +} |
| 15 | + |
| 16 | +#[test] |
| 17 | +fn it_works() { |
| 18 | + println!("Libsixel version is {:?}", sixel::VERSION); |
| 19 | + |
| 20 | + let expected_ver: Version = Version { |
| 21 | + major: 1, |
| 22 | + minor: 7, |
| 23 | + patch: 3, |
| 24 | + }; |
| 25 | + |
| 26 | + assert_eq!(expected_ver, sixel::VERSION); |
| 27 | +} |
| 28 | + |
| 29 | +// Note: Sixel images do not show up in tmux |
| 30 | +fn print_sixel<P: AsRef<Path>>(path: P) { |
| 31 | + use std::fs::File; |
| 32 | + use std::io::prelude::*; |
| 33 | + use std::io::stdout; |
| 34 | + |
| 35 | + let path_str = path.as_ref().to_str().unwrap().to_owned(); |
| 36 | + |
| 37 | + let mut file = File::open(path).unwrap(); |
| 38 | + let mut contents: Vec<u8> = vec![]; |
| 39 | + file.read_to_end(&mut contents); |
| 40 | + |
| 41 | + println!("Printing file {:?}", path_str); |
| 42 | + |
| 43 | + let stdout = stdout(); |
| 44 | + let mut handle = stdout.lock(); |
| 45 | + |
| 46 | + handle.write_all(&contents); |
| 47 | +} |
| 48 | + |
| 49 | +fn image_root<'a>() -> &'a Path { |
| 50 | + Path::new("libsixel/images/") |
| 51 | +} |
| 52 | + |
| 53 | +fn snake_of(extension: &str) -> CString { |
| 54 | + let snake_path = image_root() |
| 55 | + .join("snake") |
| 56 | + .with_extension(extension); |
| 57 | + |
| 58 | + let snake_path = snake_path.to_str().unwrap(); |
| 59 | + |
| 60 | + CString::new(snake_path).unwrap() |
| 61 | +} |
| 62 | + |
| 63 | +/// Just make sure that it works |
| 64 | +#[test] |
| 65 | +fn convert_snake() { |
| 66 | + round_trip_with_ext("bmp"); |
| 67 | + round_trip_with_ext("gif"); |
| 68 | + round_trip_with_ext("pbm"); |
| 69 | + round_trip_with_ext("pgm"); |
| 70 | + round_trip_with_ext("ppm"); |
| 71 | + round_trip_with_ext("tga"); |
| 72 | + round_trip_with_ext("tiff"); |
| 73 | + |
| 74 | + #[cfg(jpeg)] |
| 75 | + round_trip_with_ext("jpg"); |
| 76 | + #[cfg(png)] |
| 77 | + round_trip_with_ext("png"); |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +fn round_trip_with_ext(extension: &str) { |
| 82 | + use std::ptr; |
| 83 | + use std::os::raw::c_int; |
| 84 | + use std::os::raw::c_uint; |
| 85 | + println!("encoding with {}", extension); |
| 86 | + |
| 87 | + let assert_ok = |actual: Status| { |
| 88 | + assert_eq!(sixel::OK, |
| 89 | + actual, |
| 90 | + "when de/encoding snake from a {} file, sixel returned the status {} instead of ok", |
| 91 | + extension, |
| 92 | + actual); |
| 93 | + }; |
| 94 | + |
| 95 | + let snake_sixel = "snake_test_".to_owned() + extension + ".six"; |
| 96 | + let snake_new = "snake_test.".to_string() + extension; |
| 97 | + let snake_six_out = CString::new(snake_sixel.clone()).unwrap(); |
| 98 | + let snake_new_out = CString::new(snake_new).unwrap(); |
| 99 | + |
| 100 | + { |
| 101 | + let gaurd = lock.lock().unwrap(); |
| 102 | + |
| 103 | + unsafe { |
| 104 | + let mut encoder: *mut sixel::Encoder = ptr::null_mut() as *mut _; |
| 105 | + |
| 106 | + let result = sixel_encoder_new(&mut encoder, |
| 107 | + ptr::null_mut() as *mut sixel::Allocator); |
| 108 | + assert_ok(result); |
| 109 | + |
| 110 | + |
| 111 | + let result = sixel_encoder_setopt(encoder, |
| 112 | + Optflag::OutFile, |
| 113 | + snake_six_out.as_ptr()); |
| 114 | + assert_ok(result); |
| 115 | + |
| 116 | + let snake_path = snake_of(extension); |
| 117 | + |
| 118 | + let result = sixel_encoder_encode(encoder, snake_path.as_ptr()); |
| 119 | + assert_ok(result); |
| 120 | + |
| 121 | + sixel_encoder_unref(encoder); |
| 122 | + } |
| 123 | + |
| 124 | + unsafe { |
| 125 | + let mut decoder: *mut sixel::Decoder = ptr::null_mut() as *mut _; |
| 126 | + |
| 127 | + let result = sixel_decoder_new(&mut decoder, |
| 128 | + ptr::null_mut() as *mut sixel::Allocator); |
| 129 | + assert_ok(result); |
| 130 | + |
| 131 | + |
| 132 | + let result = sixel_decoder_setopt(decoder, |
| 133 | + DecoderOptflag::Input, |
| 134 | + snake_six_out.as_ptr()); |
| 135 | + assert_ok(result); |
| 136 | + |
| 137 | + let result = sixel_decoder_setopt(decoder, |
| 138 | + DecoderOptflag::Output, |
| 139 | + snake_new_out.as_ptr()); |
| 140 | + assert_ok(result); |
| 141 | + |
| 142 | + let result = sixel_decoder_decode(decoder); |
| 143 | + assert_ok(result); |
| 144 | + |
| 145 | + sixel_decoder_unref(decoder); |
| 146 | + } |
| 147 | + } |
| 148 | + print_sixel(snake_sixel); |
| 149 | +} |
| 150 | + |
| 151 | + |
0 commit comments