GRASS GIS 8 Programmer's Manual 8.4.1(2025)-45ca3179ab
Loading...
Searching...
No Matches
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
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 G_free(void *buf)
Free allocated memory.
Definition alloc.c:150
void cairo_write_ppm(void)
GRASS cairo display driver - header file.
struct cairo_state ca
double b
double r
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition gis/error.c:159
float g
Definition named_colr.c:7
void output(const char *fmt,...)
char * G_store(const char *s)
Copy string to allocated memory.
Definition strings.c:87
#define x