daasd commited on
Commit
db7a228
·
1 Parent(s): d24328a

Upload PNG_info_web.js

Browse files
Files changed (1) hide show
  1. PNG_info_web.js +370 -0
PNG_info_web.js ADDED
@@ -0,0 +1,370 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //crc32与extrachunks与decode
2
+ /* crc32.js (C) 2014-2015 SheetJS -- http://sheetjs.com */
3
+ /* vim: set ts=2: */
4
+ var CRC32;
5
+ (function (factory) {
6
+ if(typeof DO_NOT_EXPORT_CRC === 'undefined') {
7
+ if('object' === typeof exports) {
8
+ factory(exports);
9
+ } else if ('function' === typeof define && define.amd) {
10
+ define(function () {
11
+ var module = {};
12
+ factory(module);
13
+ return module;
14
+ });
15
+ } else {
16
+ factory(CRC32 = {});
17
+ }
18
+ } else {
19
+ factory(CRC32 = {});
20
+ }
21
+ }(function(CRC32) {
22
+ CRC32.version = '0.3.0';
23
+ /* see perf/crc32table.js */
24
+ function signed_crc_table() {
25
+ var c = 0, table = new Array(256);
26
+
27
+ for(var n =0; n != 256; ++n){
28
+ c = n;
29
+ c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
30
+ c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
31
+ c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
32
+ c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
33
+ c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
34
+ c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
35
+ c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
36
+ c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
37
+ table[n] = c;
38
+ }
39
+
40
+ return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
41
+ }
42
+
43
+ var table = signed_crc_table();
44
+ /* charCodeAt is the best approach for binary strings */
45
+ var use_buffer = typeof Buffer !== 'undefined';
46
+ function crc32_bstr(bstr) {
47
+ if(bstr.length > 32768) if(use_buffer) return crc32_buf_8(new Buffer(bstr));
48
+ var crc = -1, L = bstr.length - 1;
49
+ for(var i = 0; i < L;) {
50
+ crc = table[(crc ^ bstr.charCodeAt(i++)) & 0xFF] ^ (crc >>> 8);
51
+ crc = table[(crc ^ bstr.charCodeAt(i++)) & 0xFF] ^ (crc >>> 8);
52
+ }
53
+ if(i === L) crc = (crc >>> 8) ^ table[(crc ^ bstr.charCodeAt(i)) & 0xFF];
54
+ return crc ^ -1;
55
+ }
56
+
57
+ function crc32_buf(buf) {
58
+ if(buf.length > 10000) return crc32_buf_8(buf);
59
+ for(var crc = -1, i = 0, L=buf.length-3; i < L;) {
60
+ crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
61
+ crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
62
+ crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
63
+ crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
64
+ }
65
+ while(i < L+3) crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
66
+ return crc ^ -1;
67
+ }
68
+
69
+ function crc32_buf_8(buf) {
70
+ for(var crc = -1, i = 0, L=buf.length-7; i < L;) {
71
+ crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
72
+ crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
73
+ crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
74
+ crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
75
+ crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
76
+ crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
77
+ crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
78
+ crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
79
+ }
80
+ while(i < L+7) crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
81
+ return crc ^ -1;
82
+ }
83
+
84
+ /* much much faster to intertwine utf8 and crc */
85
+ function crc32_str(str) {
86
+ for(var crc = -1, i = 0, L=str.length, c, d; i < L;) {
87
+ c = str.charCodeAt(i++);
88
+ if(c < 0x80) {
89
+ crc = (crc >>> 8) ^ table[(crc ^ c) & 0xFF];
90
+ } else if(c < 0x800) {
91
+ crc = (crc >>> 8) ^ table[(crc ^ (192|((c>>6)&31))) & 0xFF];
92
+ crc = (crc >>> 8) ^ table[(crc ^ (128|(c&63))) & 0xFF];
93
+ } else if(c >= 0xD800 && c < 0xE000) {
94
+ c = (c&1023)+64; d = str.charCodeAt(i++) & 1023;
95
+ crc = (crc >>> 8) ^ table[(crc ^ (240|((c>>8)&7))) & 0xFF];
96
+ crc = (crc >>> 8) ^ table[(crc ^ (128|((c>>2)&63))) & 0xFF];
97
+ crc = (crc >>> 8) ^ table[(crc ^ (128|((d>>6)&15)|(c&3))) & 0xFF];
98
+ crc = (crc >>> 8) ^ table[(crc ^ (128|(d&63))) & 0xFF];
99
+ } else {
100
+ crc = (crc >>> 8) ^ table[(crc ^ (224|((c>>12)&15))) & 0xFF];
101
+ crc = (crc >>> 8) ^ table[(crc ^ (128|((c>>6)&63))) & 0xFF];
102
+ crc = (crc >>> 8) ^ table[(crc ^ (128|(c&63))) & 0xFF];
103
+ }
104
+ }
105
+ return crc ^ -1;
106
+ }
107
+ CRC32.table = table;
108
+ CRC32.bstr = crc32_bstr;
109
+ CRC32.buf = crc32_buf;
110
+ CRC32.str = crc32_str;
111
+ }));
112
+ /* crc32.js (C) 2014-2015 SheetJS -- http://sheetjs.com */
113
+ /* vim: set ts=2: */
114
+ // import crc32 from './crc32.js'
115
+
116
+ // Used for fast-ish conversion between uint8s and uint32s/int32s.
117
+ // Also required in order to remain agnostic for both Node Buffers and
118
+ // Uint8Arrays.
119
+ var uint8 = new Uint8Array(4)
120
+ var int32 = new Int32Array(uint8.buffer)
121
+ var uint32 = new Uint32Array(uint8.buffer)
122
+
123
+ function extractChunks (data) {
124
+ if (data[0] !== 0x89) throw new Error('Invalid .png file header')
125
+ if (data[1] !== 0x50) throw new Error('Invalid .png file header')
126
+ if (data[2] !== 0x4E) throw new Error('Invalid .png file header')
127
+ if (data[3] !== 0x47) throw new Error('Invalid .png file header')
128
+ if (data[4] !== 0x0D) throw new Error('Invalid .png file header: possibly caused by DOS-Unix line ending conversion?')
129
+ if (data[5] !== 0x0A) throw new Error('Invalid .png file header: possibly caused by DOS-Unix line ending conversion?')
130
+ if (data[6] !== 0x1A) throw new Error('Invalid .png file header')
131
+ if (data[7] !== 0x0A) throw new Error('Invalid .png file header: possibly caused by DOS-Unix line ending conversion?')
132
+
133
+ var ended = false
134
+ var chunks = []
135
+ var idx = 8
136
+
137
+ while (idx < data.length) {
138
+ // Read the length of the current chunk,
139
+ // which is stored as a Uint32.
140
+ uint8[3] = data[idx++]
141
+ uint8[2] = data[idx++]
142
+ uint8[1] = data[idx++]
143
+ uint8[0] = data[idx++]
144
+
145
+ // Chunk includes name/type for CRC check (see below).
146
+ var length = uint32[0] + 4
147
+ var chunk = new Uint8Array(length)
148
+ chunk[0] = data[idx++]
149
+ chunk[1] = data[idx++]
150
+ chunk[2] = data[idx++]
151
+ chunk[3] = data[idx++]
152
+
153
+ // Get the name in ASCII for identification.
154
+ var name = (
155
+ String.fromCharCode(chunk[0]) +
156
+ String.fromCharCode(chunk[1]) +
157
+ String.fromCharCode(chunk[2]) +
158
+ String.fromCharCode(chunk[3])
159
+ )
160
+
161
+ // The IHDR header MUST come first.
162
+ if (!chunks.length && name !== 'IHDR') {
163
+ throw new Error('IHDR header missing')
164
+ }
165
+
166
+ // The IEND header marks the end of the file,
167
+ // so on discovering it break out of the loop.
168
+ if (name === 'IEND') {
169
+ ended = true
170
+ chunks.push({
171
+ name: name,
172
+ data: new Uint8Array(0)
173
+ })
174
+
175
+ break
176
+ }
177
+
178
+ // Read the contents of the chunk out of the main buffer.
179
+ for (var i = 4; i < length; i++) {
180
+ chunk[i] = data[idx++]
181
+ }
182
+
183
+ // Read out the CRC value for comparison.
184
+ // It's stored as an Int32.
185
+ uint8[3] = data[idx++]
186
+ uint8[2] = data[idx++]
187
+ uint8[1] = data[idx++]
188
+ uint8[0] = data[idx++]
189
+
190
+ var crcActual = int32[0]
191
+ var crcExpect = CRC32.buf(chunk)
192
+ if (crcExpect !== crcActual) {
193
+ throw new Error(
194
+ 'CRC values for ' + name + ' header do not match, PNG file is likely corrupted'
195
+ )
196
+ }
197
+
198
+ // The chunk data is now copied to remove the 4 preceding
199
+ // bytes used for the chunk name/type.
200
+ var chunkData = new Uint8Array(chunk.buffer.slice(4))
201
+
202
+ chunks.push({
203
+ name: name,
204
+ data: chunkData
205
+ })
206
+ }
207
+
208
+ if (!ended) {
209
+ throw new Error('.png file ended prematurely: no IEND header was found')
210
+ }
211
+
212
+ return chunks
213
+ }
214
+ //decode
215
+ function decode (data) {
216
+ if (data.data && data.name) {
217
+ data = data.data
218
+ }
219
+
220
+ var naming = true
221
+ var text = ''
222
+ var name = ''
223
+
224
+ for (var i = 0; i < data.length; i++) {
225
+ var code = data[i]
226
+
227
+ if (naming) {
228
+ if (code) {
229
+ name += String.fromCharCode(code)
230
+ } else {
231
+ naming = false
232
+ }
233
+ } else {
234
+ if (code) {
235
+ text += String.fromCharCode(code)
236
+ } else {
237
+ throw new Error('Invalid NULL character found. 0x00 character is not permitted in tEXt content')
238
+ }
239
+ }
240
+ }
241
+
242
+ return {
243
+ keyword: name,
244
+ text: text
245
+ }
246
+ }
247
+
248
+
249
+
250
+
251
+ const readNovelAITag = async (file) => {
252
+ const buf = await file.arrayBuffer();
253
+ let chunks = [];
254
+ try {
255
+ chunks = extractChunks(new Uint8Array(buf));
256
+ console.log(chunks)
257
+ } catch (err) {
258
+ return chunks;
259
+ }
260
+ let textChunks = chunks
261
+ .filter(function (chunk) {
262
+ return chunk.name === "tEXt" || chunk.name === "iTXt";
263
+ })
264
+ console.log(textChunks)
265
+ textChunks=textChunks.map(function (chunk) {
266
+ if (chunk.name === "iTXt") {
267
+ let data = chunk.data.filter((x) => x != 0x0);
268
+ let txt = new TextDecoder().decode(data);
269
+ return {
270
+ keyword: "信息",
271
+ text: txt.slice(10),
272
+ };
273
+ }
274
+ return decode(chunk.data);
275
+ });
276
+ console.log(textChunks);
277
+ return textChunks;
278
+ }
279
+
280
+
281
+ function convertImageToBlob(fileUrl) {
282
+ return new Promise((resolve, reject) => {
283
+ const xhr = new XMLHttpRequest();
284
+ xhr.open('GET', fileUrl, true);
285
+ xhr.responseType = 'blob';
286
+ xhr.onload = function() {
287
+ if (xhr.status === 200) {
288
+ const blob = new Blob([xhr.response], { type: 'image/png' });
289
+ resolve(blob);
290
+ } else {
291
+ reject(new Error('Failed to load image'));
292
+ }
293
+ };
294
+ xhr.onerror = function() {
295
+ reject(new Error('Failed to load image'));
296
+ };
297
+ xhr.send();
298
+ });
299
+ }
300
+
301
+ async function convertDomImageToBlob(imageElement) {
302
+ const imageUrl = imageElement.src;
303
+ const blob = await convertImageToBlob(imageUrl);
304
+ const file = new File([blob], 'filename.png', { type: blob.type });
305
+ return file;
306
+ }
307
+
308
+
309
+ setTimeout(()=>{
310
+ const elem = document.querySelector('gradio-app');
311
+ const shadowRoot = elem.shadowRoot;
312
+ const png_info = shadowRoot.querySelector('#pnginfo_image ');
313
+ png_info.addEventListener('change', (e) => {
314
+ // 获取用户选择的文件
315
+ // const file = png_info.files[0];
316
+ setTimeout(async () => {
317
+ const png_info_img = await png_info.querySelector('img')
318
+ let png_info_img_convert = await convertDomImageToBlob(png_info_img)
319
+ console.log(png_info_img_convert)
320
+ let res=await readNovelAITag(png_info_img_convert)
321
+ console.log('res',res)
322
+ shadowRoot.querySelector('#component-637').innerText=res.length?res[0].text:'这不是一张stablediffusion图片'
323
+
324
+ //js对象形式转化
325
+ const result = {};
326
+ // const match = inputString.match(/(?<=prompt:)(.*)(?=Negative prompt:)/s)[0].trim();]
327
+ console.log(res[0].text)
328
+ result.prompt = res[0].text.match(/(.*)(?=Negative prompt:)/s)[0].trim();
329
+ result.negativePrompt = res[0].text.match(/(?<=Negative prompt:)(.*)(?=Steps:)/s)[0].trim();
330
+ let resArr=res[0].text.split(result.negativePrompt)[1].trim().split(',')
331
+ console.log(resArr)
332
+ resArr.forEach(e=>{result[e.split(':')[0].replaceAll(' ','')]=
333
+ e.split(':')[1].trim()
334
+ })
335
+ console.log(result)
336
+ console.log('result',result)
337
+ shadowRoot.querySelector('#txt2img_tab').onclick=()=>{
338
+ shadowRoot.querySelector('[class="flex border-b-2 flex-wrap dark:border-gray-700"]').children[0].click()
339
+ // let setting_CLIP_stop_at_last_layers_value=/^(.+)(?=Negative prompt: )/g.match(res)
340
+ shadowRoot.querySelectorAll('#setting_CLIP_stop_at_last_layers input')[0].value=Number(result.Clipskip)
341
+ shadowRoot.querySelectorAll('#setting_CLIP_stop_at_last_layers input')[1].value=Number(result.Clipskip)
342
+ shadowRoot.querySelector('#txt2img_prompt textarea').value=result.prompt
343
+ shadowRoot.querySelector('#txt2img_neg_prompt textarea').value=result.negativePrompt
344
+ shadowRoot.querySelectorAll('#txt2img_steps input')[0].value=Number(result.Steps)
345
+ shadowRoot.querySelectorAll('#txt2img_steps input')[1].value=Number(result.Steps)
346
+ shadowRoot.querySelector(`[value=${result.Sampler}]`).checked = true;
347
+ shadowRoot.querySelectorAll('#txt2img_width input')[0].value=Number(result.Size.split('x')[0])
348
+ shadowRoot.querySelectorAll('#txt2img_width input')[1].value=Number(result.Size.split('x')[0])
349
+ shadowRoot.querySelectorAll('#txt2img_height input')[0].value=Number(result.Size.split('x')[1])
350
+ shadowRoot.querySelectorAll('#txt2img_height input')[1].value=Number(result.Size.split('x')[1])
351
+ // shadowRoot.querySelectorAll('#txt2img_batch_count input')[0].value=2
352
+ // shadowRoot.querySelectorAll('#txt2img_batch_count input')[1].value=2
353
+ // shadowRoot.querySelectorAll('#txt2img_batch_size input')[0].value=3
354
+ // shadowRoot.querySelectorAll('#txt2img_batch_size input')[1].value=3
355
+ shadowRoot.querySelector('#txt2img_seed input').value=Number(result.Seed)
356
+ //高清修复
357
+ shadowRoot.querySelector('#txt2img_enable_hr input').checked = !!result.Hiresupscaler
358
+ shadowRoot.querySelector('#txt2img_hr_upscaler select').value = result.Hiresupscaler||'Latent'
359
+ shadowRoot.querySelectorAll('#txt2img_hires_steps input')[0].value=Number(result.Hiressteps)||0
360
+ shadowRoot.querySelectorAll('#txt2img_hires_steps input')[1].value=Number(result.Hiressteps)||0
361
+ shadowRoot.querySelectorAll('#txt2img_denoising_strength input')[0].value=Number(result.Denoisingstrength)||0
362
+ shadowRoot.querySelectorAll('#txt2img_denoising_strength input')[1].value=Number(result.Denoisingstrength)||0
363
+ shadowRoot.querySelectorAll('#txt2img_hr_scale input')[0].value=Number(result.Hiresupscale)||1
364
+ shadowRoot.querySelectorAll('#txt2img_hr_scale input')[1].value=Number(result.Hiresupscale)||1
365
+ }
366
+ },100)
367
+ });
368
+ // png_info_img
369
+ },1000
370
+ )