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