ivarec
ivarec

Reputation: 2612

How do I create the BMP headers of a bitmap in a memory stream with GraphicsMagick?

I have an array in memory that contains a bitmap image, with a known size, depth, palette and such. I want to use this array to create a GraphicsMagick representation of the image to enable me to write this file with its BMP header and make it possible to view it with other software.

So far, I have something like this (just the important lines - I could add the whole program if requested). First, the variables:

FILE *fp_out;
ExceptionInfo exception;
Image *image;
ImageInfo *image_info;
char buffer[BUFFER_SIZE] = {0};
const int w = WIDTH, h = HEIGHT;

And the relevant code:

image = ConstituteImage(w, h, "RGB", CharPixel, buffer, &exception);
image_info = CloneImageInfo((ImageInfo *) NULL);
fp_out = fopen("image.bmp", "wb");
image_info->file = fp_out;
WriteImage(image_info, image);

The image.bmp file gets created, but the BMP header is just not there and the first bytes of this bitmap contains something like this:

id=ImageMagick  version=1.0
class=DirectClass  matte=False
columns=74  rows=75  depth=8
<binary data of my image>

What am I doing wrong? What pieces of the documentation should I be focusing on? It is pretty overwhelming.

Upvotes: 1

Views: 314

Answers (1)

Bob Friesenhahn
Bob Friesenhahn

Reputation: 306

The output format used was the default MIFF format. In order to fix this, you can either do

image = ConstituteImage(w, h, "RGB", CharPixel, buffer, &exception);
image_info = CloneImageInfo((ImageInfo *) NULL);
fp_out = fopen("image.bmp", "wb");
image_info->file = fp_out;
strcpy(image_info->magick,"BMP");
WriteImage(image_info, image);

or do (more normal way)

image = ConstituteImage(w, h, "RGB", CharPixel, buffer, &exception);
image_info = CloneImageInfo((ImageInfo *) NULL);
strcpy(image->filename,"image.bmp");
WriteImage(image_info, image);

Upvotes: 1

Related Questions