-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpe.c
More file actions
1729 lines (1540 loc) · 65.3 KB
/
Copy pathcpe.c
File metadata and controls
1729 lines (1540 loc) · 65.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ComfyPromptExtractor (CPE)
* Ultra-fast, zero-dependency ComfyUI prompt & workflow metadata extractor for Linux, Windows, and macOS.
*
* Tech Stack:
* - C99 standard
* - Raylib (minimal GUI)
* - Native PNG chunk parser (skips IDAT pixel data for sub-millisecond parsing)
* - Embedded lightweight JSON parser for extracting clean prompt text
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/stat.h>
#include <raylib.h>
#define CPE_VERSION "1.1.0"
#define WIN_WIDTH 900
#define WIN_HEIGHT 600
#define PADDING 20
#define HEADER_HEIGHT 60
#define FOOTER_HEIGHT 60
/* --- Data Structures --- */
typedef enum {
PNG_OK = 0,
PNG_ERR_IO,
PNG_ERR_NOT_PNG,
PNG_ERR_NO_METADATA
} PngStatus;
typedef enum {
TARGET_PROMPT_TEXT = 0, /* Default: Clean extracted positive prompt text */
TARGET_NEGATIVE_TEXT, /* Clean extracted negative prompt text */
TARGET_RAW_PROMPT_JSON, /* Raw ComfyUI prompt graph JSON */
TARGET_WORKFLOW_JSON /* Raw ComfyUI workflow JSON */
} OutputTarget;
typedef struct {
/* Raw metadata chunks */
char *raw_prompt;
size_t raw_prompt_len;
char *raw_workflow;
size_t raw_workflow_len;
char *raw_parameters;
size_t raw_parameters_len;
char *raw_invokeai;
size_t raw_invokeai_len;
char *raw_dream;
size_t raw_dream_len;
/* Extracted clean prompt texts */
char *prompt_text;
size_t prompt_text_len;
char *negative_text;
size_t negative_text_len;
/* Active selection */
const char *selected_label;
char *selected_val;
size_t selected_len;
bool found;
PngStatus status;
} MetadataResult;
typedef struct {
char **lines;
int count;
int capacity;
} WrappedLines;
/* --- Miniature JSON Parser for Prompt Extraction --- */
typedef enum {
JSON_NULL = 0,
JSON_BOOL,
JSON_NUMBER,
JSON_STRING,
JSON_ARRAY,
JSON_OBJECT
} JsonType;
typedef struct JsonNode {
JsonType type;
char *key;
char *val_str;
double val_num;
bool val_bool;
struct JsonNode *child;
struct JsonNode *next;
} JsonNode;
static void json_free(JsonNode *node) {
if (!node) return;
if (node->key) free(node->key);
if (node->val_str) free(node->val_str);
JsonNode *c = node->child;
while (c) {
JsonNode *next = c->next;
json_free(c);
c = next;
}
free(node);
}
static const char *skip_ws(const char *p) {
while (*p && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++;
return p;
}
static char *parse_string_val(const char **p) {
if (**p != '"') return NULL;
(*p)++;
size_t cap = 256;
size_t len = 0;
char *out = (char *)malloc(cap);
if (!out) return NULL;
while (**p && **p != '"') {
char c = **p;
if (c == '\\') {
(*p)++;
if (!**p) break;
c = **p;
switch (c) {
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case '\\': c = '\\'; break;
case '"': c = '"'; break;
case '/': c = '/'; break;
default: break;
}
}
if (len + 2 >= cap) {
cap *= 2;
char *new_out = (char *)realloc(out, cap);
if (!new_out) {
free(out);
return NULL;
}
out = new_out;
}
out[len++] = c;
(*p)++;
}
if (**p == '"') (*p)++;
out[len] = '\0';
return out;
}
static JsonNode *parse_value(const char **p);
static JsonNode *parse_object(const char **p) {
if (**p != '{') return NULL;
(*p)++;
JsonNode *node = (JsonNode *)calloc(1, sizeof(JsonNode));
if (!node) return NULL;
node->type = JSON_OBJECT;
JsonNode **tail = &node->child;
*p = skip_ws(*p);
if (**p == '}') {
(*p)++;
return node;
}
while (**p) {
*p = skip_ws(*p);
if (**p != '"') break;
char *key = parse_string_val(p);
*p = skip_ws(*p);
if (**p == ':') (*p)++;
*p = skip_ws(*p);
JsonNode *val = parse_value(p);
if (val) {
val->key = key;
*tail = val;
tail = &val->next;
} else {
if (key) free(key);
}
*p = skip_ws(*p);
if (**p == ',') {
(*p)++;
continue;
}
if (**p == '}') {
(*p)++;
break;
}
}
return node;
}
static JsonNode *parse_array(const char **p) {
if (**p != '[') return NULL;
(*p)++;
JsonNode *node = (JsonNode *)calloc(1, sizeof(JsonNode));
if (!node) return NULL;
node->type = JSON_ARRAY;
JsonNode **tail = &node->child;
*p = skip_ws(*p);
if (**p == ']') {
(*p)++;
return node;
}
while (**p) {
*p = skip_ws(*p);
JsonNode *val = parse_value(p);
if (val) {
*tail = val;
tail = &val->next;
}
*p = skip_ws(*p);
if (**p == ',') {
(*p)++;
continue;
}
if (**p == ']') {
(*p)++;
break;
}
}
return node;
}
static JsonNode *parse_value(const char **p) {
*p = skip_ws(*p);
if (!**p) return NULL;
if (**p == '{') return parse_object(p);
if (**p == '[') return parse_array(p);
if (**p == '"') {
JsonNode *n = (JsonNode *)calloc(1, sizeof(JsonNode));
if (!n) return NULL;
n->type = JSON_STRING;
n->val_str = parse_string_val(p);
return n;
}
if (**p == 't' && strncmp(*p, "true", 4) == 0) {
*p += 4;
JsonNode *n = (JsonNode *)calloc(1, sizeof(JsonNode));
if (!n) return NULL;
n->type = JSON_BOOL;
n->val_bool = true;
return n;
}
if (**p == 'f' && strncmp(*p, "false", 5) == 0) {
*p += 5;
JsonNode *n = (JsonNode *)calloc(1, sizeof(JsonNode));
if (!n) return NULL;
n->type = JSON_BOOL;
n->val_bool = false;
return n;
}
if (**p == 'n' && strncmp(*p, "null", 4) == 0) {
*p += 4;
JsonNode *n = (JsonNode *)calloc(1, sizeof(JsonNode));
if (!n) return NULL;
n->type = JSON_NULL;
return n;
}
/* Number */
char *endptr;
double d = strtod(*p, &endptr);
if (endptr != *p) {
*p = endptr;
JsonNode *n = (JsonNode *)calloc(1, sizeof(JsonNode));
if (!n) return NULL;
n->type = JSON_NUMBER;
n->val_num = d;
return n;
}
return NULL;
}
static JsonNode *json_get(JsonNode *obj, const char *key) {
if (!obj || obj->type != JSON_OBJECT) return NULL;
for (JsonNode *c = obj->child; c; c = c->next) {
if (c->key && strcmp(c->key, key) == 0) return c;
}
return NULL;
}
static const char *json_get_str(JsonNode *obj, const char *key) {
JsonNode *n = json_get(obj, key);
if (n && n->type == JSON_STRING) return n->val_str;
return NULL;
}
/* --- Prompt Text Extractor --- */
/* --- Helper Utilities for Prompt Filtering & Graph Traversal --- */
static bool json_get_bool_val(JsonNode *root, JsonNode *inputs, const char *key, bool default_val) {
if (!inputs) return default_val;
JsonNode *n = json_get(inputs, key);
if (!n) return default_val;
if (n->type == JSON_BOOL) return n->val_bool;
if (n->type == JSON_ARRAY && n->child) {
const char *src_id = (n->child->type == JSON_STRING) ? n->child->val_str : NULL;
if (src_id) {
JsonNode *src_node = json_get(root, src_id);
if (src_node) {
JsonNode *src_inputs = json_get(src_node, "inputs");
if (src_inputs) {
JsonNode *val_node = json_get(src_inputs, "value");
if (val_node && val_node->type == JSON_BOOL) return val_node->val_bool;
}
}
}
}
return default_val;
}
static bool is_system_prompt_str(const char *s) {
if (!s) return false;
if (strstr(s, "FORGET EVERYTHING YOU KNOW") ||
strstr(s, "You are an expert prompt engineer") ||
strstr(s, "System Prompt") ||
strstr(s, "Faithfulness First:")) {
return true;
}
return false;
}
static char *extract_text_from_node(JsonNode *root, JsonNode *node, int depth) {
if (!node || depth > 15) return NULL;
const char *ctype = json_get_str(node, "class_type");
JsonNode *inputs = json_get(node, "inputs");
if (!inputs) return NULL;
/* 1. ComfySwitchNode / Switch / IfElse / Select */
if (ctype && (strstr(ctype, "Switch") || strstr(ctype, "IfElse") || strstr(ctype, "Select"))) {
bool sw_val = json_get_bool_val(root, inputs, "switch", false);
const char *link_key = sw_val ? "on_true" : "on_false";
JsonNode *sw_link = json_get(inputs, link_key);
if (!sw_link) sw_link = json_get(inputs, sw_val ? "input_b" : "input_a");
if (sw_link && sw_link->type == JSON_ARRAY && sw_link->child) {
const char *src_id = (sw_link->child->type == JSON_STRING) ? sw_link->child->val_str : NULL;
if (src_id) {
JsonNode *src_node = json_get(root, src_id);
if (src_node) return extract_text_from_node(root, src_node, depth + 1);
}
}
}
/* 2. PreviewAny / Reroute / PassThrough */
if (ctype && (strstr(ctype, "Preview") || strstr(ctype, "Reroute") || strstr(ctype, "PassThrough"))) {
JsonNode *src_link = json_get(inputs, "source");
if (!src_link) src_link = json_get(inputs, "input");
if (src_link && src_link->type == JSON_ARRAY && src_link->child) {
const char *src_id = (src_link->child->type == JSON_STRING) ? src_link->child->val_str : NULL;
if (src_id) {
JsonNode *src_node = json_get(root, src_id);
if (src_node) return extract_text_from_node(root, src_node, depth + 1);
}
}
}
/* 3. StringConcatenate / TextConcatenate */
if (ctype && (strstr(ctype, "StringConcatenate") || strstr(ctype, "TextConcatenate") || strstr(ctype, "Concat"))) {
char *str_a = NULL;
char *str_b = NULL;
JsonNode *link_a = json_get(inputs, "string_a");
if (!link_a) link_a = json_get(inputs, "text1");
if (link_a) {
if (link_a->type == JSON_STRING) str_a = strdup(link_a->val_str);
else if (link_a->type == JSON_ARRAY && link_a->child) {
const char *src_id = (link_a->child->type == JSON_STRING) ? link_a->child->val_str : NULL;
if (src_id) {
JsonNode *src_node = json_get(root, src_id);
if (src_node) str_a = extract_text_from_node(root, src_node, depth + 1);
}
}
}
JsonNode *link_b = json_get(inputs, "string_b");
if (!link_b) link_b = json_get(inputs, "text2");
if (link_b) {
if (link_b->type == JSON_STRING) str_b = strdup(link_b->val_str);
else if (link_b->type == JSON_ARRAY && link_b->child) {
const char *src_id = (link_b->child->type == JSON_STRING) ? link_b->child->val_str : NULL;
if (src_id) {
JsonNode *src_node = json_get(root, src_id);
if (src_node) str_b = extract_text_from_node(root, src_node, depth + 1);
}
}
}
/* Filter out system prompts */
if (str_a && is_system_prompt_str(str_a)) {
free(str_a);
str_a = NULL;
}
if (str_b && is_system_prompt_str(str_b)) {
free(str_b);
str_b = NULL;
}
if (str_a && str_b) {
const char *delim = json_get_str(inputs, "delimiter");
if (!delim) delim = "";
size_t len = strlen(str_a) + strlen(delim) + strlen(str_b) + 1;
char *combined = (char *)malloc(len);
if (combined) {
snprintf(combined, len, "%s%s%s", str_a, delim, str_b);
free(str_a);
free(str_b);
return combined;
}
}
if (str_a) return str_a;
if (str_b) return str_b;
}
/* 4. Direct inputs.text as string or link */
JsonNode *n_text = json_get(inputs, "text");
if (n_text) {
if (n_text->type == JSON_STRING && n_text->val_str && n_text->val_str[0] != '\0') {
if (!is_system_prompt_str(n_text->val_str)) return strdup(n_text->val_str);
}
if (n_text->type == JSON_ARRAY && n_text->child) {
const char *src_id = (n_text->child->type == JSON_STRING) ? n_text->child->val_str : NULL;
if (src_id) {
JsonNode *src_node = json_get(root, src_id);
if (src_node) {
char *res = extract_text_from_node(root, src_node, depth + 1);
if (res) return res;
}
}
}
}
/* 5. SDXL text_g and text_l */
const char *text_g = json_get_str(inputs, "text_g");
const char *text_l = json_get_str(inputs, "text_l");
if (text_g || text_l) {
if (text_g && text_l) {
if (strcmp(text_g, text_l) == 0) return strdup(text_g);
if (strstr(text_l, text_g)) return strdup(text_l);
if (strstr(text_g, text_l)) return strdup(text_g);
size_t len = strlen(text_g) + strlen(text_l) + 4;
char *combined = (char *)malloc(len);
if (combined) {
snprintf(combined, len, "%s\n%s", text_g, text_l);
return combined;
}
}
return strdup(text_g ? text_g : text_l);
}
/* 6. Flux clip_l and t5xxl */
const char *clip_l = json_get_str(inputs, "clip_l");
const char *t5xxl = json_get_str(inputs, "t5xxl");
if (clip_l || t5xxl) {
if (clip_l && t5xxl) {
if (strcmp(clip_l, t5xxl) == 0) return strdup(clip_l);
if (strstr(t5xxl, clip_l)) return strdup(t5xxl);
if (strstr(clip_l, t5xxl)) return strdup(clip_l);
size_t len = strlen(clip_l) + strlen(t5xxl) + 4;
char *combined = (char *)malloc(len);
if (combined) {
snprintf(combined, len, "%s\n%s", clip_l, t5xxl);
return combined;
}
}
return strdup(clip_l ? clip_l : t5xxl);
}
/* 7. Primitive / TextBox value, string, prompt */
const char *val = json_get_str(inputs, "value");
if (val && val[0] != '\0' && !is_system_prompt_str(val)) return strdup(val);
const char *str = json_get_str(inputs, "string");
if (str && str[0] != '\0' && !is_system_prompt_str(str)) return strdup(str);
const char *p_field = json_get_str(inputs, "prompt");
if (p_field && p_field[0] != '\0' && !is_system_prompt_str(p_field)) return strdup(p_field);
/* 8. Intermediate conditioning node (FluxGuidance, ConditioningCombine, ConditioningSetArea, LoraLoader, etc.) */
const char *cond_keys[] = { "conditioning", "conditioning_1", "conditioning_to", "conditioning_from", "cond", "clip", NULL };
for (int i = 0; cond_keys[i]; i++) {
JsonNode *c_link = json_get(inputs, cond_keys[i]);
if (c_link && c_link->type == JSON_ARRAY && c_link->child) {
const char *src_id = (c_link->child->type == JSON_STRING) ? c_link->child->val_str : NULL;
if (src_id) {
JsonNode *src_node = json_get(root, src_id);
if (src_node) {
char *res = extract_text_from_node(root, src_node, depth + 1);
if (res) return res;
}
}
}
}
return NULL;
}
static void extract_comfyui_prompts_from_json(const char *prompt_json, char **out_pos, char **out_neg) {
if (!prompt_json || !out_pos || !out_neg) return;
*out_pos = NULL;
*out_neg = NULL;
const char *p = prompt_json;
JsonNode *root = parse_value(&p);
if (!root || root->type != JSON_OBJECT) {
if (root) json_free(root);
return;
}
/* 1. First priority: Check if any node is explicitly titled "User Prompt" or "User Text" */
for (JsonNode *node = root->child; node; node = node->next) {
JsonNode *meta = json_get(node, "_meta");
if (meta) {
const char *title = json_get_str(meta, "title");
if (title && (strstr(title, "User Prompt") || strstr(title, "User Text"))) {
JsonNode *inputs = json_get(node, "inputs");
if (inputs) {
const char *val = json_get_str(inputs, "value");
if (!val) val = json_get_str(inputs, "string");
if (!val) val = json_get_str(inputs, "text");
if (val && val[0] != '\0' && !is_system_prompt_str(val)) {
*out_pos = strdup(val);
break;
}
}
}
}
}
/* 2. Trace Sampler nodes for positive and negative connections */
for (JsonNode *node = root->child; node; node = node->next) {
const char *ctype = json_get_str(node, "class_type");
if (ctype && strstr(ctype, "Sampler")) {
JsonNode *inputs = json_get(node, "inputs");
if (!inputs) continue;
/* Positive link */
if (!*out_pos) {
JsonNode *pos = json_get(inputs, "positive");
if (pos && pos->type == JSON_ARRAY && pos->child && pos->child->type == JSON_STRING) {
JsonNode *pos_node = json_get(root, pos->child->val_str);
if (pos_node) {
*out_pos = extract_text_from_node(root, pos_node, 0);
}
}
}
/* Negative link */
if (!*out_neg) {
JsonNode *neg = json_get(inputs, "negative");
if (neg && neg->type == JSON_ARRAY && neg->child && neg->child->type == JSON_STRING) {
JsonNode *neg_node = json_get(root, neg->child->val_str);
if (neg_node) {
*out_neg = extract_text_from_node(root, neg_node, 0);
}
}
}
}
}
/* 3. Fallback: if positive prompt was not found by tracing sampler, scan text encode nodes */
if (!*out_pos) {
for (JsonNode *node = root->child; node; node = node->next) {
const char *ctype = json_get_str(node, "class_type");
if (ctype && (strstr(ctype, "CLIPTextEncode") || strstr(ctype, "TextEncode") || strstr(ctype, "Prompt"))) {
char *txt = extract_text_from_node(root, node, 0);
if (txt && txt[0] != '\0') {
if (*out_neg && strcmp(txt, *out_neg) == 0) {
free(txt);
continue;
}
*out_pos = txt;
break;
}
}
}
}
json_free(root);
}
static void extract_comfyui_prompts_from_workflow(const char *workflow_json, char **out_pos, char **out_neg) {
if (!workflow_json || !out_pos || !out_neg) return;
*out_pos = NULL;
*out_neg = NULL;
const char *p = workflow_json;
JsonNode *root = parse_value(&p);
if (!root) return;
JsonNode *nodes = json_get(root, "nodes");
if (nodes && nodes->type == JSON_ARRAY) {
for (JsonNode *node = nodes->child; node; node = node->next) {
const char *type = json_get_str(node, "type");
const char *title = json_get_str(node, "title");
if (type && (strstr(type, "CLIPTextEncode") || strstr(type, "TextEncode") || strstr(type, "Prompt"))) {
JsonNode *wvals = json_get(node, "widgets_values");
if (wvals && wvals->type == JSON_ARRAY && wvals->child) {
for (JsonNode *val = wvals->child; val; val = val->next) {
if (val->type == JSON_STRING && val->val_str && val->val_str[0] != '\0') {
bool is_neg = (title && strstr(title, "Negative")) || (type && strstr(type, "Negative"));
if (is_neg && !*out_neg) {
*out_neg = strdup(val->val_str);
} else if (!is_neg && !*out_pos) {
*out_pos = strdup(val->val_str);
}
}
}
}
}
}
}
json_free(root);
}
static void extract_a1111_prompts(const char *params, char **out_pos, char **out_neg) {
if (!params || !out_pos || !out_neg) return;
*out_pos = NULL;
*out_neg = NULL;
const char *neg_marker = "\nNegative prompt: ";
const char *steps_marker = "\nSteps: ";
const char *neg_pos = strstr(params, neg_marker);
const char *steps_pos = strstr(params, steps_marker);
/* Positive prompt */
const char *pos_end = neg_pos ? neg_pos : (steps_pos ? steps_pos : params + strlen(params));
size_t pos_len = pos_end - params;
if (pos_len > 0) {
char *pos_str = (char *)malloc(pos_len + 1);
if (pos_str) {
memcpy(pos_str, params, pos_len);
pos_str[pos_len] = '\0';
*out_pos = pos_str;
}
}
/* Negative prompt */
if (neg_pos) {
const char *neg_start = neg_pos + strlen(neg_marker);
const char *neg_end = steps_pos ? steps_pos : params + strlen(params);
if (neg_end > neg_start) {
size_t neg_len = neg_end - neg_start;
char *neg_str = (char *)malloc(neg_len + 1);
if (neg_str) {
memcpy(neg_str, neg_start, neg_len);
neg_str[neg_len] = '\0';
*out_neg = neg_str;
}
}
}
}
static void extract_invokeai_prompts(const char *json_str, char **out_pos, char **out_neg) {
if (!json_str || !out_pos || !out_neg) return;
const char *p = json_str;
JsonNode *root = parse_value(&p);
if (!root) return;
/* Check root or nested objects (core_metadata, image, metadata) */
JsonNode *core_meta = json_get(root, "core_metadata");
if (!core_meta) core_meta = json_get(root, "image");
if (!core_meta) core_meta = json_get(root, "metadata");
/* Positive prompt search */
const char *pos = json_get_str(root, "positive_prompt");
if (!pos && core_meta) pos = json_get_str(core_meta, "positive_prompt");
if (!pos) pos = json_get_str(root, "positive_style_prompt");
if (!pos && core_meta) pos = json_get_str(core_meta, "positive_style_prompt");
if (!pos) {
JsonNode *p_node = json_get(root, "prompt");
if (!p_node && core_meta) p_node = json_get(core_meta, "prompt");
if (p_node) {
if (p_node->type == JSON_STRING) {
pos = p_node->val_str;
} else if (p_node->type == JSON_ARRAY && p_node->child) {
for (JsonNode *item = p_node->child; item; item = item->next) {
if (item->type == JSON_OBJECT) {
const char *item_p = json_get_str(item, "prompt");
if (item_p && item_p[0] != '\0') {
pos = item_p;
break;
}
} else if (item->type == JSON_STRING && item->val_str[0] != '\0') {
pos = item->val_str;
break;
}
}
}
}
}
if (pos && pos[0] != '\0' && !*out_pos) {
*out_pos = strdup(pos);
}
/* Negative prompt search */
const char *neg = json_get_str(root, "negative_prompt");
if (!neg && core_meta) neg = json_get_str(core_meta, "negative_prompt");
if (!neg) neg = json_get_str(root, "negative_style_prompt");
if (!neg && core_meta) neg = json_get_str(core_meta, "negative_style_prompt");
if (neg && neg[0] != '\0' && !*out_neg) {
*out_neg = strdup(neg);
}
/* Check InvokeAI graph nodes if prompts not found */
if (!*out_pos) {
JsonNode *nodes = json_get(root, "nodes");
if (!nodes) nodes = json_get(root, "graph");
if (nodes && nodes->type == JSON_OBJECT) {
for (JsonNode *node = nodes->child; node; node = node->next) {
if (node->type != JSON_OBJECT) continue;
const char *ntype = json_get_str(node, "type");
JsonNode *inputs = json_get(node, "inputs");
if (inputs) {
const char *p_val = json_get_str(inputs, "prompt");
if (!p_val) p_val = json_get_str(inputs, "positive_prompt");
if (!p_val) p_val = json_get_str(inputs, "value");
if (p_val && p_val[0] != '\0' && !is_system_prompt_str(p_val)) {
if (ntype && (strstr(ntype, "neg") || strstr(ntype, "negative"))) {
if (!*out_neg) *out_neg = strdup(p_val);
} else {
if (!*out_pos) *out_pos = strdup(p_val);
}
}
const char *n_val = json_get_str(inputs, "negative_prompt");
if (n_val && n_val[0] != '\0' && !*out_neg) {
*out_neg = strdup(n_val);
}
}
}
}
}
json_free(root);
}
static void extract_invokeai_dream_prompt(const char *dream_str, char **out_pos, char **out_neg) {
if (!dream_str) return;
if (out_pos && !*out_pos) {
const char *flag = strstr(dream_str, " -");
size_t len = flag ? (size_t)(flag - dream_str) : strlen(dream_str);
while (len > 0 && isspace((unsigned char)dream_str[len - 1])) len--;
if (len > 0) {
char *p = (char *)malloc(len + 1);
if (p) {
memcpy(p, dream_str, len);
p[len] = '\0';
*out_pos = p;
}
}
}
if (out_neg && !*out_neg) {
const char *neg_flag = strstr(dream_str, " -n ");
if (!neg_flag) neg_flag = strstr(dream_str, " -n=");
if (neg_flag) {
const char *val_start = neg_flag + 4;
while (*val_start && isspace((unsigned char)*val_start)) val_start++;
if (*val_start == '"') {
val_start++;
const char *val_end = strchr(val_start, '"');
if (val_end) {
size_t nlen = (size_t)(val_end - val_start);
char *np = (char *)malloc(nlen + 1);
if (np) {
memcpy(np, val_start, nlen);
np[nlen] = '\0';
*out_neg = np;
}
}
}
}
}
}
/* --- PNG Chunk Parsing Implementation --- */
static void free_metadata(MetadataResult *meta) {
if (!meta) return;
if (meta->raw_prompt) { free(meta->raw_prompt); meta->raw_prompt = NULL; }
if (meta->raw_workflow) { free(meta->raw_workflow); meta->raw_workflow = NULL; }
if (meta->raw_parameters) { free(meta->raw_parameters); meta->raw_parameters = NULL; }
if (meta->raw_invokeai) { free(meta->raw_invokeai); meta->raw_invokeai = NULL; }
if (meta->raw_dream) { free(meta->raw_dream); meta->raw_dream = NULL; }
if (meta->prompt_text) { free(meta->prompt_text); meta->prompt_text = NULL; }
if (meta->negative_text) { free(meta->negative_text); meta->negative_text = NULL; }
meta->selected_label = NULL;
meta->selected_val = NULL;
meta->selected_len = 0;
meta->found = false;
meta->status = PNG_ERR_NO_METADATA;
}
static PngStatus parse_png_metadata(const char *filepath, MetadataResult *meta) {
if (!filepath || !meta) return PNG_ERR_IO;
memset(meta, 0, sizeof(MetadataResult));
meta->status = PNG_ERR_NO_METADATA;
FILE *fp = fopen(filepath, "rb");
if (!fp) {
meta->status = PNG_ERR_IO;
return PNG_ERR_IO;
}
/* Verify 8-byte PNG header: \x89PNG\r\n\x1a\n */
uint8_t sig[8];
if (fread(sig, 1, 8, fp) != 8) {
fclose(fp);
meta->status = PNG_ERR_NOT_PNG;
return PNG_ERR_NOT_PNG;
}
const uint8_t png_sig[8] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
if (memcmp(sig, png_sig, 8) != 0) {
fclose(fp);
meta->status = PNG_ERR_NOT_PNG;
return PNG_ERR_NOT_PNG;
}
/* Iterate through chunks */
while (!feof(fp)) {
uint8_t len_buf[4];
if (fread(len_buf, 1, 4, fp) != 4) break;
uint32_t chunk_len = ((uint32_t)len_buf[0] << 24) |
((uint32_t)len_buf[1] << 16) |
((uint32_t)len_buf[2] << 8) |
(uint32_t)len_buf[3];
char chunk_type[5] = {0};
if (fread(chunk_type, 1, 4, fp) != 4) break;
/* If we hit IEND, PNG ends */
if (strcmp(chunk_type, "IEND") == 0) {
break;
}
/* Parse tEXt chunk (Latin-1 / ASCII) */
if (strcmp(chunk_type, "tEXt") == 0 && chunk_len > 0) {
char *buf = (char *)malloc(chunk_len + 1);
if (buf) {
if (fread(buf, 1, chunk_len, fp) == chunk_len) {
buf[chunk_len] = '\0';
size_t klen = strnlen(buf, chunk_len);
if (klen < chunk_len) {
const char *key = buf;
const char *val = buf + klen + 1;
size_t vlen = chunk_len - (klen + 1);
if (strcmp(key, "prompt") == 0 && !meta->raw_prompt) {
meta->raw_prompt = (char *)malloc(vlen + 1);
if (meta->raw_prompt) {
memcpy(meta->raw_prompt, val, vlen);
meta->raw_prompt[vlen] = '\0';
meta->raw_prompt_len = vlen;
}
} else if (strcmp(key, "workflow") == 0 && !meta->raw_workflow) {
meta->raw_workflow = (char *)malloc(vlen + 1);
if (meta->raw_workflow) {
memcpy(meta->raw_workflow, val, vlen);
meta->raw_workflow[vlen] = '\0';
meta->raw_workflow_len = vlen;
}
} else if (strcmp(key, "parameters") == 0 && !meta->raw_parameters) {
meta->raw_parameters = (char *)malloc(vlen + 1);
if (meta->raw_parameters) {
memcpy(meta->raw_parameters, val, vlen);
meta->raw_parameters[vlen] = '\0';
meta->raw_parameters_len = vlen;
}
} else if ((strcmp(key, "sd-metadata") == 0 || strcmp(key, "sd_metadata") == 0 || strcmp(key, "invokeai") == 0 || strcmp(key, "invokeai_metadata") == 0 || strcmp(key, "invokeai_graph") == 0 || strcmp(key, "invokeai_workflow") == 0) && !meta->raw_invokeai) {
meta->raw_invokeai = (char *)malloc(vlen + 1);
if (meta->raw_invokeai) {
memcpy(meta->raw_invokeai, val, vlen);
meta->raw_invokeai[vlen] = '\0';
meta->raw_invokeai_len = vlen;
}
} else if (strcmp(key, "Dream") == 0 && !meta->raw_dream) {
meta->raw_dream = (char *)malloc(vlen + 1);
if (meta->raw_dream) {
memcpy(meta->raw_dream, val, vlen);
meta->raw_dream[vlen] = '\0';
meta->raw_dream_len = vlen;
}
}
}
}
free(buf);
} else {
fseek(fp, chunk_len, SEEK_CUR);
}
/* Skip 4 bytes CRC */
fseek(fp, 4, SEEK_CUR);
}
/* Parse iTXt chunk (International UTF-8 text) */
else if (strcmp(chunk_type, "iTXt") == 0 && chunk_len > 5) {
uint8_t *buf = (uint8_t *)malloc(chunk_len + 1);
if (buf) {
if (fread(buf, 1, chunk_len, fp) == chunk_len) {
buf[chunk_len] = '\0';
size_t klen = strnlen((char *)buf, chunk_len);
if (klen + 5 <= chunk_len) {
const char *key = (char *)buf;
uint8_t comp_flag = buf[klen + 1];
size_t idx = klen + 3;
/* Skip lang tag */
while (idx < chunk_len && buf[idx] != 0) idx++;
idx++;
/* Skip translated key */
while (idx < chunk_len && buf[idx] != 0) idx++;
idx++;
if (idx <= chunk_len && comp_flag == 0) {
const char *val = (char *)(buf + idx);
size_t vlen = chunk_len - idx;
if (strcmp(key, "prompt") == 0 && !meta->raw_prompt) {
meta->raw_prompt = (char *)malloc(vlen + 1);
if (meta->raw_prompt) {
memcpy(meta->raw_prompt, val, vlen);
meta->raw_prompt[vlen] = '\0';
meta->raw_prompt_len = vlen;
}
} else if (strcmp(key, "workflow") == 0 && !meta->raw_workflow) {
meta->raw_workflow = (char *)malloc(vlen + 1);
if (meta->raw_workflow) {
memcpy(meta->raw_workflow, val, vlen);
meta->raw_workflow[vlen] = '\0';
meta->raw_workflow_len = vlen;
}
} else if (strcmp(key, "parameters") == 0 && !meta->raw_parameters) {
meta->raw_parameters = (char *)malloc(vlen + 1);
if (meta->raw_parameters) {
memcpy(meta->raw_parameters, val, vlen);
meta->raw_parameters[vlen] = '\0';
meta->raw_parameters_len = vlen;
}
} else if ((strcmp(key, "sd-metadata") == 0 || strcmp(key, "sd_metadata") == 0 || strcmp(key, "invokeai") == 0 || strcmp(key, "invokeai_metadata") == 0 || strcmp(key, "invokeai_graph") == 0 || strcmp(key, "invokeai_workflow") == 0) && !meta->raw_invokeai) {
meta->raw_invokeai = (char *)malloc(vlen + 1);
if (meta->raw_invokeai) {
memcpy(meta->raw_invokeai, val, vlen);
meta->raw_invokeai[vlen] = '\0';
meta->raw_invokeai_len = vlen;
}
} else if (strcmp(key, "Dream") == 0 && !meta->raw_dream) {
meta->raw_dream = (char *)malloc(vlen + 1);
if (meta->raw_dream) {
memcpy(meta->raw_dream, val, vlen);
meta->raw_dream[vlen] = '\0';
meta->raw_dream_len = vlen;
}
}
}
}
}
free(buf);
} else {
fseek(fp, chunk_len, SEEK_CUR);
}
/* Skip 4 bytes CRC */
fseek(fp, 4, SEEK_CUR);
}
/* Skip all other chunks (IDAT pixel data, pHYs, etc.) */
else {
fseek(fp, chunk_len + 4, SEEK_CUR);
}
}
fclose(fp);
/* Extract prompt text from available metadata sources */
if (meta->raw_prompt) {
extract_comfyui_prompts_from_json(meta->raw_prompt, &meta->prompt_text, &meta->negative_text);
}
if (!meta->prompt_text && meta->raw_workflow) {
extract_comfyui_prompts_from_workflow(meta->raw_workflow, &meta->prompt_text, &meta->negative_text);
}
if (!meta->prompt_text && meta->raw_invokeai) {
extract_invokeai_prompts(meta->raw_invokeai, &meta->prompt_text, &meta->negative_text);
}