GRASS 8 Programmer's Manual
8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
read_png.c
Go to the documentation of this file.
1
/*!
2
\file lib/pngdriver/read_png.c
3
4
\brief GRASS png display driver - read png
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
read_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
32
if
(fp ==
NULL
)
33
return
;
34
35
/* fread() returns 0 on error, so it is OK to store this in a png_size_t
36
* instead of an int, which is what fread() actually returns.
37
*/
38
check
=
fread
(data, 1, length, fp);
39
40
if
(
check
!= length)
41
G_fatal_error
(
_
(
"Unable to read PNG"
));
42
}
43
44
void
read_png
(
void
)
45
{
46
static
jmp_buf
jbuf
;
47
static
png_struct
*
png_ptr
;
48
static
png_info
*
info_ptr
;
49
FILE
*input;
50
int
x
, y;
51
unsigned
int
*p;
52
png_bytep
line;
53
png_uint_32
i_width
,
i_height
;
54
int
depth,
color_type
;
55
56
png_ptr
=
png_create_read_struct
(
PNG_LIBPNG_VER_STRING
, &
jbuf
,
NULL
,
NULL
);
57
if
(!
png_ptr
)
58
G_fatal_error
(
_
(
"Unable to allocate PNG structure"
));
59
60
info_ptr
=
png_create_info_struct
(
png_ptr
);
61
if
(!
info_ptr
)
62
G_fatal_error
(
_
(
"Unable to allocate PNG structure"
));
63
64
if
(
setjmp
(
png_jmpbuf
(
png_ptr
)))
65
G_fatal_error
(
_
(
"Unable to read PNG file"
));
66
67
input =
fopen
(
png
.file_name,
"rb"
);
68
if
(!input)
69
G_fatal_error
(
_
(
"Unable to open output file <%s>"
),
png
.file_name);
70
71
png_set_read_fn
(
png_ptr
, input, read_data);
72
73
png_read_info
(
png_ptr
,
info_ptr
);
74
75
png_get_IHDR
(
png_ptr
,
info_ptr
, &
i_width
, &
i_height
, &depth, &
color_type
,
76
NULL
,
NULL
,
NULL
);
77
78
if
(depth != 8)
79
G_fatal_error
(
_
(
"Input PNG file is not 8-bit"
));
80
81
if
(
i_width
!= (
unsigned
long
)
png
.width ||
82
i_height
!= (
unsigned
long
)
png
.height)
83
G_fatal_error
(
_
(
"Input PNG file has incorrect dimensions: expected: "
84
"%dx%d got: %lux%lu"
),
85
png
.width,
png
.height, (
unsigned
long
)
i_width
,
86
(
unsigned
long
)
i_height
);
87
88
if
(
png
.true_color) {
89
if
(
color_type
!=
PNG_COLOR_TYPE_RGB_ALPHA
)
90
G_fatal_error
(
_
(
"Input PNG file is not RGBA"
));
91
}
92
else
{
93
if
(
color_type
!=
PNG_COLOR_TYPE_PALETTE
)
94
G_fatal_error
(
_
(
"Input PNG file is not indexed color"
));
95
}
96
97
if
(!
png
.true_color &&
png
.has_alpha) {
98
png_bytep
trans;
99
int
num_trans
;
100
101
png_get_tRNS
(
png_ptr
,
info_ptr
, &trans, &
num_trans
,
NULL
);
102
103
if
(
num_trans
!= 1 || trans[0] != 0)
104
G_fatal_error
(
_
(
"Input PNG file has invalid palette"
));
105
}
106
107
if
(
png
.true_color)
108
png_set_invert_alpha
(
png_ptr
);
109
else
{
110
png_colorp
png_pal
;
111
int
num_palette
;
112
int
i;
113
114
png_get_PLTE
(
png_ptr
,
info_ptr
, &
png_pal
, &
num_palette
);
115
116
if
(
num_palette
> 256)
117
num_palette
= 256;
118
119
for
(i = 0; i <
num_palette
; i++) {
120
png
.palette[i][0] =
png_pal
[i].red;
121
png
.palette[i][1] =
png_pal
[i].green;
122
png
.palette[i][2] =
png_pal
[i].blue;
123
}
124
}
125
126
line =
G_malloc
(
png
.width * 4);
127
128
for
(y = 0, p =
png
.grid; y <
png
.height; y++) {
129
png_bytep
q = line;
130
131
png_read_row
(
png_ptr
, q,
NULL
);
132
133
if
(
png
.true_color)
134
for
(
x
= 0;
x
<
png
.width;
x
++, p++) {
135
int
r
= *q++;
136
int
g
= *q++;
137
int
b
= *q++;
138
int
a = *q++;
139
unsigned
int
c =
png_get_color
(
r
,
g
,
b
, a);
140
141
*p = c;
142
}
143
else
144
for
(
x
= 0;
x
<
png
.width;
x
++, p++, q++)
145
*p = (
png_byte
)*q;
146
}
147
148
G_free
(line);
149
150
png_read_end
(
png_ptr
,
NULL
);
151
152
png_destroy_read_struct
(&
png_ptr
, &
info_ptr
,
NULL
);
153
154
fclose
(input);
155
}
NULL
#define NULL
Definition
ccmath.h:32
AMI_STREAM
Definition
ami_stream.h:153
png_get_color
unsigned int png_get_color(int r, int g, int b, int a)
Definition
color_table.c:118
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.
b
double b
Definition
r_raster.c:37
r
double r
Definition
r_raster.c:37
read_png
void read_png(void)
Definition
read_png.c:44
stdio.h
stdlib.h
x
#define x
lib
pngdriver
read_png.c
Generated on Sun Sep 13 2026 06:57:50 for GRASS 8 Programmer's Manual by
1.9.8