473,320 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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

Nov 21 '06 #1
3 2215
I don't know the answer using pointers, but you may find the following
codeproject article to be a useful alternative:
http://www.codeproject.com/useritems...simageproc.asp

I haven't tested it, but it claims to be at least 10% faster than using
pointers with LockBits().

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
<pa***********@libero.itwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>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

Nov 21 '06 #2
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];
}

On Tue, 21 Nov 2006 12:35:27 +0100, <pa***********@libero.itwrote:
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


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 21 '06 #3

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]
Nov 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting...
7
by: Robert Bachmann | last post by:
Two years I wrote a simple cesar encryption program, it worked but it relied on ASCII. So today I tried to make an portable cesar encryption. Please tell me if the code below is really protable. ...
16
by: Dawn Minnis | last post by:
Hi I know you're not supposed to post OS specific questions on the newsgroup so does anyone know a good newsgroup - where there are actually people talking to eachother and not full of spam -...
7
by: Dawn Minnis | last post by:
hey guys i have char *argv to receive command line arguements. I can get characters out no problem I can get integers parsed out no problem But how do I get double values. eg if my program...
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
2
by: Dirk Reske | last post by:
Hello, I have to calculate a key out of a "Lock". But my method always returns a wrong key. my method http://home.t-online.de/home/detlef.reske/decode.cs here is described, how the key...
4
by: Andreas Pauley | last post by:
Hi all, I'm trying to implement a Python equivalent of a C# method that encrypts a string. My Python attempt is in the attached file, but does not return the same value as the C# method (see...
6
by: tommaso.gastaldi | last post by:
In a previous post I have been asking about a way to test Alpha Transparency. Bob and Michael have kindly provided some ideas. Here I would like to share the function I have prepared, for the...
2
by: Hunter | last post by:
Hi all, This is my first Usenet post! I've run into a wall with my first Python program. I'm writing some simple code to take a text file that's utf-8 and in Spanish and to use online...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.