File size: 4,870 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
const { requestGet } = require('./api')

function newOutputImageName(format = 'png') {
    const random_id = Math.floor(Math.random() * 100000000000 + 1) // Date.now() doesn't have enough resolution to avoid duplicate
    const image_name = `output- ${Date.now()}-${random_id}.${format}`
    console.log('generated image name:', image_name)
    return image_name
}

function makeImagePath(format = 'png') {
    const image_name = newOutputImageName(format)
    const image_path = `${uniqueDocumentId}/${image_name}`
    return image_path
}
function convertImageNameToPng(image_name) {
    const image_png_name = image_name.split('.')[0] + '.png'
    return image_png_name
}
function fixNativePath(native_path) {
    const fixed_native_path = native_path.replaceAll('\\', '/')

    return fixed_native_path
}
function base64ToBase64Url(base64_image) {
    return 'data:image/png;base64,' + base64_image
}
function base64UrlToBase64(base64_url) {
    const base64_image = base64_url.replace('data:image/png;base64,', '')
    return base64_image
}
const timer = (ms) => new Promise((res) => setTimeout(res, ms)) //Todo: move this line to it's own utilit function

function scaleToClosestKeepRatio(
    original_width,
    original_height,
    min_width,
    min_height
) {
    const { finalWidthHeight } = require('../selection')
    //better naming than finalWidthHeight()
    //scale an image to the closest dimension while keeping the ratio intact
    const [final_width, final_height] = finalWidthHeight(
        original_width,
        original_height,
        min_width,
        min_height
    )
    return [final_width, final_height]
}

function mapRange(x, in_min, in_max, out_min, out_max) {
    return ((x - in_min) * (out_max - out_min)) / (in_max - in_min) + out_min
}
function scaleToRatio(
    new_value_1,
    old_value_1,
    new_value_2, //get ignored
    old_value_2,
    max_value,
    min_value
) {
    const ratio = new_value_1 / old_value_1 // 1000/500 = 2
    let final_new_value_2 = old_value_2 * ratio // 500 * 2 = 1000
    let final_new_value_1 = new_value_1
    if (final_new_value_2 > max_value) {
        ;[_, final_new_value_1] = scaleToRatio(
            max_value,
            old_value_2,
            new_value_1, //get ignored
            old_value_1,
            max_value,
            min_value
        )
        final_new_value_2 = max_value
    } else if (final_new_value_2 < min_value) {
        ;[_, final_new_value_1] = scaleToRatio(
            min_value,
            old_value_2,
            new_value_1, //get ignored
            old_value_1,
            max_value,
            min_value
        )
        final_new_value_2 = min_value
    }

    return [final_new_value_1, final_new_value_2]
}

function compareVersions(version_1, version_2) {
    //remove the first character v
    version_1 = version_1.slice(1)
    const increments_1 = version_1.split('.').map((sn) => parseInt(sn))

    version_2 = version_2.slice(1)
    const increments_2 = version_2.split('.').map((sn) => parseInt(sn))

    let b_older = false // true if version_1 is < than version_2, false if version_1 >= older
    for (let i = 0; i < increments_1.length; ++i) {
        if (increments_1[i] < increments_2[i]) {
            b_older = true
            break
        }
    }
    return b_older
}
async function requestOnlineData() {
    const { requestGet } = require('./api')
    const online_data = await requestGet(g_online_data_url)
    return online_data
}
function nearestMultiple(input, multiple) {
    //use the following formula for finding the upper value instead of the lower.
    //( ( x - 1 ) | ( m - 1 ) ) + 1
    const nearest_multiple = input - (input % multiple)
    return nearest_multiple
}

function sudoTimer(progress_text = 'Loading ControlNet...') {
    //sudo timer that will count to 100 and update the progress bar.
    //use it for controlNet since block api progress call
    let current_time = 0
    let max_time = 100
    var timerId = setInterval(countdown, 1000)

    function countdown() {
        if (current_time > max_time) {
            clearTimeout(timerId)
            // doSomething()
            // html_manip.updateProgressBarsHtml(0)
        } else {
            html_manip.updateProgressBarsHtml(current_time, progress_text)
            console.log(current_time + ' seconds remaining')
            current_time++
        }
    }
    return timerId
}
function countNewLines(string) {
    const count = (string.match(/\n/g) || []).length
    // console.log(count)
    return count
}
module.exports = {
    newOutputImageName,
    makeImagePath,
    convertImageNameToPng,
    fixNativePath,
    base64ToBase64Url,
    base64UrlToBase64,
    timer,
    scaleToClosestKeepRatio,
    scaleToRatio,
    mapRange,
    compareVersions,
    requestOnlineData,
    nearestMultiple,
    sudoTimer,
    countNewLines,
}