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
22static void write_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 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
40static void output_flush(png_structp png_ptr)
41{
42 FILE *fp;
43
44 if (png_ptr == NULL)
45 return;
46
48 if (fp == NULL)
49 return;
50
51 fflush(fp);
52}
53
54void 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
67 if (!png_ptr)
68 G_fatal_error(_("Unable to allocate PNG structure"));
69
71 if (!info_ptr)
72 G_fatal_error(_("Unable to allocate PNG structure"));
73
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
88
89 if (png.true_color)
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
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)
113
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
142
144
145 fclose(output);
146}
#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: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.
void output(const char *fmt,...)
double b
Definition r_raster.c:37
double r
Definition r_raster.c:37
void write_png(void)
Definition write_png.c:54
#define x