GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
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 SPDX-FileCopyrightText: 2007-2008 Lars Ahlzen
7 SPDX-FileCopyrightText: GRASS Development Team
8 SPDX-License-Identifier: GPL-2.0-or-later
9
10 \author Lars Ahlzen <lars ahlzen.com> (original contributor)
11 \author Glynn Clements
12 */
13
14#include <grass/glocale.h>
15
16#include "cairodriver.h"
17
19{
20 char *mask_name = G_store(ca.file_name);
21 FILE *output, *mask;
22 int x, y;
23
24 output = fopen(ca.file_name, "wb");
25 if (!output)
26 G_fatal_error(_("Cairo: unable to open output file <%s>"),
27 ca.file_name);
28
29 mask_name[strlen(mask_name) - 2] = 'g';
30
31 mask = fopen(mask_name, "wb");
32 if (!mask)
33 G_fatal_error(_("Cairo: unable to open mask file <%s>"), mask_name);
34
36
37 fprintf(output, "P6\n%d %d\n255\n", ca.width, ca.height);
38 fprintf(mask, "P5\n%d %d\n255\n", ca.width, ca.height);
39
40 for (y = 0; y < ca.height; y++) {
41 const unsigned int *row =
42 (const unsigned int *)(ca.grid + y * ca.stride);
43
44 for (x = 0; x < ca.width; x++) {
45 unsigned int c = row[x];
46 int a = (c >> 24) & 0xFF;
47 int r = (c >> 16) & 0xFF;
48 int g = (c >> 8) & 0xFF;
49 int b = (c >> 0) & 0xFF;
50
51 if (a > 0 && a < 0xFF) {
52 r = r * 0xFF / a;
53 g = g * 0xFF / a;
54 b = b * 0xFF / a;
55 }
56
57 fputc((unsigned char)r, output);
58 fputc((unsigned char)g, output);
59 fputc((unsigned char)b, output);
60 fputc((unsigned char)a, mask);
61 }
62 }
63
65 fclose(mask);
66}
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:145
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:85
#define _(str)
Definition glocale.h:10
float g
Definition named_colr.c:7
void output(const char *fmt,...)
double b
Definition r_raster.c:37
double r
Definition r_raster.c:37
#define x