-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageDropResolutionExtension.cs
More file actions
118 lines (107 loc) · 5.75 KB
/
ImageDropResolutionExtension.cs
File metadata and controls
118 lines (107 loc) · 5.75 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
using SwarmUI.Core;
using SwarmUI.Text2Image;
using SwarmUI.Media;
using SwarmUI.Accounts;
using SwarmUI.WebAPI;
using Newtonsoft.Json.Linq;
namespace GlenCarpenter.Extensions.ImageDropResolutionExtension;
/// <summary>Automatically sets the Resolution (width/height) to match the dimensions of an image dropped onto the prompt area.</summary>
public class ImageDropResolutionExtension : Extension
{
/// <summary>Parameter group for Image Drop Resolution controls.</summary>
public static T2IParamGroup ImageDropResolutionGroup;
/// <summary>Whether to update output resolution when an image is dropped onto the image prompt area.</summary>
public static T2IRegisteredParam<bool> UpdatePromptResolutionParam;
/// <summary>Whether to update output resolution when an image is set as the init image.</summary>
public static T2IRegisteredParam<bool> UpdateInitResolutionParam;
/// <summary>Whether to resize images dropped onto the image prompt area to the target side length.</summary>
public static T2IRegisteredParam<bool> ResizePromptParam;
/// <summary>Whether to resize images set as the init image to the target side length.</summary>
public static T2IRegisteredParam<bool> ResizeInitParam;
/// <summary>Target side length for image scaling. Images are scaled so their area approximates this value squared, maintaining aspect ratio and rounding to multiples of 16.</summary>
public static T2IRegisteredParam<int> SideLengthParam;
/// <summary>Permission group for Image Drop Resolution extension.</summary>
public static readonly PermInfoGroup IDRPermGroup = new("ImageDropResolution", "Permissions related to Image Drop Resolution functionality.");
/// <summary>Permission to call the server-side image resize API.</summary>
public static readonly PermInfo PermResizeImage = Permissions.Register(new(
"imagedropresolution_resize_image",
"Resize Image",
"Allows the user to call the Image Drop Resolution server-side image resize API.",
PermissionDefault.USER,
IDRPermGroup));
public override void OnInit()
{
ScriptFiles.Add("Assets/image_drop_resolution.js");
API.RegisterAPICall(ResizeImage, false, PermResizeImage);
ImageDropResolutionGroup = new("Image Drop Resolution", Toggles: true, Open: false, OrderPriority: 100, Description: "Automatically update the output resolution when an image is dropped or set as init image.");
UpdatePromptResolutionParam = T2IParamTypes.Register<bool>(new(
Name: "[IDR] Update Resolution To Image Prompt",
Description: "When enabled, dropping an image onto the image prompt area will update the output resolution to match the image dimensions.",
Default: "true",
Group: ImageDropResolutionGroup,
IntentionalUnused: true,
OrderPriority: 1
));
UpdateInitResolutionParam = T2IParamTypes.Register<bool>(new(
Name: "[IDR] Update Resolution To Init Image",
Description: "When enabled, setting an init image will update the output resolution to match the image dimensions.",
Default: "true",
Group: ImageDropResolutionGroup,
IntentionalUnused: true,
OrderPriority: 2
));
ResizePromptParam = T2IParamTypes.Register<bool>(new(
Name: "[IDR] Resize Image Prompt to Side Length",
Description: "When enabled, images dropped onto the image prompt area are resized so their area approximates the target side length squared.",
Default: "true",
Group: ImageDropResolutionGroup,
IntentionalUnused: true,
OrderPriority: 3
));
ResizeInitParam = T2IParamTypes.Register<bool>(new(
Name: "[IDR] Resize Init Image to Side Length",
Description: "When enabled, images set as the init image are resized so their area approximates the target side length squared.",
Default: "true",
Group: ImageDropResolutionGroup,
IntentionalUnused: true,
OrderPriority: 4
));
SideLengthParam = T2IParamTypes.Register<int>(new(
Name: "[IDR] Side Length",
Description: "Target side length for image scaling. Images are scaled so their area is approximately this value squared, maintaining aspect ratio and rounding to multiples of 16.",
Default: "1024",
Min: 64,
Max: 4096,
Step: 64,
Group: ImageDropResolutionGroup,
ViewType: ParamViewType.SLIDER,
IntentionalUnused: true,
OrderPriority: 5
));
}
/// <summary>Resizes an image to the given dimensions using Lanczos3 high-quality resampling and returns it as a PNG data URL.</summary>
[API.APIDescription("Resizes an image to the given dimensions using Lanczos3 high-quality resampling.",
"""
"image": "data:image/png;base64,..."
""")]
public static async Task<JObject> ResizeImage(Session session,
[API.APIParameter("The source image as a base64 data URL.")] string image,
[API.APIParameter("Target width in pixels.")] int width,
[API.APIParameter("Target height in pixels.")] int height)
{
if (width < 1 || height < 1 || width > 16384 || height > 16384)
{
return new JObject() { ["error"] = "Invalid dimensions: width and height must each be between 1 and 16384." };
}
ImageFile imgFile;
try
{
imgFile = ImageFile.FromDataString(image);
}
catch (Exception)
{
return new JObject() { ["error"] = "Invalid image data." };
}
return new JObject() { ["image"] = imgFile.Resize(width, height).AsDataString() };
}
}