GRASS Programmer's Manual  6.4.2(2012)
cairodriver/write_bmp.c
Go to the documentation of this file.
00001 
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 
00006 #include <grass/gis.h>
00007 #include "cairodriver.h"
00008 
00009 static unsigned char *put_2(unsigned char *p, unsigned int n)
00010 {
00011     *p++ = n & 0xFF;
00012     n >>= 8;
00013     *p++ = n & 0xFF;
00014     return p;
00015 }
00016 
00017 static unsigned char *put_4(unsigned char *p, unsigned int n)
00018 {
00019     *p++ = n & 0xFF;
00020     n >>= 8;
00021     *p++ = n & 0xFF;
00022     n >>= 8;
00023     *p++ = n & 0xFF;
00024     n >>= 8;
00025     *p++ = n & 0xFF;
00026     return p;
00027 }
00028 
00029 static void make_bmp_header(unsigned char *p)
00030 {
00031     *p++ = 'B';
00032     *p++ = 'M';
00033 
00034     p = put_4(p, HEADER_SIZE + width * height * 4);
00035     p = put_4(p, 0);
00036     p = put_4(p, HEADER_SIZE);
00037 
00038     p = put_4(p, 40);
00039     p = put_4(p, width);
00040     p = put_4(p, -height);
00041     p = put_2(p, 1);
00042     p = put_2(p, 32);
00043     p = put_4(p, 0);
00044     p = put_4(p, width * height * 4);
00045     p = put_4(p, 0);
00046     p = put_4(p, 0);
00047     p = put_4(p, 0);
00048     p = put_4(p, 0);
00049 }
00050 
00051 void write_bmp(void)
00052 {
00053     char header[HEADER_SIZE];
00054     FILE *output;
00055 
00056     output = fopen(file_name, "wb");
00057     if (!output)
00058         G_fatal_error("cairo: couldn't open output file %s", file_name);
00059 
00060     memset(header, 0, sizeof(header));
00061     make_bmp_header(header);
00062     fwrite(header, sizeof(header), 1, output);
00063 
00064     fwrite(grid, stride, height, output);
00065 
00066     fclose(output);
00067 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines