Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

Is it possible to get RGB and not BGR pixels from a Bitmap?

$
0
0

I'm using Bitmap LockBits to get the pixels from an image as an unsigned char*. However, Bitmaps are encoded in BGR, and I would need the images in RGB for my application. Is there a way to make the transformation efficiently while using the LockBits method? 

I'm using something very similar to the code in the Bitmap.LockBits example:

__declspec(dllexport) unsigned char * avigilon_getData()
	{
		Drawing::Bitmap^ bmp = getBitmapFrame();
		Drawing::Rectangle rect = Drawing::Rectangle(0,0,bmp->Width, bmp->Height);
		Drawing::Imaging::BitmapData^ bmpData = 
			bmp->LockBits(	rect, 
							Drawing::Imaging::ImageLockMode::ReadWrite, 
							bmp->PixelFormat );

		IntPtr ptr = bmpData->Scan0;
		int bytes = Math::Abs(bmpData->Stride) * bmp->Height;

		array<Byte> ^ rgbValues = gcnew array<Byte>( bytes );
		Marshal::Copy( ptr, rgbValues, 0, bytes );
		bmp->UnlockBits( bmpData );

		myGCHandle = GCHandle::Alloc(rgbValues, GCHandleType::Pinned);
		unsigned char * data = (unsigned char*) (void*) Wrapper::Instance->handle.AddrOfPinnedObject();
		return data;
	}

I need to retrieve a unsigned char* with all the data for a native C++ application. 

I'm wondering whether I can do the Marshal::Copy operation for each colour stream, provided that it doesn't affect efficiency. This question is very similar, but the suggested solutions affect performance too much.

Other options are welcome if they are more efficient.




Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>