GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
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 
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>"), 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 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:150
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
unsigned char * grid
Definition: cairodriver.h:69
char * file_name
Definition: cairodriver.h:66
#define x