Skip to content

Commit c2cb91c

Browse files
committed
make improvements of the wrapper of the gdi+ bitmap object data
1 parent eb91dee commit c2cb91c

5 files changed

Lines changed: 185 additions & 159 deletions

File tree

Microsoft.VisualBasic.Core/src/Drawing/Bitmap/BitmapBuffer.vb

Lines changed: 74 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,79 @@
11
#Region "Microsoft.VisualBasic::bd738ba1b6fa13988cc7df87040ecff2, Microsoft.VisualBasic.Core\src\Drawing\Bitmap\BitmapBuffer.vb"
22

3-
' Author:
4-
'
5-
' asuka (amethyst.asuka@gcmodeller.org)
6-
' xie (genetics@smrucc.org)
7-
' xieguigang (xie.guigang@live.com)
8-
'
9-
' Copyright (c) 2018 GPL3 Licensed
10-
'
11-
'
12-
' GNU GENERAL PUBLIC LICENSE (GPL3)
13-
'
14-
'
15-
' This program is free software: you can redistribute it and/or modify
16-
' it under the terms of the GNU General Public License as published by
17-
' the Free Software Foundation, either version 3 of the License, or
18-
' (at your option) any later version.
19-
'
20-
' This program is distributed in the hope that it will be useful,
21-
' but WITHOUT ANY WARRANTY; without even the implied warranty of
22-
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23-
' GNU General Public License for more details.
24-
'
25-
' You should have received a copy of the GNU General Public License
26-
' along with this program. If not, see <http://www.gnu.org/licenses/>.
27-
28-
29-
30-
' /********************************************************************************/
31-
32-
' Summaries:
33-
34-
35-
' Code Statistics:
36-
37-
' Total Lines: 912
38-
' Code Lines: 535 (58.66%)
39-
' Comment Lines: 231 (25.33%)
40-
' - Xml Docs: 77.49%
41-
'
42-
' Blank Lines: 146 (16.01%)
43-
' File Size: 31.19 KB
44-
45-
46-
' Class BitmapBuffer
47-
'
48-
' Properties: Height, Size, SortBins, Stride, UInt32Black1
49-
' UInt32Black2, UInt32White, Width
50-
'
51-
' Constructor: (+8 Overloads) Sub New
52-
'
53-
' Function: A, B, (+2 Overloads) FromBitmap, FromImage, G
54-
' GetAlpha, GetARGB, GetARGBStream, GetBlue, GetColor
55-
' GetEnumerator, GetGreen, GetHandleObject, GetImage, (+2 Overloads) GetIndex
56-
' (+3 Overloads) GetPixel, GetPixelChannels, GetPixelsAll, GetRed, OutOfRange
57-
' R, ToPixel2D, ToString, (+2 Overloads) Unpack, White
58-
'
59-
' Sub: Dispose, (+2 Overloads) Save, SetAlpha, SetBlue, SetGreen
60-
' (+4 Overloads) SetPixel, SetRed, WriteARGBStream
61-
'
62-
' Operators: +
63-
'
64-
'
65-
' /********************************************************************************/
3+
' Author:
4+
'
5+
' asuka (amethyst.asuka@gcmodeller.org)
6+
' xie (genetics@smrucc.org)
7+
' xieguigang (xie.guigang@live.com)
8+
'
9+
' Copyright (c) 2018 GPL3 Licensed
10+
'
11+
'
12+
' GNU GENERAL PUBLIC LICENSE (GPL3)
13+
'
14+
'
15+
' This program is free software: you can redistribute it and/or modify
16+
' it under the terms of the GNU General Public License as published by
17+
' the Free Software Foundation, either version 3 of the License, or
18+
' (at your option) any later version.
19+
'
20+
' This program is distributed in the hope that it will be useful,
21+
' but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23+
' GNU General Public License for more details.
24+
'
25+
' You should have received a copy of the GNU General Public License
26+
' along with this program. If not, see <http://www.gnu.org/licenses/>.
27+
28+
29+
30+
' /********************************************************************************/
31+
32+
' Summaries:
33+
34+
35+
' Code Statistics:
36+
37+
' Total Lines: 912
38+
' Code Lines: 535 (58.66%)
39+
' Comment Lines: 231 (25.33%)
40+
' - Xml Docs: 77.49%
41+
'
42+
' Blank Lines: 146 (16.01%)
43+
' File Size: 31.19 KB
44+
45+
46+
' Class BitmapBuffer
47+
'
48+
' Properties: Height, Size, SortBins, Stride, UInt32Black1
49+
' UInt32Black2, UInt32White, Width
50+
'
51+
' Constructor: (+8 Overloads) Sub New
52+
'
53+
' Function: A, B, (+2 Overloads) FromBitmap, FromImage, G
54+
' GetAlpha, GetARGB, GetARGBStream, GetBlue, GetColor
55+
' GetEnumerator, GetGreen, GetHandleObject, GetImage, (+2 Overloads) GetIndex
56+
' (+3 Overloads) GetPixel, GetPixelChannels, GetPixelsAll, GetRed, OutOfRange
57+
' R, ToPixel2D, ToString, (+2 Overloads) Unpack, White
58+
'
59+
' Sub: Dispose, (+2 Overloads) Save, SetAlpha, SetBlue, SetGreen
60+
' (+4 Overloads) SetPixel, SetRed, WriteARGBStream
61+
'
62+
' Operators: +
63+
'
64+
'
65+
' /********************************************************************************/
6666

