GRASS 8 Programmer's Manual 8.6.0dev(2026)-c83afef6d3
Loading...
Searching...
No Matches
cairodriver/read_ppm.c
Go to the documentation of this file.
1/*!
2 \file lib/cairodriver/read_ppm.c
3
4 \brief GRASS cairo display driver - read PPM image (lower level functions)
5
6 SPDX-FileCopyrightText: 2007-2008, 2011 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 *input, *mask;
22 int x, y;
24
25 input = fopen(ca.file_name, "rb");
26 if (!input)
27 G_fatal_error(_("Cairo: unable to open input file <%s>"), ca.file_name);
28
29 if (fscanf(input, "P6 %d %d %d", &i_width, &i_height, &maxval) != 3)
30 G_fatal_error(_("Cairo: invalid input file <%s>"), ca.file_name);
31
32 if (fgetc(input) == EOF)
33 G_fatal_error(_("Cairo: unexpected EOF reading <%s>"), ca.file_name);
34
35 if (i_width != ca.width || i_height != ca.height)
36 G_fatal_error(_("Cairo: input file has incorrect dimensions: "
37 "expected: %dx%d got: %dx%d"),
38 ca.width, ca.height, i_width, i_height);
39
40 mask_name[strlen(mask_name) - 2] = 'g';
41
42 mask = fopen(mask_name, "rb");
43 if (!mask)
44 G_fatal_error(_("Cairo: unable to open input mask file <%s>"),
45 mask_name);
46
47 if (fscanf(mask, "P5 %d %d %d", &i_width, &i_height, &maxval) != 3)
48 G_fatal_error(_("Cairo: invalid input mask file <%s>"), mask_name);
49
50 if (fgetc(mask) == EOF)
51 G_fatal_error(_("Cairo: unexpected EOF reading mask <%s>"), mask_name);
52
53 if (i_width != ca.width || i_height != ca.height)
54 G_fatal_error(_("Cairo: input mask file has incorrect dimensions: "
55 "expected: %dx%d got: %dx%d"),
56 ca.width, ca.height, i_width, i_height);
57
59
60 for (y = 0; y < ca.height; y++) {
61 unsigned int *row = (unsigned int *)(ca.grid + y * ca.stride);
62
63 for (x = 0; x < ca.width; x++) {
64 int r = fgetc(input);
65 int g = fgetc(input);
66 int b = fgetc(input);
67 int a = fgetc(mask);
68
69 r = r * 255 / maxval;
70 g = g * 255 / maxval;
71 b = b * 255 / maxval;
72 a = a * 255 / maxval;
73
74 if (a > 0 && a < 0xFF) {
75 r = r * a / 0xFF;
76 g = g * a / 0xFF;
77 b = b * a / 0xFF;
78 }
79
80 row[x] = (a << 24) | (r << 16) | (g << 8) | (b << 0);
81 }
82 }
83
84 fclose(input);
85 fclose(mask);
86}
void cairo_read_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
double b
Definition r_raster.c:37
double r
Definition r_raster.c:37
#define x