GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-9b28d7b83c
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 contributor)
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>"), mask_name);
35 
36  G_free(mask_name);
37 
38  fprintf(output, "P6\n%d %d\n255\n", ca.width, ca.height);
39  fprintf(mask, "P5\n%d %d\n255\n", ca.width, ca.height);
40 
41  for (y = 0; y < ca.height; y++) {
42  const unsigned int *row =
43  (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 cairo_write_ppm(void)
GRASS cairo display driver - header file.
struct cairo_state ca
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
#define _(str)
Definition: glocale.h:10
float g
Definition: named_colr.c:7
void output(const char *fmt,...)
double b
Definition: r_raster.c:39
double r
Definition: r_raster.c:39
unsigned char * grid
Definition: cairodriver.h:69
char * file_name
Definition: cairodriver.h:66
#define x