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
use crate::paged_writer::PagedWriter;
use crate::Blob;
use crate::CylindricalImage;
use crate::CylindricalImageProperties;
use crate::DateTime;
use crate::Error;
use crate::Image;
use crate::ImageBlob;
use crate::ImageFormat;
use crate::PinholeImage;
use crate::PinholeImageProperties;
use crate::Projection;
use crate::Result;
use crate::SphericalImage;
use crate::SphericalImageProperties;
use crate::Transform;
use crate::VisualReferenceImage;
use crate::VisualReferenceImageProperties;
use std::io::{Read, Seek, Write};
/// Defines a new image and writes it into an E57 file.
pub struct ImageWriter<'a, T: Read + Write + Seek> {
writer: &'a mut PagedWriter<T>,
images: &'a mut Vec<Image>,
image: Image,
}
impl<'a, T: Read + Write + Seek> ImageWriter<'a, T> {
pub(crate) fn new(
writer: &'a mut PagedWriter<T>,
images: &'a mut Vec<Image>,
guid: &str,
) -> Result<Self> {
Ok(Self {
writer,
images,
image: Image {
guid: Some(guid.to_owned()),
visual_reference: None,
projection: None,
transform: None,
pointcloud_guid: None,
name: None,
description: None,
acquisition: None,
sensor_vendor: None,
sensor_model: None,
sensor_serial: None,
},
})
}
/// Set optional user-defined name for the image.
/// Not set by default.
pub fn set_name(&mut self, value: &str) {
self.image.name = Some(value.to_owned());
}
/// Set optional user-defined description for the image.
/// Not set by default.
pub fn set_description(&mut self, value: &str) {
self.image.description = Some(value.to_owned());
}
/// Set optional GUID of the point cloud that is connected to this image.
/// Not set by default.
pub fn set_pointcloud_guid(&mut self, value: &str) {
self.image.pointcloud_guid = Some(value.to_owned());
}
/// Set optional transformation to convert data from the local
/// image coordinates to the file-level coordinate system.
/// By default this is not set, meaning the image has no transformation.
pub fn set_transform(&mut self, value: Transform) {
self.image.transform = Some(value);
}
/// Set optional start date and time when the images was captured.
/// Not set by default.
pub fn set_acquisition(&mut self, value: DateTime) {
self.image.acquisition = Some(value);
}
/// Set optional name of the manufacturer for the sensor used to capture the image.
/// Not set by default.
pub fn set_sensor_vendor(&mut self, value: &str) {
self.image.sensor_vendor = Some(value.to_owned());
}
/// Set optional model name of the sensor used for capturing the image.
/// Not set by default.
pub fn set_sensor_model(&mut self, value: &str) {
self.image.sensor_model = Some(value.to_owned());
}
/// Set optional serial number of the sensor used for capturing the image.
/// Not set by default.
pub fn set_sensor_serial(&mut self, value: &str) {
self.image.sensor_serial = Some(value.to_owned());
}
/// Adds an optional visual reference image, also known as preview image.
/// See also `VisualReferenceImageProperties` struct for more details.
/// The optional PNG mask image can be used to indicate valid/invalid
/// pixels in the image, for example if the image is not rectangular.
/// The mask must have the same size as the actual image.
/// Non-zero-valued pixels mark valid pixel locations and
/// zero-valued pixels mark invalid pixels.
pub fn add_visual_reference(
&mut self,
format: ImageFormat,
image: &mut dyn Read,
properties: VisualReferenceImageProperties,
mask: Option<&mut dyn Read>,
) -> Result<()> {
let data = Blob::write(self.writer, image)?;
let blob = ImageBlob { data, format };
let mask = if let Some(mask_data) = mask {
Some(Blob::write(self.writer, mask_data)?)
} else {
None
};
self.image.visual_reference = Some(VisualReferenceImage {
properties,
mask,
blob,
});
Ok(())
}
/// Adds pinhole image data.
/// Width and height must match the actual binary PNG or JPEG image.
/// See also `PinholeImageProperties` struct for more details.
/// The optional PNG mask image can be used to indicate valid/invalid
/// pixels in the image, for example if the image is not rectangular.
/// The mask must have the same size as the actual image.
/// Non-zero-valued pixels mark valid pixel locations and
/// zero-valued pixels mark invalid pixels.
pub fn add_pinhole(
&mut self,
format: ImageFormat,
image: &mut dyn Read,
properties: PinholeImageProperties,
mask: Option<&mut dyn Read>,
) -> Result<()> {
if self.image.projection.is_some() {
Error::invalid("A projected image is already set")?
}
let data = Blob::write(self.writer, image)?;
let blob = ImageBlob { data, format };
let mask = if let Some(mask_data) = mask {
Some(Blob::write(self.writer, mask_data)?)
} else {
None
};
let rep = PinholeImage {
blob,
mask,
properties,
};
self.image.projection = Some(Projection::Pinhole(rep));
Ok(())
}
/// Adds spherical image data.
/// See also `SphericalImageProperties` struct for more details.
/// The optional PNG mask image can be used to indicate valid/invalid
/// pixels in the image, for example if the image is not rectangular.
/// The mask must have the same size as the actual image.
/// Non-zero-valued pixels mark valid pixel locations and
/// zero-valued pixels mark invalid pixels.
pub fn add_spherical(
&mut self,
format: ImageFormat,
image: &mut dyn Read,
properties: SphericalImageProperties,
mask: Option<&mut dyn Read>,
) -> Result<()> {
if self.image.projection.is_some() {
Error::invalid("A projected image is already set")?
}
let data = Blob::write(self.writer, image)?;
let blob = ImageBlob { data, format };
let mask = if let Some(mask_data) = mask {
Some(Blob::write(self.writer, mask_data)?)
} else {
None
};
let rep = SphericalImage {
blob,
mask,
properties,
};
self.image.projection = Some(Projection::Spherical(rep));
Ok(())
}
/// Adds cylindrical image data.
/// See also `CylindricalImageProperties` struct for more details.
/// The optional PNG mask image can be used to indicate valid/invalid
/// pixels in the image, for example if the image is not rectangular.
/// The mask must have the same size as the actual image.
/// Non-zero-valued pixels mark valid pixel locations and
/// zero-valued pixels mark invalid pixels.
pub fn add_cylindrical(
&mut self,
format: ImageFormat,
image_data: &mut dyn Read,
properties: CylindricalImageProperties,
mask_data: Option<&mut dyn Read>,
) -> Result<()> {
if self.image.projection.is_some() {
Error::invalid("A projected image is already set")?
}
let data = Blob::write(self.writer, image_data)?;
let blob = ImageBlob { data, format };
let mask = if let Some(mask_data) = mask_data {
Some(Blob::write(self.writer, mask_data)?)
} else {
None
};
let rep = CylindricalImage {
blob,
mask,
properties,
};
self.image.projection = Some(Projection::Cylindrical(rep));
Ok(())
}
/// Must be called after image is complete to finishing adding the new image.
/// Binary image and mask data is directly written into the E57 file earlier,
/// but the XML metadata will be only added to the E57 if you call finalize.
/// Skipping the finalize call after you added image or mask data means
/// that the data will be part of the E57 file but is never referenced by
/// its XML header section.
pub fn finalize(&mut self) -> Result<()> {
if self.image.visual_reference.is_none() && self.image.projection.is_none() {
Error::invalid("Image must have a visual reference or a projection")?
}
// Add metadata for XML generation later, when the file is completed.
self.images.push(self.image.clone());
Ok(())
}
}