GRASS GIS 7 Programmer's Manual
7.8.3(2020)-exported
cairodriver/write_ppm.c
Go to the documentation of this file.
1
/*!
2
\file lib/cairodriver/write_ppm.c
3
4
\brief GRASS cairo display driver - write PPM image (lower level functions)
5
6
(C) 2007-2008 by Lars Ahlzen 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 Lars Ahlzen <lars ahlzen.com> (original contibutor)
12
\author Glynn Clements
13
*/
14
15
#include <grass/glocale.h>
16
17
#include "
cairodriver.h
"
18
19
void
cairo_write_ppm
(
void
)
20
{
21
char
*mask_name =
G_store
(
ca
.
file_name
);
22
FILE *
output
, *mask;
23
int
x
, y;
24
25
output
= fopen(
ca
.
file_name
,
"wb"
);
26
if
(!
output
)
27
G_fatal_error
(_(
"Cairo: unable to open output file <%s>"
),
28
ca
.
file_name
);
29
30
mask_name[strlen(mask_name) - 2] =
'g'
;
31
32
mask = fopen(mask_name,
"wb"
);
33
if
(!mask)
34
G_fatal_error
(_(
"Cairo: unable to open mask file <%s>"
),
35
mask_name);
36
37
G_free
(mask_name);
38
39
fprintf(
output
,
"P6\n%d %d\n255\n"
,
ca
.
width
,
ca
.
height
);
40
fprintf(mask,
"P5\n%d %d\n255\n"
,
ca
.
width
,
ca
.
height
);
41
42
for
(y = 0; y <
ca
.
height
; y++) {
43
const
unsigned
int
*row = (
const
unsigned
int
*)(
ca
.
grid
+ y *
ca
.
stride
);
44
45
for
(
x
= 0;
x
<
ca
.
width
;
x
++) {
46
unsigned
int
c = row[
x
];
47
int
a = (c >> 24) & 0xFF;
48
int
r
= (c >> 16) & 0xFF;
49
int
g
= (c >> 8) & 0xFF;
50
int
b
= (c >> 0) & 0xFF;
51
52
if
(a > 0 && a < 0xFF) {
53
r
=
r
* 0xFF / a;
54
g
=
g
* 0xFF / a;
55
b
=
b
* 0xFF / a;
56
}
57
58
fputc((
unsigned
char
)
r
,
output
);
59
fputc((
unsigned
char
)
g
,
output
);
60
fputc((
unsigned
char
)
b
,
output
);
61
fputc((
unsigned
char
)a, mask);
62
}
63
}
64
65
fclose(
output
);
66
fclose(mask);
67
}
output
void output(const char *fmt,...)
Definition:
psdriver/graph_set.c:233
ca
struct cairo_state ca
Definition:
cairodriver/graph.c:42
G_store
char * G_store(const char *s)
Copy string to allocated memory.
Definition:
strings.c:86
g
float g
Definition:
named_colr.c:8
cairo_state::grid
unsigned char * grid
Definition:
cairodriver.h:68
G_fatal_error
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition:
gis/error.c:160
x
#define x
b
double b
Definition:
driver/set_window.c:5
cairo_write_ppm
void cairo_write_ppm(void)
Definition:
cairodriver/write_ppm.c:19
cairo_state::width
int width
Definition:
cairodriver.h:67
G_free
void G_free(void *buf)
Free allocated memory.
Definition:
alloc.c:149
cairo_state::height
int height
Definition:
cairodriver.h:67
cairo_state::file_name
char * file_name
Definition:
cairodriver.h:65
cairodriver.h
GRASS cairo display driver - header file.
cairo_state::stride
int stride
Definition:
cairodriver.h:67
r
double r
Definition:
driver/set_window.c:5
cairodriver
write_ppm.c
Generated on Tue May 5 2020 09:45:27 for GRASS GIS 7 Programmer's Manual by
1.8.17