Skip to content

Commit 06de997

Browse files
author
Wayfarer
committed
Work ony DirectBitmap again!
1 parent 8900d54 commit 06de997

11 files changed

Lines changed: 55 additions & 38 deletions

File tree

ImageCompare/AnalysisProcessing.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ internal static Bitmap DifferenceImage(Bitmap first, Bitmap second, Color highli
258258
}
259259
});
260260

261-
return new Bitmap(dbmCanvas.UnsafeBitmap);
261+
return dbmCanvas.ToBitmap();
262262
}
263263
}
264264
}

Imaging/Cif.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public Color GetColor(int id)
333333
dbm.SetPixel(coordinate.X, coordinate.Y, key);
334334
}
335335

336-
return new Bitmap(dbm.UnsafeBitmap);
336+
return dbm.ToBitmap();
337337
}
338338

339339
/// <summary>

Imaging/CifProcessing.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ internal static Dictionary<Color, SortedSet<int>> ConvertToCifFromBitmap(Bitmap
7373
dbm.SetArea(ids, color);
7474
}
7575

76-
return new Bitmap(dbm.UnsafeBitmap);
76+
return dbm.ToBitmap();
7777
}
7878

7979
/// <summary>

Imaging/DirectBitmap.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ public DirectBitmap(string filePath)
143143
/// </value>
144144
public Bitmap UnsafeBitmap { get; private set; }
145145

146+
/// <summary>
147+
/// Creates a standalone, managed copy of the current state of this bitmap.
148+
/// This copy survives even after this DirectBitmap is disposed.
149+
/// </summary>
150+
public Bitmap ToBitmap()
151+
{
152+
if (Disposed) throw new ObjectDisposedException(nameof(DirectBitmap));
153+
154+
// We clone the UnsafeBitmap (which is pinned) into a new,
155+
// unpinned managed Bitmap.
156+
return (Bitmap)UnsafeBitmap.Clone();
157+
}
158+
146159
/// <summary>
147160
/// Gets a value indicating whether this <see cref="DirectBitmap" /> is disposed.
148161
/// </summary>

Imaging/DirectBitmapImage.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,12 @@ private void Dispose(bool disposing)
331331
if (disposing && _bitsHandle.IsAllocated)
332332
{
333333
_cachedImage = null;
334-
if (_bitsHandle.IsAllocated)
335-
_bitsHandle.Free();
334+
336335
}
337336

337+
if (_bitsHandle.IsAllocated)
338+
_bitsHandle.Free();
339+
338340
_disposed = true;
339341
}
340342

Imaging/Helpers/FiltersStream.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ private static Bitmap ApplyCrosshatch(Image image)
404404
private static Bitmap? ApplyAnisotropicKuwahara(Bitmap image, int baseWindowSize = 5)
405405
{
406406
var dbmBase = new DirectBitmap(image);
407-
var result = new DirectBitmap(image.Width, image.Height);
407+
var dbm = new DirectBitmap(image.Width, image.Height);
408408
var halfBaseWindow = baseWindowSize / 2;
409409

410410
// Prepare a list to store the pixels to set in bulk using SIMD
@@ -427,10 +427,10 @@ private static Bitmap ApplyCrosshatch(Image image)
427427
// Use SIMD to set all the pixels in bulk
428428
try
429429
{
430-
result.SetPixels(pixelsToSet);
430+
dbm.SetPixels(pixelsToSet);
431431

432432
// CRITICAL: We must clone because 'result' is about to die
433-
return (Bitmap)result.UnsafeBitmap.Clone();
433+
return dbm.ToBitmap();
434434
}
435435
catch (Exception ex)
436436
{
@@ -440,7 +440,7 @@ private static Bitmap ApplyCrosshatch(Image image)
440440
}
441441
finally
442442
{
443-
result.Dispose(); // Now we can safely dispose the wrapper
443+
dbm.Dispose(); // Now we can safely dispose the wrapper
444444
}
445445
}
446446

@@ -483,7 +483,7 @@ private static Bitmap ApplyFloydSteinbergDithering(Bitmap image)
483483
}
484484

485485
// Dispose the base bitmap and return the dithered result
486-
return new Bitmap(dbm.UnsafeBitmap);
486+
return dbm.ToBitmap();
487487
}
488488

489489
/// <summary>
@@ -567,7 +567,7 @@ private static Bitmap ApplyPostProcessingAntialiasing(Bitmap image, double sigma
567567
var gaussianKernel = ImageHelper.GenerateGaussianKernel(sigma, 5);
568568

569569
// Apply the Gaussian blur filter
570-
return ApplyFilter(dbmBase.UnsafeBitmap, gaussianKernel);
570+
return ApplyFilter(dbmBase.ToBitmap(), gaussianKernel);
571571
}
572572

