Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
ComfyUI
Commits
b935bea3
Commit
b935bea3
authored
Oct 23, 2023
by
comfyanonymous
Browse files
The frontend can now load workflows from webp exif.
parent
2ec6158e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
113 additions
and
1 deletion
+113
-1
web/scripts/app.js
web/scripts/app.js
+10
-1
web/scripts/pnginfo.js
web/scripts/pnginfo.js
+103
-0
No files found.
web/scripts/app.js
View file @
b935bea3
...
...
@@ -3,7 +3,7 @@ import { ComfyWidgets } from "./widgets.js";
import
{
ComfyUI
,
$el
}
from
"
./ui.js
"
;
import
{
api
}
from
"
./api.js
"
;
import
{
defaultGraph
}
from
"
./defaultGraph.js
"
;
import
{
getPngMetadata
,
importA1111
,
getLatentMetadata
}
from
"
./pnginfo.js
"
;
import
{
getPngMetadata
,
getWebpMetadata
,
importA1111
,
getLatentMetadata
}
from
"
./pnginfo.js
"
;
/**
* @typedef {import("types/comfy").ComfyExtension} ComfyExtension
...
...
@@ -1790,6 +1790,15 @@ export class ComfyApp {
importA1111
(
this
.
graph
,
pngInfo
.
parameters
);
}
}
}
else
if
(
file
.
type
===
"
image/webp
"
)
{
const
pngInfo
=
await
getWebpMetadata
(
file
);
if
(
pngInfo
)
{
if
(
pngInfo
.
workflow
)
{
this
.
loadGraphData
(
JSON
.
parse
(
pngInfo
.
workflow
));
}
else
if
(
pngInfo
.
Workflow
)
{
this
.
loadGraphData
(
JSON
.
parse
(
pngInfo
.
Workflow
));
// Support loading workflows from that webp custom node.
}
}
}
else
if
(
file
.
type
===
"
application/json
"
||
file
.
name
?.
endsWith
(
"
.json
"
))
{
const
reader
=
new
FileReader
();
reader
.
onload
=
()
=>
{
...
...
web/scripts/pnginfo.js
View file @
b935bea3
...
...
@@ -47,6 +47,109 @@ export function getPngMetadata(file) {
});
}
function
parseExifData
(
exifData
)
{
// Check for the correct TIFF header (0x4949 for little-endian or 0x4D4D for big-endian)
const
isLittleEndian
=
new
Uint16Array
(
exifData
.
slice
(
0
,
2
))[
0
]
===
0x4949
;
console
.
log
(
exifData
);
// Function to read 16-bit and 32-bit integers from binary data
function
readInt
(
offset
,
isLittleEndian
,
length
)
{
let
arr
=
exifData
.
slice
(
offset
,
offset
+
length
)
if
(
length
===
2
)
{
return
new
DataView
(
arr
.
buffer
,
arr
.
byteOffset
,
arr
.
byteLength
).
getUint16
(
0
,
isLittleEndian
);
}
else
if
(
length
===
4
)
{
return
new
DataView
(
arr
.
buffer
,
arr
.
byteOffset
,
arr
.
byteLength
).
getUint32
(
0
,
isLittleEndian
);
}
}
// Read the offset to the first IFD (Image File Directory)
const
ifdOffset
=
readInt
(
4
,
isLittleEndian
,
4
);
function
parseIFD
(
offset
)
{
const
numEntries
=
readInt
(
offset
,
isLittleEndian
,
2
);
const
result
=
{};
for
(
let
i
=
0
;
i
<
numEntries
;
i
++
)
{
const
entryOffset
=
offset
+
2
+
i
*
12
;
const
tag
=
readInt
(
entryOffset
,
isLittleEndian
,
2
);
const
type
=
readInt
(
entryOffset
+
2
,
isLittleEndian
,
2
);
const
numValues
=
readInt
(
entryOffset
+
4
,
isLittleEndian
,
4
);
const
valueOffset
=
readInt
(
entryOffset
+
8
,
isLittleEndian
,
4
);
// Read the value(s) based on the data type
let
value
;
if
(
type
===
2
)
{
// ASCII string
value
=
String
.
fromCharCode
(...
exifData
.
slice
(
valueOffset
,
valueOffset
+
numValues
-
1
));
}
result
[
tag
]
=
value
;
}
return
result
;
}
// Parse the first IFD
const
ifdData
=
parseIFD
(
ifdOffset
);
return
ifdData
;
}
function
splitValues
(
input
)
{
var
output
=
{};
for
(
var
key
in
input
)
{
var
value
=
input
[
key
];
var
splitValues
=
value
.
split
(
'
:
'
,
2
);
output
[
splitValues
[
0
]]
=
splitValues
[
1
];
}
return
output
;
}
export
function
getWebpMetadata
(
file
)
{
return
new
Promise
((
r
)
=>
{
const
reader
=
new
FileReader
();
reader
.
onload
=
(
event
)
=>
{
// Get the PNG data as a Uint8Array
const
pngData
=
new
Uint8Array
(
event
.
target
.
result
);
const
dataView
=
new
DataView
(
pngData
.
buffer
);
// Check that the PNG signature is present
if
(
dataView
.
getUint32
(
0
)
!==
0x52494646
||
dataView
.
getUint32
(
8
)
!==
0x57454250
)
{
console
.
error
(
"
Not a valid WEBP file
"
);
r
();
return
;
}
// Start searching for chunks after the PNG signature
let
offset
=
12
;
let
txt_chunks
=
{};
// Loop through the chunks in the PNG file
while
(
offset
<
pngData
.
length
)
{
// Get the length of the chunk
const
length
=
dataView
.
getUint32
(
offset
+
4
,
true
);
// Get the chunk type
const
type
=
String
.
fromCharCode
(...
pngData
.
slice
(
offset
,
offset
+
4
));
console
.
log
(
length
,
type
);
if
(
type
===
"
EXIF
"
)
{
// Get the keyword
let
data
=
parseExifData
(
pngData
.
slice
(
offset
+
8
,
offset
+
8
+
length
));
for
(
var
key
in
data
)
{
var
value
=
data
[
key
];
let
index
=
value
.
indexOf
(
'
:
'
);
txt_chunks
[
value
.
slice
(
0
,
index
)]
=
value
.
slice
(
index
+
1
);
}
}
offset
+=
8
+
length
;
}
console
.
log
(
txt_chunks
);
r
(
txt_chunks
);
};
reader
.
readAsArrayBuffer
(
file
);
});
}
export
function
getLatentMetadata
(
file
)
{
return
new
Promise
((
r
)
=>
{
const
reader
=
new
FileReader
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment