GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pngdriver/read_bmp.c
Go to the documentation of this file.
1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include <grass/gis.h>
7 #include "pngdriver.h"
8 
9 static unsigned int get_2(const unsigned char **q)
10 {
11  const unsigned char *p = *q;
12  unsigned int n = (p[0] << 0) | (p[1] << 8);
13 
14  *q += 2;
15  return n;
16 }
17 
18 static unsigned int get_4(const unsigned char **q)
19 {
20  const unsigned char *p = *q;
21  unsigned int n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
22 
23  *q += 4;
24  return n;
25 }
26 
27 static int read_bmp_header(const unsigned char *p)
28 {
29  if (*p++ != 'B')
30  return 0;
31  if (*p++ != 'M')
32  return 0;
33 
34  if (get_4(&p) != HEADER_SIZE + width * height * 4)
35  return 0;
36 
37  get_4(&p);
38 
39  if (get_4(&p) != HEADER_SIZE)
40  return 0;
41 
42  if (get_4(&p) != 40)
43  return 0;
44 
45  if (get_4(&p) != width)
46  return 0;
47  if (get_4(&p) != -height)
48  return 0;
49 
50  get_2(&p);
51  if (get_2(&p) != 32)
52  return 0;
53 
54  if (get_4(&p) != 0)
55  return 0;
56  if (get_4(&p) != width * height * 4)
57  return 0;
58 
59  get_4(&p);
60  get_4(&p);
61  get_4(&p);
62  get_4(&p);
63 
64  return 1;
65 }
66 
67 void read_bmp(void)
68 {
69  char header[HEADER_SIZE];
70  FILE *input;
71  int x, y;
72  unsigned int *p;
73 
74  if (!true_color)
75  G_fatal_error("PNG: cannot use BMP with indexed color");
76 
77  input = fopen(file_name, "rb");
78  if (!input)
79  G_fatal_error("PNG: couldn't open input file %s", file_name);
80 
81  if (fread(header, sizeof(header), 1, input) != 1)
82  G_fatal_error("PNG: invalid input file %s", file_name);
83 
84  if (!read_bmp_header(header))
85  G_fatal_error("PNG: invalid BMP header for %s", file_name);
86 
87  for (y = 0, p = grid; y < height; y++) {
88  for (x = 0; x < width; x++, p++) {
89  int b = fgetc(input);
90  int g = fgetc(input);
91  int r = fgetc(input);
92  int a = fgetc(input);
93  unsigned int c = get_color(r, g, b, a);
94 
95  *p = c;
96  }
97  }
98 
99  fclose(input);
100 }
#define HEADER_SIZE
Definition: cairodriver.h:17
float b
Definition: named_colr.c:8
tuple q
Definition: forms.py:2019
float r
Definition: named_colr.c:8
tuple width
unsigned char * grid
int y
Definition: plot.c:34
int true_color
char * file_name
float g
Definition: named_colr.c:8
fclose(fd)
unsigned int get_color(int r, int g, int b, int a)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int height
int n
Definition: dataquad.c:291
void read_bmp(void)