GRASS Programmer's Manual
6.4.3(2013)-r
Main Page
Related Pages
Namespaces
Data Structures
Files
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
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
}
lib
pngdriver
write_bmp.c
Generated on Thu Sep 26 2013 09:48:07 for GRASS Programmer's Manual by
1.8.4