GRASS GIS 7 Programmer's Manual
7.8.4(2020)-exported
pngdriver/write_ppm.c
Go to the documentation of this file.
1
/*!
2
\file lib/pngdriver/write_ppm.c
3
4
\brief GRASS png display driver - write PPM image (lower level functions)
5
6
(C) 2007-2014 by Glynn Clements 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 Glynn Clements
12
*/
13
14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17
18
#include <grass/gis.h>
19
#include "
pngdriver.h
"
20
21
void
write_ppm
(
void
)
22
{
23
FILE *
output
;
24
int
x
, y;
25
unsigned
int
*p;
26
27
output
= fopen(
png
.
file_name
,
"wb"
);
28
if
(!
output
)
29
G_fatal_error
(
"PNG: couldn't open output file %s"
,
png
.
file_name
);
30
31
fprintf(
output
,
"P6\n%d %d\n255\n"
,
png
.
width
,
png
.
height
);
32
33
for
(y = 0, p =
png
.
grid
; y <
png
.
height
; y++) {
34
for
(
x
= 0;
x
<
png
.
width
;
x
++, p++) {
35
unsigned
int
c = *p;
36
int
r
,
g
,
b
, a;
37
38
png_get_pixel
(c, &
r
, &
g
, &
b
, &a);
39
40
fputc((
unsigned
char
)
r
,
output
);
41
fputc((
unsigned
char
)
g
,
output
);
42
fputc((
unsigned
char
)
b
,
output
);
43
}
44
}
45
46
fclose(
output
);
47
}
48
49
void
write_pgm
(
void
)
50
{
51
char
*mask_name =
G_store
(
png
.
file_name
);
52
FILE *
output
;
53
int
x
, y;
54
unsigned
int
*p;
55
56
mask_name[strlen(mask_name) - 2] =
'g'
;
57
58
output
= fopen(mask_name,
"wb"
);
59
if
(!
output
)
60
G_fatal_error
(
"PNG: couldn't open mask file %s"
, mask_name);
61
62
G_free
(mask_name);
63
64
fprintf(
output
,
"P5\n%d %d\n255\n"
,
png
.
width
,
png
.
height
);
65
66
for
(y = 0, p =
png
.
grid
; y <
png
.
height
; y++) {
67
for
(
x
= 0;
x
<
png
.
width
;
x
++, p++) {
68
unsigned
int
c = *p;
69
int
r
,
g
,
b
, a;
70
71
png_get_pixel
(c, &
r
, &
g
, &
b
, &a);
72
73
fputc((
unsigned
char
)(255 - a),
output
);
74
}
75
}
76
77
fclose(
output
);
78
}
write_ppm
void write_ppm(void)
Definition:
pngdriver/write_ppm.c:21
output
void output(const char *fmt,...)
Definition:
psdriver/graph_set.c:233
png_state::width
int width
Definition:
pngdriver.h:43
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
png
struct png_state png
Definition:
pngdriver/graph_set.c:32
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
png_state::height
int height
Definition:
pngdriver.h:43
png_state::file_name
char * file_name
Definition:
pngdriver.h:33
png_state::grid
unsigned int * grid
Definition:
pngdriver.h:44
G_free
void G_free(void *buf)
Free allocated memory.
Definition:
alloc.c:149
pngdriver.h
GRASS png display driver - header file.
write_pgm
void write_pgm(void)
Definition:
pngdriver/write_ppm.c:49
r
double r
Definition:
driver/set_window.c:5
png_get_pixel
void png_get_pixel(unsigned int pixel, int *r, int *g, int *b, int *a)
Definition:
color_table.c:112
pngdriver
write_ppm.c
Generated on Mon Oct 5 2020 08:56:03 for GRASS GIS 7 Programmer's Manual by
1.8.20