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
22static void read_data(png_structp png_ptr, png_bytep data, png_size_t length)
23{
25 FILE *fp;
26
27 if (png_ptr == NULL)
28 return;
29
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
44void 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;
54 int depth, color_type;
55
57 if (!png_ptr)
58 G_fatal_error(_("Unable to allocate PNG structure"));
59
61 if (!info_ptr)
62 G_fatal_error(_("Unable to allocate PNG structure"));
63
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
74
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) {
90 G_fatal_error(_("Input PNG file is not RGBA"));
91 }
92 else {
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
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)
109 else {
111 int num_palette;
112 int i;
113
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
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
151
153
154 fclose(input);
155}
#define NULL
Definition ccmath.h:32
unsigned int png_get_color(int r, int g, int b, int a)
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition defs/gis.h:136
#define _(str)
Definition glocale.h:10
float g
Definition named_colr.c:7
struct png_state png
GRASS png display driver - header file.
double b
Definition r_raster.c:37
double r
Definition r_raster.c:37
void read_png(void)
Definition read_png.c:44
#define x