GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pngdriver/write_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 char *put_2(unsigned char *p, unsigned int n)
10 {
11  *p++ = n & 0xFF;
12  n >>= 8;
13  *p++ = n & 0xFF;
14  return p;
15 }
16 
17 static unsigned char *put_4(unsigned char *p, unsigned int n)
18 {
19  *p++ = n & 0xFF;
20  n >>= 8;
21  *p++ = n & 0xFF;
22  n >>= 8;
23  *p++ = n & 0xFF;
24  n >>= 8;
25  *p++ = n & 0xFF;
26  return p;
27 }
28 
29 static void make_bmp_header(unsigned char *p)
30 {
31  *p++ = 'B';
32  *p++ = 'M';
33 
34  p = put_4(p, HEADER_SIZE + width * height * 4);
35  p = put_4(p, 0);
36  p = put_4(p, HEADER_SIZE);
37 
38  p = put_4(p, 40);
39  p = put_4(p, width);
40  p = put_4(p, -height);
41  p = put_2(p, 1);
42  p = put_2(p, 32);
43  p = put_4(p, 0);
44  p = put_4(p, width * height * 4);
45  p = put_4(p, 0);
46  p = put_4(p, 0);
47  p = put_4(p, 0);
48  p = put_4(p, 0);
49 }
50 
51 void write_bmp(void)
52 {
53  char header[HEADER_SIZE];
54  FILE *output;
55  int x, y;
56  unsigned int *p;
57 
58  output = fopen(file_name, "wb");
59  if (!output)
60  G_fatal_error("PNG: couldn't open output file %s", file_name);
61 
62  memset(header, 0, sizeof(header));
63  make_bmp_header(header);
64  fwrite(header, sizeof(header), 1, output);
65 
66  for (y = 0, p = grid; y < height; y++) {
67  for (x = 0; x < width; x++, p++) {
68  unsigned int c = *p;
69  int r, g, b, a;
70 
71  get_pixel(c, &r, &g, &b, &a);
72 
73  fputc((unsigned char)b, output);
74  fputc((unsigned char)g, output);
75  fputc((unsigned char)r, output);
76  fputc((unsigned char)a, output);
77  }
78  }
79 
80  fclose(output);
81 }
#define HEADER_SIZE
Definition: cairodriver.h:17
float b
Definition: named_colr.c:8
void get_pixel(unsigned int pixel, int *r, int *g, int *b, int *a)
float r
Definition: named_colr.c:8
tuple width
void write_bmp(void)
unsigned char * grid
int y
Definition: plot.c:34
char * file_name
float g
Definition: named_colr.c:8
fclose(fd)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int height
int n
Definition: dataquad.c:291
void output(const char *fmt,...)