GRASS 8 Programmer's Manual
8.5.0(2026)-8d6ceba290
Toggle main menu visibility
Loading...
Searching...
No Matches
cairodriver/read_ppm.c
Go to the documentation of this file.
1
/*!
2
\file lib/cairodriver/read_ppm.c
3
4
\brief GRASS cairo display driver - read PPM image (lower level functions)
5
6
(C) 2007-2008, 2011 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 contributor)
12
\author Glynn Clements
13
*/
14
15
#include <grass/glocale.h>
16
17
#include "
cairodriver.h
"
18
19
void
cairo_read_ppm
(
void
)
20
{
21
char
*mask_name =
G_store
(
ca
.
file_name
);
22
FILE *input, *mask;
23
int
x
, y;
24
int
i_width, i_height, maxval;
25
26
input = fopen(
ca
.
file_name
,
"rb"
);
27
if
(!input)
28
G_fatal_error
(_(
"Cairo: unable to open input file <%s>"
),
ca
.
file_name
);
29
30
if
(fscanf(input,
"P6 %d %d %d"
, &i_width, &i_height, &maxval) != 3)
31
G_fatal_error
(_(
"Cairo: invalid input file <%s>"
),
ca
.
file_name
);
32
33
if
(fgetc(input) == EOF)
34
G_fatal_error
(_(
"Cairo: unexpected EOF reading <%s>"
),
ca
.
file_name
);
35
36
if
(i_width !=
ca
.
width
|| i_height !=
ca
.
height
)
37
G_fatal_error
(_(
"Cairo: input file has incorrect dimensions: "
38
"expected: %dx%d got: %dx%d"
),
39
ca
.
width
,
ca
.
height
, i_width, i_height);
40
41
mask_name[strlen(mask_name) - 2] =
'g'
;
42
43
mask = fopen(mask_name,
"rb"
);
44
if
(!mask)
45
G_fatal_error
(_(
"Cairo: unable to open input mask file <%s>"
),
46
mask_name);
47
48
if
(fscanf(mask,
"P5 %d %d %d"
, &i_width, &i_height, &maxval) != 3)
49
G_fatal_error
(_(
"Cairo: invalid input mask file <%s>"
), mask_name);
50
51
if
(fgetc(mask) == EOF)
52
G_fatal_error
(_(
"Cairo: unexpected EOF reading mask <%s>"
), mask_name);
53
54
if
(i_width !=
ca
.
width
|| i_height !=
ca
.
height
)
55
G_fatal_error
(_(
"Cairo: input mask file has incorrect dimensions: "
56
"expected: %dx%d got: %dx%d"
),
57
ca
.
width
,
ca
.
height
, i_width, i_height);
58
59
G_free
(mask_name);
60
61
for
(y = 0; y <
ca
.
height
; y++) {
62
unsigned
int
*row = (
unsigned
int
*)(
ca
.
grid
+ y *
ca
.
stride
);
63
64
for
(
x
= 0;
x
<
ca
.
width
;
x
++) {
65
int
r = fgetc(input);
66
int
g = fgetc(input);
67
int
b = fgetc(input);
68
int
a = fgetc(mask);
69
70
r = r * 255 / maxval;
71
g = g * 255 / maxval;
72
b = b * 255 / maxval;
73
a = a * 255 / maxval;
74
75
if
(a > 0 && a < 0xFF) {
76
r = r * a / 0xFF;
77
g = g * a / 0xFF;
78
b = b * a / 0xFF;
79
}
80
81
row[
x
] = (a << 24) | (r << 16) | (g << 8) | (b << 0);
82
}
83
}
84
85
fclose(input);
86
fclose(mask);
87
}
G_free
void G_free(void *buf)
Free allocated memory.
Definition
alloc.c:147
cairodriver.h
GRASS cairo display driver - header file.
ca
struct cairo_state ca
Definition
cairodriver/graph.c:42
cairo_read_ppm
void cairo_read_ppm(void)
Definition
cairodriver/read_ppm.c:19
G_fatal_error
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition
gis/error.c:159
G_store
char * G_store(const char *s)
Copy string to allocated memory.
Definition
strings.c:87
cairo_state::grid
unsigned char * grid
Definition
cairodriver.h:69
cairo_state::height
int height
Definition
cairodriver.h:68
cairo_state::width
int width
Definition
cairodriver.h:68
cairo_state::stride
int stride
Definition
cairodriver.h:68
cairo_state::file_name
char * file_name
Definition
cairodriver.h:66
x
#define x
cairodriver
read_ppm.c
Generated on
for GRASS 8 Programmer's Manual by
1.18.0