GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 
23 
24 static void write_data(png_structp png_ptr, png_bytep data, png_size_t length)
25 {
26  png_size_t check;
27  FILE *fp;
28 
29  if (png_ptr == NULL )
30  return;
31 
32  fp = (FILE *) png_get_io_ptr(png_ptr);
33  if ( fp == NULL )
34  return;
35 
36  check = fwrite(data, 1, length, fp);
37 
38  if (check != length)
39  G_fatal_error(_("Unable to write PNG"));
40 }
41 
42 static void output_flush(png_structp png_ptr)
43 {
44  FILE *fp;
45 
46  if (png_ptr == NULL )
47  return;
48 
49  fp = (FILE *) png_get_io_ptr(png_ptr);
50  if ( fp == NULL )
51  return;
52 
53  fflush( fp );
54 }
55 
56 void write_png(void)
57 {
58  static jmp_buf jbuf;
59  static png_struct *png_ptr;
60  static png_info *info_ptr;
61  FILE *output;
62  int x, y;
63  unsigned int *p;
64  png_bytep line;
65  const char *str;
66  int compress;
67 
68  png_ptr =
69  png_create_write_struct(PNG_LIBPNG_VER_STRING, &jbuf, NULL, NULL);
70  if (!png_ptr)
71  G_fatal_error(_("Unable to allocate PNG structure"));
72 
73  info_ptr = png_create_info_struct(png_ptr);
74  if (!info_ptr)
75  G_fatal_error(_("Unable to allocate PNG structure"));
76 
77  if (setjmp(png_jmpbuf(png_ptr)))
78  G_fatal_error(_("Unable to write PNG file"));
79 
80  output = fopen(png.file_name, "wb");
81  if (!output)
82  G_fatal_error(_("Unable to open output PNG file <%s>"), png.file_name);
83 
84  png_set_write_fn(png_ptr, output, write_data, output_flush);
85 
86  png_set_IHDR(png_ptr, info_ptr,
87  png.width, png.height, 8,
88  png.true_color ? PNG_COLOR_TYPE_RGB_ALPHA :
89  PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
90  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
91 
92  if (png.true_color)
93  png_set_invert_alpha(png_ptr);
94  else {
95  png_color png_pal[256];
96  int i;
97 
98  for (i = 0; i < 256; i++) {
99  png_pal[i].red = png.palette[i][0];
100  png_pal[i].green = png.palette[i][1];
101  png_pal[i].blue = png.palette[i][2];
102  }
103 
104  png_set_PLTE(png_ptr, info_ptr, png_pal, 256);
105 
106  if (png.has_alpha) {
107  png_byte trans = (png_byte) 0;
108 
109  png_set_tRNS(png_ptr, info_ptr, &trans, 1, NULL);
110  }
111  }
112 
113  str = getenv("GRASS_RENDER_FILE_COMPRESSION");
114  if (str && sscanf(str, "%d", &compress) == 1)
115  png_set_compression_level(png_ptr, compress);
116 
117  png_write_info(png_ptr, info_ptr);
118 
119  line = G_malloc(png.width * 4);
120 
121  for (y = 0, p = png.grid; y < png.height; y++) {
122  png_bytep q = line;
123 
124  if (png.true_color)
125  for (x = 0; x < png.width; x++, p++) {
126  unsigned int c = *p;
127  int r, g, b, a;
128 
129  png_get_pixel(c, &r, &g, &b, &a);
130  *q++ = (png_byte) r;
131  *q++ = (png_byte) g;
132  *q++ = (png_byte) b;
133  *q++ = (png_byte) a;
134  }
135  else
136  for (x = 0; x < png.width; x++, p++, q++)
137  *q = (png_byte) * p;
138 
139  png_write_row(png_ptr, line);
140  }
141 
142  G_free(line);
143 
144  png_write_end(png_ptr, info_ptr);
145 
146  png_destroy_write_struct(&png_ptr, &info_ptr);
147 
148  fclose(output);
149 }
#define G_malloc(n)
Definition: defs/gis.h:112
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int width
Definition: pngdriver.h:43
GRASS png display driver - header file.
unsigned char palette[256][4]
Definition: pngdriver.h:45
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
int height
Definition: pngdriver.h:43
void png_get_pixel(unsigned int pixel, int *r, int *g, int *b, int *a)
Definition: color_table.c:112
#define NULL
Definition: ccmath.h:32
#define x
struct png_state png
double b
Definition: r_raster.c:39
char * file_name
Definition: pngdriver.h:33
unsigned int * grid
Definition: pngdriver.h:44
float g
Definition: named_colr.c:8
void output(const char *fmt,...)
int has_alpha
Definition: pngdriver.h:36
void write_png(void)
Definition: write_png.c:56
#define _(str)
Definition: glocale.h:10
int true_color
Definition: pngdriver.h:35
char * getenv()
double r
Definition: r_raster.c:39