Page 1 of 1

BITMAPINFOHEADER + buffer to Magick::Image

Posted: 2010-12-30T17:37:43-07:00
by anotherprogrammer123
Hello,
(Here is a similar topic I posted back earlier (and found a solution to).)
For a DirectShow application, I want to transfer a BITMAP image info a Magick::Image. Basically, I have a BITMAPINFOHEADER and a BYTE* buffer pointing to the pixel data of the bitmap described by the BITMAPINFOHEADER.

Code: Select all

int bitmapToMagick(BITMAPINFOHEADER* bmiHeader, char* bitmap, size_t length, Magick::Image &Image) {
        //...code here...//
}
I have spent quite a bit of time trying to figure this out, but couldn't.
I found this post, but extracting the image_info members would be a really complicated function (converting from BITMAPINFOHEADER to Magick::ImageInfo). Also, for some reason ImageInfo.size is a char* array (how can I specify a 720x480 image with a char* array?). With more searching, I did find a DIBToImage() function here which does the job, but this seems too complicated.

The thing is that I can easily write the BITMAP to disk and read it back into a Magick::Image with ReadImage, but this is a very expensive operation. I looked through the Magick source code (Image.cpp, constitute.cpp), but could not find the function used to read from .bmp files (very similar to what I want to do).

There should be an easy way to do this right?
Thank you very much in advance for any help.

Also, please post back if anything is unclear.

Re: BITMAPINFOHEADER + buffer to Magick::Image

Posted: 2010-12-30T17:48:22-07:00
by magick
You can get direct access to the image pixels with Magick::Pixels (http://www.imagemagick.org/Magick++/Pixels.html) or you write directly to a char buffer with Magick::Image::write() (e.g. image.write( 0, 0, 640, 1, "RGB", 0, pixels )).

Re: BITMAPINFOHEADER + buffer to Magick::Image

Posted: 2010-12-31T12:21:39-07:00
by anotherprogrammer123
[please delete this post]

Re: BITMAPINFOHEADER + buffer to Magick::Image

Posted: 2011-01-02T09:46:39-07:00
by anotherprogrammer123
[please delete this post]

Re: BITMAPINFOHEADER + buffer to Magick::Image

Posted: 2011-01-05T21:47:34-07:00
by anotherprogrammer123
Hello again. This is the solution that I came up with:

Code: Select all

int bitmapToMagick(BITMAPINFOHEADER* bmiHeader, char* pixels, size_t length, Magick::Image* Image) {
	try {
		Image->read(bmiHeader->biWidth, abs(bmiHeader->biHeight), "BGR", Magick::CharPixel, pixels);
		Image->flip();
	}
	catch (...)  {
		return 1; ///error
        }
	return 0;
}

However, this is probably not the most efficient method (due to flip()). It also assumes the BITMAP is in BGR format, bottom-up, and has no compression.
This was very tricky. Hopefully the next Magick++ release will have more functions for dealing with BITMAPs ;-)

Re: BITMAPINFOHEADER + buffer to Magick::Image

Posted: 2011-01-09T22:30:08-07:00
by amilkh
In case anybody else runs in to this problem, I posted some guides to my website:
Converting uncompressed BITMAPs to Magick++ objects
Converting compressed BITMAPs to Magick++ objects