File size: 9,490 Bytes
34097e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { ReactEventHandler } from 'react'
import ReactDOM from 'react-dom/client'

import { action, makeAutoObservable, reaction, toJS } from 'mobx'
import { Provider, inject, observer } from 'mobx-react'

import { SliderType, SpMenu, SpSliderWithLabel } from './elements'

import * as sdapi from '../../sdapi_py_re'

import { ui_config } from './config'

export let script_name: string = 'Ultimate SD upscale'
export enum ScriptMode {
    Txt2Img = 'txt2img',
    Img2Img = 'img2img',
    Inpaint = 'inpaint',
    Outpaint = 'outpaint',
}

export let script_mode = [
    ScriptMode.Img2Img,
    ScriptMode.Inpaint,
    ScriptMode.Outpaint,
]
interface UltimateSDUpscalerData {
    _: string
    tile_width: number
    tile_height: number
    mask_blur: number
    padding: number
    seams_fix_width: number
    seams_fix_denoise: number
    seams_fix_padding: number
    upscaler_index: number
    save_upscaled_image: boolean
    redraw_mode: number
    save_seams_fix_image: boolean
    seams_fix_mask_blur: number
    seams_fix_type: number
    target_size_type: number
    custom_width: number
    custom_height: number
    custom_scale: number
}
export const script_args_ordered = [
    '_',
    'tile_width',
    'tile_height',
    'mask_blur',
    'padding',
    'seams_fix_width',
    'seams_fix_denoise',
    'seams_fix_padding',
    'upscaler_index',
    'save_upscaled_image',
    'redraw_mode',
    'save_seams_fix_image',
    'seams_fix_mask_blur',
    'seams_fix_type',
    'target_size_type',
    'custom_width',
    'custom_height',
    'custom_scale',
]

class UltimateSDUpscalerStore {
    data: UltimateSDUpscalerData
    // test_value: number = 10
    // test_value_2: number = 2
    test_value: number
    test_value_2: number
    is_active: boolean
    constructor(data: UltimateSDUpscalerData) {
        this.data = data
        this.test_value = 10
        this.test_value_2 = 2
        this.is_active = false
        makeAutoObservable(this)

        // reaction(
        //     () => [this.test_value],
        //     () => {
        //         this.test_value_2 = this.test_value * 2
        //         console.log('reaction to test_value change:', this.test_value)
        //         console.log('this.test_value_2:', this.test_value_2)
        //     }
        // )
    }
    setIsActive(b_value: boolean) {
        this.is_active = b_value
    }
    setTestValue(new_value: number) {
        this.test_value = new_value
        console.log('setTestValue: new_value ', new_value)
        console.log('setTestValue: this.test_value: ', this.test_value)
    }

    updateProperty(key: keyof UltimateSDUpscalerData, value: any) {
        ;(this.data as any)[key] = value
    }

    toJsFunc() {
        return toJS(this)
    }
}

const configValues = Object.entries(ui_config).reduce(
    (acc, [key, value]) => ({ ...acc, [key]: value.value }),
    {}
)
const default_values: any = {
    _: '',
    ...configValues,
}
export const ultimate_sd_upscaler_store = new UltimateSDUpscalerStore(
    default_values
)

