GRASS GIS 8 Programmer's Manual 8.3.2(2024)-exported
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;
24 int i_width, i_height, maxval;
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 fgetc(input);
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 fgetc(mask);
51
52 if (i_width != ca.width || i_height != ca.height)
53 G_fatal_error(_("Cairo: input mask file has incorrect dimensions: "
54 "expected: %dx%d got: %dx%d"),
55 ca.width, ca.height, i_width, i_height);
56
57 G_free(mask_name);
58
59 for (y = 0; y < ca.height; y++) {
60 unsigned int *row = (unsigned int *)(ca.grid + y * ca.stride);
61
62 for (x = 0; x < ca.width; x++) {
63 int r = fgetc(input);
64 int g = fgetc(input);
65 int b = fgetc(input);
66 int a = fgetc(mask);
67
68 r = r * 255 / maxval;
69 g = g * 255 / maxval;
70 b = b * 255 / maxval;
71 a = a * 255 / maxval;
72
73 if (a > 0 && a < 0xFF) {
74 r = r * a / 0xFF;
75 g = g * a / 0xFF;
76 b = b * a / 0xFF;
77 }
78
79 row[x] = (a << 24) | (r << 16) | (g << 8) | (b << 0);
80 }
81 }
82
83 fclose(input);
84 fclose(mask);
85}
void G_free(void *buf)
Free allocated memory.
Definition alloc.c:150
void cairo_read_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
char * G_store(const char *s)
Copy string to allocated memory.
Definition strings.c:87
unsigned char * grid
Definition cairodriver.h:69
char * file_name
Definition cairodriver.h:66
#define x