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
use core::slice; use std::{ffi::CString, path::Path}; use ffi::{self, BLImageCore}; use crate::{ array::Array, codec::ImageCodec, error::{errcode_to_result, Result}, format::ImageFormat, geometry::SizeI, variant::WrappedBlCore, }; use bitflags::bitflags; use ffi::BLImageInfoFlags::*; bitflags! { pub struct ImageInfoFlags: u32 { const PROGRESSIVE = BL_IMAGE_INFO_FLAG_PROGRESSIVE as u32; } } use ffi::BLImageScaleFilter::*; bl_enum! { pub enum ImageScaleFilter { None = BL_IMAGE_SCALE_FILTER_NONE, Nearest = BL_IMAGE_SCALE_FILTER_NEAREST, Bilinear = BL_IMAGE_SCALE_FILTER_BILINEAR, Bicubic = BL_IMAGE_SCALE_FILTER_BICUBIC, Bell = BL_IMAGE_SCALE_FILTER_BELL, Gauss = BL_IMAGE_SCALE_FILTER_GAUSS, Hermite = BL_IMAGE_SCALE_FILTER_HERMITE, Hanning = BL_IMAGE_SCALE_FILTER_HANNING, Catrom = BL_IMAGE_SCALE_FILTER_CATROM, Bessel = BL_IMAGE_SCALE_FILTER_BESSEL, Sinc = BL_IMAGE_SCALE_FILTER_SINC, Lanczos = BL_IMAGE_SCALE_FILTER_LANCZOS, Blackman = BL_IMAGE_SCALE_FILTER_BLACKMAN, Mitchell = BL_IMAGE_SCALE_FILTER_MITCHELL, User = BL_IMAGE_SCALE_FILTER_USER, } Default => None } #[repr(transparent)] pub struct Image { core: BLImageCore, } unsafe impl WrappedBlCore for Image { type Core = ffi::BLImageCore; const IMPL_TYPE_INDEX: usize = ffi::BLImplType::BL_IMPL_TYPE_IMAGE as usize; } impl Image { pub fn new(width: i32, height: i32, format: ImageFormat) -> Result<Self> { unsafe { let mut this = Image { core: *Self::none(), }; errcode_to_result(ffi::blImageInitAs( this.core_mut(), width, height, format.into(), )) .map(|_| this) } } pub fn from_data<R: AsRef<[u8]>>( width: i32, height: i32, format: ImageFormat, data: &R, codecs: &Array<ImageCodec>, ) -> Result<()> { let mut this = Self::new(width, height, format)?; unsafe { errcode_to_result(ffi::blImageReadFromData( this.core_mut(), data.as_ref().as_ptr() as *const _, data.as_ref().len(), codecs.core(), )) } } pub fn from_path<P: AsRef<Path>>(path: P, codecs: &Array<ImageCodec>) -> Result<Self> { unsafe { let mut this = Image { core: *Self::none(), }; let path = CString::new(path.as_ref().to_string_lossy().as_bytes()).expect("Invalid Path"); errcode_to_result(ffi::blImageReadFromFile( this.core_mut(), path.as_ptr(), codecs.core(), )) .map(|_| this) } } pub fn size(&self) -> SizeI { let ffi::BLSizeI { w, h } = self.impl_().size; SizeI { w, h } } pub fn width(&self) -> i32 { self.size().w } pub fn height(&self) -> i32 { self.size().h } pub fn data(&self) -> Result<ImageData<'_>> { unsafe { let mut data = std::mem::zeroed(); errcode_to_result(ffi::blImageGetData(self.core(), &mut data)).map(|_| { let ffi::BLSizeI { w, h } = data.size; ImageData { data: slice::from_raw_parts(data.pixelData as *mut _, (w * h) as usize), stride: data.stride, size: (w, h), format: data.format.into(), flags: ImageInfoFlags::from_bits_truncate(data.flags), } }) } } pub fn write_to_file<P: AsRef<Path>>(&self, path: P, codec: &ImageCodec) -> Result<()> { unsafe { let path = CString::new(path.as_ref().to_string_lossy().as_bytes()).expect("Invalid Path"); errcode_to_result(ffi::blImageWriteToFile( self.core(), path.as_ptr(), codec.core(), )) } } } impl PartialEq for Image { fn eq(&self, other: &Self) -> bool { unsafe { ffi::blImageEquals(self.core(), other.core()) } } } impl Clone for Image { fn clone(&self) -> Self { let mut new = Image { core: *Self::none(), }; unsafe { ffi::blImageAssignDeep(new.core_mut(), self.core()) }; new } } impl Drop for Image { fn drop(&mut self) { unsafe { ffi::blImageReset(&mut self.core) }; } } #[derive(Debug)] pub struct ImageData<'a> { pub data: &'a [u8], pub stride: isize, pub size: (i32, i32), pub format: ImageFormat, pub flags: ImageInfoFlags, }