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
use std::{path::Path, io::{self, Read}, fs::File, ops::Deref};
use super::packedfile;
use crate::utils::{LibraryId, ObjectId};
use hashbrown::HashMap;

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Clone,Debug,Copy)]
#[repr(i32)]
pub enum Color{
    NonColor = 0,
    Color = 1,
}

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Clone,Debug,Copy)]
#[repr(i32)]
pub enum Wrap{
    Repeat = 0,
    Extend = 1,
    Clip = 2,
}

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Clone,Debug,Copy)]
#[repr(i32)]
pub enum Projection{
    Flat = 0,
    BoxP = 1,
    Sphere = 2,
    Tube = 3,
}

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Clone,Debug,Copy)]
#[repr(i32)]
pub enum Interpolation{
    Linear = 0,
    Closest = 1,
    Cubic = 2,
    Smart = 3,
}

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Clone,Debug,Copy)]
pub enum Modifier{
    Hue(f32),
    Saturation(f32),
    Value(f32),
    Hsv(f32,f32,f32),
}

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Clone)]
pub struct Image{
    pub data: ObjectId,
    pub wrap: Wrap,
    pub color_space: Color,
    pub interpolation: Interpolation,
    pub projection: Projection,
    pub modifiers: Vec<(Modifier, f32)>,
}

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Clone)]
pub struct Data{
    pub data: Vec<u8>,
}

impl Deref for Data{
    type Target = [u8];
    fn deref(&self) -> &[u8]{
        &self.data
    }
}

pub struct Texture<'a>{
    pub data: &'a Data,
    pub image: &'a Image
}


pub fn parse_img_data<P:AsRef<Path>>(img: &blender::Object, container_path: Option<P>) -> Result<Data, io::Error>{
    match img.get_object("packedfile"){
        Ok(packedfile) => {
            let img_data = packedfile::data(&packedfile)
                .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
            Ok(Data{
                data: img_data.to_vec(),
            })
        }
        Err(_) =>{
            if let Some(container_path) = container_path {
                let mut path = img.get_str("name").unwrap().to_owned();
                if path.starts_with("//"){
                    path = container_path.as_ref().join(&path[2..]).to_str().unwrap().to_owned();
                }
                let mut file = File::open(path)?;
                let mut data = Vec::new();
                file.read_to_end(&mut data)?;
                Ok(Data{
                    data,
                })
            }else{
                let msg = "Can't load non packed textures with file loaded from memory";
                let error = blender::Error::from(msg.to_string());
                Err(io::Error::new(io::ErrorKind::NotFound, error))
            }
        }
    }
}


pub fn parse_img_node(
    img: &blender::Object,
    library_id: LibraryId,
    node: Option<blender::Object>,
    libraries: &HashMap<LibraryId, blender::File>,
    images_data: &mut HashMap<ObjectId, Data>) -> Result<Image, io::Error>
{
    let wrap = node.as_ref()
        .and_then(|n| *n.get("extension").unwrap())
        .unwrap_or(Wrap::Repeat);
    let color_space = node.as_ref()
        .and_then(|n| *n.get("color_space").unwrap())
        .unwrap_or(Color::Color);
    let projection = node.as_ref()
        .and_then(|n| *n.get("projection").unwrap())
        .unwrap_or(Projection::Flat);
    let interpolation = node.as_ref()
        .and_then(|n| *n.get("interpolation").unwrap())
        .unwrap_or(Interpolation::Linear);
    let data = library_id.object_id(img);
    if !images_data.contains_key(&data){
        let library = &libraries[&data.source_file];
        let container_dir = library.file_path().and_then(|p| p.parent());
        let img_data = parse_img_data(&img, container_dir)?;
        images_data.insert(data.clone(), img_data);
    }

    Ok(Image{
        data,
        wrap,
        color_space,
        projection,
        interpolation,
        modifiers: vec![],
    })
}

pub fn parse_images(
    blend: &blender::File,
    library_id: &LibraryId,
    libraries: &HashMap<LibraryId, blender::File>) -> HashMap<ObjectId, Data>
{
    let container_dir = blend.file_path().and_then(|p| p.parent());
    blend.objects("Image")
        .filter_map(|img| {
            if let Some(lib_id) = library_id.linked_library_id(&img) {
                let library = &libraries[&lib_id];
                let img = library.object_by_name(img.name().unwrap())?;
                let img_data = parse_img_data(&img, container_dir).ok();
                let id = ObjectId::new(lib_id, img.name().unwrap());
                img_data.map(|img_data| (id, img_data))
            }else{
                let img_data = parse_img_data(&img, container_dir).ok();
                let id = ObjectId::new(library_id.clone(), img.name().unwrap());
                img_data.map(|img_data| (id, img_data))
            }
        }).collect()
}