GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
pngdriver/read_bmp.c
Go to the documentation of this file.
1/*!
2 \file lib/pngdriver/read_bmp.c
3
4 \brief GRASS png display driver - read bitmap (lower level functions)
5
6 SPDX-FileCopyrightText: 2007-2014 Glynn Clements
7 SPDX-FileCopyrightText: GRASS Development Team
8 SPDX-License-Identifier: GPL-2.0-or-later
9
10 \author Glynn Clements
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17#include <grass/gis.h>
18#include "pngdriver.h"
19
20static unsigned int get_2(const unsigned char **q)
21{
22 const unsigned char *p = *q;
23 unsigned int n = (p[0] << 0) | (p[1] << 8);
24
25 *q += 2;
26 return n;
27}
28
29static unsigned int get_4(const unsigned char **q)
30{
31 const unsigned char *p = *q;
32 unsigned int n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
33
34 *q += 4;
35 return n;
36}
37
38static int read_bmp_header(const unsigned char *p)
39{
40 if (*p++ != 'B')
41 return 0;
42 if (*p++ != 'M')
43 return 0;
44
45 if (get_4(&p) != (unsigned int)HEADER_SIZE + png.width * png.height * 4)
46 return 0;
47
48 get_4(&p);
49
50 if (get_4(&p) != HEADER_SIZE)
51 return 0;
52
53 if (get_4(&p) != 40)
54 return 0;
55
56 if (get_4(&p) != (unsigned int)png.width)
57 return 0;
58 if (get_4(&p) != (unsigned int)-png.height)
59 return 0;
60
61 get_2(&p);
62 if (get_2(&p) != 32)
63 return 0;
64
65 if (get_4(&p) != 0)
66 return 0;
67 if (get_4(&p) != (unsigned int)png.width * png.height * 4)
68 return 0;
69
70 get_4(&p);
71 get_4(&p);
72 get_4(&p);
73 get_4(&p);
74
75 return 1;
76}
77
78void read_bmp(void)
79{
80 unsigned char header[HEADER_SIZE];
81 FILE *input;
82 int x, y;
83 unsigned int *p;
84
85 if (!png.true_color)
86 G_fatal_error("PNG: cannot use BMP with indexed color");
87
88 input = fopen(png.file_name, "rb");
89 if (!input)
90 G_fatal_error("PNG: couldn't open input file %s", png.file_name);
91
92 if (fread(header, sizeof(header), 1, input) != 1)
93 G_fatal_error("PNG: invalid input file %s", png.file_name);
94
95 if (!read_bmp_header(header))
96 G_fatal_error("PNG: invalid BMP header for %s", png.file_name);
97
98 for (y = 0, p = png.grid; y < png.height; y++) {
99 for (x = 0; x < png.width; x++, p++) {
100 int b = fgetc(input);
101 int g = fgetc(input);
102 int r = fgetc(input);
103 int a = fgetc(input);
104 unsigned int c = png_get_color(r, g, b, a);
105
106 *p = c;
107 }
108 }
109
110 fclose(input);
111}
#define HEADER_SIZE
Definition cairodriver.h:45
unsigned int png_get_color(int r, int g, int b, int a)
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
float g
Definition named_colr.c:7
struct png_state png
void read_bmp(void)
GRASS png display driver - header file.
double b
Definition r_raster.c:37
double r
Definition r_raster.c:37
#define x