GRASS 8 Programmer's Manual
8.6.0dev(2026)-55de52a352
Loading...
Searching...
No Matches
write_png.c
Go to the documentation of this file.
1
/*!
2
\file lib/pngdriver/write_png.c
3
4
\brief GRASS png display driver - write PPM image (lower level functions)
5
6
SPDX-FileCopyrightText: 2007-2014 Glynn Clements
7
SPDX-FileCopyrightText: GRASS Development Team
8
SPDX-License-Identifier: GPL-2.0-or-later
9
10
\author Glynn Clements
11
*/
12
13
#include <
stdio.h
>
14
#include <
stdlib.h
>
15
#include <png.h>
16
17
#include <
grass/gis.h
>
18
#include <
grass/glocale.h
>
19
20
#include "
pngdriver.h
"
21
22
static
void
write_data(
png_structp
png_ptr
,
png_bytep
data,
png_size_t
length)
23
{
24
png_size_t
check
;
25
FILE
*fp;
26
27
if
(
png_ptr
==
NULL
)
28
return
;
29
30
fp = (
FILE
*)
png_get_io_ptr
(
png_ptr
);
31
if
(fp ==
NULL
)
32
return
;
33
34
check
=
fwrite
(data, 1, length, fp);
35
36
if
(
check
!= length)
37
G_fatal_error
(
_
(
"Unable to write PNG"
));
38
}
39
40
static
void
output_flush(
png_structp
png_ptr
)
41
{
42
FILE
*fp;
43
44
if
(
png_ptr
==
NULL
)
45
return
;
46
47
fp = (
FILE
*)
png_get_io_ptr
(
png_ptr
);
48
if
(fp ==
NULL
)
49
return
;
50
51
fflush
(fp);
52
}
53
54
void
write_png
(
void
)
55
{
56
static
jmp_buf
jbuf
;
57
static
png_struct
*
png_ptr
;
58
static
png_info
*
info_ptr
;
59
FILE
*
output
;
60
int
x
, y;
61
unsigned
int
*p;
62
png_bytep
line;
63
const
char
*str;
64
int
compress;
65
66
png_ptr
=
png_create_write_struct
(
PNG_LIBPNG_VER_STRING
, &
jbuf
,
NULL
,
NULL
);
67
if
(!
png_ptr
)
68
G_fatal_error
(
_
(
"Unable to allocate PNG structure"
));
69
70
info_ptr
=
png_create_info_struct
(
png_ptr
);
71
if
(!
info_ptr
)
72
G_fatal_error
(
_
(
"Unable to allocate PNG structure"
));
73
74
if
(
setjmp
(
png_jmpbuf
(
png_ptr
)))
75
G_fatal_error
(
_
(
"Unable to write PNG file"
));
76
77
output
=
fopen
(
png
.file_name,
"wb"
);
78
if
(!
output
)
79
G_fatal_error
(
_
(
"Unable to open output PNG file <%s>"
),
png
.file_name);
80
81
png_set_write_fn
(
png_ptr
,
output
, write_data, output_flush);
82
83
png_set_IHDR
(
png_ptr
,
info_ptr
,
png
.width,
png
.height, 8,
84
png
.true_color ?
PNG_COLOR_TYPE_RGB_ALPHA
85
:
PNG_COLOR_TYPE_PALETTE
,
86
PNG_INTERLACE_NONE
,
PNG_COMPRESSION_TYPE_DEFAULT
,
87
PNG_FILTER_TYPE_DEFAULT
);
88
89
if
(
png
.true_color)
90
png_set_invert_alpha
(
png_ptr
);
91
else
{
92
png_color
png_pal
[256];
93
int
i;
94
95
for
(i = 0; i < 256; i++) {
96
png_pal
[i].red =
png
.palette[i][0];
97
png_pal
[i].green =
png
.palette[i][1];
98
png_pal
[i].blue =
png
.palette[i][2];
99
}
100
101
png_set_PLTE
(
png_ptr
,
info_ptr
,
png_pal
, 256);
102
103
if
(
png
.has_alpha) {
104
png_byte
trans = (
png_byte
)0;
105
106
png_set_tRNS
(
png_ptr
,
info_ptr
, &trans, 1,
NULL
);
107
}
108
}
109
110
str =
getenv
(
"GRASS_RENDER_FILE_COMPRESSION"
);
111
if
(str &&
sscanf
(str,
"%d"
, &compress) == 1)
112
png_set_compression_level
(
png_ptr
, compress);
113
114
png_write_info
(
png_ptr
,
info_ptr
);
115
116
line =
G_malloc
(
png
.width * 4);
117
118
for
(y = 0, p =
png
.grid; y <
png
.height; y++) {
119
png_bytep
q = line;
120
121
if
(
png
.true_color)
122
for
(
x
= 0;
x
<
png
.width;
x
++, p++) {
123
unsigned
int
c = *p;
124
int
r
,
g
,
b
, a;
125
126
png_get_pixel
(c, &
r
, &
g
, &
b
, &a);
127
*q++ = (
png_byte
)
r
;
128
*q++ = (
png_byte
)
g
;
129
*q++ = (
png_byte
)
b
;
130
*q++ = (
png_byte
)a;
131
}
132
else
133
for
(
x
= 0;
x
<
png
.width;
x
++, p++, q++)
134
*q = (
png_byte
)*p;
135
136
png_write_row
(
png_ptr
, line);
137
}
138
139
G_free
(line);
140
141
png_write_end
(
png_ptr
,
info_ptr
);
142
143
png_destroy_write_struct
(&
png_ptr
, &
info_ptr
);
144
145
fclose
(
output
);
146
}
NULL
#define NULL
Definition
ccmath.h:32
AMI_STREAM
Definition
ami_stream.h:153
png_get_pixel
void png_get_pixel(unsigned int pixel, int *r, int *g, int *b, int *a)
Definition
color_table.c:110
G_free
void G_free(void *)
Free allocated memory.
Definition
gis/alloc.c:145
G_fatal_error
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
G_malloc
#define G_malloc(n)
Definition
defs/gis.h:136
gis.h
glocale.h
_
#define _(str)
Definition
glocale.h:10
g
float g
Definition
named_colr.c:7
png
struct png_state png
Definition
pngdriver/graph_set.c:31
pngdriver.h
GRASS png display driver - header file.
output
void output(const char *fmt,...)
Definition
psdriver/graph_set.c:233
b
double b
Definition
r_raster.c:37
r
double r
Definition
r_raster.c:37
stdio.h
stdlib.h
write_png
void write_png(void)
Definition
write_png.c:54
x
#define x
lib
pngdriver
write_png.c
Generated on Thu Sep 10 2026 06:57:38 for GRASS 8 Programmer's Manual by
1.9.8