573573
/// <summary>
@@ -932,7 +932,7 @@ private static void DistributeError(DirectBitmap dbmBase, int x, int y, int erro
932932
{
933933
dbm.SetPixels(pixelsToSet);
934934

935-
return new Bitmap(dbm.UnsafeBitmap);
935+
return dbm.ToBitmap();
936936
}
937937
catch (Exception ex) when (ex is InvalidOperationException or NullReferenceException or ArgumentException)
938938
{

Imaging/Helpers/ImageStream.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -544,16 +544,16 @@ internal static Bitmap ConvertWhiteToTransparent(Bitmap image, int threshold)
544544
ImageHelper.ValidateImage(nameof(ConvertWhiteToTransparent), image);
545545

546546
//use our new Format
547-
var result = DirectBitmap.GetInstance(image);
547+
var dbm = DirectBitmap.GetInstance(image);
548548

549549
//255,255,255 is White
550550
var replacementColor = Color.FromArgb(255, 255, 255);
551551
var pixelsToSet = new List<(int x, int y, Color color)>();
552552

553-
for (var x = 0; x < result.Width; x++)
554-
for (var y = 0; y < result.Height; y++)
553+
for (var x = 0; x < dbm.Width; x++)
554+
for (var y = 0; y < dbm.Height; y++)
555555
{
556-
var color = result.GetPixel(x, y);
556+
var color = dbm.GetPixel(x, y);
557557

558558
//not in the area? continue, 255 is White
559559
if (255 - color.R >= threshold || 255 - color.G >= threshold || 255 - color.B >= threshold)
@@ -567,14 +567,14 @@ internal static Bitmap ConvertWhiteToTransparent(Bitmap image, int threshold)
567567

568568
try
569569
{
570-
result.SetPixels(pixelsToSet);
570+
dbm.SetPixels(pixelsToSet);
571571

572572
//get the Bitmap
573-
var btm = new Bitmap(result.UnsafeBitmap);
573+
var btm = dbm.ToBitmap();
574574
//make Transparent
575575
btm.MakeTransparent(replacementColor);
576576
//cleanup
577-
result.Dispose();
577+
dbm.Dispose();
578578
return btm;
579579
}
580580
catch (Exception ex)
@@ -672,7 +672,7 @@ internal static Bitmap SetPixel(Bitmap image, Point point, Color color)
672672
var dbm = DirectBitmap.GetInstance(image);
673673
dbm.SetPixel(point.X, point.Y, color);
674674

675-
return new Bitmap(dbm.UnsafeBitmap);
675+
return dbm.ToBitmap();
676676
}
677677

678678
/// <summary>
@@ -688,7 +688,7 @@ internal static Bitmap AdjustBrightness(Bitmap image, float brightnessFactor)
688688
ImageHelper.ValidateImage(nameof(GetPixel), image);
689689

690690
var source = new DirectBitmap(image);
691-
var result = new DirectBitmap(source.Width, source.Height);
691+
var dbm = new DirectBitmap(source.Width, source.Height);
692692

693693
for (var y = 0; y < source.Height; y++)
694694
for (var x = 0; x < source.Width; x++)
@@ -700,10 +700,10 @@ internal static Bitmap AdjustBrightness(Bitmap image, float brightnessFactor)
700700
var newGreen = ImageHelper.Clamp(pixelColor.G * brightnessFactor);
701701
var newBlue = ImageHelper.Clamp(pixelColor.B * brightnessFactor);
702702

703-
result.SetPixel(x, y, Color.FromArgb(newRed, newGreen, newBlue));
703+
dbm.SetPixel(x, y, Color.FromArgb(newRed, newGreen, newBlue));
704704
}
705705

706-
return new Bitmap(result.UnsafeBitmap);
706+
return dbm.ToBitmap();
707707
}
708708

