Skip to content

Commit

Permalink
feat(exported): more redundant img fields removed (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
francisdb authored May 10, 2024
1 parent bd2d756 commit 8fb367c
Showing 1 changed file with 75 additions and 38 deletions.
113 changes: 75 additions & 38 deletions src/vpx/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ pub struct ImageData {
// TODO seems to be 1 for some kind of link type img, related to screenshots.
// we only see this where a screenshot is set on the table info.
// https://github.com/vpinball/vpinball/blob/1a70aa35eb57ec7b5fbbb9727f6735e8ef3183e0/Texture.cpp#L588
pub link: Option<u32>, // LINK
pub alpha_test_value: f32, // ALTV
pub link: Option<u32>, // LINK
/// ALTV
/// Alpha test value, used for transparency
/// Used to default to 1.0, now defaults to -1.0 since 10.8
pub alpha_test_value: f32, // ALTV
pub is_opaque: Option<bool>, // OPAQ (added in 10.8)
pub is_signed: Option<bool>, // SIGN (added in 10.8)
// TODO we can probably only have one of these so we can make an enum
Expand All @@ -72,6 +75,8 @@ pub struct ImageData {
}

impl ImageData {
const ALPHA_TEST_VALUE_DEFAULT: f32 = -1.0;

pub fn is_link(&self) -> bool {
self.link == Some(1)
}
Expand All @@ -85,33 +90,6 @@ impl ImageData {
}
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
pub(crate) struct ImageDataJpegJson {
path: String,
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
inme: Option<String>,
}

impl ImageDataJpegJson {
pub fn from_image_data_jpeg(image_data_jpeg: &ImageDataJpeg) -> Self {
ImageDataJpegJson {
path: image_data_jpeg.path.clone(),
name: image_data_jpeg.name.clone(),
inme: image_data_jpeg.internal_name.clone(),
}
}

pub fn to_image_data_jpeg(&self) -> ImageDataJpeg {
ImageDataJpeg {
path: self.path.clone(),
name: self.name.clone(),
internal_name: self.inme.clone(),
data: vec![], // data is stored in a separate file
}
}
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
pub(crate) struct ImageDataJson {
pub(crate) name: String,
Expand All @@ -124,47 +102,106 @@ pub(crate) struct ImageDataJson {
pub(crate) height: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
link: Option<u32>,
alpha_test_value: f32,
#[serde(skip_serializing_if = "Option::is_none")]
alpha_test_value: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
is_opaque: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
is_signed: Option<bool>,
jpeg: Option<ImageDataJpegJson>,

// these are just for full compatibility with the original file
#[serde(skip_serializing_if = "Option::is_none")]
jpeg_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
jpeg_internal_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
jpeg_path: Option<String>,

// in case we have a duplicate name
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) name_dedup: Option<String>,
}

impl ImageDataJson {
pub fn from_image_data(image_data: &ImageData) -> Self {
let (jpeg_name, jpeg_path, jpeg_internal_name) = if let Some(jpeg) = &image_data.jpeg {
let jpeg_name = if jpeg.name == image_data.name {
None
} else {
Some(jpeg.name.clone())
};
let jpeg_path = if jpeg.path == image_data.path {
None
} else {
Some(jpeg.path.clone())
};
let jpeg_internal_name = jpeg.internal_name.clone();
(jpeg_name, jpeg_path, jpeg_internal_name)
} else {
(None, None, None)
};

// TODO we might want to generate a warning if the alpha_test_value is the old default of 1.0
// which caused overhead in the shader
let alpha_test_value = if image_data.alpha_test_value == ImageData::ALPHA_TEST_VALUE_DEFAULT
{
None
} else {
Some(image_data.alpha_test_value)
};

ImageDataJson {
name: image_data.name.clone(),
internal_name: image_data.internal_name.clone(),
path: image_data.path.clone(),
width: None, // will be set later if needed
height: None, // will be set later if needed
link: image_data.link,
alpha_test_value: image_data.alpha_test_value,
alpha_test_value,
is_opaque: image_data.is_opaque,
is_signed: image_data.is_signed,
jpeg: image_data
.jpeg
.as_ref()
.map(ImageDataJpegJson::from_image_data_jpeg),
jpeg_name,
jpeg_internal_name,
jpeg_path,
name_dedup: None,
}
}

pub fn to_image_data(&self, width: u32, height: u32, bits: Option<ImageDataBits>) -> ImageData {
let mut jpeg = None;
if !self.is_bmp() && !self.is_link() {
let name = match &self.jpeg_name {
Some(name) => name.clone(),
None => self.name.clone(),
};
let path = match &self.jpeg_path {
Some(path) => path.clone(),
None => self.path.clone(),
};
let internal_name = self.jpeg_internal_name.clone();

jpeg = Some(ImageDataJpeg {
path,
name,
internal_name,
data: vec![], // populated later
});
}

let alpha_test_value = self
.alpha_test_value
.unwrap_or(ImageData::ALPHA_TEST_VALUE_DEFAULT);
ImageData {
name: self.name.clone(),
internal_name: self.internal_name.clone(),
path: self.path.clone(),
width,
height,
link: self.link,
alpha_test_value: self.alpha_test_value,
alpha_test_value,
is_opaque: self.is_opaque,
is_signed: self.is_signed,
jpeg: self.jpeg.as_ref().map(|jpeg| jpeg.to_image_data_jpeg()),
jpeg,
bits,
}
}
Expand Down

0 comments on commit 8fb367c

Please sign in to comment.