@enslo/sd-metadata 2.1.1 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/README.ja.md +7 -3
- package/README.md +8 -4
- package/dist/index.d.ts +4 -2
- package/dist/index.global.js +6 -6
- package/dist/index.js +308 -35
- package/dist/index.js.map +1 -1
- package/docs/types.ja.md +7 -4
- package/docs/types.md +8 -5
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,30 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [2.2.0] - 2026-02-27
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **Draw Things parser** (#181): Read and write metadata from [Draw Things](https://drawthings.ai/) (macOS/iOS) generated images
|
|
13
|
+
- Extract generation parameters from XMP (Adobe XML Metadata) packets
|
|
14
|
+
- Native XMP read/write for JPEG (APP1 segment) and WebP (XMP chunk)
|
|
15
|
+
- XMP reading from PNG iTXt chunks
|
|
16
|
+
|
|
17
|
+
### Potentially Breaking Changes
|
|
18
|
+
|
|
19
|
+
- **New `GenerationSoftware` member** (#181): `'draw-things'` added. TypeScript users with exhaustive `switch` or `Record<GenerationSoftware, ...>` will need to handle the new value.
|
|
20
|
+
- **New `MetadataSegmentSource` variant** (#181): `{ type: 'xmpPacket' }` added for XMP segment tracking. Exhaustive patterns on `MetadataSegmentSource` will need updating.
|
|
21
|
+
|
|
22
|
+
### Security
|
|
23
|
+
|
|
24
|
+
- **XMP regex ReDoS protection** (#181): Add 64KB size limit on XMP text input to prevent exponential backtracking on malformed XML
|
|
25
|
+
- **XML entity decoder hardening** (#181): Catch `RangeError` from `String.fromCodePoint` on invalid Unicode code points in XMP entities
|
|
26
|
+
- **JPEG COM segment overflow guard** (#181): Add 64KB size limit check to `buildComSegment`, matching the existing guard on `buildXmpApp1Segment`
|
|
27
|
+
|
|
28
|
+
### Maintenance
|
|
29
|
+
|
|
30
|
+
- Update development dependencies
|
|
31
|
+
|
|
8
32
|
## [2.1.1] - 2026-02-24
|
|
9
33
|
|
|
10
34
|
### Fixed
|
|
@@ -380,6 +404,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
380
404
|
- Biome for code formatting and linting
|
|
381
405
|
- CONTRIBUTING.md for community contributions
|
|
382
406
|
|
|
407
|
+
[2.2.0]: https://github.com/enslo/sd-metadata/releases/tag/core@2.2.0
|
|
383
408
|
[2.1.1]: https://github.com/enslo/sd-metadata/releases/tag/core@2.1.1
|
|
384
409
|
[2.1.0]: https://github.com/enslo/sd-metadata/releases/tag/v2.1.0
|
|
385
410
|
[2.0.1]: https://github.com/enslo/sd-metadata/releases/tag/v2.0.1
|
package/README.ja.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# sd-metadata
|
|
1
|
+
# @enslo/sd-metadata
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@enslo/sd-metadata)
|
|
4
4
|
[](https://www.npmjs.com/package/@enslo/sd-metadata)
|
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
|
|
7
7
|
🌐 **[English version](./README.md)**
|
|
8
8
|
|
|
9
|
+
🔗 **[ライブデモ](https://sd-metadata.pages.dev/)**
|
|
10
|
+
|
|
9
11
|
AI生成画像に埋め込まれたメタデータを読み書きするためのTypeScriptライブラリです。
|
|
10
12
|
|
|
11
13
|
## 特徴
|
|
@@ -45,6 +47,7 @@ npm install @enslo/sd-metadata
|
|
|
45
47
|
| [Fooocus](https://github.com/lllyasviel/Fooocus) | ⚠️ | ⚠️ | ⚠️ |
|
|
46
48
|
| [Ruined Fooocus](https://github.com/runew0lf/RuinedFooocus) | ✅ | 🔄️ | 🔄️ |
|
|
47
49
|
| [Easy Diffusion](https://github.com/easydiffusion/easydiffusion) | ⚠️ | ⚠️ | ⚠️ |
|
|
50
|
+
| [Draw Things](https://drawthings.ai/) | ⚠️ | ⚠️ | ⚠️ |
|
|
48
51
|
|
|
49
52
|
**凡例:**
|
|
50
53
|
|
|
@@ -145,7 +148,7 @@ fileInput.addEventListener('change', async (e) => {
|
|
|
145
148
|
// ==UserScript==
|
|
146
149
|
// @name My Script
|
|
147
150
|
// @namespace https://example.com
|
|
148
|
-
// @require https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@2.
|
|
151
|
+
// @require https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@2.2.0/dist/index.global.js
|
|
149
152
|
// ==/UserScript==
|
|
150
153
|
|
|
151
154
|
const response = await fetch(imageUrl);
|
|
@@ -557,7 +560,8 @@ type GenerationSoftware =
|
|
|
557
560
|
| 'novelai' | 'comfyui' | 'swarmui' | 'tensorart' | 'stability-matrix'
|
|
558
561
|
| 'sd-webui' | 'forge' | 'forge-classic' | 'forge-neo'
|
|
559
562
|
| 'reforge'| 'easy-reforge' | 'sd-next' | 'civitai' | 'hf-space'
|
|
560
|
-
| 'invokeai' | 'easydiffusion' | 'fooocus' | 'ruined-fooocus'
|
|
563
|
+
| 'invokeai' | 'easydiffusion' | 'fooocus' | 'ruined-fooocus'
|
|
564
|
+
| 'draw-things';
|
|
561
565
|
```
|
|
562
566
|
|
|
563
567
|
### `EmbedMetadata`
|
package/README.md
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
# sd-metadata
|
|
1
|
+
# @enslo/sd-metadata
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@enslo/sd-metadata)
|
|
4
4
|
[](https://www.npmjs.com/package/@enslo/sd-metadata)
|
|
5
5
|
[](https://github.com/enslo/sd-metadata/blob/main/LICENSE)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
🌐 **[日本語版はこちら](./README.ja.md)**
|
|
8
|
+
|
|
9
|
+
🔗 **[Live Demo](https://sd-metadata.pages.dev/)**
|
|
8
10
|
|
|
9
11
|
A TypeScript library to read and write metadata embedded in AI-generated images.
|
|
10
12
|
|
|
@@ -45,6 +47,7 @@ npm install @enslo/sd-metadata
|
|
|
45
47
|
| [Fooocus](https://github.com/lllyasviel/Fooocus) | ⚠️ | ⚠️ | ⚠️ |
|
|
46
48
|
| [Ruined Fooocus](https://github.com/runew0lf/RuinedFooocus) | ✅ | 🔄️ | 🔄️ |
|
|
47
49
|
| [Easy Diffusion](https://github.com/easydiffusion/easydiffusion) | ⚠️ | ⚠️ | ⚠️ |
|
|
50
|
+
| [Draw Things](https://drawthings.ai/) | ⚠️ | ⚠️ | ⚠️ |
|
|
48
51
|
|
|
49
52
|
**Legend:**
|
|
50
53
|
|
|
@@ -145,7 +148,7 @@ For userscripts (Tampermonkey, Violentmonkey, etc.), load the IIFE build via `@r
|
|
|
145
148
|
// ==UserScript==
|
|
146
149
|
// @name My Script
|
|
147
150
|
// @namespace https://example.com
|
|
148
|
-
// @require https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@2.
|
|
151
|
+
// @require https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@2.2.0/dist/index.global.js
|
|
149
152
|
// ==/UserScript==
|
|
150
153
|
|
|
151
154
|
const response = await fetch(imageUrl);
|
|
@@ -557,7 +560,8 @@ type GenerationSoftware =
|
|
|
557
560
|
| 'novelai' | 'comfyui' | 'swarmui' | 'tensorart' | 'stability-matrix'
|
|
558
561
|
| 'sd-webui' | 'forge' | 'forge-classic' | 'forge-neo'
|
|
559
562
|
| 'reforge'| 'easy-reforge' | 'sd-next' | 'civitai' | 'hf-space'
|
|
560
|
-
| 'invokeai' | 'easydiffusion' | 'fooocus' | 'ruined-fooocus'
|
|
563
|
+
| 'invokeai' | 'easydiffusion' | 'fooocus' | 'ruined-fooocus'
|
|
564
|
+
| 'draw-things';
|
|
561
565
|
```
|
|
562
566
|
|
|
563
567
|
### `EmbedMetadata`
|
package/dist/index.d.ts
CHANGED
|
@@ -44,6 +44,8 @@ type MetadataSegmentSource = {
|
|
|
44
44
|
prefix?: string;
|
|
45
45
|
} | {
|
|
46
46
|
type: 'jpegCom';
|
|
47
|
+
} | {
|
|
48
|
+
type: 'xmpPacket';
|
|
47
49
|
};
|
|
48
50
|
/**
|
|
49
51
|
* A single metadata segment with source tracking
|
|
@@ -70,7 +72,7 @@ type RawMetadata = {
|
|
|
70
72
|
/**
|
|
71
73
|
* Known AI image generation software
|
|
72
74
|
*/
|
|
73
|
-
type GenerationSoftware = 'novelai' | 'comfyui' | 'swarmui' | 'tensorart' | 'stability-matrix' | 'invokeai' | 'forge' | 'forge-classic' | 'forge-neo' | 'reforge' | 'easy-reforge' | 'sd-webui' | 'sd-next' | 'civitai' | 'hf-space' | 'easydiffusion' | 'fooocus' | 'ruined-fooocus';
|
|
75
|
+
type GenerationSoftware = 'novelai' | 'comfyui' | 'swarmui' | 'tensorart' | 'stability-matrix' | 'invokeai' | 'forge' | 'forge-classic' | 'forge-neo' | 'reforge' | 'easy-reforge' | 'sd-webui' | 'sd-next' | 'civitai' | 'hf-space' | 'easydiffusion' | 'fooocus' | 'ruined-fooocus' | 'draw-things';
|
|
74
76
|
/**
|
|
75
77
|
* Base metadata fields shared by all tools
|
|
76
78
|
*/
|
|
@@ -200,7 +202,7 @@ type ComfyUIMetadata = BasicComfyUIMetadata | SwarmUIMetadata;
|
|
|
200
202
|
* NovelAI's character prompts or ComfyUI's node graphs.
|
|
201
203
|
*/
|
|
202
204
|
interface StandardMetadata extends BaseMetadata {
|
|
203
|
-
software: 'sd-webui' | 'sd-next' | 'forge' | 'forge-classic' | 'forge-neo' | 'reforge' | 'easy-reforge' | 'invokeai' | 'civitai' | 'hf-space' | 'easydiffusion' | 'fooocus' | 'ruined-fooocus';
|
|
205
|
+
software: 'sd-webui' | 'sd-next' | 'forge' | 'forge-classic' | 'forge-neo' | 'reforge' | 'easy-reforge' | 'invokeai' | 'civitai' | 'hf-space' | 'easydiffusion' | 'fooocus' | 'ruined-fooocus' | 'draw-things';
|
|
204
206
|
}
|
|
205
207
|
/**
|
|
206
208
|
* Unified generation metadata (discriminated union)
|
package/dist/index.global.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
"use strict";var sdMetadata=(()=>{var ye=Object.defineProperty;var tn=Object.getOwnPropertyDescriptor;var nn=Object.getOwnPropertyNames;var rn=Object.prototype.hasOwnProperty;var on=(e,t)=>{for(var n in t)ye(e,n,{get:t[n],enumerable:!0})},sn=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of nn(t))!rn.call(e,o)&&o!==n&&ye(e,o,{get:()=>t[o],enumerable:!(r=tn(t,o))||r.enumerable});return e};var an=e=>sn(ye({},"__esModule",{value:!0}),e);var lo={};on(lo,{embed:()=>Ee,formatAsWebUI:()=>H,formatRaw:()=>Te,read:()=>At,softwareLabels:()=>Zt,stringify:()=>Ge,write:()=>Yt,writeAsWebUI:()=>Ee});var M=(e,t)=>t!==void 0?[{type:"tEXt",keyword:e,text:t}]:[],_e=(e,t)=>t!==void 0?[{type:"iTXt",keyword:e,compressionFlag:0,compressionMethod:0,languageTag:"",translatedKeyword:"",text:t}]:[],S=(e,t)=>e.find(n=>n.source.type===t),k=e=>{if(e!==void 0)return typeof e=="string"?e:JSON.stringify(e)};function un(e){return e.replace(/[\u0100-\uffff]/g,t=>`\\u${t.charCodeAt(0).toString(16).padStart(4,"0")}`)}function J(e){return e.replace(/\\u([0-9a-fA-F]{4})/g,(t,n)=>String.fromCharCode(Number.parseInt(n,16)))}function cn(e){return/[^\x00-\xFF]/.test(e)}function h(e,t,n){if(t===void 0)return[];switch(n){case"dynamic":return(cn(t)?"iTXt":"tEXt")==="iTXt"?_e(e,t):M(e,t);case"text-unicode-escape":{let r=un(t);return M(e,r)}case"text-utf8-raw":return M(e,t)}}function re(e,t){return e.flatMap(([n,r])=>{let o=t[n]??t.default;return h(n,r,o)})}var a={ok:e=>({ok:!0,value:e}),error:e=>({ok:!1,error:e})};function W(e){return e instanceof ArrayBuffer?new Uint8Array(e):e}function V(e){return e.length<8?!1:e[0]===137&&e[1]===80&&e[2]===78&&e[3]===71&&e[4]===13&&e[5]===10&&e[6]===26&&e[7]===10}function B(e){return e.length<2?!1:e[0]===255&&e[1]===216}function X(e){return e.length<12?!1:e[0]===82&&e[1]===73&&e[2]===70&&e[3]===70&&e[8]===87&&e[9]===69&&e[10]===66&&e[11]===80}function j(e){return V(e)?"png":B(e)?"jpeg":X(e)?"webp":null}function P(e,t){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint16(t)}function U(e,t,n){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint16(t,n)}function oe(e,t){return(e[t]??0)|(e[t+1]??0)<<8|(e[t+2]??0)<<16}function _(e,t){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(t)}function R(e,t){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(t,!0)}function $(e,t,n){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(t,n)}function T(e,t){return String.fromCharCode(e[t]??0,e[t+1]??0,e[t+2]??0,e[t+3]??0)}function he(e,t,n){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint16(t,n)}function L(e,t,n,r){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint16(t,n,r)}function xe(e,t,n){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint32(t,n)}function Se(e,t,n){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint32(t,n,!0)}function N(e,t,n,r){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint32(t,n,r)}function ie(e){let t=e.filter(f=>f.source.type==="exifImageDescription"||f.source.type==="exifMake"),n=e.filter(f=>f.source.type==="exifUserComment");if(t.length===0&&n.length===0)return new Uint8Array(0);let r=!0,o=[],s=[];for(let f of t)if(f.source.type==="exifImageDescription"){let b=Ne(f.data,f.source.prefix);o.push({tag:270,type:2,data:b})}else if(f.source.type==="exifMake"){let b=Ne(f.data,f.source.prefix);o.push({tag:271,type:2,data:b})}for(let f of n)if(f.source.type==="exifUserComment"){let b=mn(f.data);s.push({tag:37510,type:7,data:b})}let i=s.length>0;i&&o.push({tag:34665,type:4,data:new Uint8Array(4)}),o.sort((f,b)=>f.tag-b.tag),s.sort((f,b)=>f.tag-b.tag);let u=8,c=o.length,m=2+12*c+4,g=s.length,y=i?2+12*g+4:0,l=u,I=l+m,x=I+y;if(i){let f=o.find(b=>b.tag===34665);f&&N(f.data,0,I,r)}let C=new Map;for(let f of[...o,...s])f.data.length>4&&(C.set(f,x),x+=f.data.length,f.data.length%2!==0&&(x+=1));let ge=x,w=new Uint8Array(ge);w[0]=73,w[1]=73,L(w,2,42,r),N(w,4,l,r);let v=l;L(w,v,c,r),v+=2;for(let f of o)Re(w,v,f,C.get(f),r),v+=12;if(N(w,v,0,r),v+=4,i){L(w,v,g,r),v+=2;for(let f of s)Re(w,v,f,C.get(f),r),v+=12;N(w,v,0,r)}for(let[f,b]of C)w.set(f.data,b);return w}function Re(e,t,n,r,o){L(e,t,n.tag,o),L(e,t+2,n.type,o),N(e,t+4,n.data.length,o),n.data.length<=4?e.set(n.data,t+8):N(e,t+8,r??0,o)}var pn=new Uint8Array([85,78,73,67,79,68,69,0]);function mn(e){let t=new Uint8Array(8+e.length*2),n=new DataView(t.buffer);t.set(pn);for(let r=0;r<e.length;r++)n.setUint16(8+r*2,e.charCodeAt(r),!0);return t}function Ne(e,t){let n=t?`${t}: ${e}`:e,r=new TextEncoder().encode(n),o=new Uint8Array(r.length+1);return o.set(r,0),o[r.length]=0,o}var Oe=225,Fe=254,fn=218,dn=217,we=new Uint8Array([69,120,105,102,0,0]);function K(e,t){if(!B(e))return a.error({type:"invalidSignature"});let n=t.filter(l=>l.source.type==="jpegCom"),r=t.filter(l=>l.source.type==="exifUserComment"||l.source.type==="exifImageDescription"||l.source.type==="exifMake"),o=ln(e);if(!o.ok)return o;let{beforeSos:s,scanData:i}=o.value,u=r.length>0?gn(r):null,c=n.map(l=>yn(l.data)),m=2;u&&(m+=u.length);for(let l of s)m+=l.length;for(let l of c)m+=l.length;m+=i.length;let g=new Uint8Array(m),y=0;g[y++]=255,g[y++]=216,u&&(g.set(u,y),y+=u.length);for(let l of s)g.set(l,y),y+=l.length;for(let l of c)g.set(l,y),y+=l.length;return g.set(i,y),a.ok(g)}function ln(e){let t=[],n=2;for(;n<e.length-1;){if(e[n]!==255)return a.error({type:"corruptedStructure",message:`Expected marker at offset ${n}`});for(;e[n]===255&&n<e.length-1;)n++;let r=e[n];if(n++,r===fn){let m=e.slice(n-2);return a.ok({beforeSos:t,scanData:m})}if(r===dn)return a.ok({beforeSos:t,scanData:new Uint8Array([255,217])});if(n+2>e.length)return a.error({type:"corruptedStructure",message:"Unexpected end of file"});let o=P(e,n),s=n-2,i=n+o;if(i>e.length)return a.error({type:"corruptedStructure",message:"Segment extends beyond file"});!(r===Oe&&n+2+6<=e.length&&e[n+2]===69&&e[n+3]===120&&e[n+4]===105&&e[n+5]===102&&e[n+6]===0&&e[n+7]===0)&&!(r===Fe)&&t.push(e.slice(s,i)),n=i}return a.error({type:"corruptedStructure",message:"No SOS marker found"})}function gn(e){let t=ie(e);if(t.length===0)return new Uint8Array(0);let n=2+we.length+t.length,r=new Uint8Array(2+n);return r[0]=255,r[1]=Oe,he(r,2,n),r.set(we,4),r.set(t,4+we.length),r}function yn(e){let t=new TextEncoder().encode(e),n=2+t.length,r=new Uint8Array(2+n);return r[0]=255,r[1]=Fe,he(r,2,n),r.set(t,4),r}var Z=new Uint8Array([137,80,78,71,13,10,26,10]);function q(e,t){if(!V(e))return a.error({type:"invalidSignature"});if(hn(e)===-1)return a.error({type:"noIhdrChunk"});let r=xn(e),o=t.map(c=>c.type==="tEXt"?Sn(c):bn(c)),s=Z.length+r.ihdr.length+o.reduce((c,m)=>c+m.length,0)+r.others.reduce((c,m)=>c+m.length,0),i=new Uint8Array(s),u=0;i.set(Z,u),u+=Z.length,i.set(r.ihdr,u),u+=r.ihdr.length;for(let c of o)i.set(c,u),u+=c.length;for(let c of r.others)i.set(c,u),u+=c.length;return a.ok(i)}function hn(e){let t=Z.length;if(t+8>e.length)return-1;let n=_(e,t);return T(e,t+4)!=="IHDR"?-1:t+4+4+n+4}function xn(e){let t=[],n=Z.length,r=new Uint8Array(0);for(;n<e.length;){let o=n;if(n+4>e.length)break;let s=_(e,n);if(n+=4,n+4>e.length)break;let i=T(e,n);if(n+=4,n+s+4>e.length)break;n+=s,n+=4;let u=n,c=e.slice(o,u);if(i==="IHDR"?r=c:i!=="tEXt"&&i!=="iTXt"&&t.push(c),i==="IEND")break}return{ihdr:r,others:t}}function Sn(e){let t=kn(e.keyword),n=Y(e.text),r=new Uint8Array(t.length+1+n.length);return r.set(t,0),r[t.length]=0,r.set(n,t.length+1),De("tEXt",r)}function bn(e){let t=Y(e.keyword),n=Y(e.languageTag),r=Y(e.translatedKeyword),o=Y(e.text),s=t.length+1+1+1+n.length+1+r.length+1+o.length,i=new Uint8Array(s),u=0;return i.set(t,u),u+=t.length,i[u++]=0,i[u++]=e.compressionFlag,i[u++]=e.compressionMethod,i.set(n,u),u+=n.length,i[u++]=0,i.set(r,u),u+=r.length,i[u++]=0,i.set(o,u),De("iTXt",i)}function De(e,t){let n=new Uint8Array(8+t.length+4);xe(n,0,t.length),n.set(new TextEncoder().encode(e),4),n.set(t,8);let r=n.slice(4,8+t.length),o=vn(r);return xe(n,8+t.length,o),n}function kn(e){let t=new Uint8Array(e.length);for(let n=0;n<e.length;n++)t[n]=e.charCodeAt(n)&255;return t}function Y(e){return new TextEncoder().encode(e)}var Cn=wn();function wn(){let e=new Uint32Array(256);for(let t=0;t<256;t++){let n=t;for(let r=0;r<8;r++)n&1?n=3988292384^n>>>1:n=n>>>1;e[t]=n>>>0}return e}function vn(e){let t=4294967295;for(let n=0;n<e.length;n++)t=(Cn[(t^(e[n]??0))&255]??0)^t>>>8;return(t^4294967295)>>>0}var Tn=new Uint8Array([82,73,70,70]),En=new Uint8Array([87,69,66,80]);function Q(e,t){if(!X(e))return a.error({type:"invalidSignature"});let n=Un(e);if(!n.ok)return n;let{chunks:r}=n.value,o=Pn(t),s=4;for(let m of r)s+=m.length;o&&(s+=o.length);let i=new Uint8Array(8+s),u=0;i.set(Tn,u),u+=4,Se(i,u,s),u+=4,i.set(En,u),u+=4;let c=!1;for(let m of r)i.set(m,u),u+=m.length,!c&&o&&Mn(m)&&(i.set(o,u),u+=o.length,c=!0);return!c&&o&&i.set(o,u),a.ok(i)}function Mn(e){if(e.length<4)return!1;let t=T(e,0);return t==="VP8 "||t==="VP8L"||t==="VP8X"}function Un(e){let t=[],n="",r=12;for(;r<e.length-8;){let o=T(e,r),s=R(e,r+4);if(n||(n=o),r+8+s>e.length)return a.error({type:"invalidRiffStructure",message:`Chunk extends beyond file at offset ${r}`});if(o!=="EXIF"){let u=s+s%2,c=e.slice(r,r+8+u);t.push(c)}let i=s+s%2;r+=8+i}return a.ok({chunks:t,firstChunkType:n})}function Pn(e){let t=e.filter(i=>i.source.type==="exifUserComment"||i.source.type==="exifImageDescription"||i.source.type==="exifMake");if(t.length===0)return null;let n=ie(t);if(n.length===0)return null;let r=n.length,o=r+r%2,s=new Uint8Array(8+o);return s.set(new TextEncoder().encode("EXIF")),Se(s,4,r),s.set(n,8),s}function ve(e){return e.replace(/\r\n/g,`
|
|
1
|
+
"use strict";var sdMetadata=(()=>{var we=Object.defineProperty;var fn=Object.getOwnPropertyDescriptor;var dn=Object.getOwnPropertyNames;var ln=Object.prototype.hasOwnProperty;var gn=(e,t)=>{for(var n in t)we(e,n,{get:t[n],enumerable:!0})},yn=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of dn(t))!ln.call(e,o)&&o!==n&&we(e,o,{get:()=>t[o],enumerable:!(r=fn(t,o))||r.enumerable});return e};var hn=e=>yn(we({},"__esModule",{value:!0}),e);var No={};gn(No,{embed:()=>Re,formatAsWebUI:()=>H,formatRaw:()=>Ie,read:()=>Gt,softwareLabels:()=>un,stringify:()=>He,write:()=>an,writeAsWebUI:()=>Re});var M=(e,t)=>t!==void 0?[{type:"tEXt",keyword:e,text:t}]:[],oe=(e,t)=>t!==void 0?[{type:"iTXt",keyword:e,compressionFlag:0,compressionMethod:0,languageTag:"",translatedKeyword:"",text:t}]:[],b=(e,t)=>e.find(n=>n.source.type===t),w=e=>{if(e!==void 0)return typeof e=="string"?e:JSON.stringify(e)};function xn(e){return e.replace(/[\u0100-\uffff]/g,t=>`\\u${t.charCodeAt(0).toString(16).padStart(4,"0")}`)}function j(e){return e.replace(/\\u([0-9a-fA-F]{4})/g,(t,n)=>String.fromCharCode(Number.parseInt(n,16)))}function Sn(e){return/[^\x00-\xFF]/.test(e)}function k(e,t,n){if(t===void 0)return[];switch(n){case"dynamic":return(Sn(t)?"iTXt":"tEXt")==="iTXt"?oe(e,t):M(e,t);case"text-unicode-escape":{let r=xn(t);return M(e,r)}case"text-utf8-raw":return M(e,t)}}function se(e,t){return e.flatMap(([n,r])=>{let o=t[n]??t.default;return k(n,r,o)})}var a={ok:e=>({ok:!0,value:e}),error:e=>({ok:!1,error:e})};function W(e){return e instanceof ArrayBuffer?new Uint8Array(e):e}function V(e){return e.length<8?!1:e[0]===137&&e[1]===80&&e[2]===78&&e[3]===71&&e[4]===13&&e[5]===10&&e[6]===26&&e[7]===10}function B(e){return e.length<2?!1:e[0]===255&&e[1]===216}function $(e){return e.length<12?!1:e[0]===82&&e[1]===73&&e[2]===70&&e[3]===70&&e[8]===87&&e[9]===69&&e[10]===66&&e[11]===80}function L(e){return V(e)?"png":B(e)?"jpeg":$(e)?"webp":null}function A(e,t){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint16(t)}function I(e,t,n){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint16(t,n)}function ie(e,t){return(e[t]??0)|(e[t+1]??0)<<8|(e[t+2]??0)<<16}function _(e,t){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(t)}function N(e,t){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(t,!0)}function K(e,t,n){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(t,n)}function E(e,t){return String.fromCharCode(e[t]??0,e[t+1]??0,e[t+2]??0,e[t+3]??0)}function ae(e,t,n){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint16(t,n)}function X(e,t,n,r){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint16(t,n,r)}function ve(e,t,n){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint32(t,n)}function ue(e,t,n){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint32(t,n,!0)}function O(e,t,n,r){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint32(t,n,r)}var bn="XML:com.adobe.xmp",U=new TextEncoder().encode("http://ns.adobe.com/xap/1.0/\0");function ce(e,t){if(t+U.length>e.length)return!1;for(let n=0;n<U.length;n++)if(e[t+n]!==U[n])return!1;return!0}function pe(e){return e===bn}var kn=65535;function Te(e){if(e.length>kn)return null;let n=[["CreatorTool",Cn(e,"xmp","CreatorTool")],["UserComment",Ge(e,"exif","UserComment")],["parameters",Ge(e,"dc","description")]].filter(r=>r[1]!==void 0);return n.length>0?Object.fromEntries(n):null}function Cn(e,t,n){let r=new RegExp(`<${t}:${n}>([^<]*)</${t}:${n}>`),o=e.match(r);return o?.[1]?Je(o[1]):void 0}function Ge(e,t,n){let r=new RegExp(`<${t}:${n}>[\\s\\S]*?<rdf:li[^>]*(?<!/)>([\\s\\S]*?)</rdf:li>[\\s\\S]*?</${t}:${n}>`),o=e.match(r);return o?.[1]?Je(o[1]):void 0}function Je(e){return e.replace(/&#x([0-9a-fA-F]+);/g,(t,n)=>{try{return String.fromCodePoint(parseInt(n,16))}catch{return t}}).replace(/&#(\d+);/g,(t,n)=>{try{return String.fromCodePoint(parseInt(n,10))}catch{return t}}).replace(/</g,"<").replace(/>/g,">").replace(/"/g,'"').replace(/'/g,"'").replace(/&/g,"&")}function fe(e){let t=e.filter(l=>l.source.type==="exifImageDescription"||l.source.type==="exifMake"),n=e.filter(l=>l.source.type==="exifUserComment");if(t.length===0&&n.length===0)return new Uint8Array(0);let r=!0,o=[],s=[];for(let l of t)if(l.source.type==="exifImageDescription"){let C=We(l.data,l.source.prefix);o.push({tag:270,type:2,data:C})}else if(l.source.type==="exifMake"){let C=We(l.data,l.source.prefix);o.push({tag:271,type:2,data:C})}for(let l of n)if(l.source.type==="exifUserComment"){let C=vn(l.data);s.push({tag:37510,type:7,data:C})}let i=s.length>0;i&&o.push({tag:34665,type:4,data:new Uint8Array(4)}),o.sort((l,C)=>l.tag-C.tag),s.sort((l,C)=>l.tag-C.tag);let u=8,c=o.length,m=2+12*c+4,y=s.length,x=i?2+12*y+4:0,h=u,S=h+m,f=S+x;if(i){let l=o.find(C=>C.tag===34665);l&&O(l.data,0,S,r)}let d=new Map;for(let l of[...o,...s])l.data.length>4&&(d.set(l,f),f+=l.data.length,l.data.length%2!==0&&(f+=1));let Ce=f,v=new Uint8Array(Ce);v[0]=73,v[1]=73,X(v,2,42,r),O(v,4,h,r);let T=h;X(v,T,c,r),T+=2;for(let l of o)je(v,T,l,d.get(l),r),T+=12;if(O(v,T,0,r),T+=4,i){X(v,T,y,r),T+=2;for(let l of s)je(v,T,l,d.get(l),r),T+=12;O(v,T,0,r)}for(let[l,C]of d)v.set(l.data,C);return v}function je(e,t,n,r,o){X(e,t,n.tag,o),X(e,t+2,n.type,o),O(e,t+4,n.data.length,o),n.data.length<=4?e.set(n.data,t+8):O(e,t+8,r??0,o)}var wn=new Uint8Array([85,78,73,67,79,68,69,0]);function vn(e){let t=new Uint8Array(8+e.length*2),n=new DataView(t.buffer);t.set(wn);for(let r=0;r<e.length;r++)n.setUint16(8+r*2,e.charCodeAt(r),!0);return t}function We(e,t){let n=t?`${t}: ${e}`:e,r=new TextEncoder().encode(n),o=new Uint8Array(r.length+1);return o.set(r,0),o[r.length]=0,o}var de=225,Le=254,Tn=218,En=217,Ue=new Uint8Array([69,120,105,102,0,0]);function Y(e,t){if(!B(e))return a.error({type:"invalidSignature"});let n=t.filter(d=>d.source.type==="jpegCom"),r=t.filter(d=>d.source.type==="xmpPacket"),o=t.filter(d=>d.source.type==="exifUserComment"||d.source.type==="exifImageDescription"||d.source.type==="exifMake"),s=Pn(e);if(!s.ok)return s;let{beforeSos:i,scanData:u}=s.value,c=o.length>0?Mn(o):null,m=r[0],y=m?Un(m.data):null,x=n.map(d=>An(d.data)).filter(d=>d!==null),h=2;c&&(h+=c.length),y&&(h+=y.length);for(let d of i)h+=d.length;for(let d of x)h+=d.length;h+=u.length;let S=new Uint8Array(h),f=0;S[f++]=255,S[f++]=216,c&&(S.set(c,f),f+=c.length),y&&(S.set(y,f),f+=y.length);for(let d of i)S.set(d,f),f+=d.length;for(let d of x)S.set(d,f),f+=d.length;return S.set(u,f),a.ok(S)}function Pn(e){let t=[],n=2;for(;n<e.length-1;){if(e[n]!==255)return a.error({type:"corruptedStructure",message:`Expected marker at offset ${n}`});for(;e[n]===255&&n<e.length-1;)n++;let r=e[n];if(n++,r===Tn){let y=e.slice(n-2);return a.ok({beforeSos:t,scanData:y})}if(r===En)return a.ok({beforeSos:t,scanData:new Uint8Array([255,217])});if(n+2>e.length)return a.error({type:"corruptedStructure",message:"Unexpected end of file"});let o=A(e,n),s=n-2,i=n+o;if(i>e.length)return a.error({type:"corruptedStructure",message:"Segment extends beyond file"});let u=r===de&&n+2+6<=e.length&&e[n+2]===69&&e[n+3]===120&&e[n+4]===105&&e[n+5]===102&&e[n+6]===0&&e[n+7]===0,c=r===de&&!u&&ce(e,n+2);!u&&!c&&!(r===Le)&&t.push(e.slice(s,i)),n=i}return a.error({type:"corruptedStructure",message:"No SOS marker found"})}function Mn(e){let t=fe(e);if(t.length===0)return new Uint8Array(0);let n=2+Ue.length+t.length,r=new Uint8Array(2+n);return r[0]=255,r[1]=de,ae(r,2,n),r.set(Ue,4),r.set(t,4+Ue.length),r}function Un(e){let t=new TextEncoder().encode(e),n=2+U.length+t.length;if(n>65535)return null;let r=new Uint8Array(2+n);return r[0]=255,r[1]=de,ae(r,2,n),r.set(U,4),r.set(t,4+U.length),r}function An(e){let t=new TextEncoder().encode(e),n=2+t.length;if(n>65535)return null;let r=new Uint8Array(2+n);return r[0]=255,r[1]=Le,ae(r,2,n),r.set(t,4),r}var q=new Uint8Array([137,80,78,71,13,10,26,10]);function Q(e,t){if(!V(e))return a.error({type:"invalidSignature"});if(In(e)===-1)return a.error({type:"noIhdrChunk"});let r=Rn(e),o=t.map(c=>c.type==="tEXt"?_n(c):Nn(c)),s=q.length+r.ihdr.length+o.reduce((c,m)=>c+m.length,0)+r.others.reduce((c,m)=>c+m.length,0),i=new Uint8Array(s),u=0;i.set(q,u),u+=q.length,i.set(r.ihdr,u),u+=r.ihdr.length;for(let c of o)i.set(c,u),u+=c.length;for(let c of r.others)i.set(c,u),u+=c.length;return a.ok(i)}function In(e){let t=q.length;if(t+8>e.length)return-1;let n=_(e,t);return E(e,t+4)!=="IHDR"?-1:t+4+4+n+4}function Rn(e){let t=[],n=q.length,r=new Uint8Array(0);for(;n<e.length;){let o=n;if(n+4>e.length)break;let s=_(e,n);if(n+=4,n+4>e.length)break;let i=E(e,n);if(n+=4,n+s+4>e.length)break;n+=s,n+=4;let u=n,c=e.slice(o,u);if(i==="IHDR"?r=c:i!=="tEXt"&&i!=="iTXt"&&t.push(c),i==="IEND")break}return{ihdr:r,others:t}}function _n(e){let t=On(e.keyword),n=Z(e.text),r=new Uint8Array(t.length+1+n.length);return r.set(t,0),r[t.length]=0,r.set(n,t.length+1),Xe("tEXt",r)}function Nn(e){let t=Z(e.keyword),n=Z(e.languageTag),r=Z(e.translatedKeyword),o=Z(e.text),s=t.length+1+1+1+n.length+1+r.length+1+o.length,i=new Uint8Array(s),u=0;return i.set(t,u),u+=t.length,i[u++]=0,i[u++]=e.compressionFlag,i[u++]=e.compressionMethod,i.set(n,u),u+=n.length,i[u++]=0,i.set(r,u),u+=r.length,i[u++]=0,i.set(o,u),Xe("iTXt",i)}function Xe(e,t){let n=new Uint8Array(8+t.length+4);ve(n,0,t.length),n.set(new TextEncoder().encode(e),4),n.set(t,8);let r=n.slice(4,8+t.length),o=Gn(r);return ve(n,8+t.length,o),n}function On(e){let t=new Uint8Array(e.length);for(let n=0;n<e.length;n++)t[n]=e.charCodeAt(n)&255;return t}function Z(e){return new TextEncoder().encode(e)}var Fn=Dn();function Dn(){let e=new Uint32Array(256);for(let t=0;t<256;t++){let n=t;for(let r=0;r<8;r++)n&1?n=3988292384^n>>>1:n=n>>>1;e[t]=n>>>0}return e}function Gn(e){let t=4294967295;for(let n=0;n<e.length;n++)t=(Fn[(t^(e[n]??0))&255]??0)^t>>>8;return(t^4294967295)>>>0}var Jn=new Uint8Array([82,73,70,70]),jn=new Uint8Array([87,69,66,80]);function ee(e,t){if(!$(e))return a.error({type:"invalidSignature"});let n=t.filter(f=>f.source.type==="xmpPacket"),r=t.filter(f=>f.source.type!=="xmpPacket"),o=Ln(e);if(!o.ok)return o;let{chunks:s}=o.value,i=Xn(r),u=n[0],c=u?Hn(u.data):null,m=[];i&&m.push(i),c&&m.push(c);let y=4;for(let f of s)y+=f.length;for(let f of m)y+=f.length;let x=new Uint8Array(8+y),h=0;x.set(Jn,h),h+=4,ue(x,h,y),h+=4,x.set(jn,h),h+=4;let S=!1;for(let f of s)if(x.set(f,h),h+=f.length,!S&&m.length>0&&Wn(f)){for(let d of m)x.set(d,h),h+=d.length;S=!0}if(!S)for(let f of m)x.set(f,h),h+=f.length;return a.ok(x)}function Wn(e){if(e.length<4)return!1;let t=E(e,0);return t==="VP8 "||t==="VP8L"||t==="VP8X"}function Ln(e){let t=[],n="",r=12;for(;r<e.length-8;){let o=E(e,r),s=N(e,r+4);if(n||(n=o),r+8+s>e.length)return a.error({type:"invalidRiffStructure",message:`Chunk extends beyond file at offset ${r}`});if(o!=="EXIF"&&o!=="XMP "){let u=s+s%2,c=e.slice(r,r+8+u);t.push(c)}let i=s+s%2;r+=8+i}return a.ok({chunks:t,firstChunkType:n})}function Xn(e){if(e.length===0)return null;let t=fe(e);if(t.length===0)return null;let n=t.length,r=n+n%2,o=new Uint8Array(8+r);return o.set(new TextEncoder().encode("EXIF")),ue(o,4,n),o.set(t,8),o}function Hn(e){let t=new TextEncoder().encode(e),n=t.length,r=n+n%2,o=new Uint8Array(8+r);return o.set(new TextEncoder().encode("XMP ")),ue(o,4,n),o.set(t,8),o}function Ae(e){return e.replace(/\r\n/g,`
|
|
2
2
|
`).replace(/\r/g,`
|
|
3
|
-
`)}function
|
|
4
|
-
`):void 0,e.negativePrompt?`Negative prompt: ${
|
|
5
|
-
`)}function
|
|
3
|
+
`)}function zn(e){return e.flatMap((t,n)=>{let r=t.center?` [${t.center.x}, ${t.center.y}]`:"";return[`# Character ${n+1}${r}:`,Ae(t.prompt)]})}function Vn(e,t){let n=e.hires??e.upscale,r={Steps:e.sampling?.steps,Sampler:e.sampling?.sampler,"Schedule type":e.sampling?.scheduler,"CFG scale":e.sampling?.cfg,Seed:e.sampling?.seed,Size:e.width>0&&e.height>0?`${e.width}x${e.height}`:void 0,"Model hash":e.model?.hash,Model:e.model?.name,"Clip skip":e.sampling?.clipSkip,"Denoising strength":n?.denoise,"Hires upscale":n?.scale,"Hires steps":n?.steps,"Hires upscaler":n?.upscaler};return Object.entries({...r,...t}).filter(([,o])=>o!==void 0).map(([o,s])=>`${o}: ${s}`).join(", ")}function H(e){return[Ae(e.prompt),e.characterPrompts?.length?zn(e.characterPrompts).join(`
|
|
4
|
+
`):void 0,e.negativePrompt?`Negative prompt: ${Ae(e.negativePrompt)}`:void 0,Vn(e,e.extras)||void 0].filter(t=>t!==void 0).join(`
|
|
5
|
+
`)}function Ie(e){switch(e.format){case"png":return e.chunks.map(t=>t.text).join(`
|
|
6
6
|
|
|
7
7
|
`);case"jpeg":case"webp":return e.segments.map(t=>t.data).join(`
|
|
8
8
|
|
|
9
|
-
`)}}function
|
|
9
|
+
`)}}function He(e){if("status"in e)switch(e.status){case"success":return H(e.metadata);case"unrecognized":return Ie(e.raw);case"empty":case"invalid":return""}return H(e)}function Re(e,t){let n=W(e),r=L(n);if(!r)return a.error({type:"unsupportedFormat"});let o=H(t),s=Bn(r,n,o);return s.ok?a.ok(s.value):a.error({type:"writeFailed",message:s.error.type})}function Bn(e,t,n){switch(e){case"png":return Q(t,k("parameters",n,"dynamic"));case"jpeg":return Y(t,[{source:{type:"exifUserComment"},data:n}]);case"webp":return ee(t,[{source:{type:"exifUserComment"},data:n}])}}function g(e){let t=Object.fromEntries(Object.entries(e).filter(([,n])=>n!==void 0));return Object.keys(t).length===0?void 0:t}function te(e,t){let n=e.parameters??e.UserComment;if(!n)return a.error({type:"unsupportedFormat"});let{prompt:r,negativePrompt:o,settings:s}=$n(n),i=Kn(s),u=i.get("Size")??"0x0",[c,m]=ze(u),y=i.get("Model"),x=i.get("Model hash"),h=i.get("Sampler"),S=i.get("Schedule type"),f=F(i.get("Steps")),d=F(i.get("CFG scale")??i.get("CFG Scale")),Ce=F(i.get("Seed")),v=F(i.get("Clip skip")),T=F(i.get("Hires upscale")),l=i.get("Hires upscaler"),C=F(i.get("Hires steps")),cn=F(i.get("Denoising strength")),pn=i.get("Hires size"),[De]=ze(pn??""),mn=T??(De>0?De/c:void 0);return a.ok({software:t,prompt:r,negativePrompt:o,width:c,height:m,model:g({name:y,hash:x}),sampling:g({sampler:h,scheduler:S,steps:f,cfg:d,seed:Ce,clipSkip:v}),hires:g({scale:mn,upscaler:l,steps:C,denoise:cn})})}function $n(e){let t=e.indexOf("Negative prompt:"),n=e.indexOf("Steps:");if(t===-1&&n===-1)return{prompt:e.trim(),negativePrompt:"",settings:""};if(t===-1){let o=e.lastIndexOf(`
|
|
10
10
|
`,n);return{prompt:e.slice(0,o).trim(),negativePrompt:"",settings:e.slice(o).trim()}}if(n===-1)return{prompt:e.slice(0,t).trim(),negativePrompt:e.slice(t+16).trim(),settings:""};let r=e.lastIndexOf(`
|
|
11
|
-
`,n);return{prompt:e.slice(0,t).trim(),negativePrompt:e.slice(t+16,r).trim(),settings:e.slice(r).trim()}}function Nn(e){let t=new Map;if(!e||e.length>1e4)return t;let n=/([A-Za-z][A-Za-z0-9 ]*?):\s*([^,]+?)(?=,\s*[A-Za-z][A-Za-z0-9 ]*?:|$)/g;for(let r of e.matchAll(n)){let o=(r[1]??"").trim(),s=(r[2]??"").trim();t.set(o,s)}return t}function Je(e){let t=e.match(/(\d+)x(\d+)/);return t?[Number.parseInt(t[1]??"0",10),Number.parseInt(t[2]??"0",10)]:[0,0]}function O(e){if(e===void 0)return;let t=Number.parseFloat(e);return Number.isNaN(t)?void 0:t}function p(e){try{let t=JSON.parse(e);return On(t)}catch{return{ok:!1,error:{type:"parseError",message:"Invalid JSON"}}}}function On(e){if(e===null)return{ok:!0,type:"null",value:null};if(Array.isArray(e))return{ok:!0,type:"array",value:e};switch(typeof e){case"object":return{ok:!0,type:"object",value:e};case"string":return{ok:!0,type:"string",value:e};case"number":return{ok:!0,type:"number",value:e};case"boolean":return{ok:!0,type:"boolean",value:e};default:return{ok:!1,error:{type:"parseError",message:"Unexpected JSON type"}}}}var We=["KSampler","KSamplerAdvanced","SamplerCustomAdvanced","SamplerCustom","DetailerForEach"],Fn=["EmptyLatentImage"],Dn=["SDXL Empty Latent Image (rgthree)"],Gn=["CheckpointLoaderSimple","CheckpointLoader"],Jn=["UNETLoader"],Wn=["CLIPSetLastLayer"],jn=["UpscaleModelLoader"],je=["ImageScale","ImageScaleBy"],Le=["LatentUpscale","LatentUpscaleBy"],He=["VAEEncode","VAEEncodeTiled"];function ze(e){let t={};for(let n of Object.values(e)){let r=n.class_type;!t.sampler&&We.includes(r)?t.sampler=n:!t.latentImage&&Fn.includes(r)?t.latentImage=n:!t.latentImageRgthree&&Dn.includes(r)?t.latentImageRgthree=n:!t.checkpoint&&Gn.includes(r)?t.checkpoint=n:!t.unetLoader&&Jn.includes(r)?t.unetLoader=n:!t.clipSetLastLayer&&Wn.includes(r)?t.clipSetLastLayer=n:!t.hiresModelUpscale&&jn.includes(r)?t.hiresModelUpscale=n:!t.hiresImageScale&&je.includes(r)?t.hiresImageScale=n:!t.latentUpscale&&Le.includes(r)?t.latentUpscale=n:!t.vaeEncode&&He.includes(r)&&(t.vaeEncode=n)}return t}function F(e,t){if(A(t))return e[String(t[0])]}function A(e){return Array.isArray(e)&&e.length===2&&(typeof e[0]=="string"||typeof e[0]=="number")&&typeof e[1]=="number"}function Ve(e,t,n=10){if(n<=0)return"";let r=e[t];if(!r)return"";let o=r.inputs.text??r.inputs.prompt??r.inputs.Text;return typeof o=="string"?o:A(o)?Ve(e,String(o[0]),n-1):""}function Ln(e,t){let n=F(e,t.inputs.guider);return n||t}function Be(e,t){if(!t)return{promptText:"",negativeText:""};let n=Ln(e,t);return{promptText:Me(e,n.inputs.positive,"positive"),negativeText:Me(e,n.inputs.negative,"negative")}}function Me(e,t,n,r=10){if(r<=0||!A(t))return"";let o=String(t[0]),s=Ve(e,o);if(s)return s;let i=e[o];if(!i)return"";let u=i.inputs[n];return Me(e,u,n,r-1)}function Xe(e,t){if(e){let n=Number(e.inputs.width)||0,r=Number(e.inputs.height)||0;if(n>0&&r>0)return{width:n,height:r}}if(t&&typeof t.inputs.dimensions=="string"){let n=t.inputs.dimensions.match(/^(\d+)\s*x\s*(\d+)/);if(n?.[1]&&n[2])return{width:Number.parseInt(n[1],10),height:Number.parseInt(n[2],10)}}return{width:0,height:0}}function Ue(e,t){if(!t)return;if(t.class_type==="SamplerCustomAdvanced")return Hn(e,t);if(t.class_type==="SamplerCustom")return zn(e,t);let n=t.inputs.seed;return A(n)&&(n=e[String(n[0])]?.inputs.seed),{seed:n,steps:t.inputs.steps,cfg:t.inputs.cfg,sampler:t.inputs.sampler_name,scheduler:t.inputs.scheduler,denoise:t.inputs.denoise}}function Hn(e,t){let n=F(e,t.inputs.noise),r=F(e,t.inputs.guider),o=F(e,t.inputs.sampler),s=F(e,t.inputs.sigmas);return{seed:n?.inputs.noise_seed,steps:s?.inputs.steps,cfg:r?.inputs.cfg,sampler:o?.inputs.sampler_name,scheduler:s?.inputs.scheduler,denoise:s?.inputs.denoise}}function zn(e,t){let n=F(e,t.inputs.sampler),r=F(e,t.inputs.sigmas),o=t.inputs.noise_seed;return A(o)&&(o=e[String(o[0])]?.inputs.seed),{seed:o,steps:r?.inputs.steps,cfg:t.inputs.cfg,sampler:n?.inputs.sampler_name,scheduler:r?.inputs.scheduler,denoise:r?.inputs.denoise}}function $e(e,t){if(e?.inputs?.ckpt_name)return{name:String(e.inputs.ckpt_name)};if(t?.inputs?.unet_name)return{name:String(t.inputs.unet_name)}}function Ke(e){if(!e)return;let t=e.inputs.stop_at_clip_layer;if(typeof t=="number")return-t}function ae(e,t){if(!(t<=0||e<=0))return Math.round(e/t*100)/100}function Vn(e,t){let n=t.inputs.latent_image;if(!A(n))return!1;let r=e[String(n[0])];if(!r)return!1;if(Le.includes(r.class_type))return!0;if(!He.includes(r.class_type))return!1;let o=r.inputs.pixels;if(!A(o))return!1;let s=e[String(o[0])];return s?je.includes(s.class_type):!1}function Ye(e){return Object.values(e).find(t=>We.includes(t.class_type)&&Vn(e,t))}function Ze(e,t){let n=e.extraMetadata;if(typeof n=="string"){let r=p(n);if(r.ok&&r.type==="object")return r.value}if(t?.extraMetadata){let r=p(t.extraMetadata);if(r.ok&&r.type==="object")return r.value}}function qe(e){if(!e)return;let t=Bn(e),n=Xn(e);return d({prompt:e.prompt,negativePrompt:e.negativePrompt,width:e.width,height:e.height,model:e.baseModel?{name:e.baseModel}:void 0,...n,...t})}function Bn(e){if(!e.transformations)return{};let t=e.transformations.find(r=>r.type==="upscale");if(!t?.upscaleWidth)return{};let n=ae(t.upscaleWidth,e.width??0);return n===void 0?{}:{upscale:{scale:n}}}function Xn(e){return e.seed===void 0&&e.steps===void 0&&e.cfgScale===void 0&&e.sampler===void 0?{}:{sampling:{seed:e.seed,steps:e.steps,cfg:e.cfgScale,sampler:e.sampler}}}var $n=["extra","extraMetadata","resource-stack"];function te(e){let t=Kn(e);if(!t)return a.error({type:"unsupportedFormat"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in prompt entry"});let r=n.value,o=Pe(r),s=qe(Ze(r,e)),i=qn(s,o);return a.ok({software:"comfyui",nodes:Object.fromEntries(Object.entries(r).filter(([u])=>!$n.includes(u))),...i})}function Qe(e){if(!e||typeof e.denoise!="number")return e;if(e.denoise>=1){let{denoise:t,...n}=e;return n}return e}function et(e){return e.replace(/\0+$/,"").replace(/:\s*NaN\b/g,": null")}function Kn(e){if(e.prompt)return et(e.prompt);let t=[e.UserComment,e.ImageDescription,e.Make,e.Prompt,e.Workflow];for(let n of t)if(n&&n.startsWith("{")){let r=et(n),o=p(r);if(!o.ok||o.type!=="object")continue;if(o.value.prompt&&typeof o.value.prompt=="object")return JSON.stringify(o.value.prompt);if(Object.values(o.value).some(i=>i&&typeof i=="object"&&"class_type"in i))return r}}function Pe(e){let t=ze(e),{promptText:n,negativeText:r}=Be(e,t.sampler),{width:o,height:s}=Xe(t.latentImage,t.latentImageRgthree),i=Ye(e),u=Qe(i?Ue(e,i):void 0),c=Yn(e,t,o),m=t.hiresModelUpscale?.inputs.model_name,g=Qe(Ue(e,t.sampler)),y=Ke(t.clipSetLastLayer);return d({prompt:n||void 0,negativePrompt:r||void 0,width:o>0?o:void 0,height:s>0?s:void 0,model:$e(t.checkpoint,t.unetLoader),sampling:d({...g,clipSkip:y}),...Zn(m,c,u)})}function Yn(e,t,n){let r=t.latentUpscale?.inputs;if(r?.scale_by!==void 0)return r.scale_by;let o=t.hiresImageScale?.inputs.width;if(o!==void 0){if(A(o)){let s=e[String(o[0])];return typeof s?.inputs.clip_scale=="number"?s.inputs.clip_scale:void 0}if(typeof o=="number")return ae(o,n)}}function Zn(e,t,n){return!e&&t===void 0&&!n?{}:n?{hires:{upscaler:e,scale:t,steps:n.steps,denoise:n.denoise}}:e?{upscale:{upscaler:e,scale:t}}:{}}function tt(e,t){if(!e&&!t)return;let n={};for(let r of[e,t])if(r)for(let[o,s]of Object.entries(r))s!==void 0&&(n[o]=s);return Object.keys(n).length>0?n:void 0}function qn(e,t){let n=tt(e?.upscale,t?.upscale),r=tt(e?.hires,t?.hires);return{prompt:t?.prompt??e?.prompt??"",negativePrompt:t?.negativePrompt??e?.negativePrompt??"",width:t?.width??e?.width??0,height:t?.height??e?.height??0,...d({model:t?.model??e?.model,sampling:t?.sampling??e?.sampling,hires:r,upscale:n})}}var Qn="invokeai_metadata",er="generation_data",tr="smproj",Ae="extraMetadata",z="sui_image_params",st="swarm_version",nr="class_type",nt="noise_schedule",rt="v4_prompt",rr="uncond_scale",or="civitai:",sr="Civitai resources:",ot="RuinedFooocus",ir="use_stable_diffusion_model",ar='"Model"',ur='"resolution"',cr='"base_model"';function it(e){let t=pr(e);if(t)return t;let n=fr(e);if(n)return n;let r=e.parameters??e.UserComment??e.Comment;return r?dr(r):null}function at(e){return Qn in e?"invokeai":er in e?"tensorart":tr in e?"stability-matrix":Ae in e?"civitai":null}function pr(e){if(e.Software?.startsWith("NovelAI"))return"novelai";let t=at(e);if(t)return t;if("fooocus_scheme"in e)return"fooocus";if("use_stable_diffusion_model"in e)return"easydiffusion";if(e.parameters?.includes(z))return"swarmui";let r=e.UserComment??e.Comment;return r?.startsWith("{")?mr(r):null}function mr(e){let t=p(e);if(!t.ok||t.type!=="object")return null;let n=t.value,r=at(n);if(r)return r;if("prompt"in n&&"workflow"in n){let o=n.workflow,s=n.prompt,i=typeof o=="object"||typeof s=="object",u=typeof o=="string"&&o.startsWith("{")||typeof s=="string"&&s.startsWith("{");if(i||u)return"comfyui"}if(z in n)return"swarmui";if("prompt"in n&&"parameters"in n){let o=String(n.parameters||"");if(o.includes(z)||o.includes(st))return"swarmui"}return null}function fr(e){if("prompt"in e&&"workflow"in e||"Prompt"in e&&"Workflow"in e||"workflow"in e||"Workflow"in e)return"comfyui";let t=e.prompt??e.Prompt;if(t?.startsWith("{")){if(t.includes(z))return"swarmui";if(t.includes(`"${Ae}"`))return"civitai";if(t.includes(nr))return"comfyui"}return null}function dr(e){return e.startsWith("{")?lr(e):gr(e)}function lr(e){return e.includes(z)?"swarmui":e.includes(`"software":"${ot}"`)||e.includes(`"software": "${ot}"`)?"ruined-fooocus":e.includes(`"${ir}"`)?"easydiffusion":e.includes(or)||e.includes(`"${Ae}"`)?"civitai":e.includes(`"${rt}"`)||e.includes(`"${nt}"`)||e.includes(`"${rr}"`)||e.includes('"Software":"NovelAI"')||e.includes(`\\"${nt}\\"`)||e.includes(`\\"${rt}\\"`)?"novelai":e.includes(ar)&&e.includes(ur)?"hf-space":e.includes('"prompt"')&&e.includes(cr)?"fooocus":e.includes('"prompt"')||e.includes('"nodes"')?"comfyui":null}function gr(e){if(e.includes(z)||e.includes(st))return"swarmui";let t=e.match(/Version:\s*([^\s,]+)/);if(t){let n=t[1];if(n&&/^v\d/.test(n))return"sd-webui";if(n==="classic")return"forge-classic";if(n==="neo"||n?.startsWith("neo"))return"forge-neo";if(n?.startsWith("f")&&/^f\d/.test(n))return/^f\d+\.\d+(\.\d+)?-v/.test(n)?"easy-reforge":/^f\d+\.\d+(\.\d+)?v\d+-v/.test(n)?"reforge":"forge";if(n?.startsWith("Fooocus"))return"fooocus";if(n==="ComfyUI")return"comfyui"}return e.includes("App: SD.Next")||e.includes("App:SD.Next")?"sd-next":e.includes(sr)?"civitai":e.includes("Steps:")||e.includes("Sampler:")||e.includes("Negative prompt:")?"sd-webui":null}function yr(e){if(!e)return;let t=e.replace(/\\/g,"/").split("/");return t[t.length-1]}function ct(e){if("use_stable_diffusion_model"in e)return ut(e);let t=e.UserComment?.startsWith("{")?e.UserComment:e.parameters?.startsWith("{")?e.parameters:void 0;if(!t)return a.error({type:"unsupportedFormat"});let n=p(t);return!n.ok||n.type!=="object"?a.error({type:"parseError",message:"Invalid JSON in Easy Diffusion metadata"}):ut(n.value)}function ut(e){let t=c=>{let m=e[c];return typeof m=="string"?m:void 0},n=c=>Number(e[c])||void 0,r=(t("prompt")??"").trim(),o=(t("negative_prompt")??"").trim(),s=t("use_stable_diffusion_model"),i=d({upscaler:t("use_upscale"),scale:n("upscale_amount")}),u={software:"easydiffusion",prompt:r,negativePrompt:o,width:n("width")??0,height:n("height")??0,model:d({name:yr(s),vae:t("use_vae_model")}),sampling:d({sampler:t("sampler_name"),steps:n("num_inference_steps"),cfg:n("guidance_scale"),seed:n("seed"),clipSkip:n("clip_skip"),denoise:n("prompt_strength")}),...i?{upscale:i}:{}};return a.ok(u)}function hr(e){if(!e)return{width:0,height:0};let t=e.match(/\(\s*(\d+)\s*,\s*(\d+)\s*\)/);return!t?.[1]||!t[2]?{width:0,height:0}:{width:Number(t[1]),height:Number(t[2])}}function pt(e){let t=e.parameters??e.UserComment;if(!t||!t.startsWith("{"))return a.error({type:"unsupportedFormat"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in Fooocus metadata"});let r=n.value,{width:o,height:s}=hr(r.resolution),i={software:"fooocus",prompt:r.prompt?.trim()??"",negativePrompt:r.negative_prompt?.trim()??"",width:o,height:s,model:d({name:r.base_model,hash:r.base_model_hash,vae:r.vae}),sampling:d({sampler:r.sampler,scheduler:r.scheduler,steps:r.steps,cfg:r.guidance_scale,seed:r.seed,clipSkip:r.clip_skip})};return a.ok(i)}function mt(e){let t=e.parameters??e.UserComment;if(!t)return a.error({type:"unsupportedFormat"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in parameters entry"});let r=n.value,o=c=>{let m=c?.match(/(\d+)\s*x\s*(\d+)/);return m?.[1]&&m?.[2]?{width:Number.parseInt(m[1],10),height:Number.parseInt(m[2],10)}:{width:0,height:0}},{width:s,height:i}=o(r.resolution),u={software:"hf-space",prompt:r.prompt??"",negativePrompt:r.negative_prompt??"",width:s,height:i,model:d({name:r.Model,hash:r["Model hash"]}),sampling:d({sampler:r.sampler,steps:r.num_inference_steps,cfg:r.guidance_scale,seed:r.seed}),hires:r.use_upscaler?d({upscaler:r.use_upscaler.upscale_method,denoise:r.use_upscaler.upscaler_strength,scale:r.use_upscaler.upscale_by,steps:r.use_upscaler.upscale_steps}):void 0};return a.ok(u)}function D(e,t){if(!e.UserComment?.startsWith("{"))return;let n=p(e.UserComment);if(!n.ok||n.type!=="object")return;let r=n.value[t];if(typeof r=="string")return r;if(typeof r=="object"&&r!==null)return JSON.stringify(r)}function xr(e){return e.invokeai_metadata??D(e,"invokeai_metadata")}function ft(e){let t=xr(e);if(!t)return a.error({type:"unsupportedFormat"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in invokeai_metadata entry"});let r=n.value,o=r.width??0,s=r.height??0,i=Sr(r);return a.ok({software:"invokeai",prompt:r.positive_prompt??"",negativePrompt:r.negative_prompt??"",width:o,height:s,model:d({name:r.model?.name,hash:r.model?.hash}),sampling:d({seed:r.seed,steps:r.steps,cfg:r.cfg_scale,sampler:r.scheduler}),...i})}function Sr(e){if(!e.upscale_model?.name&&e.upscale_scale===void 0)return{};let t=d({upscaler:e.upscale_model?.name,scale:e.upscale_scale});return t?{upscale:t}:{}}function dt(e){let t=e.UserComment??e.Comment;if(!t)return a.error({type:"parseError",message:"Missing Comment/UserComment entry"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in Comment entry"});let r=n.value,o=r.width??0,s=r.height??0,i=r.v4_prompt?.caption?.base_caption??r.prompt??"",u=r.v4_negative_prompt?.caption?.base_caption??r.uc??"",c=r.v4_prompt?.caption?.char_captions,m=c&&c.length>0?c.map(g=>g.char_caption?{prompt:g.char_caption,center:g.centers?.[0]}:null).filter(g=>g!==null):void 0;return a.ok({software:"novelai",prompt:i,negativePrompt:u,width:o,height:s,sampling:d({steps:r.steps,cfg:r.scale,seed:r.seed,sampler:r.sampler,scheduler:r.noise_schedule}),characterPrompts:m,useCoords:m?r.v4_prompt?.use_coords:void 0,useOrder:m?r.v4_prompt?.use_order:void 0})}function lt(e){let t=e.parameters??e.UserComment;if(!t||!t.startsWith("{"))return a.error({type:"unsupportedFormat"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in Ruined Fooocus metadata"});let r=n.value,o={software:"ruined-fooocus",prompt:r.Prompt?.trim()??"",negativePrompt:r.Negative?.trim()??"",width:r.width??0,height:r.height??0,model:{name:r.base_model_name,hash:r.base_model_hash},sampling:{sampler:r.sampler_name,scheduler:r.scheduler,steps:r.steps,cfg:r.cfg,seed:r.seed,clipSkip:r.clip_skip}};return a.ok(o)}function gt(e){let t=te(e);if(!t.ok||t.value.software!=="comfyui")return a.error({type:"unsupportedFormat"});let n=e["parameters-json"]??D(e,"parameters-json"),r=n?p(n):void 0,o=r?.ok&&r.type==="object"?r.value:void 0;return a.ok({...t.value,software:"stability-matrix",prompt:o?.PositivePrompt??t.value.prompt,negativePrompt:o?.NegativePrompt??t.value.negativePrompt,model:o?.ModelName!==void 0||o?.ModelHash!==void 0?{name:o?.ModelName,hash:o?.ModelHash}:t.value.model})}function br(e){if(e.parameters)return e.parameters;if(!e.UserComment)return;let t=p(e.UserComment);if(!(!t.ok||t.type!=="object")&&"sui_image_params"in t.value)return e.UserComment}function yt(e){let t=br(e);if(!t)return a.error({type:"unsupportedFormat"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in parameters entry"});let r=n.value.sui_image_params;if(!r)return a.error({type:"unsupportedFormat"});let o=r.width??0,s=r.height??0,i=e.prompt||e.Make,u=i?p(i):void 0,c=u?.ok&&u.type==="object"?u.value:void 0;return a.ok({software:"swarmui",prompt:r.prompt??"",negativePrompt:r.negativeprompt??"",width:o,height:s,nodes:c,model:d({name:r.model}),sampling:d({seed:r.seed,steps:r.steps,cfg:r.cfgscale,sampler:r.sampler,scheduler:r.scheduler}),hires:d({scale:r.refinerupscale,upscaler:r.refinerupscalemethod,denoise:r.refinercontrolpercentage})})}function ht(e){let t=e.generation_data??D(e,"generation_data"),n=e.prompt??D(e,"prompt");if(!t)return a.error({type:"unsupportedFormat"});let r=t.replace(/\0+$/,""),o=p(r);if(!o.ok||o.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in generation_data entry"});let s=o.value;if(!n)return a.error({type:"unsupportedFormat"});let i=p(n);if(!i.ok||i.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in prompt chunk"});let u=i.value,c=Pe(u),m=d({name:s.baseModel?.modelFileName,hash:s.baseModel?.hash});return a.ok({software:"tensorart",nodes:u,prompt:s.prompt??c?.prompt??"",negativePrompt:s.negativePrompt??c?.negativePrompt??"",width:c?.width??0,height:c?.height??0,model:m??c?.model,sampling:c?.sampling,hires:c?.hires,upscale:c?.upscale})}function xt(e){let t=it(e);switch(t){case"novelai":return dt(e);case"sd-webui":case"forge":case"forge-classic":case"forge-neo":case"reforge":case"easy-reforge":case"sd-next":return ee(e,t);case"hf-space":return mt(e);case"civitai":{let n=te(e);return n.ok?a.ok({...n.value,software:"civitai"}):ee(e,"civitai")}case"comfyui":{let n=te(e);return n.ok?n:ee(e,"sd-webui")}case"invokeai":return ft(e);case"swarmui":return yt(e);case"tensorart":return ht(e);case"stability-matrix":return gt(e);case"easydiffusion":return ct(e);case"fooocus":{let n=e.fooocus_scheme,r=e.parameters??e.UserComment;return n==="a1111"||r&&!r.startsWith("{")?ee(e,"fooocus"):pt(e)}case"ruined-fooocus":return lt(e);default:return a.error({type:"unsupportedFormat"})}}function St(e,t){return t==="png"?kr(e):t==="jpeg"?Cr(e):wr(e)}function kr(e){return e.length<24?null:{width:_(e,16),height:_(e,20)}}function Cr(e){let t=2;for(;t<e.length-4;){if(e[t]!==255){t++;continue}let n=e[t+1]??0;if(n===255){t++;continue}let r=P(e,t+2);if(n>=192&&n<=207&&n!==196&&n!==200&&n!==204){let o=P(e,t+5);return{width:P(e,t+7),height:o}}if(t+=2+r,n===218)break}return null}function wr(e){let t=12;for(;t<e.length&&!(t+8>e.length);){let n=T(e,t),r=R(e,t+4),o=r+r%2;if(n==="VP8X"){let s=oe(e,t+12),i=oe(e,t+15);return{width:s+1,height:i+1}}if(n==="VP8 "){let s=t+8;if(!(oe(e,s)&1)&&e[s+3]===157&&e[s+4]===1&&e[s+5]===42){let c=U(e,s+6,!0),m=U(e,s+8,!0);return{width:c&16383,height:m&16383}}}if(n==="VP8L"&&e[t+8]===47){let s=R(e,t+9),i=(s&16383)+1,u=(s>>14&16383)+1;return{width:i,height:u}}t+=8+o}return null}function ue(e){if(e.length<8)return[];let t=e[0]===73&&e[1]===73,n=e[0]===77&&e[1]===77;if(!t&&!n)return[];if(U(e,2,t)!==42)return[];let o=$(e,4,t),s=bt(e,o,t),i=Tr(e,o,t),u=i!==null?bt(e,i,t):[];return[...s,...u]}function bt(e,t,n){let r=[];if(t+2>e.length)return r;let o=U(e,t,n),s=t+2;for(let i=0;i<o;i++){if(s+12>e.length)return r;let u=U(e,s,n),c=U(e,s+2,n),m=$(e,s+4,n),g=vr(c),y=m*g;if(y>e.length){s+=12;continue}let l;if(y<=4?l=s+8:l=$(e,s+8,n),l+y>e.length){s+=12;continue}let I=e.slice(l,l+y);if(u===270){let x=Ct(I);if(x){let C=kt(x);r.push({source:{type:"exifImageDescription",prefix:C??void 0},data:C?x.slice(C.length+2):x})}}else if(u===271){let x=Ct(I);if(x){let C=kt(x);r.push({source:{type:"exifMake",prefix:C??void 0},data:C?x.slice(C.length+2):x})}}else if(u===37510){let x=Er(I);x&&r.push({source:{type:"exifUserComment"},data:x})}s+=12}return r}function kt(e){return e.match(/^([A-Za-z]+):\s/)?.[1]??null}function vr(e){switch(e){case 1:return 1;case 2:return 1;case 3:return 2;case 4:return 4;case 5:return 8;case 7:return 1;default:return 1}}function Ct(e){try{let n=new TextDecoder("utf-8",{fatal:!1}).decode(e);return n.endsWith("\0")&&(n=n.slice(0,-1)),n.trim()||null}catch{return null}}function Tr(e,t,n){if(t+2>e.length)return null;let r=U(e,t,n),o=t+2;for(let s=0;s<r;s++){if(o+12>e.length)return null;if(U(e,o,n)===34665)return $(e,o+8,n);o+=12}return null}function Er(e){if(e.length<8)return null;if(e[0]===85&&e[1]===78&&e[2]===73&&e[3]===67&&e[4]===79&&e[5]===68&&e[6]===69&&e[7]===0){let t=e.slice(8);return t.length>=2&&t[0]!==0&&t[1]===0?Mr(t):wt(t)}if(e[0]===65&&e[1]===83&&e[2]===67&&e[3]===73&&e[4]===73&&e[5]===0&&e[6]===0&&e[7]===0)return Ur(e.slice(8));try{let n=new TextDecoder("utf-8",{fatal:!0}).decode(e);return n.endsWith("\0")&&(n=n.slice(0,-1)),n}catch{return null}}function wt(e){let t=new TextDecoder("utf-16be").decode(e),n=t.indexOf("\0");return n>=0?t.slice(0,n):t}function Mr(e){let t=new TextDecoder("utf-16le").decode(e),n=t.indexOf("\0");return n>=0?t.slice(0,n):t}function Ur(e){let t=e.indexOf(0),n=t>=0?e.subarray(0,t):e;return new TextDecoder("ascii").decode(n)}var Pr=225,Ar=254;function Ir(e,t){return e[t]===69&&e[t+1]===120&&e[t+2]===105&&e[t+3]===102&&e[t+4]===0&&e[t+5]===0}function vt(e){if(!B(e))return a.error({type:"invalidSignature"});let t=[],n=_r(e);if(n){let o=e.slice(n.offset,n.offset+n.length),s=ue(o);t.push(...s)}let r=Rr(e);if(r){let o=e.slice(r.offset,r.offset+r.length),s=Nr(o);s!==null&&t.push({source:{type:"jpegCom"},data:s})}return a.ok(t)}function _r(e){let t=2;for(;t<e.length-4;){if(e[t]!==255){t++;continue}let n=e[t+1];if(n===255){t++;continue}let r=P(e,t+2);if(n===Pr&&r>=8){let o=t+4;if(o+6<=e.length&&Ir(e,o))return{offset:o+6,length:r-8}}if(t+=2+r,n===218||n===217)break}return null}function Rr(e){let t=2;for(;t<e.length-4;){if(e[t]!==255){t++;continue}let n=e[t+1];if(n===255){t++;continue}let r=P(e,t+2);if(n===Ar&&r>=2)return{offset:t+4,length:r-2};if(t+=2+r,n===218||n===217)break}return null}function Nr(e){try{return new TextDecoder("utf-8",{fatal:!0}).decode(e)}catch{return null}}function Et(e){if(!V(e))return a.error({type:"invalidSignature"});let t=Fr(e);return t.ok?a.ok(t.value):t}var Or=8;function Fr(e){let t=[],n=Or;for(;n<e.length;){if(n+4>e.length)return a.error({type:"corruptedChunk",message:"Unexpected end of file while reading chunk length"});let r=_(e,n);if(n+=4,n+4>e.length)return a.error({type:"corruptedChunk",message:"Unexpected end of file while reading chunk type"});let o=T(e,n);if(n+=4,n+r>e.length)return a.error({type:"corruptedChunk",message:`Unexpected end of file while reading chunk data (${o})`});let s=e.slice(n,n+r);if(n+=r,n+=4,o==="tEXt"){let i=Dr(s);i&&t.push(i)}else if(o==="iTXt"){let i=Jr(s);i&&t.push(i)}if(o==="IEND")break}return a.ok(t)}function Dr(e){let t=e.indexOf(0);if(t===-1)return null;let n=Tt(e.slice(0,t)),r=e.slice(t+1),o=Gr(r)??Tt(r);return{type:"tEXt",keyword:n,text:o}}function Gr(e){try{return new TextDecoder("utf-8",{fatal:!0}).decode(e)}catch{return null}}function Jr(e){let t=0,n=e.indexOf(0,t);if(n===-1)return null;let r=ne(e.slice(t,n));if(t=n+1,t>=e.length)return null;let o=e[t]??0;if(t+=1,t>=e.length)return null;let s=e[t]??0;t+=1;let i=e.indexOf(0,t);if(i===-1)return null;let u=ne(e.slice(t,i));t=i+1;let c=e.indexOf(0,t);if(c===-1)return null;let m=ne(e.slice(t,c));t=c+1;let g;if(o===1){let y=Wr(e.slice(t));if(!y)return null;g=ne(y)}else g=ne(e.slice(t));return{type:"iTXt",keyword:r,compressionFlag:o,compressionMethod:s,languageTag:u,translatedKeyword:m,text:g}}function Tt(e){return new TextDecoder("iso-8859-1").decode(e)}function ne(e){return new TextDecoder("utf-8").decode(e)}function Wr(e){return null}function Mt(e){if(!X(e))return a.error({type:"invalidSignature"});let t=jr(e);if(!t)return a.ok([]);let n=e.slice(t.offset,t.offset+t.length),r=ue(n);return a.ok(r)}function jr(e){let t=12;for(;t+8<=e.length;){let n=R(e,t+4);if(T(e,t)==="EXIF")return{offset:t+8,length:n};let r=n+n%2;t+=8+r}return null}function Ut(e){return Object.fromEntries(e.map(t=>[t.keyword,t.text]))}function Pt(e){let t={};for(let n of e){let r=Hr(n.source),o=n.data;if(n.source.type==="exifUserComment"&&o.startsWith("{")){let s=Lr(o);if(s){Object.assign(t,s);continue}}t[r]=o}return t}function Lr(e){let t=p(e);if(!t.ok||t.type!=="object")return null;let n=t.value;if(typeof n.Software=="string"&&!n.Software.startsWith("NovelAI")||typeof n.Comment!="string")return null;let r=p(n.Comment);return{Software:typeof n.Software=="string"?n.Software:"NovelAI",Comment:r.ok?JSON.stringify(r.value):n.Comment}}function Hr(e){switch(e.type){case"jpegCom":return"Comment";case"exifUserComment":return"UserComment";case"exifImageDescription":return e.prefix??"ImageDescription";case"exifMake":return e.prefix??"Make"}}function At(e,t){let n=W(e),r=j(n);if(!r)return{status:"invalid",message:"Unknown image format"};let o=zr(n,r);if(o.status!=="success")return o;let s=o.raw,i=s.format==="png"?Ut(s.chunks):Pt(s.segments),u=xt(i);if(!u.ok)return{status:"unrecognized",raw:s};let c=u.value;if(!t?.strict&&(c.width===0||c.height===0)){let m=St(n,r);m&&(c.width=c.width||m.width,c.height=c.height||m.height)}return{status:"success",metadata:c,raw:s}}function zr(e,t){let n=t==="png"?Et(e):t==="jpeg"?vt(e):Mt(e);return n.ok?n.value.length===0?{status:"empty"}:t==="png"?{status:"success",raw:{format:"png",chunks:n.value}}:{status:"success",raw:{format:t,segments:n.value}}:{status:"invalid",message:n.error.type==="invalidSignature"?`Invalid ${t.toUpperCase()} signature`:n.error.message}}function ce(e){let t=e.find(n=>n.keyword==="parameters");return t?[{source:{type:"exifUserComment"},data:t.text}]:[]}function pe(e){let t=e.find(n=>n.source.type==="exifUserComment");return t?h("parameters",t.data,"dynamic"):[]}function It(e){let t=e.find(r=>r.keyword==="parameters");if(t&&!t.text.trimStart().startsWith("{"))return ce(e);let n={};for(let r of e)if(r.keyword==="prompt"){let o=p(r.text);o.ok&&o.type==="object"&&Object.assign(n,o.value)}else if(r.keyword==="extraMetadata")n[r.keyword]=r.text;else{let o=p(r.text);n[r.keyword]=o.ok?o.value:r.text}return[{source:{type:"exifUserComment"},data:JSON.stringify(n)}]}function _t(e){let t=S(e,"exifUserComment");return t?t.data.trimStart().startsWith("{")?h("prompt",t.data,"text-unicode-escape"):pe(e):[]}function me(e){let t={};for(let n of e){let r=p(n.text);r.ok?t[n.keyword]=r.value:t[n.keyword]=n.text}return[{source:{type:"exifUserComment"},data:JSON.stringify(t)}]}function fe(e,t){let n=S(e,"exifUserComment");if(!n)return[];let r=p(n.data);return!r.ok||r.type!=="object"?[]:Object.entries(r.value).flatMap(([o,s])=>{let i=k(s);return h(o,i!==void 0?J(i):void 0,t)})}function Rt(e){return me(e)}var Vr=e=>{let t=S(e,"exifImageDescription"),n=S(e,"exifMake");return!t&&!n?null:[...h("prompt",n?.data,"text-unicode-escape"),...h("workflow",t?.data,"text-unicode-escape")]},Br=e=>{let t=fe(e,"text-unicode-escape");return t.length>0?t:null};function Nt(e){return Vr(e)??Br(e)??[]}function Ot(e){let t=Object.fromEntries(e.map(n=>[n.keyword,n.text]));return[{source:{type:"exifUserComment"},data:JSON.stringify(t)}]}function Ft(e){let t=S(e,"exifUserComment");if(!t)return[];let n=p(t.data);return!n.ok||n.type!=="object"?[]:Object.entries(n.value).flatMap(([r,o])=>h(r,k(o),"dynamic"))}function Dt(e){return me(e)}function Gt(e){return fe(e,"dynamic")}var Xr="NovelAI generated image",$r="NovelAI";function Jt(e){let t=e.find(s=>s.keyword==="Description"),n=t&&{source:{type:"exifImageDescription"},data:`\0\0\0\0${t.text}`},r=Kr(e),o={source:{type:"exifUserComment"},data:JSON.stringify(r)};return[n,o].filter(s=>s!==void 0)}function Kr(e){return Object.fromEntries(Yr.flatMap(t=>{let n=e.find(r=>r.keyword===t);return n?[[t,n.text]]:[]}))}var Yr=["Comment","Description","Generation time","Software","Source","Title"];function Wt(e){let t=S(e,"exifUserComment"),n=S(e,"exifImageDescription");return Zr(t,n)}function Zr(e,t){if(!e||!t)return[];let n=p(e.data);if(!n.ok||n.type!=="object")return M("Comment",e.data);let r=n.value,o=qr(t,k(r.Description));return[M("Title",k(r.Title)??Xr),h("Description",o,"dynamic"),M("Software",k(r.Software)??$r),M("Source",k(r.Source)),M("Generation time",k(r["Generation time"])),M("Comment",k(r.Comment))].flat()}function qr(e,t){if(e?.data){let n=e.data;return n.startsWith("\0\0\0\0")?n.slice(4):n}if(t)return t.startsWith("\0\0\0\0")?t.slice(4):t}function de(e){return t=>{let n=t.find(r=>r.keyword===e);return n?[{source:{type:"exifUserComment"},data:n.text}]:[]}}function le(e,t){return n=>{let r=n.find(o=>o.source.type==="exifUserComment");return r?h(e,r.data,t):[]}}var Qr={parameters:"text-utf8-raw",default:"text-unicode-escape"};function jt(e){let t={};for(let n of e){let r=p(n.text);t[n.keyword]=r.ok?r.value:n.text}return[{source:{type:"exifUserComment"},data:JSON.stringify(t)}]}function Lt(e){let t=S(e,"exifUserComment");if(!t)return[];let n=p(t.data);if(!n.ok||n.type!=="object")return[];let r=n.value,o=Object.entries(r).map(([s,i])=>{let u=k(i);return[s,u!==void 0?J(u):void 0]});return re(o,Qr)}function Ht(e){let t=e.find(s=>s.keyword==="parameters");if(!t)return[];let n=p(t.text),r=[{source:{type:"exifUserComment"},data:n.ok?JSON.stringify(n.value):t.text}],o=e.find(s=>s.keyword==="prompt");return o&&r.push({source:{type:"exifMake"},data:o.text}),r}function zt(e){let t=S(e,"exifUserComment"),n=S(e,"exifMake");return[h("prompt",n?.data,"text-unicode-escape"),h("parameters",t?.data,"text-unicode-escape")].flat()}var eo={generation_data:"text-utf8-raw",default:"text-unicode-escape"};function Vt(e){let t={};for(let n of e){let r=p(n.text);t[n.keyword]=r.ok?r.value:n.text}return[{source:{type:"exifUserComment"},data:JSON.stringify(t)}]}function Bt(e){let t=S(e,"exifUserComment");if(!t)return[];let n=p(t.data);if(!n.ok||n.type!=="object")return[];let r=n.value,o=Object.entries(r).map(([s,i])=>{let u=k(i);return[s,u!==void 0?J(u):void 0]});return re(o,eo)}function Xt(e,t){if(e.status==="empty")return a.error({type:"missingRawData"});if(e.status==="invalid")return a.error({type:"invalidParseResult",status:e.status});if(e.status==="unrecognized")return a.error({type:"unsupportedSoftware",software:"unknown"});let n=e.raw;if(n.format===t)return a.ok(n);let r=e.metadata.software,o=fo[r];return o?o(n,t):a.error({type:"unsupportedSoftware",software:r})}function E(e,t){return(n,r)=>{if(n.format==="png"){if(r==="png")return a.ok(n);let s=e(n.chunks);return a.ok({format:r,segments:s})}if(r==="jpeg"||r==="webp")return a.ok({format:r,segments:n.segments});let o=t(n.segments);return a.ok({format:"png",chunks:o})}}var to=E(Jt,Wt),G=E(ce,pe),no=E(Rt,Nt),ro=E(Ot,Ft),oo=E(de("parameters"),le("parameters","text-unicode-escape")),so=E(de("parameters"),le("parameters","text-unicode-escape")),io=E(Ht,zt),ao=E(Dt,Gt),uo=E(de("parameters"),le("parameters","text-unicode-escape")),co=E(It,_t),po=E(jt,Lt),mo=E(Vt,Bt),fo={novelai:to,"sd-webui":G,"sd-next":G,forge:G,"forge-classic":G,"forge-neo":G,reforge:G,"easy-reforge":G,civitai:co,comfyui:no,tensorart:mo,"stability-matrix":po,easydiffusion:ro,fooocus:oo,"ruined-fooocus":so,swarmui:io,invokeai:ao,"hf-space":uo};function Yt(e,t){let n=W(e),r=j(n);if(!r)return{ok:!1,error:{type:"unsupportedFormat"}};if(t.status==="empty"){let s=Kt[r].writeEmpty(n,[]);return s.ok?{ok:!0,value:s.value}:{ok:!1,error:{type:"writeFailed",message:s.error.type}}}if(t.status==="invalid")return{ok:!1,error:{type:"writeFailed",message:"Cannot write invalid metadata"}};if(t.status==="unrecognized"){if(t.raw.format===r)return $t(n,r,t.raw);let i=Kt[r].writeEmpty(n,[]);return i.ok?{ok:!0,value:i.value,warning:{type:"metadataDropped",reason:"unrecognizedCrossFormat"}}:{ok:!1,error:{type:"writeFailed",message:i.error.type}}}let o=Xt(t,r);return o.ok?$t(n,r,o.value):{ok:!1,error:{type:"conversionFailed",message:`Failed to convert metadata: ${o.error.type}`}}}function $t(e,t,n){if(t==="png"&&n.format==="png"){let r=q(e,n.chunks);return r.ok?{ok:!0,value:r.value}:{ok:!1,error:{type:"writeFailed",message:r.error.type}}}if(t==="jpeg"&&n.format==="jpeg"){let r=K(e,n.segments);return r.ok?{ok:!0,value:r.value}:{ok:!1,error:{type:"writeFailed",message:r.error.type}}}if(t==="webp"&&n.format==="webp"){let r=Q(e,n.segments);return r.ok?{ok:!0,value:r.value}:{ok:!1,error:{type:"writeFailed",message:r.error.type}}}return{ok:!1,error:{type:"writeFailed",message:"Internal error: format mismatch after conversion"}}}var Kt={png:{writeEmpty:q},jpeg:{writeEmpty:K},webp:{writeEmpty:Q}};var Zt=Object.freeze({novelai:"NovelAI",comfyui:"ComfyUI",swarmui:"SwarmUI",tensorart:"TensorArt","stability-matrix":"Stability Matrix",invokeai:"InvokeAI","sd-webui":"Stable Diffusion WebUI",forge:"Forge","forge-classic":"Forge - Classic","forge-neo":"Forge - Neo",reforge:"reForge","easy-reforge":"EasyReforge","sd-next":"SD.Next",civitai:"Civitai","hf-space":"Hugging Face Space",easydiffusion:"Easy Diffusion",fooocus:"Fooocus","ruined-fooocus":"Ruined Fooocus"});return an(lo);})();
|
|
11
|
+
`,n);return{prompt:e.slice(0,t).trim(),negativePrompt:e.slice(t+16,r).trim(),settings:e.slice(r).trim()}}function Kn(e){let t=new Map;if(!e||e.length>1e4)return t;let n=/([A-Za-z][A-Za-z0-9 ]*?):\s*([^,]+?)(?=,\s*[A-Za-z][A-Za-z0-9 ]*?:|$)/g;for(let r of e.matchAll(n)){let o=(r[1]??"").trim(),s=(r[2]??"").trim();t.set(o,s)}return t}function ze(e){let t=e.match(/(\d+)x(\d+)/);return t?[Number.parseInt(t[1]??"0",10),Number.parseInt(t[2]??"0",10)]:[0,0]}function F(e){if(e===void 0)return;let t=Number.parseFloat(e);return Number.isNaN(t)?void 0:t}function p(e){try{let t=JSON.parse(e);return Yn(t)}catch{return{ok:!1,error:{type:"parseError",message:"Invalid JSON"}}}}function Yn(e){if(e===null)return{ok:!0,type:"null",value:null};if(Array.isArray(e))return{ok:!0,type:"array",value:e};switch(typeof e){case"object":return{ok:!0,type:"object",value:e};case"string":return{ok:!0,type:"string",value:e};case"number":return{ok:!0,type:"number",value:e};case"boolean":return{ok:!0,type:"boolean",value:e};default:return{ok:!1,error:{type:"parseError",message:"Unexpected JSON type"}}}}var Ve=["KSampler","KSamplerAdvanced","SamplerCustomAdvanced","SamplerCustom","DetailerForEach"],Zn=["EmptyLatentImage"],qn=["SDXL Empty Latent Image (rgthree)"],Qn=["CheckpointLoaderSimple","CheckpointLoader"],er=["UNETLoader"],tr=["CLIPSetLastLayer"],nr=["UpscaleModelLoader"],Be=["ImageScale","ImageScaleBy"],$e=["LatentUpscale","LatentUpscaleBy"],Ke=["VAEEncode","VAEEncodeTiled"];function Ye(e){let t={};for(let n of Object.values(e)){let r=n.class_type;!t.sampler&&Ve.includes(r)?t.sampler=n:!t.latentImage&&Zn.includes(r)?t.latentImage=n:!t.latentImageRgthree&&qn.includes(r)?t.latentImageRgthree=n:!t.checkpoint&&Qn.includes(r)?t.checkpoint=n:!t.unetLoader&&er.includes(r)?t.unetLoader=n:!t.clipSetLastLayer&&tr.includes(r)?t.clipSetLastLayer=n:!t.hiresModelUpscale&&nr.includes(r)?t.hiresModelUpscale=n:!t.hiresImageScale&&Be.includes(r)?t.hiresImageScale=n:!t.latentUpscale&&$e.includes(r)?t.latentUpscale=n:!t.vaeEncode&&Ke.includes(r)&&(t.vaeEncode=n)}return t}function D(e,t){if(R(t))return e[String(t[0])]}function R(e){return Array.isArray(e)&&e.length===2&&(typeof e[0]=="string"||typeof e[0]=="number")&&typeof e[1]=="number"}function Ze(e,t,n=10){if(n<=0)return"";let r=e[t];if(!r)return"";let o=r.inputs.text??r.inputs.prompt??r.inputs.Text;return typeof o=="string"?o:R(o)?Ze(e,String(o[0]),n-1):""}function rr(e,t){let n=D(e,t.inputs.guider);return n||t}function qe(e,t){if(!t)return{promptText:"",negativeText:""};let n=rr(e,t);return{promptText:_e(e,n.inputs.positive,"positive"),negativeText:_e(e,n.inputs.negative,"negative")}}function _e(e,t,n,r=10){if(r<=0||!R(t))return"";let o=String(t[0]),s=Ze(e,o);if(s)return s;let i=e[o];if(!i)return"";let u=i.inputs[n];return _e(e,u,n,r-1)}function Qe(e,t){if(e){let n=Number(e.inputs.width)||0,r=Number(e.inputs.height)||0;if(n>0&&r>0)return{width:n,height:r}}if(t&&typeof t.inputs.dimensions=="string"){let n=t.inputs.dimensions.match(/^(\d+)\s*x\s*(\d+)/);if(n?.[1]&&n[2])return{width:Number.parseInt(n[1],10),height:Number.parseInt(n[2],10)}}return{width:0,height:0}}function Ne(e,t){if(!t)return;if(t.class_type==="SamplerCustomAdvanced")return or(e,t);if(t.class_type==="SamplerCustom")return sr(e,t);let n=t.inputs.seed;return R(n)&&(n=e[String(n[0])]?.inputs.seed),{seed:n,steps:t.inputs.steps,cfg:t.inputs.cfg,sampler:t.inputs.sampler_name,scheduler:t.inputs.scheduler,denoise:t.inputs.denoise}}function or(e,t){let n=D(e,t.inputs.noise),r=D(e,t.inputs.guider),o=D(e,t.inputs.sampler),s=D(e,t.inputs.sigmas);return{seed:n?.inputs.noise_seed,steps:s?.inputs.steps,cfg:r?.inputs.cfg,sampler:o?.inputs.sampler_name,scheduler:s?.inputs.scheduler,denoise:s?.inputs.denoise}}function sr(e,t){let n=D(e,t.inputs.sampler),r=D(e,t.inputs.sigmas),o=t.inputs.noise_seed;return R(o)&&(o=e[String(o[0])]?.inputs.seed),{seed:o,steps:r?.inputs.steps,cfg:t.inputs.cfg,sampler:n?.inputs.sampler_name,scheduler:r?.inputs.scheduler,denoise:r?.inputs.denoise}}function et(e,t){if(e?.inputs?.ckpt_name)return{name:String(e.inputs.ckpt_name)};if(t?.inputs?.unet_name)return{name:String(t.inputs.unet_name)}}function tt(e){if(!e)return;let t=e.inputs.stop_at_clip_layer;if(typeof t=="number")return-t}function le(e,t){if(!(t<=0||e<=0))return Math.round(e/t*100)/100}function ir(e,t){let n=t.inputs.latent_image;if(!R(n))return!1;let r=e[String(n[0])];if(!r)return!1;if($e.includes(r.class_type))return!0;if(!Ke.includes(r.class_type))return!1;let o=r.inputs.pixels;if(!R(o))return!1;let s=e[String(o[0])];return s?Be.includes(s.class_type):!1}function nt(e){return Object.values(e).find(t=>Ve.includes(t.class_type)&&ir(e,t))}function rt(e,t){let n=e.extraMetadata;if(typeof n=="string"){let r=p(n);if(r.ok&&r.type==="object")return r.value}if(t?.extraMetadata){let r=p(t.extraMetadata);if(r.ok&&r.type==="object")return r.value}}function ot(e){if(!e)return;let t=ar(e),n=ur(e);return g({prompt:e.prompt,negativePrompt:e.negativePrompt,width:e.width,height:e.height,model:e.baseModel?{name:e.baseModel}:void 0,...n,...t})}function ar(e){if(!e.transformations)return{};let t=e.transformations.find(r=>r.type==="upscale");if(!t?.upscaleWidth)return{};let n=le(t.upscaleWidth,e.width??0);return n===void 0?{}:{upscale:{scale:n}}}function ur(e){return e.seed===void 0&&e.steps===void 0&&e.cfgScale===void 0&&e.sampler===void 0?{}:{sampling:{seed:e.seed,steps:e.steps,cfg:e.cfgScale,sampler:e.sampler}}}var cr=["extra","extraMetadata","resource-stack"];function ne(e){let t=pr(e);if(!t)return a.error({type:"unsupportedFormat"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in prompt entry"});let r=n.value,o=Oe(r),s=ot(rt(r,e)),i=dr(s,o);return a.ok({software:"comfyui",nodes:Object.fromEntries(Object.entries(r).filter(([u])=>!cr.includes(u))),...i})}function st(e){if(!e||typeof e.denoise!="number")return e;if(e.denoise>=1){let{denoise:t,...n}=e;return n}return e}function it(e){return e.replace(/\0+$/,"").replace(/:\s*NaN\b/g,": null")}function pr(e){if(e.prompt)return it(e.prompt);let t=[e.UserComment,e.ImageDescription,e.Make,e.Prompt,e.Workflow];for(let n of t)if(n&&n.startsWith("{")){let r=it(n),o=p(r);if(!o.ok||o.type!=="object")continue;if(o.value.prompt&&typeof o.value.prompt=="object")return JSON.stringify(o.value.prompt);if(Object.values(o.value).some(i=>i&&typeof i=="object"&&"class_type"in i))return r}}function Oe(e){let t=Ye(e),{promptText:n,negativeText:r}=qe(e,t.sampler),{width:o,height:s}=Qe(t.latentImage,t.latentImageRgthree),i=nt(e),u=st(i?Ne(e,i):void 0),c=mr(e,t,o),m=t.hiresModelUpscale?.inputs.model_name,y=st(Ne(e,t.sampler)),x=tt(t.clipSetLastLayer);return g({prompt:n||void 0,negativePrompt:r||void 0,width:o>0?o:void 0,height:s>0?s:void 0,model:et(t.checkpoint,t.unetLoader),sampling:g({...y,clipSkip:x}),...fr(m,c,u)})}function mr(e,t,n){let r=t.latentUpscale?.inputs;if(r?.scale_by!==void 0)return r.scale_by;let o=t.hiresImageScale?.inputs.width;if(o!==void 0){if(R(o)){let s=e[String(o[0])];return typeof s?.inputs.clip_scale=="number"?s.inputs.clip_scale:void 0}if(typeof o=="number")return le(o,n)}}function fr(e,t,n){return!e&&t===void 0&&!n?{}:n?{hires:{upscaler:e,scale:t,steps:n.steps,denoise:n.denoise}}:e?{upscale:{upscaler:e,scale:t}}:{}}function at(e,t){if(!e&&!t)return;let n={};for(let r of[e,t])if(r)for(let[o,s]of Object.entries(r))s!==void 0&&(n[o]=s);return Object.keys(n).length>0?n:void 0}function dr(e,t){let n=at(e?.upscale,t?.upscale),r=at(e?.hires,t?.hires);return{prompt:t?.prompt??e?.prompt??"",negativePrompt:t?.negativePrompt??e?.negativePrompt??"",width:t?.width??e?.width??0,height:t?.height??e?.height??0,...g({model:t?.model??e?.model,sampling:t?.sampling??e?.sampling,hires:r,upscale:n})}}var lr="invokeai_metadata",gr="generation_data",yr="smproj",Fe="extraMetadata",z="sui_image_params",mt="swarm_version",hr="class_type",ut="noise_schedule",ct="v4_prompt",xr="uncond_scale",Sr="civitai:",br="Civitai resources:",pt="RuinedFooocus",kr="use_stable_diffusion_model",Cr='"Model"',wr='"resolution"',vr='"base_model"';function ft(e){let t=Tr(e);if(t)return t;let n=Pr(e);if(n)return n;let r=e.parameters??e.UserComment??e.Comment;return r?Mr(r):null}function dt(e){return lr in e?"invokeai":gr in e?"tensorart":yr in e?"stability-matrix":Fe in e?"civitai":null}function Tr(e){if(e.Software?.startsWith("NovelAI"))return"novelai";if(e.CreatorTool?.startsWith("Draw Things"))return"draw-things";let t=dt(e);if(t)return t;if("fooocus_scheme"in e)return"fooocus";if("use_stable_diffusion_model"in e)return"easydiffusion";if(e.parameters?.includes(z))return"swarmui";let r=e.UserComment??e.Comment;return r?.startsWith("{")?Er(r):null}function Er(e){let t=p(e);if(!t.ok||t.type!=="object")return null;let n=t.value,r=dt(n);if(r)return r;if("prompt"in n&&"workflow"in n){let o=n.workflow,s=n.prompt,i=typeof o=="object"||typeof s=="object",u=typeof o=="string"&&o.startsWith("{")||typeof s=="string"&&s.startsWith("{");if(i||u)return"comfyui"}if(z in n)return"swarmui";if("prompt"in n&&"parameters"in n){let o=String(n.parameters||"");if(o.includes(z)||o.includes(mt))return"swarmui"}return null}function Pr(e){if("prompt"in e&&"workflow"in e||"Prompt"in e&&"Workflow"in e||"workflow"in e||"Workflow"in e)return"comfyui";let t=e.prompt??e.Prompt;if(t?.startsWith("{")){if(t.includes(z))return"swarmui";if(t.includes(`"${Fe}"`))return"civitai";if(t.includes(hr))return"comfyui"}return null}function Mr(e){return e.startsWith("{")?Ur(e):Ar(e)}function Ur(e){return e.includes(z)?"swarmui":e.includes(`"software":"${pt}"`)||e.includes(`"software": "${pt}"`)?"ruined-fooocus":e.includes(`"${kr}"`)?"easydiffusion":e.includes(Sr)||e.includes(`"${Fe}"`)?"civitai":e.includes(`"${ct}"`)||e.includes(`"${ut}"`)||e.includes(`"${xr}"`)||e.includes('"Software":"NovelAI"')||e.includes(`\\"${ut}\\"`)||e.includes(`\\"${ct}\\"`)?"novelai":e.includes(Cr)&&e.includes(wr)?"hf-space":e.includes('"prompt"')&&e.includes(vr)?"fooocus":e.includes('"prompt"')||e.includes('"nodes"')?"comfyui":null}function Ar(e){if(e.includes(z)||e.includes(mt))return"swarmui";let t=e.match(/Version:\s*([^\s,]+)/);if(t){let n=t[1];if(n&&/^v\d/.test(n))return"sd-webui";if(n==="classic")return"forge-classic";if(n==="neo"||n?.startsWith("neo"))return"forge-neo";if(n?.startsWith("f")&&/^f\d/.test(n))return/^f\d+\.\d+(\.\d+)?-v/.test(n)?"easy-reforge":/^f\d+\.\d+(\.\d+)?v\d+-v/.test(n)?"reforge":"forge";if(n?.startsWith("Fooocus"))return"fooocus";if(n==="ComfyUI")return"comfyui"}return e.includes("App: SD.Next")||e.includes("App:SD.Next")?"sd-next":e.includes(br)?"civitai":e.includes("Steps:")||e.includes("Sampler:")||e.includes("Negative prompt:")?"sd-webui":null}function lt(e){let t=e.UserComment?.startsWith("{")?e.UserComment:e.Comment?.startsWith("{")?e.Comment:void 0;if(!t)return a.error({type:"unsupportedFormat"});let n=p(t);return!n.ok||n.type!=="object"?a.error({type:"parseError",message:"Invalid JSON in Draw Things metadata"}):Rr(n.value)}function Ir(e){if(typeof e!="string")return{width:0,height:0};let t=e.match(/^(\d+)x(\d+)$/);return t?{width:Number(t[1]),height:Number(t[2])}:{width:0,height:0}}function Rr(e){let t=c=>{let m=e[c];return typeof m=="string"?m:void 0},n=c=>{let m=e[c];if(typeof m=="number"&&m!==0)return m},r=(t("c")??"").trim(),o=(t("uc")??"").trim(),{width:s,height:i}=Ir(e.size),u={software:"draw-things",prompt:r,negativePrompt:o,width:s,height:i,model:g({name:t("model")}),sampling:g({sampler:t("sampler"),steps:n("steps"),cfg:n("scale"),seed:n("seed"),denoise:n("strength")})};return a.ok(u)}function _r(e){if(!e)return;let t=e.replace(/\\/g,"/").split("/");return t[t.length-1]}function yt(e){if("use_stable_diffusion_model"in e)return gt(e);let t=e.UserComment?.startsWith("{")?e.UserComment:e.parameters?.startsWith("{")?e.parameters:void 0;if(!t)return a.error({type:"unsupportedFormat"});let n=p(t);return!n.ok||n.type!=="object"?a.error({type:"parseError",message:"Invalid JSON in Easy Diffusion metadata"}):gt(n.value)}function gt(e){let t=c=>{let m=e[c];return typeof m=="string"?m:void 0},n=c=>Number(e[c])||void 0,r=(t("prompt")??"").trim(),o=(t("negative_prompt")??"").trim(),s=t("use_stable_diffusion_model"),i=g({upscaler:t("use_upscale"),scale:n("upscale_amount")}),u={software:"easydiffusion",prompt:r,negativePrompt:o,width:n("width")??0,height:n("height")??0,model:g({name:_r(s),vae:t("use_vae_model")}),sampling:g({sampler:t("sampler_name"),steps:n("num_inference_steps"),cfg:n("guidance_scale"),seed:n("seed"),clipSkip:n("clip_skip"),denoise:n("prompt_strength")}),...i?{upscale:i}:{}};return a.ok(u)}function Nr(e){if(!e)return{width:0,height:0};let t=e.match(/\(\s*(\d+)\s*,\s*(\d+)\s*\)/);return!t?.[1]||!t[2]?{width:0,height:0}:{width:Number(t[1]),height:Number(t[2])}}function ht(e){let t=e.parameters??e.UserComment;if(!t||!t.startsWith("{"))return a.error({type:"unsupportedFormat"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in Fooocus metadata"});let r=n.value,{width:o,height:s}=Nr(r.resolution),i={software:"fooocus",prompt:r.prompt?.trim()??"",negativePrompt:r.negative_prompt?.trim()??"",width:o,height:s,model:g({name:r.base_model,hash:r.base_model_hash,vae:r.vae}),sampling:g({sampler:r.sampler,scheduler:r.scheduler,steps:r.steps,cfg:r.guidance_scale,seed:r.seed,clipSkip:r.clip_skip})};return a.ok(i)}function xt(e){let t=e.parameters??e.UserComment;if(!t)return a.error({type:"unsupportedFormat"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in parameters entry"});let r=n.value,o=c=>{let m=c?.match(/(\d+)\s*x\s*(\d+)/);return m?.[1]&&m?.[2]?{width:Number.parseInt(m[1],10),height:Number.parseInt(m[2],10)}:{width:0,height:0}},{width:s,height:i}=o(r.resolution),u={software:"hf-space",prompt:r.prompt??"",negativePrompt:r.negative_prompt??"",width:s,height:i,model:g({name:r.Model,hash:r["Model hash"]}),sampling:g({sampler:r.sampler,steps:r.num_inference_steps,cfg:r.guidance_scale,seed:r.seed}),hires:r.use_upscaler?g({upscaler:r.use_upscaler.upscale_method,denoise:r.use_upscaler.upscaler_strength,scale:r.use_upscaler.upscale_by,steps:r.use_upscaler.upscale_steps}):void 0};return a.ok(u)}function G(e,t){if(!e.UserComment?.startsWith("{"))return;let n=p(e.UserComment);if(!n.ok||n.type!=="object")return;let r=n.value[t];if(typeof r=="string")return r;if(typeof r=="object"&&r!==null)return JSON.stringify(r)}function Or(e){return e.invokeai_metadata??G(e,"invokeai_metadata")}function St(e){let t=Or(e);if(!t)return a.error({type:"unsupportedFormat"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in invokeai_metadata entry"});let r=n.value,o=r.width??0,s=r.height??0,i=Fr(r);return a.ok({software:"invokeai",prompt:r.positive_prompt??"",negativePrompt:r.negative_prompt??"",width:o,height:s,model:g({name:r.model?.name,hash:r.model?.hash}),sampling:g({seed:r.seed,steps:r.steps,cfg:r.cfg_scale,sampler:r.scheduler}),...i})}function Fr(e){if(!e.upscale_model?.name&&e.upscale_scale===void 0)return{};let t=g({upscaler:e.upscale_model?.name,scale:e.upscale_scale});return t?{upscale:t}:{}}function bt(e){let t=e.UserComment??e.Comment;if(!t)return a.error({type:"parseError",message:"Missing Comment/UserComment entry"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in Comment entry"});let r=n.value,o=r.width??0,s=r.height??0,i=r.v4_prompt?.caption?.base_caption??r.prompt??"",u=r.v4_negative_prompt?.caption?.base_caption??r.uc??"",c=r.v4_prompt?.caption?.char_captions,m=c&&c.length>0?c.map(y=>y.char_caption?{prompt:y.char_caption,center:y.centers?.[0]}:null).filter(y=>y!==null):void 0;return a.ok({software:"novelai",prompt:i,negativePrompt:u,width:o,height:s,sampling:g({steps:r.steps,cfg:r.scale,seed:r.seed,sampler:r.sampler,scheduler:r.noise_schedule}),characterPrompts:m,useCoords:m?r.v4_prompt?.use_coords:void 0,useOrder:m?r.v4_prompt?.use_order:void 0})}function kt(e){let t=e.parameters??e.UserComment;if(!t||!t.startsWith("{"))return a.error({type:"unsupportedFormat"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in Ruined Fooocus metadata"});let r=n.value,o={software:"ruined-fooocus",prompt:r.Prompt?.trim()??"",negativePrompt:r.Negative?.trim()??"",width:r.width??0,height:r.height??0,model:{name:r.base_model_name,hash:r.base_model_hash},sampling:{sampler:r.sampler_name,scheduler:r.scheduler,steps:r.steps,cfg:r.cfg,seed:r.seed,clipSkip:r.clip_skip}};return a.ok(o)}function Ct(e){let t=ne(e);if(!t.ok||t.value.software!=="comfyui")return a.error({type:"unsupportedFormat"});let n=e["parameters-json"]??G(e,"parameters-json"),r=n?p(n):void 0,o=r?.ok&&r.type==="object"?r.value:void 0;return a.ok({...t.value,software:"stability-matrix",prompt:o?.PositivePrompt??t.value.prompt,negativePrompt:o?.NegativePrompt??t.value.negativePrompt,model:o?.ModelName!==void 0||o?.ModelHash!==void 0?{name:o?.ModelName,hash:o?.ModelHash}:t.value.model})}function Dr(e){if(e.parameters)return e.parameters;if(!e.UserComment)return;let t=p(e.UserComment);if(!(!t.ok||t.type!=="object")&&"sui_image_params"in t.value)return e.UserComment}function wt(e){let t=Dr(e);if(!t)return a.error({type:"unsupportedFormat"});let n=p(t);if(!n.ok||n.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in parameters entry"});let r=n.value.sui_image_params;if(!r)return a.error({type:"unsupportedFormat"});let o=r.width??0,s=r.height??0,i=e.prompt||e.Make,u=i?p(i):void 0,c=u?.ok&&u.type==="object"?u.value:void 0;return a.ok({software:"swarmui",prompt:r.prompt??"",negativePrompt:r.negativeprompt??"",width:o,height:s,nodes:c,model:g({name:r.model}),sampling:g({seed:r.seed,steps:r.steps,cfg:r.cfgscale,sampler:r.sampler,scheduler:r.scheduler}),hires:g({scale:r.refinerupscale,upscaler:r.refinerupscalemethod,denoise:r.refinercontrolpercentage})})}function vt(e){let t=e.generation_data??G(e,"generation_data"),n=e.prompt??G(e,"prompt");if(!t)return a.error({type:"unsupportedFormat"});let r=t.replace(/\0+$/,""),o=p(r);if(!o.ok||o.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in generation_data entry"});let s=o.value;if(!n)return a.error({type:"unsupportedFormat"});let i=p(n);if(!i.ok||i.type!=="object")return a.error({type:"parseError",message:"Invalid JSON in prompt chunk"});let u=i.value,c=Oe(u),m=g({name:s.baseModel?.modelFileName,hash:s.baseModel?.hash});return a.ok({software:"tensorart",nodes:u,prompt:s.prompt??c?.prompt??"",negativePrompt:s.negativePrompt??c?.negativePrompt??"",width:c?.width??0,height:c?.height??0,model:m??c?.model,sampling:c?.sampling,hires:c?.hires,upscale:c?.upscale})}function Tt(e){let t=ft(e);switch(t){case"novelai":return bt(e);case"sd-webui":case"forge":case"forge-classic":case"forge-neo":case"reforge":case"easy-reforge":case"sd-next":return te(e,t);case"hf-space":return xt(e);case"civitai":{let n=ne(e);return n.ok?a.ok({...n.value,software:"civitai"}):te(e,"civitai")}case"comfyui":{let n=ne(e);return n.ok?n:te(e,"sd-webui")}case"invokeai":return St(e);case"swarmui":return wt(e);case"tensorart":return vt(e);case"stability-matrix":return Ct(e);case"easydiffusion":return yt(e);case"fooocus":{let n=e.fooocus_scheme,r=e.parameters??e.UserComment;return n==="a1111"||r&&!r.startsWith("{")?te(e,"fooocus"):ht(e)}case"ruined-fooocus":return kt(e);case"draw-things":return lt(e);default:return a.error({type:"unsupportedFormat"})}}function Et(e,t){return t==="png"?Gr(e):t==="jpeg"?Jr(e):jr(e)}function Gr(e){return e.length<24?null:{width:_(e,16),height:_(e,20)}}function Jr(e){let t=2;for(;t<e.length-4;){if(e[t]!==255){t++;continue}let n=e[t+1]??0;if(n===255){t++;continue}let r=A(e,t+2);if(n>=192&&n<=207&&n!==196&&n!==200&&n!==204){let o=A(e,t+5);return{width:A(e,t+7),height:o}}if(t+=2+r,n===218)break}return null}function jr(e){let t=12;for(;t<e.length&&!(t+8>e.length);){let n=E(e,t),r=N(e,t+4),o=r+r%2;if(n==="VP8X"){let s=ie(e,t+12),i=ie(e,t+15);return{width:s+1,height:i+1}}if(n==="VP8 "){let s=t+8;if(!(ie(e,s)&1)&&e[s+3]===157&&e[s+4]===1&&e[s+5]===42){let c=I(e,s+6,!0),m=I(e,s+8,!0);return{width:c&16383,height:m&16383}}}if(n==="VP8L"&&e[t+8]===47){let s=N(e,t+9),i=(s&16383)+1,u=(s>>14&16383)+1;return{width:i,height:u}}t+=8+o}return null}function ge(e){if(e.length<8)return[];let t=e[0]===73&&e[1]===73,n=e[0]===77&&e[1]===77;if(!t&&!n)return[];if(I(e,2,t)!==42)return[];let o=K(e,4,t),s=Pt(e,o,t),i=Lr(e,o,t),u=i!==null?Pt(e,i,t):[];return[...s,...u]}function Pt(e,t,n){let r=[];if(t+2>e.length)return r;let o=I(e,t,n),s=t+2;for(let i=0;i<o;i++){if(s+12>e.length)return r;let u=I(e,s,n),c=I(e,s+2,n),m=K(e,s+4,n),y=Wr(c),x=m*y;if(x>e.length){s+=12;continue}let h;if(x<=4?h=s+8:h=K(e,s+8,n),h+x>e.length){s+=12;continue}let S=e.slice(h,h+x);if(u===270){let f=Ut(S);if(f){let d=Mt(f);r.push({source:{type:"exifImageDescription",prefix:d??void 0},data:d?f.slice(d.length+2):f})}}else if(u===271){let f=Ut(S);if(f){let d=Mt(f);r.push({source:{type:"exifMake",prefix:d??void 0},data:d?f.slice(d.length+2):f})}}else if(u===37510){let f=Xr(S);f&&r.push({source:{type:"exifUserComment"},data:f})}s+=12}return r}function Mt(e){return e.match(/^([A-Za-z]+):\s/)?.[1]??null}function Wr(e){switch(e){case 1:return 1;case 2:return 1;case 3:return 2;case 4:return 4;case 5:return 8;case 7:return 1;default:return 1}}function Ut(e){try{let n=new TextDecoder("utf-8",{fatal:!1}).decode(e);return n.endsWith("\0")&&(n=n.slice(0,-1)),n.trim()||null}catch{return null}}function Lr(e,t,n){if(t+2>e.length)return null;let r=I(e,t,n),o=t+2;for(let s=0;s<r;s++){if(o+12>e.length)return null;if(I(e,o,n)===34665)return K(e,o+8,n);o+=12}return null}function Xr(e){if(e.length<8)return null;if(e[0]===85&&e[1]===78&&e[2]===73&&e[3]===67&&e[4]===79&&e[5]===68&&e[6]===69&&e[7]===0){let t=e.slice(8);return t.length>=2&&t[0]!==0&&t[1]===0?Hr(t):At(t)}if(e[0]===65&&e[1]===83&&e[2]===67&&e[3]===73&&e[4]===73&&e[5]===0&&e[6]===0&&e[7]===0)return zr(e.slice(8));try{let n=new TextDecoder("utf-8",{fatal:!0}).decode(e);return n.endsWith("\0")&&(n=n.slice(0,-1)),n}catch{return null}}function At(e){let t=new TextDecoder("utf-16be").decode(e),n=t.indexOf("\0");return n>=0?t.slice(0,n):t}function Hr(e){let t=new TextDecoder("utf-16le").decode(e),n=t.indexOf("\0");return n>=0?t.slice(0,n):t}function zr(e){let t=e.indexOf(0),n=t>=0?e.subarray(0,t):e;return new TextDecoder("ascii").decode(n)}var It=225,Vr=254;function Br(e,t){return e[t]===69&&e[t+1]===120&&e[t+2]===105&&e[t+3]===102&&e[t+4]===0&&e[t+5]===0}function Rt(e){if(!B(e))return a.error({type:"invalidSignature"});let t=[],n=$r(e);if(n){let s=e.slice(n.offset,n.offset+n.length),i=ge(s);t.push(...i)}let r=Kr(e);if(r){let s=e.slice(r.offset,r.offset+r.length),i=new TextDecoder("utf-8").decode(s);t.push({source:{type:"xmpPacket"},data:i})}let o=Yr(e);if(o){let s=e.slice(o.offset,o.offset+o.length),i=Zr(s);i!==null&&t.push({source:{type:"jpegCom"},data:i})}return a.ok(t)}function $r(e){let t=2;for(;t<e.length-4;){if(e[t]!==255){t++;continue}let n=e[t+1];if(n===255){t++;continue}let r=A(e,t+2);if(n===It&&r>=8){let o=t+4;if(o+6<=e.length&&Br(e,o))return{offset:o+6,length:r-8}}if(t+=2+r,n===218||n===217)break}return null}function Kr(e){let t=2;for(;t<e.length-4;){if(e[t]!==255){t++;continue}let n=e[t+1];if(n===255){t++;continue}let r=A(e,t+2);if(n===It&&r>=U.length+2){let o=t+4;if(ce(e,o))return{offset:o+U.length,length:r-2-U.length}}if(t+=2+r,n===218||n===217)break}return null}function Yr(e){let t=2;for(;t<e.length-4;){if(e[t]!==255){t++;continue}let n=e[t+1];if(n===255){t++;continue}let r=A(e,t+2);if(n===Vr&&r>=2)return{offset:t+4,length:r-2};if(t+=2+r,n===218||n===217)break}return null}function Zr(e){try{return new TextDecoder("utf-8",{fatal:!0}).decode(e)}catch{return null}}function Nt(e){if(!V(e))return a.error({type:"invalidSignature"});let t=Qr(e);return t.ok?a.ok(t.value):t}var qr=8;function Qr(e){let t=[],n=qr;for(;n<e.length;){if(n+4>e.length)return a.error({type:"corruptedChunk",message:"Unexpected end of file while reading chunk length"});let r=_(e,n);if(n+=4,n+4>e.length)return a.error({type:"corruptedChunk",message:"Unexpected end of file while reading chunk type"});let o=E(e,n);if(n+=4,n+r>e.length)return a.error({type:"corruptedChunk",message:`Unexpected end of file while reading chunk data (${o})`});let s=e.slice(n,n+r);if(n+=r,n+=4,o==="tEXt"){let i=eo(s);i&&t.push(i)}else if(o==="iTXt"){let i=no(s);i&&t.push(i)}if(o==="IEND")break}return a.ok(t)}function eo(e){let t=e.indexOf(0);if(t===-1)return null;let n=_t(e.slice(0,t)),r=e.slice(t+1),o=to(r)??_t(r);return{type:"tEXt",keyword:n,text:o}}function to(e){try{return new TextDecoder("utf-8",{fatal:!0}).decode(e)}catch{return null}}function no(e){let t=0,n=e.indexOf(0,t);if(n===-1)return null;let r=re(e.slice(t,n));if(t=n+1,t>=e.length)return null;let o=e[t]??0;if(t+=1,t>=e.length)return null;let s=e[t]??0;t+=1;let i=e.indexOf(0,t);if(i===-1)return null;let u=re(e.slice(t,i));t=i+1;let c=e.indexOf(0,t);if(c===-1)return null;let m=re(e.slice(t,c));t=c+1;let y;if(o===1){let x=ro(e.slice(t));if(!x)return null;y=re(x)}else y=re(e.slice(t));return{type:"iTXt",keyword:r,compressionFlag:o,compressionMethod:s,languageTag:u,translatedKeyword:m,text:y}}function _t(e){return new TextDecoder("iso-8859-1").decode(e)}function re(e){return new TextDecoder("utf-8").decode(e)}function ro(e){return null}function Ot(e){if(!$(e))return a.error({type:"invalidSignature"});let t=[],n=oo(e);if(n){let o=e.slice(n.offset,n.offset+n.length);t.push(...ge(o))}let r=so(e);if(r){let o=e.slice(r.offset,r.offset+r.length),s=new TextDecoder("utf-8").decode(o);t.push({source:{type:"xmpPacket"},data:s})}return a.ok(t)}function oo(e){let t=12;for(;t+8<=e.length;){let n=N(e,t+4);if(E(e,t)==="EXIF")return t+8+n>e.length?null:{offset:t+8,length:n};let r=n+n%2;t+=8+r}return null}function so(e){let t=12;for(;t+8<=e.length;){let n=N(e,t+4);if(E(e,t)==="XMP ")return t+8+n>e.length?null:{offset:t+8,length:n};let r=n+n%2;t+=8+r}return null}function Ft(e){return Object.fromEntries(e.flatMap(t=>{if(pe(t.keyword)){let n=Te(t.text);if(n)return Object.entries(n)}return[[t.keyword,t.text]]}))}function Dt(e){let t={};for(let n of e){let r=ao(n.source),o=n.data;if(n.source.type==="xmpPacket"){let s=Te(o);if(s){Object.assign(t,s);continue}}if(n.source.type==="exifUserComment"&&o.startsWith("{")){let s=io(o);if(s){Object.assign(t,s);continue}}t[r]=o}return t}function io(e){let t=p(e);if(!t.ok||t.type!=="object")return null;let n=t.value;if(typeof n.Software=="string"&&!n.Software.startsWith("NovelAI")||typeof n.Comment!="string")return null;let r=p(n.Comment);return{Software:typeof n.Software=="string"?n.Software:"NovelAI",Comment:r.ok?JSON.stringify(r.value):n.Comment}}function ao(e){switch(e.type){case"jpegCom":return"Comment";case"exifUserComment":return"UserComment";case"exifImageDescription":return e.prefix??"ImageDescription";case"exifMake":return e.prefix??"Make";case"xmpPacket":return"XML:com.adobe.xmp"}}function Gt(e,t){let n=W(e),r=L(n);if(!r)return{status:"invalid",message:"Unknown image format"};let o=uo(n,r);if(o.status!=="success")return o;let s=o.raw,i=s.format==="png"?Ft(s.chunks):Dt(s.segments),u=Tt(i);if(!u.ok)return{status:"unrecognized",raw:s};let c=u.value;if(!t?.strict&&(c.width===0||c.height===0)){let m=Et(n,r);m&&(c.width=c.width||m.width,c.height=c.height||m.height)}return{status:"success",metadata:c,raw:s}}function uo(e,t){let n=t==="png"?Nt(e):t==="jpeg"?Rt(e):Ot(e);return n.ok?n.value.length===0?{status:"empty"}:t==="png"?{status:"success",raw:{format:"png",chunks:n.value}}:{status:"success",raw:{format:t,segments:n.value}}:{status:"invalid",message:n.error.type==="invalidSignature"?`Invalid ${t.toUpperCase()} signature`:n.error.message}}function ye(e){let t=e.find(n=>n.keyword==="parameters");return t?[{source:{type:"exifUserComment"},data:t.text}]:[]}function he(e){let t=e.find(n=>n.source.type==="exifUserComment");return t?k("parameters",t.data,"dynamic"):[]}function Jt(e){let t=e.find(r=>r.keyword==="parameters");if(t&&!t.text.trimStart().startsWith("{"))return ye(e);let n={};for(let r of e)if(r.keyword==="prompt"){let o=p(r.text);o.ok&&o.type==="object"&&Object.assign(n,o.value)}else if(r.keyword==="extraMetadata")n[r.keyword]=r.text;else{let o=p(r.text);n[r.keyword]=o.ok?o.value:r.text}return[{source:{type:"exifUserComment"},data:JSON.stringify(n)}]}function jt(e){let t=b(e,"exifUserComment");return t?t.data.trimStart().startsWith("{")?k("prompt",t.data,"text-unicode-escape"):he(e):[]}function xe(e){let t={};for(let n of e){let r=p(n.text);r.ok?t[n.keyword]=r.value:t[n.keyword]=n.text}return[{source:{type:"exifUserComment"},data:JSON.stringify(t)}]}function Se(e,t){let n=b(e,"exifUserComment");if(!n)return[];let r=p(n.data);return!r.ok||r.type!=="object"?[]:Object.entries(r.value).flatMap(([o,s])=>{let i=w(s);return k(o,i!==void 0?j(i):void 0,t)})}function Wt(e){return xe(e)}var co=e=>{let t=b(e,"exifImageDescription"),n=b(e,"exifMake");return!t&&!n?null:[...k("prompt",n?.data,"text-unicode-escape"),...k("workflow",t?.data,"text-unicode-escape")]},po=e=>{let t=Se(e,"text-unicode-escape");return t.length>0?t:null};function Lt(e){return co(e)??po(e)??[]}var mo="XML:com.adobe.xmp";function Xt(e){let t=e.find(n=>pe(n.keyword));return t?[{source:{type:"xmpPacket"},data:t.text}]:[]}function Ht(e){let t=b(e,"xmpPacket");return t?oe(mo,t.data):[]}function zt(e){let t=Object.fromEntries(e.map(n=>[n.keyword,n.text]));return[{source:{type:"exifUserComment"},data:JSON.stringify(t)}]}function Vt(e){let t=b(e,"exifUserComment");if(!t)return[];let n=p(t.data);return!n.ok||n.type!=="object"?[]:Object.entries(n.value).flatMap(([r,o])=>k(r,w(o),"dynamic"))}function Bt(e){return xe(e)}function $t(e){return Se(e,"dynamic")}var fo="NovelAI generated image",lo="NovelAI";function Kt(e){let t=e.find(s=>s.keyword==="Description"),n=t&&{source:{type:"exifImageDescription"},data:`\0\0\0\0${t.text}`},r=go(e),o={source:{type:"exifUserComment"},data:JSON.stringify(r)};return[n,o].filter(s=>s!==void 0)}function go(e){return Object.fromEntries(yo.flatMap(t=>{let n=e.find(r=>r.keyword===t);return n?[[t,n.text]]:[]}))}var yo=["Comment","Description","Generation time","Software","Source","Title"];function Yt(e){let t=b(e,"exifUserComment"),n=b(e,"exifImageDescription");return ho(t,n)}function ho(e,t){if(!e||!t)return[];let n=p(e.data);if(!n.ok||n.type!=="object")return M("Comment",e.data);let r=n.value,o=xo(t,w(r.Description));return[M("Title",w(r.Title)??fo),k("Description",o,"dynamic"),M("Software",w(r.Software)??lo),M("Source",w(r.Source)),M("Generation time",w(r["Generation time"])),M("Comment",w(r.Comment))].flat()}function xo(e,t){if(e?.data){let n=e.data;return n.startsWith("\0\0\0\0")?n.slice(4):n}if(t)return t.startsWith("\0\0\0\0")?t.slice(4):t}function be(e){return t=>{let n=t.find(r=>r.keyword===e);return n?[{source:{type:"exifUserComment"},data:n.text}]:[]}}function ke(e,t){return n=>{let r=n.find(o=>o.source.type==="exifUserComment");return r?k(e,r.data,t):[]}}var So={parameters:"text-utf8-raw",default:"text-unicode-escape"};function Zt(e){let t={};for(let n of e){let r=p(n.text);t[n.keyword]=r.ok?r.value:n.text}return[{source:{type:"exifUserComment"},data:JSON.stringify(t)}]}function qt(e){let t=b(e,"exifUserComment");if(!t)return[];let n=p(t.data);if(!n.ok||n.type!=="object")return[];let r=n.value,o=Object.entries(r).map(([s,i])=>{let u=w(i);return[s,u!==void 0?j(u):void 0]});return se(o,So)}function Qt(e){let t=e.find(s=>s.keyword==="parameters");if(!t)return[];let n=p(t.text),r=[{source:{type:"exifUserComment"},data:n.ok?JSON.stringify(n.value):t.text}],o=e.find(s=>s.keyword==="prompt");return o&&r.push({source:{type:"exifMake"},data:o.text}),r}function en(e){let t=b(e,"exifUserComment"),n=b(e,"exifMake");return[k("prompt",n?.data,"text-unicode-escape"),k("parameters",t?.data,"text-unicode-escape")].flat()}var bo={generation_data:"text-utf8-raw",default:"text-unicode-escape"};function tn(e){let t={};for(let n of e){let r=p(n.text);t[n.keyword]=r.ok?r.value:n.text}return[{source:{type:"exifUserComment"},data:JSON.stringify(t)}]}function nn(e){let t=b(e,"exifUserComment");if(!t)return[];let n=p(t.data);if(!n.ok||n.type!=="object")return[];let r=n.value,o=Object.entries(r).map(([s,i])=>{let u=w(i);return[s,u!==void 0?j(u):void 0]});return se(o,bo)}function rn(e,t){if(e.status==="empty")return a.error({type:"missingRawData"});if(e.status==="invalid")return a.error({type:"invalidParseResult",status:e.status});if(e.status==="unrecognized")return a.error({type:"unsupportedSoftware",software:"unknown"});let n=e.raw;if(n.format===t)return a.ok(n);let r=e.metadata.software,o=_o[r];return o?o(n,t):a.error({type:"unsupportedSoftware",software:r})}function P(e,t){return(n,r)=>{if(n.format==="png"){if(r==="png")return a.ok(n);let s=e(n.chunks);return a.ok({format:r,segments:s})}if(r==="jpeg"||r==="webp")return a.ok({format:r,segments:n.segments});let o=t(n.segments);return a.ok({format:"png",chunks:o})}}var ko=P(Kt,Yt),J=P(ye,he),Co=P(Wt,Lt),wo=P(zt,Vt),vo=P(be("parameters"),ke("parameters","text-unicode-escape")),To=P(be("parameters"),ke("parameters","text-unicode-escape")),Eo=P(Qt,en),Po=P(Bt,$t),Mo=P(be("parameters"),ke("parameters","text-unicode-escape")),Uo=P(Xt,Ht),Ao=P(Jt,jt),Io=P(Zt,qt),Ro=P(tn,nn),_o={novelai:ko,"sd-webui":J,"sd-next":J,forge:J,"forge-classic":J,"forge-neo":J,reforge:J,"easy-reforge":J,civitai:Ao,comfyui:Co,tensorart:Ro,"stability-matrix":Io,easydiffusion:wo,fooocus:vo,"ruined-fooocus":To,swarmui:Eo,invokeai:Po,"hf-space":Mo,"draw-things":Uo};function an(e,t){let n=W(e),r=L(n);if(!r)return{ok:!1,error:{type:"unsupportedFormat"}};if(t.status==="empty"){let s=sn[r].writeEmpty(n,[]);return s.ok?{ok:!0,value:s.value}:{ok:!1,error:{type:"writeFailed",message:s.error.type}}}if(t.status==="invalid")return{ok:!1,error:{type:"writeFailed",message:"Cannot write invalid metadata"}};if(t.status==="unrecognized"){if(t.raw.format===r)return on(n,r,t.raw);let i=sn[r].writeEmpty(n,[]);return i.ok?{ok:!0,value:i.value,warning:{type:"metadataDropped",reason:"unrecognizedCrossFormat"}}:{ok:!1,error:{type:"writeFailed",message:i.error.type}}}let o=rn(t,r);return o.ok?on(n,r,o.value):{ok:!1,error:{type:"conversionFailed",message:`Failed to convert metadata: ${o.error.type}`}}}function on(e,t,n){if(t==="png"&&n.format==="png"){let r=Q(e,n.chunks);return r.ok?{ok:!0,value:r.value}:{ok:!1,error:{type:"writeFailed",message:r.error.type}}}if(t==="jpeg"&&n.format==="jpeg"){let r=Y(e,n.segments);return r.ok?{ok:!0,value:r.value}:{ok:!1,error:{type:"writeFailed",message:r.error.type}}}if(t==="webp"&&n.format==="webp"){let r=ee(e,n.segments);return r.ok?{ok:!0,value:r.value}:{ok:!1,error:{type:"writeFailed",message:r.error.type}}}return{ok:!1,error:{type:"writeFailed",message:"Internal error: format mismatch after conversion"}}}var sn={png:{writeEmpty:Q},jpeg:{writeEmpty:Y},webp:{writeEmpty:ee}};var un=Object.freeze({novelai:"NovelAI",comfyui:"ComfyUI",swarmui:"SwarmUI",tensorart:"TensorArt","stability-matrix":"Stability Matrix",invokeai:"InvokeAI","sd-webui":"Stable Diffusion WebUI",forge:"Forge","forge-classic":"Forge - Classic","forge-neo":"Forge - Neo",reforge:"reForge","easy-reforge":"EasyReforge","sd-next":"SD.Next",civitai:"Civitai","hf-space":"Hugging Face Space",easydiffusion:"Easy Diffusion",fooocus:"Fooocus","ruined-fooocus":"Ruined Fooocus","draw-things":"Draw Things"});return hn(No);})();
|