GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
write_png.c
Go to the documentation of this file.
1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <png.h>
5 
6 #include <grass/gis.h>
7 #include "pngdriver.h"
8 
9 void write_png(void)
10 {
11  static jmp_buf jbuf;
12  static png_struct *png_ptr;
13  static png_info *info_ptr;
14  FILE *output;
15  int x, y;
16  unsigned int *p;
17  png_bytep line;
18  const char *str;
19  int compress;
20 
21  png_ptr =
22  png_create_write_struct(PNG_LIBPNG_VER_STRING, &jbuf, NULL, NULL);
23  if (!png_ptr)
24  G_fatal_error("PNG: couldn't allocate PNG structure");
25 
26  info_ptr = png_create_info_struct(png_ptr);
27  if (!info_ptr)
28  G_fatal_error("PNG: couldn't allocate PNG structure");
29 
30  if (setjmp(png_jmpbuf(png_ptr)))
31  G_fatal_error("error writing PNG file");
32 
33  output = fopen(file_name, "wb");
34  if (!output)
35  G_fatal_error("PNG: couldn't open output file %s", file_name);
36 
37  png_init_io(png_ptr, output);
38 
39  png_set_IHDR(png_ptr, info_ptr,
40  width, height, 8,
41  true_color ? PNG_COLOR_TYPE_RGB_ALPHA :
42  PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
43  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
44 
45  if (true_color)
46  png_set_invert_alpha(png_ptr);
47  else {
48  png_color png_pal[256];
49  int i;
50 
51  for (i = 0; i < 256; i++) {
52  png_pal[i].red = png_palette[i][0];
53  png_pal[i].green = png_palette[i][1];
54  png_pal[i].blue = png_palette[i][2];
55  }
56 
57  png_set_PLTE(png_ptr, info_ptr, png_pal, 256);
58 
59  if (has_alpha) {
60  png_byte trans = (png_byte) 0;
61 
62  png_set_tRNS(png_ptr, info_ptr, &trans, 1, NULL);
63  }
64  }
65 
66  str = getenv("GRASS_PNG_COMPRESSION");
67  if (str && sscanf(str, "%d", &compress) == 1)
68  png_set_compression_level(png_ptr, compress);
69 
70  png_write_info(png_ptr, info_ptr);
71 
72  line = G_malloc(width * 4);
73 
74  for (y = 0, p = grid; y < height; y++) {
75  png_bytep q = line;
76 
77  if (true_color)
78  for (x = 0; x < width; x++, p++) {
79  unsigned int c = *p;
80  int r, g, b, a;
81 
82  get_pixel(c, &r, &g, &b, &a);
83  *q++ = (png_byte) r;
84  *q++ = (png_byte) g;
85  *q++ = (png_byte) b;
86  *q++ = (png_byte) a;
87  }
88  else
89  for (x = 0; x < width; x++, p++, q++)
90  *q = (png_byte) * p;
91 
92  png_write_row(png_ptr, line);
93  }
94 
95  G_free(line);
96 
97  png_write_end(png_ptr, info_ptr);
98 
99  png_destroy_write_struct(&png_ptr, &info_ptr);
100 
101  fclose(output);
102 }
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
float b
Definition: named_colr.c:8
int has_alpha
tuple q
Definition: forms.py:2018
void get_pixel(unsigned int pixel, int *r, int *g, int *b, int *a)
void write_png(void)
Definition: write_png.c:9
float r
Definition: named_colr.c:8
struct transport * trans
Definition: com_io.c:143
tuple width
unsigned char * grid
int y
Definition: plot.c:34
int true_color
char * getenv()
char * file_name
float g
Definition: named_colr.c:8
unsigned char png_palette[256][4]
return NULL
Definition: dbfopen.c:1394
fclose(fd)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int height
void output(const char *fmt,...)