GRASS 8 Programmer's Manual 8.6.0dev(2026)-8843f13794
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 2007-2008 Lars Ahlzen
7 SPDX-FileCopyrightText: GRASS Development Team
8 SPDX-License-Identifier: GPL-2.0-or-later
9
10 \author Lars Ahlzen <lars ahlzen.com> (original contributor)
11 \author Glynn Clements
12 */
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <errno.h>
18
19#include <grass/gis.h>
20#include <grass/glocale.h>
21#include "cairodriver.h"
22
23static 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
32static 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
41static 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) != (unsigned int)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) != (unsigned int)ca.width)
60 return 0;
61 if (get_4(&p) != (unsigned int)-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) != (unsigned int)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
82{
83 unsigned 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>"), ca.file_name);
89
90 if (fread(header, sizeof(header), 1, input) != 1)
91 G_fatal_error(_("Cairo: invalid input file <%s>"), ca.file_name);
92
93 if (!read_bmp_header(header))
94 G_fatal_error(_("Cairo: Invalid BMP header for <%s>"), ca.file_name);
95
96 if (fread(ca.grid, ca.stride, ca.height, input) !=
97 (unsigned int)ca.height) {
98 if (feof(input))
99 G_fatal_error(_("Cairo: error reading BMP file <%s>: "
100 "unexpected end of file"),
101 ca.file_name);
102 else if (ferror(input))
103 G_fatal_error(_("Cairo: error reading BMP file <%s>: %s"),
104 ca.file_name, strerror(errno));
105 }
106
107 fclose(input);
108}
void cairo_read_bmp(void)
GRASS cairo display driver - header file.
#define HEADER_SIZE
Definition cairodriver.h:45
struct cairo_state ca
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define _(str)
Definition glocale.h:10