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