GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71933
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cairodriver/read_bmp.c
Go to the documentation of this file.
1 /*!
2  \file lib/cairodriver/read_bmp.c
3 
4  \brief GRASS cairo display driver - read 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 int get_2(const unsigned char **q)
24 {
25  const unsigned char *p = *q;
26  unsigned int n = (p[0] << 0) | (p[1] << 8);
27 
28  *q += 2;
29  return n;
30 }
31 
32 static unsigned int get_4(const unsigned char **q)
33 {
34  const unsigned char *p = *q;
35  unsigned int n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
36 
37  *q += 4;
38  return n;
39 }
40 
41 static int read_bmp_header(const unsigned char *p)
42 {
43  if (*p++ != 'B')
44  return 0;
45  if (*p++ != 'M')
46  return 0;
47 
48  if (get_4(&p) != HEADER_SIZE + ca.width * ca.height * 4)
49  return 0;
50 
51  get_4(&p);
52 
53  if (get_4(&p) != HEADER_SIZE)
54  return 0;
55 
56  if (get_4(&p) != 40)
57  return 0;
58 
59  if (get_4(&p) != ca.width)
60  return 0;
61  if (get_4(&p) != -ca.height)
62  return 0;
63 
64  get_2(&p);
65  if (get_2(&p) != 32)
66  return 0;
67 
68  if (get_4(&p) != 0)
69  return 0;
70  if (get_4(&p) != ca.width * ca.height * 4)
71  return 0;
72 
73  get_4(&p);
74  get_4(&p);
75  get_4(&p);
76  get_4(&p);
77 
78  return 1;
79 }
80 
81 void cairo_read_bmp(void)
82 {
83  char header[HEADER_SIZE];
84  FILE *input;
85 
86  input = fopen(ca.file_name, "rb");
87  if (!input)
88  G_fatal_error(_("Cairo: unable to open input file <%s>"),
89  ca.file_name);
90 
91  if (fread(header, sizeof(header), 1, input) != 1)
92  G_fatal_error(_("Cairo: invalid input file <%s>"),
93  ca.file_name);
94 
95  if (!read_bmp_header(header))
96  G_fatal_error(_("Cairo: Invalid BMP header for <%s>"),
97  ca.file_name);
98 
99  fread(ca.grid, ca.stride, ca.height, input);
100 
101  fclose(input);
102 }
#define HEADER_SIZE
Definition: cairodriver.h:45
struct cairo_state ca
char * file_name
Definition: cairodriver.h:65
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
fclose(fd)
GRASS cairo display driver - header file.
unsigned char * grid
Definition: cairodriver.h:68
void cairo_read_bmp(void)
#define _(str)
Definition: glocale.h:13