473,548 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++.NET equivalent code of C#.NET GrayScale code

Hello
I translated this code (witch convert an image to grayscale) from C#.NET to
C++.NET
The C#.NET code is :

private void GrayScale(Bitma p b)
{
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width,
b.Height),Image LockMode.ReadWr ite,PixelFormat .Format24bppRgb );
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* p = (byte*)(void*)S can0;
int nOffset = stride - b.Width * 3;
byte red, green, blue;
for (int y = 0; y < b.Height; ++y)
{
for (int x = 0; x < b.Width; ++x)
{
blue = p[0];
green = p[1];
red = p[2];
p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);
p += 3;
}
p += nOffset;
}
}
b.UnlockBits(bm Data);
}
(code of Christian Graus)

And the C++.NET code is:

private: System::Void GrayScale(Bitma p ^b)
{
BitmapData ^bmData = b->LockBits(*ne w Rectangle(0, 0, b->Width,
b->Height),ImageL ockMode::ReadWr ite,PixelFormat ::Format24bppRg b);
int stride = bmData->Stride;
System::IntPtr Scan0 = bmData->Scan0;
unsigned char* p = (unsigned char*)(void*)Sc an0;
int nOffset = stride - b->Width * 3;
unsigned char red, green, blue;
for (int y = 0; y < b->Height; ++y)
{
for (int x = 0; x < b->Width; ++x)
{
blue = p[0];
green = p[1];
red = p[2];
p[0] = p[1] = p[2] = (unsigned char)(.299 * red + .587 * green + .114 *
blue);
p += 3;
}
p += nOffset;
}
b->UnlockBits(bmD ata);
}

The problem is that the C++.NETcode take about 10 seconds in execution
and the C#.NET take about only 1 second !
Please, What shall I do to correct this problem?
Nov 17 '05 #1
0 2123

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

Similar topics

3
3975
by: tjh | last post by:
hi - i'm a bit new to PHP - i've been messing around with the GD2 image functions and was wondering: i see a lot of information about converting a color image to grayscale but i can't figure out how to determine whether an image (GIF or JPG) is grayscale in the first place. is there a way to do this in PHP?
0
3833
by: Chad | last post by:
I have a one dimensional byte array that stores the pixel information of my image. The image is 8-bit grayscale so each byte in the array holds a value from 0(black) to 255(white) for a single pixel. I need a way to save this this byte array as a gif. Does anyone have any suggestions???
2
10916
by: Anirudh Saria | last post by:
Does anyone know how to read in a grayscale bitmap image in Visual C++ and store the pixel values in an array ? Thanks, Anirudh
1
5041
by: Sinora | last post by:
I am trying to cahnge pixel value of a grayscale image using Bitmap object . Each pixel is consists of 8 bits. Bitmap bigim(720,480,PixelFormat8bppIndexed) ( I tried 8 instead PixelFormat8bppIndexed) I wanted to assign the values of each pixel using bigim.SetPixel(x,y,integer value between 0 to 255)
4
2016
by: RicercatoreSbadato | last post by:
I'm using bmp.Save() and the bmp is in PixelFormat.Format8bppIndexed. But when I open the image with Gimp, it tells me that the image is RGB and not grayscale.
8
11384
by: RicercatoreSbadato | last post by:
I'm using bmp.Save() and the bmp is in PixelFormat.Format8bppIndexed. But when I open the image with Gimp, it tells me that the image is RGB and not grayscale.
2
3817
by: Harry Simpson | last post by:
I'm having trouble reducing the size of a bitmap programmatically: The file size of the original is 2400k. The picture is 816x1056. 16 million colors i think. I only need grayscale render. Public Sub ConvertBadBitmap(ByVal FilePath As String) Dim objPicture As New Bitmap(816, 1056,...
8
47858
by: platinumhimani | last post by:
-How to convert any image(8,16,24,32 or 64-bit) to 8-bit grayscale -i have tried to convert a 24-bit image to grayscale using setpixel and getpixel functions, in vb.net but i am unable to save that image in grayscale format.How do i do that.It still saves in true color format. -i used picturebox1.image.save() method -i tried to convert...
5
2368
by: Speed | last post by:
Hi, I have a monochrome camera connected to my computer through the USB port. I would like to capture the images from it using C++ and save them as 8-bit grayscale Bitmaps. I am using MS Visual Studio .NET 2005. Could you please point me in the right direction. Thanks a ton.
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.