GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
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 (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 <png.h>
17
18#include <grass/gis.h>
19#include <grass/glocale.h>
20
21#include "pngdriver.h"
22
23static void read_data(png_structp png_ptr, png_bytep data, png_size_t length)
24{
26 FILE *fp;
27
28 if (png_ptr == NULL)
29 return;
30
32
33 if (fp == NULL)
34 return;
35
36 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
37 * instead of an int, which is what fread() actually returns.
38 */
39 check = fread(data, 1, length, fp);
40
41 if (check != length)
42 G_fatal_error(_("Unable to read PNG"));
43}
44
45void read_png(void)
46{
47 static jmp_buf jbuf;
48 static png_struct *png_ptr;
49 static png_info *info_ptr;
50 FILE *input;
51 int x, y;
52 unsigned int *p;
53 png_bytep line;
55 int depth, color_type;
56
58 if (!png_ptr)
59 G_fatal_error(_("Unable to allocate PNG structure"));
60
62 if (!info_ptr)
63 G_fatal_error(_("Unable to allocate PNG structure"));
64
66 G_fatal_error(_("Unable to read PNG file"));
67
68 input = fopen(png.file_name, "rb");
69 if (!input)
70 G_fatal_error(_("Unable to open output file <%s>"), png.file_name);
71
72 png_set_read_fn(png_ptr, input, read_data);
73
75
77 NULL, NULL, NULL);
78
79 if (depth != 8)
80 G_fatal_error(_("Input PNG file is not 8-bit"));
81
82 if (i_width != (unsigned long)png.width ||
83 i_height != (unsigned long)png.height)
84 G_fatal_error(_("Input PNG file has incorrect dimensions: expected: "
85 "%dx%d got: %lux%lu"),
86 png.width, png.height, (unsigned long)i_width,
87 (unsigned long)i_height);
88
89 if (png.true_color) {
91 G_fatal_error(_("Input PNG file is not RGBA"));
92 }
93 else {
95 G_fatal_error(_("Input PNG file is not indexed color"));
96 }
97
98 if (!png.true_color && png.has_alpha) {
99 png_bytep trans;
100 int num_trans;
101
103
104 if (num_trans != 1 || trans[0] != 0)
105 G_fatal_error(_("Input PNG file has invalid palette"));
106 }
107
108 if (png.true_color)
110 else {
112 int num_palette;
113 int i;
114
116
117 if (num_palette > 256)
118 num_palette = 256;
119
120 for (i = 0; i < num_palette; i++) {
121 png.palette[i][0] = png_pal[i].red;
122 png.palette[i][1] = png_pal[i].green;
123 png.palette[i][2] = png_pal[i].blue;
124 }
125 }
126
127 line = G_malloc(png.width * 4);
128
129 for (y = 0, p = png.grid; y < png.height; y++) {
130 png_bytep q = line;
131
133
134 if (png.true_color)
135 for (x = 0; x < png.width; x++, p++) {
136 int r = *q++;
137 int g = *q++;
138 int b = *q++;
139 int a = *q++;
140 unsigned int c = png_get_color(r, g, b, a);
141
142 *p = c;
143 }
144 else
145 for (x = 0; x < png.width; x++, p++, q++)
146 *p = (png_byte)*q;
147 }
148
149 G_free(line);
150
152
154
155 fclose(input);
156}
#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:147
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition defs/gis.h:139
#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:39
double r
Definition r_raster.c:39
void read_png(void)
Definition read_png.c:45
#define x