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
use comfyui_api::models::AsAny;
use dyn_clone::DynClone;
use stable_diffusion_api::ImgInfo;

dyn_clone::clone_trait_object!(ImageParams);

/// Trait representing an interface to the parameters used to generate an image.
pub trait ImageParams: std::fmt::Debug + AsAny + Send + Sync + DynClone {
    /// Returns the seed.
    fn seed(&self) -> Option<i64>;
    /// Returns the number of steps.
    fn steps(&self) -> Option<u32>;
    /// Returns the CFG scale.
    fn cfg(&self) -> Option<f32>;
    /// Returns the width.
    fn width(&self) -> Option<u32>;
    /// Returns the height.
    fn height(&self) -> Option<u32>;
    /// Returns the prompt.
    fn prompt(&self) -> Option<String>;
    /// Returns the negative prompt.
    fn negative_prompt(&self) -> Option<String>;
    /// Returns the denoising strength.
    fn denoising(&self) -> Option<f32>;
    /// Returns the model.
    fn model(&self) -> Option<String>;
    /// Returns the sampler.
    fn sampler(&self) -> Option<String>;
}

impl ImageParams for comfyui_api::models::Prompt {
    fn seed(&self) -> Option<i64> {
        comfyui_api::comfy::getter::SeedExt::seed(self)
            .ok()
            .copied()
    }

    fn steps(&self) -> Option<u32> {
        comfyui_api::comfy::getter::StepsExt::steps(self)
            .ok()
            .copied()
    }

    fn cfg(&self) -> Option<f32> {
        comfyui_api::comfy::getter::CfgExt::cfg(self).ok().copied()
    }

    fn width(&self) -> Option<u32> {
        comfyui_api::comfy::getter::WidthExt::width(self)
            .ok()
            .copied()
    }

    fn height(&self) -> Option<u32> {
        comfyui_api::comfy::getter::HeightExt::height(self)
            .ok()
            .copied()
    }

    fn prompt(&self) -> Option<String> {
        comfyui_api::comfy::getter::PromptExt::prompt(self)
            .ok()
            .cloned()
    }

    fn negative_prompt(&self) -> Option<String> {
        comfyui_api::comfy::getter::NegativePromptExt::negative_prompt(self)
            .ok()
            .cloned()
    }

    fn denoising(&self) -> Option<f32> {
        comfyui_api::comfy::getter::DenoiseExt::denoise(self)
            .ok()
            .copied()
    }

    fn model(&self) -> Option<String> {
        comfyui_api::comfy::getter::ModelExt::ckpt_name(self)
            .ok()
            .cloned()
    }

    fn sampler(&self) -> Option<String> {
        comfyui_api::comfy::getter::SamplerExt::sampler_name(self)
            .ok()
            .cloned()
    }
}

impl ImageParams for ImgInfo {
    fn seed(&self) -> Option<i64> {
        self.seed
    }

    fn steps(&self) -> Option<u32> {
        self.steps
    }

    fn cfg(&self) -> Option<f32> {
        self.cfg_scale.map(|c| c as f32)
    }

    fn width(&self) -> Option<u32> {
        self.width.map(|w| w as u32)
    }

    fn height(&self) -> Option<u32> {
        self.height.map(|h| h as u32)
    }

    fn prompt(&self) -> Option<String> {
        self.prompt.clone()
    }

    fn negative_prompt(&self) -> Option<String> {
        self.negative_prompt.clone()
    }

    fn denoising(&self) -> Option<f32> {
        self.denoising_strength.map(|d| d as f32)
    }

    fn model(&self) -> Option<String> {
        self.sd_model_name.clone()
    }

    fn sampler(&self) -> Option<String> {
        self.sampler_name.clone()
    }
}