GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
cairodriver/read_ppm.c
Go to the documentation of this file.
1 #include "cairodriver.h"
2 
3 void read_ppm(void)
4 {
5  char *mask_name = G_store(file_name);
6  FILE *input, *mask;
7  int x, y;
8  int i_width, i_height, maxval;
9 
10  input = fopen(file_name, "rb");
11  if (!input)
12  G_fatal_error("cairo: couldn't open input file %s", file_name);
13 
14  if (fscanf(input, "P6 %d %d %d", &i_width, &i_height, &maxval) != 3)
15  G_fatal_error("cairo: invalid input file %s", file_name);
16 
17  fgetc(input);
18 
19  if (i_width != width || i_height != height)
21  ("cairo: input file has incorrect dimensions: expected: %dx%d got: %dx%d",
22  width, height, i_width, i_height);
23 
24  mask_name[strlen(mask_name) - 2] = 'g';
25 
26  mask = fopen(mask_name, "rb");
27  if (!mask)
28  G_fatal_error("cairo: couldn't open input mask file %s", mask_name);
29 
30  if (fscanf(mask, "P5 %d %d %d", &i_width, &i_height, &maxval) != 3)
31  G_fatal_error("cairo: invalid input mask file %s", mask_name);
32 
33  fgetc(mask);
34 
35  if (i_width != width || i_height != height)
37  ("cairo: input mask file has incorrect dimensions: expected: %dx%d got: %dx%d",
38  width, height, i_width, i_height);
39 
40  G_free(mask_name);
41 
42  for (y = 0; y < height; y++) {
43  unsigned int *row = (unsigned int *)(grid + y * stride);
44 
45  for (x = 0; x < width; x++) {
46  int r = fgetc(input);
47  int g = fgetc(input);
48  int b = fgetc(input);
49  int a = fgetc(mask);
50 
51  r = r * 255 / maxval;
52  g = g * 255 / maxval;
53  b = b * 255 / maxval;
54  a = a * 255 / maxval;
55 
56  if (a > 0 && a < 0xFF) {
57  r = r * a / 0xFF;
58  g = g * a / 0xFF;
59  b = b * a / 0xFF;
60  }
61 
62  row[x] = (a << 24) | (r << 16) | (g << 8) | (b << 0);
63  }
64  }
65 
66  fclose(input);
67  fclose(mask);
68 }
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
float b
Definition: named_colr.c:8
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
float r
Definition: named_colr.c:8
tuple width
unsigned char * grid
int y
Definition: plot.c:34
int stride
char * file_name
float g
Definition: named_colr.c:8
fclose(fd)
void read_ppm(void)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int height