GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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 contibutor)
12  \author Glynn Clements
13 */
14 
15 #include <grass/glocale.h>
16 
17 #include "cairodriver.h"
18 
19 void cairo_read_ppm(void)
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>"),
29  ca.file_name);
30 
31  if (fscanf(input, "P6 %d %d %d", &i_width, &i_height, &maxval) != 3)
32  G_fatal_error(_("Cairo: invalid input file <%s>"),
33  ca.file_name);
34 
35  fgetc(input);
36 
37  if (i_width != ca.width || i_height != ca.height)
38  G_fatal_error(_("Cairo: input file has incorrect dimensions: "
39  "expected: %dx%d got: %dx%d"),
40  ca.width, ca.height, i_width, i_height);
41 
42  mask_name[strlen(mask_name) - 2] = 'g';
43 
44  mask = fopen(mask_name, "rb");
45  if (!mask)
46  G_fatal_error(_("Cairo: unable to open input mask file <%s>"),
47  mask_name);
48 
49  if (fscanf(mask, "P5 %d %d %d", &i_width, &i_height, &maxval) != 3)
50  G_fatal_error(_("Cairo: invalid input mask file <%s>"),
51  mask_name);
52 
53  fgetc(mask);
54 
55  if (i_width != ca.width || i_height != ca.height)
56  G_fatal_error(_("Cairo: input mask file has incorrect dimensions: "
57  "expected: %dx%d got: %dx%d"),
58  ca.width, ca.height, i_width, i_height);
59 
60  G_free(mask_name);
61 
62  for (y = 0; y < ca.height; y++) {
63  unsigned int *row = (unsigned int *)(ca.grid + y * ca.stride);
64 
65  for (x = 0; x < ca.width; x++) {
66  int r = fgetc(input);
67  int g = fgetc(input);
68  int b = fgetc(input);
69  int a = fgetc(mask);
70 
71  r = r * 255 / maxval;
72  g = g * 255 / maxval;
73  b = b * 255 / maxval;
74  a = a * 255 / maxval;
75 
76  if (a > 0 && a < 0xFF) {
77  r = r * a / 0xFF;
78  g = g * a / 0xFF;
79  b = b * a / 0xFF;
80  }
81 
82  row[x] = (a << 24) | (r << 16) | (g << 8) | (b << 0);
83  }
84  }
85 
86  fclose(input);
87  fclose(mask);
88 }
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
struct cairo_state ca
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
#define x
char * file_name
Definition: cairodriver.h:65
double b
Definition: r_raster.c:39
float g
Definition: named_colr.c:8
GRASS cairo display driver - header file.
unsigned char * grid
Definition: cairodriver.h:68
void cairo_read_ppm(void)
#define _(str)
Definition: glocale.h:10
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
double r
Definition: r_raster.c:39