GRASS GIS 8 Programmer's Manual 8.4.1(2025)-45ca3179ab
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{
25 png_size_t check;
26 FILE *fp;
27
28 if (png_ptr == NULL)
29 return;
30
31 fp = (FILE *)png_get_io_ptr(png_ptr);
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
48 fp = (FILE *)png_get_io_ptr(png_ptr);
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
67 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, &jbuf, NULL, NULL);
68 if (!png_ptr)
69 G_fatal_error(_("Unable to allocate PNG structure"));
70
71 info_ptr = png_create_info_struct(png_ptr);
72 if (!info_ptr)
73 G_fatal_error(_("Unable to allocate PNG structure"));
74
75 if (setjmp(png_jmpbuf(png_ptr)))
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
86 : PNG_COLOR_TYPE_PALETTE,
87 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
88 PNG_FILTER_TYPE_DEFAULT);
89
90 if (png.true_color)
91 png_set_invert_alpha(png_ptr);
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
102 png_set_PLTE(png_ptr, info_ptr, png_pal, 256);
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)
113 png_set_compression_level(png_ptr, compress);
114
115 png_write_info(png_ptr, info_ptr);
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
142 png_write_end(png_ptr, info_ptr);
143
144 png_destroy_write_struct(&png_ptr, &info_ptr);
145
146 fclose(output);
147}
void G_free(void *buf)
Free allocated memory.
Definition alloc.c:150
#define NULL
Definition ccmath.h:32
void png_get_pixel(unsigned int pixel, int *r, int *g, int *b, int *a)
double b
double r
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition gis/error.c:159
float g
Definition named_colr.c:7
struct png_state png
GRASS png display driver - header file.
void output(const char *fmt,...)
void write_png(void)
Definition write_png.c:55
#define x