BITMAPINFOHEADER + buffer to Magick::Image

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
anotherprogrammer123
Posts: 36
Joined: 2010-02-21T18:02:40-07:00
Authentication code: 8675308

BITMAPINFOHEADER + buffer to Magick::Image

Post 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.
Last edited by anotherprogrammer123 on 2011-01-10T11:22:03-07:00, edited 2 times in total.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: BITMAPINFOHEADER + buffer to Magick::Image

Post 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 )).
anotherprogrammer123
Posts: 36
Joined: 2010-02-21T18:02:40-07:00
Authentication code: 8675308

Re: BITMAPINFOHEADER + buffer to Magick::Image

Post by anotherprogrammer123 »

[please delete this post]
Last edited by anotherprogrammer123 on 2011-01-10T11:21:08-07:00, edited 2 times in total.
anotherprogrammer123
Posts: 36
Joined: 2010-02-21T18:02:40-07:00
Authentication code: 8675308

Re: BITMAPINFOHEADER + buffer to Magick::Image

Post by anotherprogrammer123 »

[please delete this post]
Last edited by anotherprogrammer123 on 2011-01-10T11:21:15-07:00, edited 2 times in total.
anotherprogrammer123
Posts: 36
Joined: 2010-02-21T18:02:40-07:00
Authentication code: 8675308

Re: BITMAPINFOHEADER + buffer to Magick::Image

Post 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 ;-)
Last edited by anotherprogrammer123 on 2011-01-10T11:19:15-07:00, edited 1 time in total.
amilkh
Posts: 1
Joined: 2011-01-09T22:27:33-07:00
Authentication code: 8675308

Re: BITMAPINFOHEADER + buffer to Magick::Image

Post 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
Post Reply