GRASS 8 Programmer's Manual 8.6.0dev(2026)-f6f2c534ea
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 (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 write_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 if (fp == NULL)
33 return;
34
35 check = fwrite(data, 1, length, fp);
36
37 if (check != length)
38 G_fatal_error(_("Unable to write PNG"));
39}
40
41static void output_flush(png_structp png_ptr)
42{
43 FILE *fp;
44
45 if (png_ptr == NULL)
46 return;
47
49 if (fp == NULL)
50 return;
51
52 fflush(fp);
53}
54
55void write_png(void)
56{
57 static jmp_buf jbuf;
58 static png_struct *png_ptr;
59 static png_info *info_ptr;
60 FILE *output;
61 int x, y;
62 unsigned int *p;
63 png_bytep line;
64 const char *str;
65 int compress;
66
68 if (!png_ptr)
69 G_fatal_error(_("Unable to allocate PNG structure"));
70
72 if (!info_ptr)
73 G_fatal_error(_("Unable to allocate PNG structure"));
74
76 G_fatal_error(_("Unable to write PNG file"));
77
78 output = fopen(png.file_name, "wb");
79 if (!output)
80 G_fatal_error(_("Unable to open output PNG file <%s>"), png.file_name);
81
82 png_set_write_fn(png_ptr, output, write_data, output_flush);
83
84 png_set_IHDR(png_ptr, info_ptr, png.width, png.height, 8,
85 png.true_color ? PNG_COLOR_TYPE_RGB_ALPHA
89
90 if (png.true_color)
92 else {
93 png_color png_pal[256];
94 int i;
95
96 for (i = 0; i < 256; i++) {
97 png_pal[i].red = png.palette[i][0];
98 png_pal[i].green = png.palette[i][1];
99 png_pal[i].blue = png.palette[i][2];
100 }
101
103
104 if (png.has_alpha) {
105 png_byte trans = (png_byte)0;
106
107 png_set_tRNS(png_ptr, info_ptr, &trans, 1, NULL);
108 }
109 }
110
111 str = getenv("GRASS_RENDER_FILE_COMPRESSION");
112 if (str && sscanf(str, "%d", &compress) == 1)
114
116
117 line = G_malloc(png.width * 4);
118
119 for (y = 0, p = png.grid; y < png.height; y++) {
120 png_bytep q = line;
121
122 if (png.true_color)
123 for (x = 0; x < png.width; x++, p++) {
124 unsigned int c = *p;
125 int r, g, b, a;
126
127 png_get_pixel(c, &r, &g, &b, &a);
128 *q++ = (png_byte)r;
129 *q++ = (png_byte)g;
130 *q++ = (png_byte)b;
131 *q++ = (png_byte)a;
132 }
133 else
134 for (x = 0; x < png.width; x++, p++, q++)
135 *q = (png_byte)*p;
136
137 png_write_row(png_ptr, line);
138 }
139
140 G_free(line);
141
143
145
146 fclose(output);
147}
#define NULL
Definition ccmath.h:32
void png_get_pixel(unsigned int pixel, 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.
void output(const char *fmt,...)
double b
Definition r_raster.c:39
double r
Definition r_raster.c:39
void write_png(void)
Definition write_png.c:55
#define x