709709
/// <summary>
@@ -748,7 +748,7 @@ internal static Bitmap FillAreaWithColor(
748748
int? height,
749749
Color color,
750750
MaskShape shape,
751-
object shapeParams = null,
751+
object? shapeParams = null,
752752
Point? startPoint = null)
753753
{
754754
// Validate input
@@ -871,7 +871,7 @@ internal static Bitmap FloodFillScanLineStack(Bitmap image, int x, int y, Color
871871
pixelData.Clear();
872872

873873
// Return the modified image as a Bitmap
874-
return new Bitmap(result.UnsafeBitmap);
874+
return result.ToBitmap();
875875
}
876876

877877
/// <summary>

Imaging/Helpers/ImageStreamHsv.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
using System;
1010
using System.Drawing;
11+
using static OpenTK.Graphics.OpenGL.GL;
1112

1213
namespace Imaging.Helpers
1314
{
@@ -88,7 +89,7 @@ private static Bitmap ProcessImage(Bitmap image, Action<ColorHsv> pixelOp)
8889
ImageHelper.ValidateImage(nameof(ProcessImage), image);
8990

9091
var source = new DirectBitmap(image);
91-
var result = new DirectBitmap(source.Width, source.Height);
92+
var dbm = new DirectBitmap(source.Width, source.Height);
9293

9394
for (var y = 0; y < source.Height; y++)
9495
{
@@ -103,11 +104,11 @@ private static Bitmap ProcessImage(Bitmap image, Action<ColorHsv> pixelOp)
103104
pixelOp(hsv);
104105

105106
// Convert HSV -> RGB (your class handles it)
106-
result.SetPixel(x, y, Color.FromArgb(hsv.A, hsv.R, hsv.G, hsv.B));
107+
dbm.SetPixel(x, y, Color.FromArgb(hsv.A, hsv.R, hsv.G, hsv.B));
107108
}
108109
}
109110

110-
return new Bitmap(result.UnsafeBitmap);
111+
return dbm.ToBitmap();
111112
}
112113
}
113114
}

Imaging/Helpers/TextureStream.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Drawing;
1212
using System.Drawing.Drawing2D;
1313
using System.Runtime.CompilerServices;
14+
using static OpenTK.Graphics.OpenGL.GL;
1415

1516
// ReSharper disable UnusedMember.Local
1617

@@ -80,7 +81,7 @@ internal static Bitmap GenerateNoiseBitmap(
8081
pixelData.Clear();
8182

8283
// Return the final Bitmap, disposing of the DirectBitmap to free resources
83-
return new Bitmap(noiseBitmap.UnsafeBitmap);
84+
return noiseBitmap.ToBitmap();
8485
}
8586

8687
/// <summary>
@@ -133,7 +134,7 @@ internal static Bitmap GenerateCloudsBitmap(
133134
pixelData.Clear();
134135

135136
// Return the final Bitmap, disposing of the DirectBitmap to free resources
136-
return new Bitmap(cloudsBitmap.UnsafeBitmap);
137+
return cloudsBitmap.ToBitmap();
137138
}
138139

139140
/// <summary>
@@ -188,7 +189,7 @@ internal static Bitmap GenerateMarbleBitmap(
188189
pixelData.Clear();
189190

190191
// Return the final Bitmap, disposing of the DirectBitmap to free resources
191-
return new Bitmap(marbleBitmap.UnsafeBitmap);
192+
return marbleBitmap.ToBitmap();
192193
}
193194

194195
/// <summary>
@@ -244,7 +245,7 @@ internal static Bitmap GenerateWoodBitmap(
244245
pixelData.Clear();
245246

246247
// Return the final Bitmap, disposing of the DirectBitmap to free resources
247-
return new Bitmap(woodBitmap.UnsafeBitmap);
248+
return woodBitmap.ToBitmap();
248249
}
249250

250251
/// <summary>
@@ -298,7 +299,7 @@ internal static Bitmap GenerateWaveBitmap(
298299
waveBitmap.SetPixels(pixelArray);
299300

300301
// Return the final Bitmap, disposing of the DirectBitmap to free resources
301-
return new Bitmap(waveBitmap.UnsafeBitmap);
302+
return waveBitmap.ToBitmap();
302303
}
303304

304305
/// <summary>

Imaging/ImageOverlays.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public static Bitmap AmplitudeImages(Image imgOne, Image imgTwo)
174174
var width = imgOne.Width;
175175
var height = imgOne.Height;
176176

177-
var result = new DirectBitmap(width, height);
177+
var dbm = new DirectBitmap(width, height);
178178
var pixelsToSet = new List<(int x, int y, Color color)>();
179179

180180
using (var dbmOne = new DirectBitmap(imgOne))
@@ -195,8 +195,8 @@ public static Bitmap AmplitudeImages(Image imgOne, Image imgTwo)
195195

196196
try
197197
{
198-
result.SetPixels(pixelsToSet);
199-
return new Bitmap(result.UnsafeBitmap);
198+
dbm.SetPixels(pixelsToSet);
199+
return dbm.ToBitmap();
200200
}
201201
catch (Exception ex)
202202
{

0 commit comments

Comments
 (0)