This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
generated from EmbarkStudios/opensource-template
-
Notifications
You must be signed in to change notification settings - Fork 84
/
lib.rs
800 lines (712 loc) · 26.1 KB
/
lib.rs
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
//! `texture-synthesis` is a light API for Multiresolution Stochastic Texture Synthesis,
//! a non-parametric example-based algorithm for image generation.
//!
//! First, you build a `Session` via a `SessionBuilder`, which follows the builder pattern. Calling
//! `build` on the SessionBuilder loads all of the input images and checks for various errors.
//!
//! `Session` has a `run()` method that takes all of the parameters and inputs added in the session
//! builder to generated an image, which is returned as a `GeneratedImage`.
//!
//! You can save, stream, or inspect the image from `GeneratedImage`.
//!
//! ## Features
//!
//! 1. Single example generation
//! 2. Multi example generation
//! 3. Guided synthesis
//! 4. Style transfer
//! 5. Inpainting
//! 6. Tiling textures
//!
//! Please, refer to the examples folder in the [repository](https://github.com/EmbarkStudios/texture-synthesis) for the features usage examples.
//!
//! ## Usage
//! Session follows a "builder pattern" for defining parameters, meaning you chain functions together.
//!
//! ```no_run
//! // Create a new session with default parameters
//! let session = texture_synthesis::Session::builder()
//! // Set some parameters
//! .seed(10)
//! .nearest_neighbors(20)
//! // Specify example images
//! .add_example(&"imgs/1.jpg")
//! // Build the session
//! .build().expect("failed to build session");
//!
//! // Generate a new image
//! let generated_img = session.run(None);
//!
//! // Save the generated image to disk
//! generated_img.save("my_generated_img.jpg").expect("failed to save generated image");
//! ```
mod img_pyramid;
use img_pyramid::*;
mod utils;
use utils::*;
mod multires_stochastic_texture_synthesis;
use multires_stochastic_texture_synthesis::*;
use std::path::Path;
pub use image;
pub use utils::ImageSource;
pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
struct Parameters {
tiling_mode: bool,
nearest_neighbors: u32,
random_sample_locations: u64,
cauchy_dispersion: f32,
backtrack_percent: f32,
backtrack_stages: u32,
resize_input: Option<(u32, u32)>,
output_size: (u32, u32),
guide_alpha: f32,
random_resolve: Option<u64>,
seed: u64,
}
impl Default for Parameters {
fn default() -> Self {
Self {
tiling_mode: false,
nearest_neighbors: 50,
random_sample_locations: 50,
cauchy_dispersion: 1.0,
backtrack_percent: 0.5,
backtrack_stages: 5,
resize_input: None,
output_size: (500, 500),
guide_alpha: 0.8,
random_resolve: None,
seed: 0,
}
}
}
impl Parameters {
fn to_generator_params(&self) -> GeneratorParams {
GeneratorParams {
nearest_neighbors: self.nearest_neighbors,
random_sample_locations: self.random_sample_locations,
cauchy_dispersion: self.cauchy_dispersion,
p: self.backtrack_percent,
p_stages: self.backtrack_stages as i32,
seed: self.seed,
alpha: self.guide_alpha,
}
}
}
/// An image generated by a `Session::run()`
pub struct GeneratedImage {
inner: multires_stochastic_texture_synthesis::Generator,
}
impl GeneratedImage {
/// Saves the generated image to the specified path
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
let path = path.as_ref();
if let Some(parent_path) = path.parent() {
std::fs::create_dir_all(&parent_path)?;
}
self.inner.color_map.save(&path)?;
Ok(())
}
/// Writes the generated image to the specified stream
pub fn write<W: std::io::Write>(
self,
writer: &mut W,
fmt: image::ImageOutputFormat,
) -> Result<(), Error> {
let dyn_img = self.into_image();
Ok(dyn_img.write_to(writer, fmt)?)
}
/// Saves debug information such as copied patches ids, map ids (if you have multi example generation)
/// and a map indicating generated pixels the generator was "uncertain" of.
pub fn save_debug<P: AsRef<Path>>(&self, dir: P) -> Result<(), Error> {
let dir = dir.as_ref();
std::fs::create_dir_all(&dir)?;
self.inner
.get_uncertainty_map()
.save(&dir.join("uncertainty.png"))?;
let id_maps = self.inner.get_id_maps();
id_maps[0].save(&dir.join("patch_id.png"))?;
id_maps[1].save(&dir.join("map_id.png"))?;
Ok(())
}
/// Returns the generated output image
pub fn into_image(self) -> image::DynamicImage {
image::DynamicImage::ImageRgba8(self.inner.color_map)
}
}
impl AsRef<image::RgbaImage> for GeneratedImage {
fn as_ref(&self) -> &image::RgbaImage {
&self.inner.color_map
}
}
/// Method used for sampling an example image.
pub enum SampleMethod<'a> {
/// All pixels in the example image can be sampled.
All,
/// No pixels in the example image will be sampled.
Ignore,
/// Pixels are selectively sampled based on an image.
Image(ImageSource<'a>),
}
impl<'a> SampleMethod<'a> {
fn is_ignore(&self) -> bool {
match self {
Self::Ignore => true,
_ => false,
}
}
}
impl<'a, S> From<&'a S> for SampleMethod<'a>
where
S: AsRef<Path> + 'a,
{
fn from(path: &'a S) -> Self {
Self::Image(ImageSource::Path(path.as_ref()))
}
}
/// Internal sample method
pub enum SamplingMethod {
All,
Ignore,
Image(image::RgbaImage),
}
impl SamplingMethod {
fn is_ignore(&self) -> bool {
match self {
Self::Ignore => true,
_ => false,
}
}
}
/// A builder for an `Example`
pub struct ExampleBuilder<'a> {
img: ImageSource<'a>,
guide: Option<ImageSource<'a>>,
sample_method: SampleMethod<'a>,
}
impl<'a> ExampleBuilder<'a> {
/// Creates a new example builder from the specified image source
pub fn new<I: Into<ImageSource<'a>>>(img: I) -> Self {
Self {
img: img.into(),
guide: None,
sample_method: SampleMethod::All,
}
}
/// Use a guide map that describe a 'FROM' transformation.
///
/// Note: If any one example has a guide, then they **all** must have
/// a guide, otherwise a session will not be created.
pub fn with_guide<G: Into<ImageSource<'a>>>(mut self, guide: G) -> Self {
self.guide = Some(guide.into());
self
}
/// Specify how the example image is sampled during texture generation.
///
/// By default, all pixels in the example can be sampled.
pub fn set_sample_method<M: Into<SampleMethod<'a>>>(mut self, method: M) -> Self {
self.sample_method = method.into();
self
}
}
impl<'a> Into<Example<'a>> for ExampleBuilder<'a> {
fn into(self) -> Example<'a> {
Example {
img: self.img,
guide: self.guide,
sample_method: self.sample_method,
}
}
}
/// An example to be used in texture generation
pub struct Example<'a> {
img: ImageSource<'a>,
guide: Option<ImageSource<'a>>,
sample_method: SampleMethod<'a>,
}
impl<'a> Example<'a> {
/// Creates a new example builder from the specified image source
pub fn builder<I: Into<ImageSource<'a>>>(img: I) -> ExampleBuilder<'a> {
ExampleBuilder::new(img)
}
/// Creates a new example input from the specified image source
pub fn new<I: Into<ImageSource<'a>>>(img: I) -> Self {
Self {
img: img.into(),
guide: None,
sample_method: SampleMethod::All,
}
}
/// Use a guide map that describe a 'FROM' transformation.
///
/// Note: If any one example has a guide, then they **all** must have
/// a guide, otherwise a session will not be created.
pub fn with_guide<G: Into<ImageSource<'a>>>(&mut self, guide: G) -> &mut Self {
self.guide = Some(guide.into());
self
}
/// Specify how the example image is sampled during texture generation.
///
/// By default, all pixels in the example can be sampled.
pub fn set_sample_method<M: Into<SampleMethod<'a>>>(&mut self, method: M) -> &mut Self {
self.sample_method = method.into();
self
}
fn resolve(
self,
backtracks: u32,
resize: Option<(u32, u32)>,
target_guide: &Option<ImagePyramid>,
) -> Result<ResolvedExample, Error> {
let image = ImagePyramid::new(load_image(self.img, resize)?, Some(backtracks));
let guide = match target_guide {
Some(tg) => {
Some(match self.guide {
Some(exguide) => {
let exguide = load_image(exguide, resize)?;
ImagePyramid::new(exguide, Some(backtracks))
}
None => {
// if we do not have an example guide, create it as a b/w maps of the example
let mut gm = transform_to_guide_map(image.bottom().clone(), resize, 2.0);
match_histograms(&mut gm, tg.bottom());
ImagePyramid::new(gm, Some(backtracks))
}
})
}
None => None,
};
let method = match self.sample_method {
SampleMethod::All => SamplingMethod::All,
SampleMethod::Ignore => SamplingMethod::Ignore,
SampleMethod::Image(src) => {
let img = load_image(src, resize)?;
SamplingMethod::Image(img)
}
};
Ok(ResolvedExample {
image,
guide,
method,
})
}
}
impl<'a, S> From<&'a S> for Example<'a>
where
S: AsRef<Path> + 'a,
{
fn from(path: &'a S) -> Self {
Example::new(path)
}
}
/// Builds a session by setting parameters and adding input images, calling
/// `build` will check all of the provided inputs to verify that texture
/// synthesis will provide valid output
#[derive(Default)]
pub struct SessionBuilder<'a> {
examples: Vec<Example<'a>>,
target_guide: Option<ImageSource<'a>>,
inpaint_mask: Option<(ImageSource<'a>, usize)>,
params: Parameters,
}
impl<'a> SessionBuilder<'a> {
/// Creates a new `SessionBuilder`, can also be created via `Session::builder()`
pub fn new() -> Self {
Self::default()
}
/// Adds an `Example` from which a generator will synthesize a new image.
///
/// See [examples/01_single_example_synthesis](https://github.com/EmbarkStudios/texture-synthesis/tree/master/lib/examples/01_single_example_synthesis.rs)
///
/// # Examples
///
/// ```no_run
/// let tex_synth = texture_synthesis::Session::builder()
/// .add_example(&"imgs/1.jpg")
/// .build().expect("failed to build session");
/// ```
pub fn add_example<E: Into<Example<'a>>>(mut self, example: E) -> Self {
self.examples.push(example.into());
self
}
/// Adds Examples from which a generator will synthesize a new image.
///
/// See [examples/02_multi_example_synthesis](https://github.com/EmbarkStudios/texture-synthesis/tree/master/lib/examples/02_multi_example_synthesis.rs)
///
/// # Examples
///
/// ```no_run
/// let tex_synth = texture_synthesis::Session::builder()
/// .add_examples(&[&"imgs/1.jpg", &"imgs/2.jpg"])
/// .build().expect("failed to build session");
/// ```
pub fn add_examples<E: Into<Example<'a>>, I: IntoIterator<Item = E>>(
mut self,
examples: I,
) -> Self {
self.examples.extend(examples.into_iter().map(|e| e.into()));
self
}
/// Inpaints an example.
///
/// To prevent sampling from the example, you can specify `SamplingMethod::Ignore` with `Example::set_sample_method`.
///
/// See [examples/05_inpaint](https://github.com/EmbarkStudios/texture-synthesis/tree/master/lib/examples/05_inpaint.rs)
///
/// # Examples
///
/// ```no_run
/// let tex_synth = texture_synthesis::Session::builder()
/// .add_examples(&[&"imgs/1.jpg", &"imgs/3.jpg"])
/// .inpaint_example(
/// &"masks/inpaint.jpg",
/// // This will prevent sampling from the imgs/2.jpg, note that
/// // we *MUST* provide at least one example to source from!
/// texture_synthesis::Example::builder(&"imgs/2.jpg")
/// .set_sample_method(texture_synthesis::SampleMethod::Ignore)
/// )
/// .build().expect("failed to build session");
/// ```
pub fn inpaint_example<I: Into<ImageSource<'a>>, E: Into<Example<'a>>>(
mut self,
inpaint_mask: I,
example: E,
) -> Self {
self.inpaint_mask = Some((inpaint_mask.into(), self.examples.len()));
self.examples.push(example.into());
self
}
/// Loads a target guide map.
///
/// If no `Example` guide maps are provided, this will produce a style transfer effect,
/// where the Examples are styles and the target guide is content.
///
/// See [examples/03_guided_synthesis](https://github.com/EmbarkStudios/texture-synthesis/tree/master/lib/examples/03_guided_synthesis.rs),
/// or [examples/04_style_transfer](https://github.com/EmbarkStudios/texture-synthesis/tree/master/lib/examples/04_style_transfer.rs),
pub fn load_target_guide<I: Into<ImageSource<'a>>>(mut self, guide: I) -> Self {
self.target_guide = Some(guide.into());
self
}
/// Overwrite incoming images sizes
pub fn resize_input(mut self, w: u32, h: u32) -> Self {
self.params.resize_input = Some((w, h));
self
}
/// Changes pseudo-deterministic seed.
/// Global structures will stay same, if same seed is provided, but smaller details may change due to undeterministic nature of multithreading.
pub fn seed(mut self, value: u64) -> Self {
self.params.seed = value;
self
}
/// Makes the generator output tiling image.
/// Default: false.
pub fn tiling_mode(mut self, is_tiling: bool) -> Self {
self.params.tiling_mode = is_tiling;
self
}
/// How many neighboring pixels each pixel is aware of during the generation (bigger number -> more global structures are captured).
/// Default: 50
pub fn nearest_neighbors(mut self, count: u32) -> Self {
self.params.nearest_neighbors = count;
self
}
/// How many random locations will be considered during a pixel resolution apart from its immediate neighbors.
/// If unsure, keep same as nearest neighbors.
/// Default: 50
pub fn random_sample_locations(mut self, count: u64) -> Self {
self.params.random_sample_locations = count;
self
}
/// Make first X pixels to be randomly resolved and prevent them from being overwritten.
/// Can be an enforcing factor of remixing multiple images together.
pub fn random_init(mut self, count: u64) -> Self {
self.params.random_resolve = Some(count);
self
}
/// The distribution dispersion used for picking best candidate (controls the distribution 'tail flatness').
/// Values close to 0.0 will produce 'harsh' borders between generated 'chunks'. Values closer to 1.0 will produce a smoother gradient on those borders.
/// For futher reading, check out P.Harrison's "Image Texture Tools".
/// Default: 1.0
pub fn cauchy_dispersion(mut self, value: f32) -> Self {
self.params.cauchy_dispersion = value;
self
}
/// Controls the trade-off between guide and example map.
/// If doing style transfer, set to about 0.8-0.6 to allow for more global structures of the style.
/// If you'd like the guide maps to be considered through all generation stages, set to 1.0 (which would prevent guide maps weight "decay" during the score calculation).
/// Default: 0.8
pub fn guide_alpha(mut self, value: f32) -> Self {
self.params.guide_alpha = value;
self
}
/// The percentage of pixels to be backtracked during each p_stage. Range (0,1).
/// Default: 0.5
pub fn backtrack_percent(mut self, value: f32) -> Self {
self.params.backtrack_percent = value;
self
}
/// Controls the number of backtracking stages. Backtracking prevents 'garbage' generation.
/// Right now, the depth of image pyramid for multiresolution synthesis
/// depends on this parameter as well.
/// Default: 5
pub fn backtrack_stages(mut self, stages: u32) -> Self {
self.params.backtrack_stages = stages;
self
}
/// Specify size of the generated image.
/// Default: 500x500
pub fn output_size(mut self, w: u32, h: u32) -> Self {
self.params.output_size = (w, h);
self
}
/// Creates a `Session`, or returns an error if invalid parameters or input
/// images were specified.
pub fn build(self) -> Result<Session, Error> {
self.check_parameters_validity()?;
self.check_images_validity()?;
let target_guide = match self.target_guide {
Some(tg) => {
let tg_img = load_image(tg, Some(self.params.output_size))?;
let num_guides = self.examples.iter().filter(|ex| ex.guide.is_some()).count();
let tg_img = if num_guides == 0 {
transform_to_guide_map(tg_img, None, 2.0)
} else {
tg_img
};
Some(ImagePyramid::new(
tg_img,
Some(self.params.backtrack_stages as u32),
))
}
None => None,
};
struct InpaintExample {
inpaint_mask: image::RgbaImage,
color_map: image::RgbaImage,
example_index: usize,
}
let inpaint = match self.inpaint_mask {
Some((src, ind)) => {
let inpaint_mask = load_image(src, Some(self.params.output_size))?;
let color_map = load_image(
self.examples[ind].img.clone(),
Some(self.params.output_size),
)?;
Some(InpaintExample {
inpaint_mask,
color_map,
example_index: ind,
})
}
None => None,
};
let example_len = self.examples.len();
let mut examples = Vec::with_capacity(example_len);
let mut guides = if target_guide.is_some() {
Vec::with_capacity(example_len)
} else {
Vec::new()
};
let mut methods = Vec::with_capacity(example_len);
for example in self.examples {
let resolved = example.resolve(
self.params.backtrack_stages,
self.params.resize_input,
&target_guide,
)?;
examples.push(resolved.image);
if let Some(guide) = resolved.guide {
guides.push(guide);
}
methods.push(resolved.method);
}
// Initialize generator based on availability of an inpaint_mask.
let generator = match inpaint {
None => multires_stochastic_texture_synthesis::Generator::new(self.params.output_size),
Some(inpaint) => multires_stochastic_texture_synthesis::Generator::new_from_inpaint(
self.params.output_size,
inpaint.inpaint_mask,
inpaint.color_map,
inpaint.example_index,
),
};
let session = Session {
examples,
guides: target_guide.map(|tg| GuidesPyramidStruct {
target_guide: tg,
example_guides: guides,
}),
sampling_methods: methods,
params: self.params,
generator,
};
Ok(session)
}
fn check_parameters_validity(&self) -> Result<(), Error> {
if self.params.cauchy_dispersion < 0.0 || self.params.cauchy_dispersion > 1.0 {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Invalid parameter range: cauchy dispersion. Make sure it is within 0.0-1.0 range",
)));
}
if self.params.backtrack_percent < 0.0 || self.params.backtrack_percent > 1.0 {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Invalid parameter range: backtrack percent. Make sure it is within 0.0-1.0 range",
)));
}
if self.params.guide_alpha < 0.0 || self.params.guide_alpha > 1.0 {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Invalid parameter range: guide alpha. Make sure it is within 0.0-1.0 range",
)));
}
if self.inpaint_mask.is_some() {
if let Some(resize_input) = self.params.resize_input {
if resize_input.0 != self.params.output_size.0
|| resize_input.1 != self.params.output_size.1
{
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Input and output sizes dont match. Make sure resize_input = output_size if using inpaint",
)));
}
} else {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Input and output sizes dont match. Make sure resize_input = output_size if using inpaint",
)));
}
}
Ok(())
}
fn check_images_validity(&self) -> Result<(), Error> {
if self.examples.is_empty() {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Missing input: example image(s)",
)));
}
let num_guides = self.examples.iter().filter(|ex| ex.guide.is_some()).count();
if num_guides != 0 && self.examples.len() != num_guides {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"Mismatch of maps: {} example guide(s) vs {} example(s)",
num_guides,
self.examples.len()
),
)));
}
// We must have at least one example image to source pixels from
let input_count = self
.examples
.iter()
.filter(|ex| !ex.sample_method.is_ignore())
.count();
if input_count == 0 {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"At least 1 example image must be sampled to generate an output image",
)));
}
Ok(())
}
}
struct ResolvedExample {
image: ImagePyramid,
guide: Option<ImagePyramid>,
method: SamplingMethod,
}
/// Texture synthesis session.
///
/// Calling `run()` will generate a new image and return it, consuming the session in the
/// process. You can provide a `GeneratorProgress` implementation to periodically get
/// updates with the currently generated image and the number of pixels that
/// have been resolved both in the current stage and globally
///
/// # Example
/// ```no_run
/// let tex_synth = texture_synthesis::Session::builder()
/// .seed(10)
/// .tiling_mode(true)
/// .add_example(&"imgs/1.jpg")
/// .build().expect("failed to build session");
///
/// let generated_img = tex_synth.run(None);
/// generated_img.save("my_generated_img.jpg").expect("failed to save image");
/// ```
pub struct Session {
examples: Vec<ImagePyramid>,
guides: Option<GuidesPyramidStruct>,
sampling_methods: Vec<SamplingMethod>,
generator: multires_stochastic_texture_synthesis::Generator,
params: Parameters,
}
impl Session {
/// Creates a new session with default parameters.
pub fn builder<'a>() -> SessionBuilder<'a> {
SessionBuilder::default()
}
/// Runs the generator and outputs a generated image.
/// Now, only runs Multiresolution Stochastic Texture Synthesis.
/// Might be interesting to include more algorithms in the future.
pub fn run(mut self, progress: Option<Box<dyn GeneratorProgress>>) -> GeneratedImage {
// random resolve
// TODO: Instead of consuming the generator, we could instead make the
// seed and random_resolve parameters, so that you could rerun the
// generator with the same inputs
if let Some(count) = self.params.random_resolve {
let lvl = self.examples[0].pyramid.len();
let imgs: Vec<image::RgbaImage> = self
.examples
.iter()
.map(|a| a.pyramid[lvl - 1].clone()) //take the blurriest image
.collect();
let imgs_ref = imgs.iter().collect::<Vec<_>>();
self.generator
.resolve_random_batch(count as usize, &imgs_ref, self.params.seed);
}
// run generator
self.generator.main_resolve_loop(
&self.params.to_generator_params(),
&self.examples,
progress,
&self.guides,
&self.sampling_methods,
self.params.tiling_mode,
);
GeneratedImage {
inner: self.generator,
}
}
}
/// Helper struct for passing progress information to external callers
pub struct ProgressStat {
/// The current amount of work that has been done
pub current: usize,
/// The total amount of work to do
pub total: usize,
}
/// The current state of the image generator
pub struct ProgressUpdate<'a> {
/// The currenty resolved image
pub image: &'a image::RgbaImage,
/// The total progress for the final image
pub total: ProgressStat,
/// The progress for the current stage
pub stage: ProgressStat,
}
/// Allows the generator to update external callers with the current
/// progress of the image synthesis
pub trait GeneratorProgress {
fn update(&mut self, info: ProgressUpdate<'_>);
}
impl<G> GeneratorProgress for G
where
G: FnMut(ProgressUpdate<'_>) + Send,
{
fn update(&mut self, info: ProgressUpdate<'_>) {
self(info)
}
}