|
1 | 1 | using System; |
2 | 2 | using System.Runtime.InteropServices; |
| 3 | +using JetBrains.Annotations; |
3 | 4 |
|
4 | 5 | namespace StableDiffusion.NET; |
5 | 6 |
|
| 7 | +[PublicAPI] |
6 | 8 | public sealed unsafe class StableDiffusionModel : IDisposable |
7 | 9 | { |
8 | 10 | #region Properties & Fields |
@@ -206,20 +208,102 @@ private StableDiffusionImage ImageToImage(string prompt, Native.sd_image_t image |
206 | 208 | { |
207 | 209 | ObjectDisposedException.ThrowIf(_disposed, this); |
208 | 210 |
|
209 | | - Native.sd_image_t* result = Native.img2img(_ctx, |
210 | | - image, |
211 | | - prompt, |
212 | | - parameter.NegativePrompt, |
213 | | - parameter.ClipSkip, |
214 | | - parameter.CfgScale, |
215 | | - parameter.Width, |
216 | | - parameter.Height, |
217 | | - parameter.SampleMethod, |
218 | | - parameter.SampleSteps, |
219 | | - parameter.Strength, |
220 | | - parameter.Seed, |
221 | | - 1); |
| 211 | + Native.sd_image_t* result; |
| 212 | + if (parameter.ControlNet.IsEnabled) |
| 213 | + { |
| 214 | + fixed (byte* imagePtr = parameter.ControlNet.Image) |
| 215 | + { |
| 216 | + |
| 217 | + if (parameter.ControlNet.CannyPreprocess) |
| 218 | + { |
| 219 | + Native.sd_image_t controlNetImage = new() |
| 220 | + { |
| 221 | + width = (uint)parameter.Width, |
| 222 | + height = (uint)parameter.Height, |
| 223 | + channel = 3, |
| 224 | + data = Native.preprocess_canny(imagePtr, |
| 225 | + parameter.Width, |
| 226 | + parameter.Height, |
| 227 | + parameter.ControlNet.CannyHighThreshold, |
| 228 | + parameter.ControlNet.CannyLowThreshold, |
| 229 | + parameter.ControlNet.CannyWeak, |
| 230 | + parameter.ControlNet.CannyStrong, |
| 231 | + parameter.ControlNet.CannyInverse) |
| 232 | + }; |
| 233 | + |
| 234 | + result = Native.img2img(_ctx, |
| 235 | + image, |
| 236 | + prompt, |
| 237 | + parameter.NegativePrompt, |
| 238 | + parameter.ClipSkip, |
| 239 | + parameter.CfgScale, |
| 240 | + parameter.Width, |
| 241 | + parameter.Height, |
| 242 | + parameter.SampleMethod, |
| 243 | + parameter.SampleSteps, |
| 244 | + parameter.Strength, |
| 245 | + parameter.Seed, |
| 246 | + 1, |
| 247 | + &controlNetImage, |
| 248 | + parameter.ControlNet.Strength, |
| 249 | + parameter.PhotoMaker.StyleRatio, |
| 250 | + parameter.PhotoMaker.NormalizeInput, |
| 251 | + parameter.PhotoMaker.InputIdImageDirectory); |
| 252 | + |
| 253 | + Marshal.FreeHGlobal((nint)controlNetImage.data); |
| 254 | + } |
| 255 | + else |
| 256 | + { |
| 257 | + Native.sd_image_t controlNetImage = new() |
| 258 | + { |
| 259 | + width = (uint)parameter.Width, |
| 260 | + height = (uint)parameter.Height, |
| 261 | + channel = 3, |
| 262 | + data = imagePtr |
| 263 | + }; |
222 | 264 |
|
| 265 | + result = Native.img2img(_ctx, |
| 266 | + image, |
| 267 | + prompt, |
| 268 | + parameter.NegativePrompt, |
| 269 | + parameter.ClipSkip, |
| 270 | + parameter.CfgScale, |
| 271 | + parameter.Width, |
| 272 | + parameter.Height, |
| 273 | + parameter.SampleMethod, |
| 274 | + parameter.SampleSteps, |
| 275 | + parameter.Strength, |
| 276 | + parameter.Seed, |
| 277 | + 1, |
| 278 | + &controlNetImage, |
| 279 | + parameter.ControlNet.Strength, |
| 280 | + parameter.PhotoMaker.StyleRatio, |
| 281 | + parameter.PhotoMaker.NormalizeInput, |
| 282 | + parameter.PhotoMaker.InputIdImageDirectory); |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + else |
| 287 | + { |
| 288 | + result = Native.img2img(_ctx, |
| 289 | + image, |
| 290 | + prompt, |
| 291 | + parameter.NegativePrompt, |
| 292 | + parameter.ClipSkip, |
| 293 | + parameter.CfgScale, |
| 294 | + parameter.Width, |
| 295 | + parameter.Height, |
| 296 | + parameter.SampleMethod, |
| 297 | + parameter.SampleSteps, |
| 298 | + parameter.Strength, |
| 299 | + parameter.Seed, |
| 300 | + 1, |
| 301 | + null, |
| 302 | + 0, |
| 303 | + parameter.PhotoMaker.StyleRatio, |
| 304 | + parameter.PhotoMaker.NormalizeInput, |
| 305 | + parameter.PhotoMaker.InputIdImageDirectory); |
| 306 | + } |
223 | 307 |
|
224 | 308 | return new StableDiffusionImage(result); |
225 | 309 | } |
|
0 commit comments