6767
#End Region
6868

6969
Imports System.Drawing
70-
Imports System.Drawing.Imaging
7170
Imports System.IO
7271
Imports System.Runtime.CompilerServices
7372
Imports Microsoft.VisualBasic.Language
7473
Imports Microsoft.VisualBasic.Language.Java
7574
Imports Microsoft.VisualBasic.Linq
7675
Imports BitsPerPixelEnum = Microsoft.VisualBasic.Imaging.BitmapImage.FileStream.BitsPerPixelEnum
7776
Imports MemoryBmp = Microsoft.VisualBasic.Imaging.BitmapImage.FileStream.Bitmap
78-
Imports std = System.Math
7977

8078
Namespace Imaging.BitmapImage
8179

@@ -89,10 +87,6 @@ Namespace Imaging.BitmapImage
8987
Implements IDisposable
9088
Implements Enumeration(Of Color)
9189

92-
#If NET48 Then
93-
Protected raw As Bitmap
94-
#End If
95-
9690
''' <summary>
9791
''' BitmapData
9892
''' </summary>
@@ -120,36 +114,18 @@ Namespace Imaging.BitmapImage
120114
End Get
121115
End Property
122116

123-
#If NET48 Then
124-
125117
''' <summary>
126-
''' constructor for gdi+ image data object
118+
'''
127119
''' </summary>
128120
''' <param name="ptr"></param>
129121
''' <param name="byts"></param>
130-
''' <param name="raw"></param>
131-
''' <param name="handle"></param>
122+
''' <param name="size"></param>
123+
''' <param name="stride"></param>
132124
''' <param name="channel"></param>
133-
Public Sub New(ptr As IntPtr,
134-
byts%,
135-
raw As Bitmap,
136-
handle As BitmapData,
137-
channel As Integer)
138-
139-
Call MyBase.New(ptr, byts)
140-
141-
Me.raw = raw
142-
Me.handle = handle
143-
144-
Me.Stride = handle.Stride
145-
Me.Width = raw.Width
146-
Me.Height = raw.Height
147-
Me.Size = New Size(Width, Height)
148-
Me.channels = channel
149-
Me.memoryBuffer = False
150-
End Sub
151-
#End If
152-
125+
''' <param name="handle"></param>
126+
''' <remarks>
127+
''' memory buffer is false
128+
''' </remarks>
153129
Sub New(ptr As IntPtr, byts%, size As Size, stride As Integer, channel As Integer, Optional handle As Object = Nothing)
154130
Call MyBase.New(ptr, byts)
155131

@@ -189,6 +165,7 @@ Namespace Imaging.BitmapImage
189165
_Height = size.Height
190166
End Sub
191167

168+
<MethodImpl(MethodImplOptions.AggressiveInlining)>
192169
Sub New(width As Integer, height As Integer, Optional channels As Integer = TYPE_INT_ARGB)
193170
Call Me.New(New Byte(width * height * channels - 1) {}, New Size(width, height), channels)
194171
End Sub
@@ -197,6 +174,8 @@ Namespace Imaging.BitmapImage
197174
''' make in-memory data copy
198175
''' </summary>
199176
''' <param name="source"></param>
177+
'''
178+
<MethodImpl(MethodImplOptions.AggressiveInlining)>
200179
Sub New(source As BitmapBuffer)
201180
Call Me.New(source.buffer.ToArray, source.Size, source.channels)
202181
End Sub
@@ -290,16 +269,12 @@ Namespace Imaging.BitmapImage
290269
''' <returns></returns>
291270
'''
292271
<MethodImpl(MethodImplOptions.AggressiveInlining)>
293-
Public Function GetImage(Optional flush As Boolean = False) As Bitmap
272+
Public Overridable Function GetImage(Optional flush As Boolean = False) As Bitmap
294273
If flush Then
295274
Call Write()
296275
End If
297276

