1515using SixLabors . ImageSharp . Formats . Png ;
1616using SixLabors . ImageSharp . PixelFormats ;
1717using System . Diagnostics ;
18+ using System . Diagnostics . CodeAnalysis ;
1819using System . Runtime . InteropServices ;
1920using CoenM . ImageHash ;
2021using CommunityToolkit . HighPerformance ;
2122using Kantan . Net . Utilities ;
2223using Microsoft . IO ;
24+ using SixLabors . ImageSharp . Memory ;
2325using SmartImage . Lib . Model ;
2426using SmartImage . Lib . Utilities ;
2527
@@ -128,7 +130,7 @@ protected set
128130
129131#endregion
130132
131- #region
133+ #region
132134
133135 public double ? Similarity
134136 {
@@ -142,7 +144,7 @@ public virtual bool CalculateSimilarity(IHashable hashable)
142144 return Similarity . HasValue ;
143145 }
144146
145- #endregion
147+ #endregion
146148
147149#region
148150
@@ -172,9 +174,6 @@ public Stream GetStream()
172174#endregion
173175
174176
175- public static readonly UniImage Null = null ;
176-
177-
178177 private protected UniImage ( string value , UniImageType type )
179178 {
180179 Value = value ;
@@ -184,10 +183,13 @@ private protected UniImage(string value, UniImageType type)
184183
185184#region
186185
187- protected abstract ValueTask < bool > AllocAsync ( CancellationToken ct = default ) ;
186+ /// <summary>
187+ /// Allocates <see cref="Bytes"/>
188+ /// </summary>
189+ protected abstract ValueTask < bool > AllocSourceAsync ( CancellationToken ct = default ) ;
188190
189191 /// <summary>
190- /// Allocates <see cref="Image"/>
192+ /// Allocates <see cref="Image"/> from <see cref="Bytes"/>
191193 /// </summary>
192194 public virtual async ValueTask < bool > AllocImageAsync ( CancellationToken ct = default )
193195 {
@@ -211,13 +213,26 @@ public virtual async ValueTask<bool> AllocImageAsync(CancellationToken ct = defa
211213
212214 }
213215
216+ /// <returns><see cref="AllocSourceAsync"/>, <see cref="AllocImageAsync"/></returns>
217+ protected virtual async ValueTask < ( bool AllocOk , bool AllocImageOk ) > AllocAll ( CancellationToken ct )
218+ {
219+ bool allocOk = await AllocSourceAsync ( ct ) ;
220+ bool allocImgOk = false ;
221+
222+ if ( allocOk ) {
223+ allocImgOk = await AllocImageAsync ( ct ) ;
224+ }
225+
226+ return ( allocOk , allocImgOk ) ;
227+ }
228+
214229 /// <summary>
215230 /// Attempts to create the appropriate <see cref="UniImage" /> for <paramref name="o" />.
216231 /// </summary>
217232 public static async Task < UniImage > TryCreateAsync ( object o , bool autoInit = true , bool autoDisposeOnError = true ,
218233 CancellationToken ct = default )
219234 {
220- UniImage ui = Null ;
235+ UniImage ui = null ;
221236
222237 try {
223238
@@ -235,11 +250,7 @@ public static async Task<UniImage> TryCreateAsync(object o, bool autoInit = true
235250 bool allocOk = false ;
236251 bool allocImgOk = false ;
237252
238- allocOk = await ui . AllocAsync ( ct ) ;
239-
240- if ( allocOk ) {
241- allocImgOk = await ui . AllocImageAsync ( ct ) ;
242- }
253+ ( allocOk , allocImgOk ) = await ui . AllocAll ( ct ) ;
243254
244255 s_logger . LogTrace ( "{Value} :: {AllocOk} {AllocImgOk}" , o , allocOk , allocImgOk ) ;
245256
@@ -270,7 +281,7 @@ public static bool IsValidSourceType(object o)
270281
271282#endregion
272283
273- #region New region
284+ #region New region
274285
275286 public bool TryWriteOrGetFile ( string fn = null )
276287 {
@@ -314,13 +325,14 @@ public virtual string WriteImageToFile([CBN] string fn = null)
314325 return path ;
315326 }
316327
317- #endregion
328+ #endregion
318329
319330
320331 public virtual void Dispose ( )
321332 {
322333 GC . SuppressFinalize ( this ) ;
323334 Image ? . Dispose ( ) ;
335+ s_logger . LogTrace ( "Disposing {Uv} {Ut}" , Value , Type ) ;
324336 }
325337
326338 public override string ToString ( )
0 commit comments