Translation of 1 line of code into C# : Alpha Byte

P

pamelafluente

I am trying to convert in C# a VB.NET routine (which probably came from
C# ! )

I need to translate using pointers (this should get the Alpha byte in a
32bppPArgb bitmap):

Function AlphaValue32bppPArgb(ByVal x As Int32, ByVal y As Int32,
ByVal data As BitmapData) As Byte

Return Marshal.ReadByte(data.Scan0, y * data.Stride +
x * 4 + 3)

End Function

What would be the correct translation using C# pointers ?

-P
 
M

Morten Wennevik

Hi Pamela,

You don't need pointers for this as it can be translated easily

byte AlphaValue322bppPArgb(Int32 x, Int32 y, BitmapData data)
{
return Marshal.ReadByte(data.Scan0, y * data.Stride + x * 4 + 3);
}

If you still want pointers, change the code to this (requires unsafe
compilation flag)

unsafe byte AlphaValue322bppPArgb(Int32 x, Int32 y, BitmapData data)
{
byte* p = (byte*)data.Scan0;
p += y * data.Stride + x * 4 + 3;

return p[0];
}
 
P

pamelafluente

Morten Wennevik ha scritto:
Hi Pamela,

You don't need pointers for this as it can be translated easily

byte AlphaValue322bppPArgb(Int32 x, Int32 y, BitmapData data)
{
return Marshal.ReadByte(data.Scan0, y * data.Stride + x * 4 + 3);
}

If you still want pointers, change the code to this (requires unsafe
compilation flag)

unsafe byte AlphaValue322bppPArgb(Int32 x, Int32 y, BitmapData data)
{
byte* p = (byte*)data.Scan0;
p += y * data.Stride + x * 4 + 3;

return p[0];
}

Thanks Mick, Thank you Morten,

well actually I am thinking that probably if I provide the entire
function, pointers can be applied in a better way. Infact, I imagine
that for the first 4 corners of the bitmap it is probably easy just to
move the pointer.

Here is the function in case you are able to see a better way to do it:

Public Function ImageUsesAlphaTransparency(ByVal b As Bitmap, _
ByVal FullCheck As
Boolean) As Boolean

'Only for PixelFormat.Format32bppPArgb

With b

Dim data As BitmapData = Nothing

Try
data = b.LockBits(New Rectangle(Point.Empty, b.Size),
ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb)

'Quick check at 4 corners and center,
'as transparency often used near image borders
If AlphaValue32bppPArgb(0, 0, data) < 255 Then Return
True
If AlphaValue32bppPArgb(.Width - 1, .Height - 1, data)
< 255 Then Return True
If AlphaValue32bppPArgb(0, .Height - 1, data) < 255
Then Return True
If AlphaValue32bppPArgb(.Width - 1, 0, data) < 255 Then
Return True
If AlphaValue32bppPArgb(.Width \ 2, b.Height \ 2, data)
< 255 Then Return True

If FullCheck Then
For y As Int32 = 0 To b.Height - 1
For x As Int32 = 0 To b.Width - 1
If Marshal.ReadByte(data.Scan0, y *
data.Stride + x * 4 + 3) < 255 Then
Return True
End If
Next x
Next y
Return False
Else
Return True 'conservative choice
End If

Catch ex As Exception
Throw

Finally
If data IsNot Nothing Then b.UnlockBits(data)
End Try

End With

End Function

Function AlphaValue32bppPArgb(ByVal x As Int32, ByVal y As Int32,
ByVal data As BitmapData) As Byte
Return Marshal.ReadByte(data.Scan0, y * data.Stride + x * 4 +
3)
End Function

---------------------------------------------------

so far what I have is the inner loop while I am missing the check
at the 4 corners and center:

Here is what I have:

public static bool ImageUsesAlphaTransparency(Bitmap b, Boolean
FullScan)
{
Rectangle r = new Rectangle(Point.Empty, b.Size);
BitmapData bmData = b.LockBits(r, ImageLockMode.ReadOnly,
PixelFormat.Format32bppPArgb);

try
{

System.IntPtr Scan0 = bmData.Scan0;
int nWidth = b.Width;
int nHeight = b.Height;

unsafe
{
int* p = (int*)(void*)Scan0;
int all = nWidth * nHeight;
for (int i = 0; i < all; ++i)
{
if (((uint)p[0] & (uint)0xFF000000) <
(uint)255) { return true; }
p += 1;
}
}
return false;
}
catch
{
throw;
}
finally
{
if (bmData != null) { b.UnlockBits(bmData); };
}

}



Thanks you very much

Morten

Happy Coding!
Morten Wennevik [C# MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top