I’m working in a project where Cairo was chosen as the graphics library (running on Xlib) in an OpenSUSE Linux environment. I have very little experience working with graphics libraries or graphic file formats, and I was wondering if it is possible to draw a Windows bitmap image to a Cairo surface? It appears to be relatively straightforward to draw a png in Cairo, but I’ve been looking around everywhere for information on drawing bitmaps and couldn’t really find anything. I pieced together the following code:
int height = 256;
int width = 256;
cairo_format_t format = CAIRO_FORMAT_RGB24;
int stride = cairo_format_stride_for_width (format, width);
unsigned char *bitmapData;
bitmapData = (unsigned char *)(malloc (stride * height));
std::ifstream myFile ("exampleBitmapImage.bmp", std::ios::in | std::ios::binary);
myFile.read ((char *)bitmapData, stride * height);
cairo_surface_t *imageSurface = cairo_image_surface_create_for_data (bitmapData, format, width, height, stride);
cairo_set_source_surface (cs, imageSurface, 0, 0);
cairo_paint (cs);
cairo_show_page (cs);
cairo_surface_destroy (imageSurface);
myFile.close();
Strangely, when I run this it displays the image upside-down and backwards at 1/64 of its size 8 times in a row, and then fills out what would be the remainder of the image size (the remaining 7/8 of the image) with black. I suspect it has something to do with the file format, and that I’m parsing and feeding the binary data incorrectly and with improper settings to Cairo. Can anyone give guidance on how to get this working properly? I apologize for my lack of knowledge and wish to understand this problem better, and any help is greatly appreciated, thanks! 🙂
Multiply the stride with -1 that should flip your bitmap.
Look up the BMP file format http://en.wikipedia.org/wiki/BMP_file_format and
implement bitmap header parser and set the encoding correctly.
Right now are guessing the encoding as RGB24 and you have Cairo interpretting the bitmap header as image data.