GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71942
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cairodriver/write_ppm.c
Go to the documentation of this file.
1 /*!
2  \file lib/cairodriver/write_ppm.c
3 
4  \brief GRASS cairo display driver - write PPM image (lower level functions)
5 
6  (C) 2007-2008 by Lars Ahlzen 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 Lars Ahlzen <lars ahlzen.com> (original contibutor)
12  \author Glynn Clements
13 */
14 
15 #include <grass/glocale.h>
16 
17 #include "cairodriver.h"
18 
19 void cairo_write_ppm(void)
20 {
21  char *mask_name = G_store(ca.file_name);
22  FILE *output, *mask;
23  int x, y;
24 
25  output = fopen(ca.file_name, "wb");
26  if (!output)
27  G_fatal_error(_("Cairo: unable to open output file <%s>"),
28  ca.file_name);
29 
30  mask_name[strlen(mask_name) - 2] = 'g';
31 
32  mask = fopen(mask_name, "wb");
33  if (!mask)
34  G_fatal_error(_("Cairo: unable to open mask file <%s>"),
35  mask_name);
36 
37  G_free(mask_name);
38 
39  fprintf(output, "P6\n%d %d\n255\n", ca.width, ca.height);
40  fprintf(mask, "P5\n%d %d\n255\n", ca.width, ca.height);
41 
42  for (y = 0; y < ca.height; y++) {
43  const unsigned int *row = (const unsigned int *)(ca.grid + y * ca.stride);
44 
45  for (x = 0; x < ca.width; x++) {
46  unsigned int c = row[x];
47  int a = (c >> 24) & 0xFF;
48  int r = (c >> 16) & 0xFF;
49  int g = (c >> 8) & 0xFF;
50  int b = (c >> 0) & 0xFF;
51 
52  if (a > 0 && a < 0xFF) {
53  r = r * 0xFF / a;
54  g = g * 0xFF / a;
55  b = b * 0xFF / a;
56  }
57 
58  fputc((unsigned char)r, output);
59  fputc((unsigned char)g, output);
60  fputc((unsigned char)b, output);
61  fputc((unsigned char)a, mask);
62  }
63  }
64 
65  fclose(output);
66  fclose(mask);
67 }
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:149
struct cairo_state ca
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
#define x
char * file_name
Definition: cairodriver.h:65
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
double b
Definition: r_raster.c:39
void cairo_write_ppm(void)
float g
Definition: named_colr.c:8
fclose(fd)
GRASS cairo display driver - header file.
void output(const char *fmt,...)
unsigned char * grid
Definition: cairodriver.h:68
#define _(str)
Definition: glocale.h:13
double r
Definition: r_raster.c:39