298-
#If NET48 Then
299-
Return DirectCast(raw.Clone, Bitmap)
300-
#Else
301277
Return New Bitmap(Me)
302-
#End If
303278
End Function
304279

305280
' pixel: (1,1)(2,1)(3,1)(4,1)(1,2)(2,2)(3,2)(4,2)
@@ -880,18 +855,7 @@ Namespace Imaging.BitmapImage
880855
'''
881856
<MethodImpl(MethodImplOptions.AggressiveInlining)>
882857
Public Shared Function FromImage(res As Image) As BitmapBuffer
883-
#If NET48 Then
884-
Dim copy As New Bitmap(res.Width, res.Height, format:=PixelFormat.Format32bppArgb)
885-
Dim g As Graphics = Graphics.FromImage(copy)
886-
887-
Call g.DrawImageUnscaled(res, New Point)
888-
Call g.Flush()
889-
Call g.Dispose()
890-
891-
Return BitmapBuffer.FromBitmap(copy)
892-
#Else
893858
Return res.GetMemoryBitmap
894-
#End If
895859
End Function
896860

897861
''' <summary>
@@ -902,60 +866,15 @@ Namespace Imaging.BitmapImage
902866
''' get the reference of the <see cref="Bitmap.MemoryBuffer"/> data directly
903867
''' </returns>
904868
Public Shared Function FromBitmap(curBitmap As Bitmap) As BitmapBuffer
905-
#If NET48 Then
906-
Return FromBitmap(curBitmap, ImageLockMode.ReadWrite)
907-
#Else
908869
Return curBitmap.MemoryBuffer
909-
#End If
910-
End Function
911-
912-
#If NET48 Then
913-
914-
''' <summary>
915-
''' 使用这个函数进行写数据的话,会修改到原图
916-
''' </summary>
917-
''' <param name="curBitmap"></param>
918-
''' <param name="mode"></param>
919-
''' <returns></returns>
920-
Public Shared Function FromBitmap(curBitmap As Bitmap, mode As ImageLockMode) As BitmapBuffer
921-
' Lock the bitmap's bits.
922-
Dim rect As New Rectangle(0, 0, curBitmap.Width, curBitmap.Height)
923-
Dim bmpData As BitmapData = curBitmap.LockBits(
924-
rect:=rect,
925-
flags:=mode,
926-
format:=curBitmap.PixelFormat
927-
)
928-
929-
' Get the address of the first line.
930-
Dim ptr As IntPtr = bmpData.Scan0
931-
' Declare an array to hold the bytes of the bitmap.
932-
Dim bytes As Integer = std.Abs(bmpData.Stride) * curBitmap.Height
933-
Dim pixels As Integer = curBitmap.Width * curBitmap.Height
934-
Dim channels As Integer
935-
936-
If bytes = pixels * 3 Then
937-
channels = 3
938-
ElseIf bytes = pixels * 4 Then
939-
channels = 4
940-
Else
941-
Throw New NotImplementedException
942-
End If
943-
944-
Return New BitmapBuffer(ptr, bytes, curBitmap, bmpData, channels)
945870
End Function
946-
#End If
947871

948872
Protected Overrides Sub Dispose(disposing As Boolean)
949873
If Not memoryBuffer Then
950874
' write data back to the memory via the
951875
' managed memory pointer
952876
Call Write()
953877
End If
954-
#If NET48 Then
955-
If TypeOf handle Is BitmapData Then
956-
Call raw.UnlockBits(DirectCast(handle, BitmapData))
957-
End If
958-
#End If
959878
End Sub
960879

961880
Public Iterator Function GetEnumerator() As IEnumerator(Of Color) Implements Enumeration(Of Color).GenericEnumerator

gr/Drawing-net4.8/Extensions.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Imports std = System.Math
7777
Public Module Extensions
7878

7979
<Extension>
80-
Public Function CreateBuffer(bmp As System.Drawing.Bitmap) As BitmapBuffer
80+
Public Function CreateBuffer(bmp As System.Drawing.Bitmap) As BitmapImage.BitmapBuffer
8181
#If WINDOWS Then
8282
' Lock the bitmap's bits.
8383
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
@@ -102,7 +102,7 @@ Public Module Extensions
102102
Throw New NotImplementedException
103103
End If
104104

105-
Return New BitmapBuffer(ptr, bytes, bmp.Size, bmpData.Stride, channels, handle:=bmpData)
105+
Return New BitmapImage.BitmapBuffer(ptr, bytes, bmp.Size, bmpData.Stride, channels, handle:=bmpData)
106106
#Else
107107
Return BitmapBuffer.FromBitmap(bmp)
108108
#End If

0 commit comments

Comments
 (0)