GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
cairodriver/write_bmp.c
Go to the documentation of this file.
1 /*!
2  \file lib/cairodriver/write_bmp.c
3 
4  \brief GRASS cairo display driver - write bitmap (lower level functions)
5 
6  (C) 2007-2008 by Lars Ahlzen and the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Lars Ahlzen <lars ahlzen.com> (original contibutor)
12  \author Glynn Clements
13 */
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include <grass/gis.h>
20 #include <grass/glocale.h>
21 #include "cairodriver.h"
22 
23 static unsigned char *put_2(unsigned char *p, unsigned int n)
24 {
25  *p++ = n & 0xFF;
26  n >>= 8;
27  *p++ = n & 0xFF;
28  return p;
29 }
30 
31 static unsigned char *put_4(unsigned char *p, unsigned int n)
32 {
33  *p++ = n & 0xFF;
34  n >>= 8;
35  *p++ = n & 0xFF;
36  n >>= 8;
37  *p++ = n & 0xFF;
38  n >>= 8;
39  *p++ = n & 0xFF;
40  return p;
41 }
42 
43 static void make_bmp_header(unsigned char *p)
44 {
45  *p++ = 'B';
46  *p++ = 'M';
47 
48  p = put_4(p, HEADER_SIZE + ca.width * ca.height * 4);
49  p = put_4(p, 0);
50  p = put_4(p, HEADER_SIZE);
51 
52  p = put_4(p, 40);
53  p = put_4(p, ca.width);
54  p = put_4(p, -ca.height);
55  p = put_2(p, 1);
56  p = put_2(p, 32);
57  p = put_4(p, 0);
58  p = put_4(p, ca.width * ca.height * 4);
59  p = put_4(p, 0);
60  p = put_4(p, 0);
61  p = put_4(p, 0);
62  p = put_4(p, 0);
63 }
64 
65 void cairo_write_bmp(void)
66 {
67  unsigned char header[HEADER_SIZE];
68  FILE *output;
69 
70  output = fopen(ca.file_name, "wb");
71  if (!output)
72  G_fatal_error(_("Cairo: unable to open output file <%s>"),
73  ca.file_name);
74 
75  memset(header, 0, sizeof(header));
76  make_bmp_header(header);
77  fwrite(header, sizeof(header), 1, output);
78 
79  fwrite(ca.grid, ca.stride, ca.height, output);
80 
81  fclose(output);
82 }
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define HEADER_SIZE
Definition: cairodriver.h:45
struct cairo_state ca
char * file_name
Definition: cairodriver.h:65
GRASS cairo display driver - header file.
void output(const char *fmt,...)
unsigned char * grid
Definition: cairodriver.h:68
void cairo_write_bmp(void)
#define _(str)
Definition: glocale.h:10