File size: 8,087 Bytes
c336648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using PNGReaderTest;
using SixLabors.ImageSharp;
using System.Data;
using System.Text.Json;
using Image = SixLabors.ImageSharp.Image;

namespace PNGMetadataViewer
{
    public partial class PNGMetadataViewerForm : Form
    {
        protected bool validData;

        string path;
        protected List<object> textValues;
        protected Image image;
        protected Thread getImageThread;

        public PNGMetadataViewerForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void formTableLayoutPanel_DragEnter(object sender, DragEventArgs e)
        {
            string filename;

            validData = GetFilename(out filename, e);
            if (validData)
            {
                path = filename;
                getImageThread = new Thread(new ThreadStart(LoadImage));
                getImageThread.Start();
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private bool GetFilename(out string filename, DragEventArgs e)
        {
            bool ret = false;
            filename = String.Empty;
            if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
            {
                Array? data = e.Data.GetData("FileDrop") as Array;
                if (data != null)
                {
                    if ((data.Length == 1) && (data.GetValue(0) is String))
                    {
                        filename = ((string[])data)[0];
                        string ext = Path.GetExtension(filename).ToLower();
                        if ((ext == ".jpg") || (ext == ".png") || (ext == ".bmp"))
                        {
                            ret = true;
                        }
                    }
                }
            }
            return ret;
        }

        private void formTableLayoutPanel_DragDrop(object sender, DragEventArgs e)
        {
            if (validData)
            {
                clearAll();

                while (getImageThread.IsAlive)
                {
                    Application.DoEvents();
                    Thread.Sleep(0);
                }

                pictureBox.Image = ImageExtensions.ToNetImage(image.ToArray());

                imageMetadataGridView.DataSource = MakeImageMetadataTable(image);

                var pngTextData = image.Metadata.GetPngMetadata().TextData;

                if (pngTextData.Count == 1)
                {
                    toolStripStatusLabel.Text = $"Automatic1111 Generated PNG: {path}";

                    // Probably Automatic111
                    var promptsData = image.Metadata.GetPngMetadata().TextData[0].Value.Split("\n");

                    promptBox.Text = promptsData[0];
                    negativePromptBox.Text = promptsData[1].Replace("Negative prompt:", string.Empty);

                    var modelData = promptsData[2].Split(',', StringSplitOptions.TrimEntries);
                    modelMetadataGridView.DataSource = MakeAutomatic1111DataTable(modelData);
                }
                else if (pngTextData.Count > 1)
                {
                    toolStripStatusLabel.Text = $"ComfyUI Generated PNG: {path}";

                    // Probably ComfyUI
                    var comfyUIMetadata = JsonSerializer.Deserialize<ComfyUIData>(pngTextData[1].Value);

                    if (comfyUIMetadata == null)
                    {
                        MessageBox.Show("Could not load ComfyUI Metadata", "Error");
                    }
                    else
                    {
                        modelMetadataGridView.DataSource = MakeComfyUIKSamplerDataTable(comfyUIMetadata);

                        textValues = comfyUIMetadata.nodes.Where(x => x.type.Equals("CLIPTextEncode")).SelectMany(x => x.widgets_values).ToList();

                        promptBox.Text = textValues.Count() > 0 ? textValues[1]?.ToString() : string.Empty;
                        negativePromptBox.Text = textValues.Count() > 0 ? textValues[0].ToString() : string.Empty;
                    }
                }
                else
                {
                    // No TextData

                    toolStripStatusLabel.Text = $"No text data found in PNG: {path}";
                }
            }
        }
        protected void LoadImage()
        {
            image = Image.Load(path);
        }

        private DataTable MakeComfyUIKSamplerDataTable(ComfyUIData data)
        {
            var table = new DataTable();

            table.Columns.Add(new DataColumn("Key", typeof(string)));
            table.Columns.Add(new DataColumn("Value", typeof(string)));

            var ksamplerFields = new[] { "Seed", "Control After Generate", "Steps", "CFG", "Sampler Name", "Scheduler", "Denoise" };

            var checkpointLoader = data.nodes.Where(x => x.type.Contains("CheckpointLoaderSimple")).Select(x => x.widgets_values).FirstOrDefault();
            table.Rows.Add("Model Name", checkpointLoader != null ? checkpointLoader[0] ?? "Unknown" : "Unknown");

            var loraLoader = data.nodes.Where(x => x.type.Equals("LoraLoader")).SelectMany(x => x.widgets_values).ToList();
            table.Rows.Add("LORA Name", loraLoader?.Count != 0 ? loraLoader?[0] : "None");

            int i = 0;

            foreach (var ksamplerItem in data.nodes.Where(x => x.type.Equals("KSampler")).Select(x => x.widgets_values).SelectMany(x => x))
            {
                table.Rows.Add(ksamplerFields[i++], ksamplerItem);
            }

            return table;
        }

        private DataTable MakeAutomatic1111DataTable(string[] data)
        {
            var table = new DataTable();

            table.Columns.Add(new DataColumn("Key", typeof(string)));
            table.Columns.Add(new DataColumn("Value", typeof(string)));

            foreach (var item in data)
            {
                var k = item.Split(':');
                table.Rows.Add(k[0], k[1]);
            }

            return table;
        }

        private DataTable MakeImageMetadataTable(Image image)
        {
            var table = new DataTable();

            table.Columns.Add(new DataColumn("Key", typeof(string)));
            table.Columns.Add(new DataColumn("Value", typeof(string)));

            table.Rows.Add("Height", image.Height);
            table.Rows.Add("Width", image.Width);
            table.Rows.Add("AlphaRepresentation", image.PixelType.AlphaRepresentation?.ToString());
            table.Rows.Add("BitsPerPixel", image.PixelType.BitsPerPixel);
            table.Rows.Add("DefaultMimeType", image.Metadata.DecodedImageFormat?.DefaultMimeType);
            table.Rows.Add("Name", image.Metadata.DecodedImageFormat?.Name);
            table.Rows.Add("ColorType", image.Metadata.GetPngMetadata().ColorType);
            table.Rows.Add("FramesCount", image.Frames.Count());

            return table;
        }

        private void copyPromptMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripItem? item = sender as ToolStripItem;
            if (item != null)
            {
                ContextMenuStrip? owner = item.Owner as ContextMenuStrip;
                if (owner != null && !string.IsNullOrEmpty(owner.SourceControl?.Text))
                {
                    Clipboard.SetText(owner.SourceControl.Text, TextDataFormat.Text);
                    toolStripStatusLabel.Text = "Copied...";
                }
            }
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            clearAll();
        }

        private void clearAll()
        {
            pictureBox.Image = null;
            promptBox.Text = string.Empty;
            negativePromptBox.Text = string.Empty;
            modelMetadataGridView.DataSource = null;
            imageMetadataGridView.DataSource = null;
            toolStripStatusLabel.Text = "Waiting for input...";
        }
    }
}