GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
cairodriver/write_ppm.c
Go to the documentation of this file.
1 #include "cairodriver.h"
2 
3 void write_ppm(void)
4 {
5  char *mask_name = G_store(file_name);
6  FILE *output, *mask;
7  int x, y;
8 
9  output = fopen(file_name, "wb");
10  if (!output)
11  G_fatal_error("cairo: couldn't open output file %s", file_name);
12 
13  mask_name[strlen(mask_name) - 2] = 'g';
14 
15  mask = fopen(mask_name, "wb");
16  if (!mask)
17  G_fatal_error("cairo: couldn't open mask file %s", mask_name);
18 
19  G_free(mask_name);
20 
21  fprintf(output, "P6\n%d %d\n255\n", width, height);
22  fprintf(mask, "P5\n%d %d\n255\n", width, height);
23 
24  for (y = 0; y < height; y++) {
25  const unsigned int *row = (const unsigned int *)(grid + y * stride);
26 
27  for (x = 0; x < width; x++) {
28  unsigned int c = row[x];
29  int a = (c >> 24) & 0xFF;
30  int r = (c >> 16) & 0xFF;
31  int g = (c >> 8) & 0xFF;
32  int b = (c >> 0) & 0xFF;
33 
34  if (a > 0 && a < 0xFF) {
35  r = r * 0xFF / a;
36  g = g * 0xFF / a;
37  b = b * 0xFF / a;
38  }
39 
40  fputc((unsigned char)r, output);
41  fputc((unsigned char)g, output);
42  fputc((unsigned char)b, output);
43  fputc((unsigned char)a, mask);
44  }
45  }
46 
47  fclose(output);
48  fclose(mask);
49 }
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
void write_ppm(void)
char * file_name
float g
Definition: named_colr.c:8
fclose(fd)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int height
void output(const char *fmt,...)