@observer
export class UltimateSDUpscalerForm extends React.Component<{
    store: UltimateSDUpscalerStore
}> {
    // slider1Ref = React.createRef<SpSliderWithLabel>()
    // slider2Ref = React.createRef<SpSliderWithLabel>()
    state = {
        items: ['Item 1', 'Item 2', 'Item 3'],
        sd_upscalers: [],
    }
    componentDidMount(): void {
        this.getUpscalers()
    }
    handleUpdateItems = () => {
        this.setState({
            items: ['New Item 1', 'New Item 2', 'New Item 3'],
        })
    }
    handleSlider1ValueChange = (newValue: any) => {
        // this.props.store.setTestValue(newValue)
        this.props.store.test_value = newValue

        // this.props.store.
        console.log('store.test_value: ', this.props.store.test_value)
        console.log('newValue: ', newValue)
    }

    handleSlider2ValueChange = (newValue: any) => {
        // scriptFormStore.setSlider2Value(newValue)
    }
    handleSliderChange = (key: any, newValue: any) => {
        this.props.store.updateProperty(key, newValue)
    }
    handleMenuChange = (key: any, new_index_value_pair: any) => {
        let config = ui_config[key as keyof typeof ui_config] as any
        if ('type' in config) {
            let value =
                config.type === 'index'
                    ? new_index_value_pair['index']
                    : new_index_value_pair['item']
            this.props.store.updateProperty(key, value)
        }
    }

    async getUpscalers() {
        const sd_upscalers_json = await sdapi.requestGetUpscalers()
        const sd_upscalers = sd_upscalers_json.map(
            (upscaler: any) => upscaler.name
        )
        this.setState({ sd_upscalers: sd_upscalers })
        return sd_upscalers
    }

    render() {
        const ids = [
            'tile_width',
            'tile_height',
            'mask_blur',
            'padding',
        ] as const
        // let config = ui_config[ids as keyof typeof ui_config] as any
        const group_1_sliders = ids.map((id) => (
            <SpSliderWithLabel
                key={id}
                id={id}
                show-value={false}
                steps={ui_config[id].step}
                out_min={ui_config[id].minimum}
                out_max={ui_config[id].maximum}
                output_value={this.props.store.data[id]}
                title={ui_config[id].label}
                label={ui_config[id].label}
                onSliderChange={this.handleSliderChange}
            />
        ))
        const seamfix_ids = [
            'seams_fix_denoise',
            'seams_fix_width',
            'seams_fix_mask_blur',
            'seams_fix_padding',
        ] as const
        const seamfix_sliders = seamfix_ids.map((id) => (
            <SpSliderWithLabel
                key={id}
                id={id}
                show-value={false}
                steps={ui_config[id].step}
                out_min={ui_config[id].minimum}
                out_max={ui_config[id].maximum}
                output_value={this.props.store.data[id]}
                title={ui_config[id].label}
                label={ui_config[id].label}
                onSliderChange={this.handleSliderChange}
                slider_type={
                    Number.isInteger(ui_config[id].step)
                        ? SliderType.Integer
                        : SliderType.Float
                }
            />
        ))
        return (
            <div>
                <SpMenu
                    title="Stable Diffusion Upscalers"
                    items={this.state.sd_upscalers}
                    label_item="Select Upscaler"
                    id={'upscaler_index'}
                    onChange={this.handleMenuChange}
                    selected_index={this.props.store.data.upscaler_index}
                />
                <SpMenu
                    title={ui_config.target_size_type.label}
                    id={'target_size_type'}
                    items={ui_config.target_size_type.choices}
                    label_item={'Select ' + ui_config.target_size_type.label}
                    onChange={this.handleMenuChange}
                    selected_index={this.props.store.data.target_size_type}
                />
                <SpSliderWithLabel
                    label={ui_config.custom_scale.label}
                    output_value={this.props.store.data.custom_scale}
                    id={'custom_scale'}
                    out_min={ui_config.custom_scale.minimum}
                    out_max={ui_config.custom_scale.maximum}
                    onSliderChange={this.handleSliderChange}
                    steps={0.01}
                    slider_type={SliderType.Float}
                />

                <sp-checkbox
                    checked={
                        this.props.store.data.save_upscaled_image
                            ? true
                            : undefined
                    }
                    onClick={(event: React.ChangeEvent<HTMLInputElement>) => {
                        this.props.store.updateProperty(
                            'save_upscaled_image',
                            event.target.checked
                        )
                    }}
                >
                    {ui_config.save_upscaled_image.label}
                </sp-checkbox>
                <sp-checkbox
                    checked={
                        this.props.store.data.save_seams_fix_image
                            ? true
                            : undefined
                    }
                    onClick={(event: React.ChangeEvent<HTMLInputElement>) => {
                        this.props.store.updateProperty(
                            'save_seams_fix_image',
                            event.target.checked
                        )
                    }}
                >
                    {ui_config.save_seams_fix_image.label}
                </sp-checkbox>
                {group_1_sliders}
                <SpMenu
                    title={'Seams Fix Type'}
                    id={'seams_fix_type'}
                    items={ui_config.seams_fix_type.choices}
                    label_item="Select Seams Fix Type"
                    onChange={this.handleMenuChange}
                    selected_index={this.props.store.data.seams_fix_type}
                />
                {seamfix_sliders}
            </div>
        )
    }
}