GRASS GIS 7 Programmer's Manual  7.0.3(2016)-r00000
write_png.c
Go to the documentation of this file.
1 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <png.h>
17 
18 #include <grass/gis.h>
19 #include "pngdriver.h"
20 
21 
22 static void write_data(png_structp png_ptr, png_bytep data, png_size_t length)
23 {
24  png_size_t check;
25  FILE *fp;
26 
27  if (png_ptr == NULL )
28  return;
29 
30  fp = (FILE *) png_get_io_ptr(png_ptr);
31  if ( fp == NULL )
32  return;
33 
34  check = fwrite(data, 1, length, fp);
35 
36  if (check != length)
37  G_fatal_error("PNG: Write Error");
38 }
39 
40 static void output_flush(png_structp png_ptr)
41 {
42  FILE *fp;
43 
44  if (png_ptr == NULL )
45  return;
46 
47  fp = (FILE *) png_get_io_ptr(png_ptr);
48  if ( fp == NULL )
49  return;
50 
51  fflush( fp );
52 }
53 
54 void 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 
66  png_ptr =
67  png_create_write_struct(PNG_LIBPNG_VER_STRING, &jbuf, NULL, NULL);
68  if (!png_ptr)
69  G_fatal_error("PNG: couldn't allocate PNG structure");
70 
71  info_ptr = png_create_info_struct(png_ptr);
72  if (!info_ptr)
73  G_fatal_error("PNG: couldn't allocate PNG structure");
74 
75  if (setjmp(png_jmpbuf(png_ptr)))
76  G_fatal_error("error writing PNG file");
77 
78  output = fopen(png.file_name, "wb");
79  if (!output)
80  G_fatal_error("PNG: couldn't open output 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,
85  png.width, png.height, 8,
86  png.true_color ? PNG_COLOR_TYPE_RGB_ALPHA :
87  PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
88  PNG_COMPRESSION_TYPE_DEFAULT, 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 }
int width
Definition: pngdriver.h:43
GRASS png display driver - header file.
unsigned char palette[256][4]
Definition: pngdriver.h:45
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
struct png_state png
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
double b
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:54
int true_color
Definition: pngdriver.h:35
double r
void G_free(void *buf)
Free allocated memory.
Definition: alloc.c:149